Skip to content

Commit 13bc6d6

Browse files
authored
testing: setting env var incompatible with parallel tests (#14405)
Neither the `os.Setenv` nor `t.Setenv` helper are safe to use in parallel tests because environment variables are process-global. The stdlib panics if you try to do this. Remove the `ci.Parallel()` call from all tests where we're setting environment variables.
1 parent 3679957 commit 13bc6d6

13 files changed

+16
-54
lines changed

api/api_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func TestRequestTime(t *testing.T) {
117117
}
118118

119119
func TestDefaultConfig_env(t *testing.T) {
120-
testutil.Parallel(t)
120+
121121
testURL := "http://1.2.3.4:5678"
122122
auth := []string{"nomaduser", "12345"}
123123
region := "test"

client/allocrunner/taskrunner/template/template_test.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -1376,14 +1376,11 @@ BAR={{key "bar"}}
13761376
// process environment variables. nomad host process environment variables
13771377
// are to be treated the same as not found environment variables.
13781378
func TestTaskTemplateManager_FiltersEnvVars(t *testing.T) {
1379-
ci.Parallel(t)
13801379

1381-
defer os.Setenv("NOMAD_TASK_NAME", os.Getenv("NOMAD_TASK_NAME"))
1382-
os.Setenv("NOMAD_TASK_NAME", "should be overridden by task")
1380+
t.Setenv("NOMAD_TASK_NAME", "should be overridden by task")
13831381

13841382
testenv := "TESTENV_" + strings.ReplaceAll(uuid.Generate(), "-", "")
1385-
os.Setenv(testenv, "MY_TEST_VALUE")
1386-
defer os.Unsetenv(testenv)
1383+
t.Setenv(testenv, "MY_TEST_VALUE")
13871384

13881385
// Make a template that will render immediately
13891386
content := `Hello Nomad Task: {{env "NOMAD_TASK_NAME"}}

client/fingerprint/env_azure_test.go

+3-10
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,17 @@ import (
55
"fmt"
66
"net/http"
77
"net/http/httptest"
8-
"os"
98
"strings"
109
"testing"
1110

12-
"github.com/hashicorp/nomad/ci"
1311
"github.com/hashicorp/nomad/client/config"
1412
"github.com/hashicorp/nomad/helper/testlog"
1513
"github.com/hashicorp/nomad/nomad/structs"
1614
)
1715

1816
func TestAzureFingerprint_nonAzure(t *testing.T) {
19-
ci.Parallel(t)
2017

21-
os.Setenv("AZURE_ENV_URL", "http://127.0.0.1/metadata/instance/")
18+
t.Setenv("AZURE_ENV_URL", "http://127.0.0.1/metadata/instance/")
2219
f := NewEnvAzureFingerprint(testlog.HCLogger(t))
2320
node := &structs.Node{
2421
Attributes: make(map[string]string),
@@ -95,7 +92,7 @@ func testFingerprint_Azure(t *testing.T, withExternalIp bool) {
9592
}
9693
}))
9794
defer ts.Close()
98-
os.Setenv("AZURE_ENV_URL", ts.URL+"/metadata/instance/")
95+
t.Setenv("AZURE_ENV_URL", ts.URL+"/metadata/instance/")
9996
f := NewEnvAzureFingerprint(testlog.HCLogger(t))
10097

10198
request := &FingerprintRequest{Config: &config.Config{}, Node: node}
@@ -208,19 +205,15 @@ const AZURE_routes = `
208205
"uri": "/metadata/instance/network/interface/0/macAddress",
209206
"content-type": "text/plain",
210207
"body": "000D3AF806EC"
211-
}
208+
}
212209
]
213210
}
214211
`
215212

216213
func TestFingerprint_AzureWithExternalIp(t *testing.T) {
217-
ci.Parallel(t)
218-
219214
testFingerprint_Azure(t, true)
220215
}
221216

222217
func TestFingerprint_AzureWithoutExternalIp(t *testing.T) {
223-
ci.Parallel(t)
224-
225218
testFingerprint_Azure(t, false)
226219
}

client/fingerprint/env_digitalocean_test.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,18 @@ import (
55
"fmt"
66
"net/http"
77
"net/http/httptest"
8-
"os"
98
"strings"
109
"testing"
1110

12-
"github.com/hashicorp/nomad/ci"
1311
"github.com/hashicorp/nomad/client/config"
1412
"github.com/hashicorp/nomad/helper/testlog"
1513
"github.com/hashicorp/nomad/nomad/structs"
1614
"github.com/stretchr/testify/assert"
1715
)
1816

1917
func TestDigitalOceanFingerprint_nonDigitalOcean(t *testing.T) {
20-
ci.Parallel(t)
2118

22-
os.Setenv("DO_ENV_URL", "http://127.0.0.1/metadata/v1/")
19+
t.Setenv("DO_ENV_URL", "http://127.0.0.1/metadata/v1/")
2320
f := NewEnvDigitalOceanFingerprint(testlog.HCLogger(t))
2421
node := &structs.Node{
2522
Attributes: make(map[string]string),
@@ -42,7 +39,6 @@ func TestDigitalOceanFingerprint_nonDigitalOcean(t *testing.T) {
4239
}
4340

4441
func TestFingerprint_DigitalOcean(t *testing.T) {
45-
ci.Parallel(t)
4642

4743
node := &structs.Node{
4844
Attributes: make(map[string]string),
@@ -82,7 +78,7 @@ func TestFingerprint_DigitalOcean(t *testing.T) {
8278
}
8379
}))
8480
defer ts.Close()
85-
os.Setenv("DO_ENV_URL", ts.URL+"/metadata/v1/")
81+
t.Setenv("DO_ENV_URL", ts.URL+"/metadata/v1/")
8682
f := NewEnvDigitalOceanFingerprint(testlog.HCLogger(t))
8783

8884
request := &FingerprintRequest{Config: &config.Config{}, Node: node}

client/fingerprint/env_gce_test.go

+2-9
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,17 @@ import (
55
"fmt"
66
"net/http"
77
"net/http/httptest"
8-
"os"
98
"strings"
109
"testing"
1110

12-
"github.com/hashicorp/nomad/ci"
1311
"github.com/hashicorp/nomad/client/config"
1412
"github.com/hashicorp/nomad/helper/testlog"
1513
"github.com/hashicorp/nomad/nomad/structs"
1614
)
1715

1816
func TestGCEFingerprint_nonGCE(t *testing.T) {
19-
ci.Parallel(t)
2017

21-
os.Setenv("GCE_ENV_URL", "http://127.0.0.1/computeMetadata/v1/instance/")
18+
t.Setenv("GCE_ENV_URL", "http://127.0.0.1/computeMetadata/v1/instance/")
2219
f := NewEnvGCEFingerprint(testlog.HCLogger(t))
2320
node := &structs.Node{
2421
Attributes: make(map[string]string),
@@ -92,7 +89,7 @@ func testFingerprint_GCE(t *testing.T, withExternalIp bool) {
9289
}
9390
}))
9491
defer ts.Close()
95-
os.Setenv("GCE_ENV_URL", ts.URL+"/computeMetadata/v1/instance/")
92+
t.Setenv("GCE_ENV_URL", ts.URL+"/computeMetadata/v1/instance/")
9693
f := NewEnvGCEFingerprint(testlog.HCLogger(t))
9794

9895
request := &FingerprintRequest{Config: &config.Config{}, Node: node}
@@ -210,13 +207,9 @@ const GCE_routes = `
210207
`
211208

212209
func TestFingerprint_GCEWithExternalIp(t *testing.T) {
213-
ci.Parallel(t)
214-
215210
testFingerprint_GCE(t, true)
216211
}
217212

218213
func TestFingerprint_GCEWithoutExternalIp(t *testing.T) {
219-
ci.Parallel(t)
220-
221214
testFingerprint_GCE(t, false)
222215
}

command/agent/host/host_test.go

+4-11
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package host
22

33
import (
4-
"os"
54
"testing"
65

7-
"github.com/hashicorp/nomad/ci"
86
"github.com/stretchr/testify/require"
97
)
108

@@ -19,16 +17,11 @@ func TestHostUtils(t *testing.T) {
1917
}
2018

2119
func TestMakeHostData(t *testing.T) {
22-
ci.Parallel(t)
2320

24-
// setenv variables that should be redacted
25-
prev := os.Getenv("VAULT_TOKEN")
26-
os.Setenv("VAULT_TOKEN", "foo")
27-
defer os.Setenv("VAULT_TOKEN", prev)
28-
29-
os.Setenv("BOGUS_TOKEN", "foo")
30-
os.Setenv("BOGUS_SECRET", "foo")
31-
os.Setenv("ryanSECRETS", "foo")
21+
t.Setenv("VAULT_TOKEN", "foo")
22+
t.Setenv("BOGUS_TOKEN", "foo")
23+
t.Setenv("BOGUS_SECRET", "foo")
24+
t.Setenv("ryanSECRETS", "foo")
3225

3326
host, err := MakeHostData()
3427
require.NoError(t, err)

command/helpers_test.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,6 @@ func TestJobGetter_LocalFile_InvalidHCL2(t *testing.T) {
332332
// TestJobGetter_HCL2_Variables asserts variable arguments from CLI
333333
// and varfiles are both honored
334334
func TestJobGetter_HCL2_Variables(t *testing.T) {
335-
ci.Parallel(t)
336335

337336
hcl := `
338337
variables {
@@ -346,7 +345,6 @@ job "example" {
346345
datacenters = ["${var.var1}", "${var.var2}", "${var.var3}", "${var.var4}"]
347346
}
348347
`
349-
350348
t.Setenv("NOMAD_VAR_var4", "from-envvar")
351349

352350
cliArgs := []string{`var2=from-cli`}
@@ -377,7 +375,6 @@ job "example" {
377375
}
378376

379377
func TestJobGetter_HCL2_Variables_StrictFalse(t *testing.T) {
380-
ci.Parallel(t)
381378

382379
hcl := `
383380
variables {
@@ -392,8 +389,7 @@ job "example" {
392389
}
393390
`
394391

395-
os.Setenv("NOMAD_VAR_var4", "from-envvar")
396-
defer os.Unsetenv("NOMAD_VAR_var4")
392+
t.Setenv("NOMAD_VAR_var4", "from-envvar")
397393

398394
// Both the CLI and var file contain variables that are not used with the
399395
// template and therefore would error, if hcl2-strict was true.

command/job_plan_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ job "job1" {
158158
}
159159

160160
func TestPlanCommand_From_Files(t *testing.T) {
161-
ci.Parallel(t)
162161

163162
// Create a Vault server
164163
v := testutil.NewTestVault(t)

command/job_validate_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ func TestValidateCommand_Implements(t *testing.T) {
1818
}
1919

2020
func TestValidateCommand_Files(t *testing.T) {
21-
ci.Parallel(t)
2221

2322
// Create a Vault server
2423
v := testutil.NewTestVault(t)

command/meta_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ func TestMeta_FlagSet(t *testing.T) {
6262
}
6363

6464
func TestMeta_Colorize(t *testing.T) {
65-
ci.Parallel(t)
6665

6766
type testCaseSetupFn func(*testing.T, *Meta)
6867

command/ui_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ import (
55
"strings"
66
"testing"
77

8-
"github.com/hashicorp/nomad/ci"
98
"github.com/mitchellh/cli"
109
"github.com/stretchr/testify/require"
1110
)
1211

1312
func TestCommand_Ui(t *testing.T) {
14-
ci.Parallel(t)
1513

1614
type testCaseSetupFn func(*testing.T)
1715

drivers/docker/driver_darwin_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
func TestMain(m *testing.M) {
1919
tmpdir := fmt.Sprintf("/tmp/nomad-docker-tests-%d", time.Now().Unix())
2020

21-
defer os.Setenv("TMPDIR", os.Getenv("TMPDIR"))
21+
os.Setenv("TMPDIR", os.Getenv("TMPDIR"))
2222
os.Setenv("TMPDIR", tmpdir)
2323

2424
os.MkdirAll(tmpdir, 0700)

drivers/docker/driver_linux_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
)
2020

2121
func TestDockerDriver_authFromHelper(t *testing.T) {
22-
ci.Parallel(t)
2322
testutil.DockerCompatible(t)
2423

2524
dir := t.TempDir()

0 commit comments

Comments
 (0)