chore(auth/grpctransport): remove env impact on direct path tests#14186
chore(auth/grpctransport): remove env impact on direct path tests#14186westarle wants to merge 1 commit intogoogleapis:mainfrom
Conversation
…irectpath tests This clears the GOOGLE_CLOUD_DISABLE_DIRECT_PATH environment variable in TestLogDirectPathMisconfig tests to prevent failures when the environment variable is set in the environment running the tests.
There was a problem hiding this comment.
Code Review
This PR correctly makes the direct path tests independent of the environment by unsetting GOOGLE_CLOUD_DISABLE_DIRECT_PATH. To improve the test suite's maintainability, I've suggested refactoring the three modified tests, which share a lot of boilerplate, into a single table-driven test. This would reduce code duplication and make future modifications easier.
| } | ||
|
|
||
| func TestLogDirectPathMisconfigDirectPathNotSet(t *testing.T) { | ||
| t.Setenv(disableDirectPathEnvVar, "") |
There was a problem hiding this comment.
This line is also added to TestLogDirectPathMisconfigWrongCredential and TestLogDirectPathMisconfigNotOnGCE. Since these three tests are very similar, consider refactoring them into a single table-driven test. This would reduce code duplication and improve maintainability. You would only need to call t.Setenv once in the parent test.
Here's an example of how it could look:
func TestLogDirectPathMisconfig(t *testing.T) {
t.Setenv(disableDirectPathEnvVar, "")
testCases := []struct {
name string
opts *Options
creds *google.Credentials
onGCE bool
wantWarning string
}{
{
name: "DirectPathNotSet",
opts: &Options{InternalOptions: &InternalOptions{
EnableDirectPathXds: true,
}},
wantWarning: directPathNotSetWarning,
},
{
name: "WrongCredential",
opts: &Options{InternalOptions: &InternalOptions{
EnableDirectPathXds: true,
EnableDirectPath: true,
}},
creds: &google.Credentials{
TokenSource: oauth2.StaticTokenSource(&oauth2.Token{}),
},
wantWarning: wrongCredsWarning,
},
{
name: "NotOnGCE",
opts: &Options{InternalOptions: &InternalOptions{
EnableDirectPath: true,
EnableDirectPathXds: true,
}},
creds: &google.Credentials{
ProjectID: "project-id",
},
wantWarning: notOnGCEWarning,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var buf bytes.Buffer
log.SetOutput(&buf)
defer log.SetOutput(os.Stderr)
logDirectPathMisconfig(tc.opts, tc.creds, tc.onGCE)
if got := buf.String(); !strings.Contains(got, tc.wantWarning) {
t.Errorf("got %q, want substring %q", got, tc.wantWarning)
}
})
}
}You would then remove TestLogDirectPathMisconfigDirectPathNotSet, TestLogDirectPathMisconfigWrongCredential, and TestLogDirectPathMisconfigNotOnGCE.
This clears the GOOGLE_CLOUD_DISABLE_DIRECT_PATH environment variable in TestLogDirectPathMisconfig tests to prevent failures when the environment variable is set in the environment running the tests. This fixes a nuisance test error when running tests on cloudtop.