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.
This commit is contained in:
Tim Gross
2022-08-30 14:49:03 -04:00
committed by GitHub
parent 36799570f6
commit 13bc6d6d8a
13 changed files with 16 additions and 54 deletions

View File

@@ -1,10 +1,8 @@
package host
import (
"os"
"testing"
"github.com/hashicorp/nomad/ci"
"github.com/stretchr/testify/require"
)
@@ -19,16 +17,11 @@ func TestHostUtils(t *testing.T) {
}
func TestMakeHostData(t *testing.T) {
ci.Parallel(t)
// setenv variables that should be redacted
prev := os.Getenv("VAULT_TOKEN")
os.Setenv("VAULT_TOKEN", "foo")
defer os.Setenv("VAULT_TOKEN", prev)
os.Setenv("BOGUS_TOKEN", "foo")
os.Setenv("BOGUS_SECRET", "foo")
os.Setenv("ryanSECRETS", "foo")
t.Setenv("VAULT_TOKEN", "foo")
t.Setenv("BOGUS_TOKEN", "foo")
t.Setenv("BOGUS_SECRET", "foo")
t.Setenv("ryanSECRETS", "foo")
host, err := MakeHostData()
require.NoError(t, err)

View File

@@ -332,7 +332,6 @@ func TestJobGetter_LocalFile_InvalidHCL2(t *testing.T) {
// TestJobGetter_HCL2_Variables asserts variable arguments from CLI
// and varfiles are both honored
func TestJobGetter_HCL2_Variables(t *testing.T) {
ci.Parallel(t)
hcl := `
variables {
@@ -346,7 +345,6 @@ job "example" {
datacenters = ["${var.var1}", "${var.var2}", "${var.var3}", "${var.var4}"]
}
`
t.Setenv("NOMAD_VAR_var4", "from-envvar")
cliArgs := []string{`var2=from-cli`}
@@ -377,7 +375,6 @@ job "example" {
}
func TestJobGetter_HCL2_Variables_StrictFalse(t *testing.T) {
ci.Parallel(t)
hcl := `
variables {
@@ -392,8 +389,7 @@ job "example" {
}
`
os.Setenv("NOMAD_VAR_var4", "from-envvar")
defer os.Unsetenv("NOMAD_VAR_var4")
t.Setenv("NOMAD_VAR_var4", "from-envvar")
// Both the CLI and var file contain variables that are not used with the
// template and therefore would error, if hcl2-strict was true.

View File

@@ -158,7 +158,6 @@ job "job1" {
}
func TestPlanCommand_From_Files(t *testing.T) {
ci.Parallel(t)
// Create a Vault server
v := testutil.NewTestVault(t)

View File

@@ -18,7 +18,6 @@ func TestValidateCommand_Implements(t *testing.T) {
}
func TestValidateCommand_Files(t *testing.T) {
ci.Parallel(t)
// Create a Vault server
v := testutil.NewTestVault(t)

View File

@@ -62,7 +62,6 @@ func TestMeta_FlagSet(t *testing.T) {
}
func TestMeta_Colorize(t *testing.T) {
ci.Parallel(t)
type testCaseSetupFn func(*testing.T, *Meta)

View File

@@ -5,13 +5,11 @@ import (
"strings"
"testing"
"github.com/hashicorp/nomad/ci"
"github.com/mitchellh/cli"
"github.com/stretchr/testify/require"
)
func TestCommand_Ui(t *testing.T) {
ci.Parallel(t)
type testCaseSetupFn func(*testing.T)