Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace os.Setenv with t.Setenv in all unit tests #6122

Closed
antoninbas opened this issue Mar 20, 2024 · 0 comments · Fixed by #6139
Closed

Replace os.Setenv with t.Setenv in all unit tests #6122

antoninbas opened this issue Mar 20, 2024 · 0 comments · Fixed by #6139
Labels
area/test Issues or PRs related to unit and integration tests. good first issue Good for newcomers kind/task Categorizes issue or PR as related to a routine task that needs to be performed priority/backlog Higher priority than priority/awaiting-more-evidence.

Comments

@antoninbas
Copy link
Contributor

We have a few unit tests which require environment variables to be set. At the moment, we typically call os.Setenv, with a deferred call to os.Unsetenv to undo the change. For example:

_ = os.Setenv(NodeNameEnvKey, k)
defer os.Unsetenv(NodeNameEnvKey)

https://pkg.go.dev/testing#T.Setenv has been available since Go 1.17. It is a cleaner way to set environment variables for unit tests, and the variable will be unset automatically during test cleanup. We already use it in one test file: https://github.com/antrea-io/antrea/blob/main/pkg/apiserver/certificate/selfsignedcert_provider_test.go

All existing unit tests should be updated to use t.Setenv instead of os.Setenv, for readability and consistency.

@antoninbas antoninbas added good first issue Good for newcomers area/test Issues or PRs related to unit and integration tests. priority/backlog Higher priority than priority/awaiting-more-evidence. kind/task Categorizes issue or PR as related to a routine task that needs to be performed labels Mar 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/test Issues or PRs related to unit and integration tests. good first issue Good for newcomers kind/task Categorizes issue or PR as related to a routine task that needs to be performed priority/backlog Higher priority than priority/awaiting-more-evidence.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant