tests: ensure that tests restore env-var values (#11309)

Fix a test corruption issue, where a test accidentally unsets
the `NOMAD_LICENSE` environment variable, that's relied on by some
tests.

As a habit, tests should always restore the environment variable value
on test completion. Golang 1.17 introduced
[`t.Setenv`](https://pkg.go.dev/testing#T.Setenv) to address this issue.
However, as 1.0.x and 1.1.x branches target golang 1.15 and 1.16, I
opted to use a helper function to ease backports.
This commit is contained in:
Mahmood Ali 2021-10-13 17:26:56 -04:00 committed by GitHub
parent 305e8e98bf
commit fa4df28fcd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 22 deletions

View File

@ -346,8 +346,7 @@ job "example" {
} }
` `
os.Setenv("NOMAD_VAR_var4", "from-envvar") setEnv(t, "NOMAD_VAR_var4", "from-envvar")
defer os.Unsetenv("NOMAD_VAR_var4")
cliArgs := []string{`var2=from-cli`} cliArgs := []string{`var2=from-cli`}
fileVars := `var3 = "from-varfile"` fileVars := `var3 = "from-varfile"`

View File

@ -5,12 +5,11 @@ import (
"os" "os"
"reflect" "reflect"
"sort" "sort"
"strings"
"testing" "testing"
"github.com/kr/pty" "github.com/kr/pty"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/require"
) )
func TestMeta_FlagSet(t *testing.T) { func TestMeta_FlagSet(t *testing.T) {
@ -90,7 +89,7 @@ func TestMeta_Colorize(t *testing.T) {
{ {
Name: "disable colors via env var", Name: "disable colors via env var",
SetupFn: func(t *testing.T, m *Meta) { SetupFn: func(t *testing.T, m *Meta) {
os.Setenv(EnvNomadCLINoColor, "1") setEnv(t, EnvNomadCLINoColor, "1")
m.SetupUi([]string{}) m.SetupUi([]string{})
}, },
ExpectColor: false, ExpectColor: false,
@ -105,7 +104,7 @@ func TestMeta_Colorize(t *testing.T) {
{ {
Name: "force colors via env var", Name: "force colors via env var",
SetupFn: func(t *testing.T, m *Meta) { SetupFn: func(t *testing.T, m *Meta) {
os.Setenv(EnvNomadCLIForceColor, "1") setEnv(t, EnvNomadCLIForceColor, "1")
m.SetupUi([]string{}) m.SetupUi([]string{})
}, },
ExpectColor: true, ExpectColor: true,
@ -120,7 +119,7 @@ func TestMeta_Colorize(t *testing.T) {
{ {
Name: "no color take predecence over force color via env var", Name: "no color take predecence over force color via env var",
SetupFn: func(t *testing.T, m *Meta) { SetupFn: func(t *testing.T, m *Meta) {
os.Setenv(EnvNomadCLINoColor, "1") setEnv(t, EnvNomadCLINoColor, "1")
m.SetupUi([]string{"-force-color"}) m.SetupUi([]string{"-force-color"})
}, },
ExpectColor: false, ExpectColor: false,
@ -131,22 +130,16 @@ func TestMeta_Colorize(t *testing.T) {
t.Run(tc.Name, func(t *testing.T) { t.Run(tc.Name, func(t *testing.T) {
// Create fake test terminal. // Create fake test terminal.
_, tty, err := pty.Open() _, tty, err := pty.Open()
if err != nil { require.NoError(t, err)
t.Fatalf("%v", err)
}
defer tty.Close() defer tty.Close()
oldStdout := os.Stdout oldStdout := os.Stdout
defer func() { os.Stdout = oldStdout }() defer func() { os.Stdout = oldStdout }()
os.Stdout = tty os.Stdout = tty
// Make sure Nomad environment variables are clean. // Make sure color related environment variables are clean.
for _, envVar := range os.Environ() { setEnv(t, EnvNomadCLIForceColor, "")
if strings.HasPrefix(envVar, "NOMAD") { setEnv(t, EnvNomadCLINoColor, "")
k := strings.SplitN(envVar, "=", 2)[0]
os.Unsetenv(k)
}
}
// Run test case. // Run test case.
m := &Meta{} m := &Meta{}
@ -154,11 +147,7 @@ func TestMeta_Colorize(t *testing.T) {
tc.SetupFn(t, m) tc.SetupFn(t, m)
} }
if tc.ExpectColor { require.Equal(t, !tc.ExpectColor, m.Colorize().Disable)
assert.False(t, m.Colorize().Disable)
} else {
assert.True(t, m.Colorize().Disable)
}
}) })
} }
} }

View File

@ -1,6 +1,7 @@
package command package command
import ( import (
"os"
"testing" "testing"
"github.com/hashicorp/nomad/api" "github.com/hashicorp/nomad/api"
@ -106,3 +107,17 @@ func testMultiRegionJob(jobID, region, datacenter string) *api.Job {
return job return job
} }
// setEnv wraps os.Setenv(key, value) and restores the environment variable to initial value in test cleanup
func setEnv(t *testing.T, key, value string) {
initial, ok := os.LookupEnv(key)
os.Setenv(key, value)
t.Cleanup(func() {
if ok {
os.Setenv(key, initial)
} else {
os.Unsetenv(key)
}
})
}