Repository: hashicorp/nomad
File: drivers/rawexec/driver_test.go (Go)

Code before:

			// set and encode task config
			taskConfig := tc.taskConfig
			require.NoError(task.EncodeConcreteDriverConfig(&taskConfig))

			if runtime.GOOS == "windows" {
				// set environment variables on task level for windows before starting task, because reasons
				task.Env = genEnv()
				_, _, err := harness.StartTask(task)
				require.NoError(err)
				// Exec a command that should work
				res, err := harness.ExecTask(task.ID, []string{"cmd.exe", "/c", "set"}, 1*time.Second)
				require.NoError(err)
				require.True(res.ExitResult.Successful())

				// confirm checked variables are as expected in windows land
				for _, v := range tc.checkVars {
					if tc.varsExpected {
						// confirm envvars are readable in control case
						require.Contains(string(res.Stdout), v)
					} else {
						// use globbing to test for denied envvars
						envGlob := "*" + v + "=*"
						require.False(glob.Glob(envGlob, string(res.Stdout)))
					}
				}
			} else {
				// start task for non-windows runtimes
				_, _, err := harness.StartTask(task)
				require.NoError(err)
				// exec an env to standard out
				res, err := harness.ExecTask(task.ID, []string{"env"}, 1*time.Second)
				require.NoError(err)
				require.True(res.ExitResult.Successful())

				// confirm checked variables are as expected in nix land
				for _, v := range tc.checkVars {
					if tc.varsExpected {
						// confirm envvars are readable in control case
						require.Contains(string(res.Stdout), v)
					} else {
						// use globbing to test for denied envvars
						envGlob := "*" + v + "=*"
						require.False(glob.Glob(envGlob, string(res.Stdout)))
					}
				}
			}
			require.NoError(harness.DestroyTask(task.ID, true))
		})
	}


Review comment (suggestion):
It's great that we're trying this, but honestly we could probably skip this Windows portion of the test. Your new code under test in this PR isn't specific to the operating system.

Code after:
			taskConfig: &TaskConfig{
				Command:       testtask.Path(),
				Args:          []string{"sleep", "10ms"},
				DeniedEnvvars: []string{"*AWS*"},
			},
			deniedVars: []string{
				"AWS_SECRET_KEY=hij",
				"TEST_AWS_VAR=qrs",
				"VAR_TEST_AWS=tuv",
			},
			varsExpected: false,
		},
	}
	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			// if set, update driver config
			if tc.driverConfig != nil {
				tc.driver.config = tc.driverConfig
			}

			harness := dtestutil.NewDriverHarness(t, tc.driver)
			defer harness.Kill()

			cleanup := harness.MkAllocDir(task, false)
			defer cleanup()

			harness.MakeTaskCgroup(allocID, taskName)

			// set and encode task config
			taskConfig := tc.taskConfig
			must.NoError(t, task.EncodeConcreteDriverConfig(&taskConfig))

			// start task
			_, _, err := harness.StartTask(task)
			must.NoError(t, err)
			// exec an env to standard out
			res, err := harness.ExecTask(task.ID, []string{"env"}, 1*time.Second)
			must.NoError(t, err)
			must.True(t, res.ExitResult.Successful())

			// confirm denied variables are not found in stdout
			for _, v := range tc.deniedVars {
				if tc.varsExpected {
					must.StrNotContains(t, string(res.Stdout), v)
				}
			}

			must.NoError(t, harness.DestroyTask(task.ID, true))
		})
	}