Skip to content

*_test.go: use t.TempDir, t.Setenv #2417

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

Merged
merged 3 commits into from
Apr 15, 2025

Conversation

kolyshkin
Copy link
Contributor

@kolyshkin kolyshkin commented Apr 11, 2025

Those are cases not reported by usetesting linter (most probably because it doesn't grok ginkgo tests).

It's hard to separate the changes into smaller commits, thus it's somewhat harder to review.

Summary by Sourcery

Refactor test code to use Ginkgo's t.TempDir() and t.Setenv() methods for better test management and cleanup

Enhancements:

  • Simplify temporary file and environment variable management in test cases
  • Remove manual cleanup of temporary resources by leveraging Ginkgo's built-in test helpers

Tests:

  • Replace manual temporary directory and environment variable management with Ginkgo's t.TempDir() and t.Setenv() methods
  • Remove redundant cleanup code in test cases

Copy link

sourcery-ai bot commented Apr 11, 2025

Reviewer's Guide by Sourcery

This PR refactors the test suite to use Ginkgo's t.TempDir() and t.Setenv() methods for improved test hygiene. This simplifies test setup and teardown by automatically cleaning up temporary directories and restoring environment variables after each test. Additionally, t.Cleanup was introduced to ensure that module paths are restored after the test.

No diagrams generated as the changes look simple and do not need a visual representation.

File-Level Changes

Change Details Files
Refactor tests to use t.TempDir() to create temporary directories, ensuring automatic cleanup after the test.
  • Replaced os.MkdirTemp() with t.TempDir() to create temporary directories.
  • Removed defer os.RemoveAll() calls as t.TempDir() handles cleanup automatically.
pkg/config/config_test.go
pkg/config/config_local_test.go
pkg/config/config_remote_test.go
libnetwork/cni/run_test.go
libnetwork/netavark/run_test.go
libnetwork/netavark/ipam_test.go
libnetwork/netavark/config_test.go
libnetwork/netavark/plugin_test.go
libnetwork/cni/config_test.go
Refactor tests to use t.Setenv() to set environment variables, ensuring automatic restoration after the test.
  • Replaced os.Setenv() with t.Setenv() to set environment variables.
  • Removed manual saving and restoring of environment variables.
  • Removed defer os.Unsetenv() calls as t.Setenv() handles cleanup automatically.
pkg/config/config_test.go
pkg/config/modules_test.go
pkg/config/config_local_test.go
pkg/auth/auth_test.go
pkg/config/connections_test.go
libnetwork/cni/run_test.go
Introduce t.Cleanup to ensure that module paths are restored after the test.
  • Replaced the returned function with t.Cleanup to restore module paths.
pkg/config/modules_test.go

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@kolyshkin kolyshkin force-pushed the use-t branch 2 times, most recently from 13669b8 to 96fb2c6 Compare April 11, 2025 22:51
@mheon
Copy link
Member

mheon commented Apr 11, 2025

Like the direction here, a lot cleaner with these helpers

@kolyshkin kolyshkin force-pushed the use-t branch 3 times, most recently from 8196af1 to 8c9ec13 Compare April 12, 2025 02:04
@kolyshkin kolyshkin marked this pull request as ready for review April 12, 2025 02:04
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @kolyshkin - I've reviewed your changes - here's some feedback:

Overall Comments:

  • This looks like a good cleanup of test code, thanks for using t.TempDir and t.Setenv!
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Member

@Luap99 Luap99 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that seems fine here but as general thing there is some IMO unexpected behavior in ginkgo when mixing AfterEach() and DeferCleanup() (used behind the scenes by ginkgo here to register the cleanup functions for the tmpdir, etc...)
ref
containers/podman@3c0176b onsi/ginkgo#1360

I cannot see any case where this would matter here but I still would have a slight preference to migrate away from AfterEach() here and use DeferCleanup() directly where needed to perfom extra cleanup in the ginkgo tests

Using os.Setenv in tests is problematic, because the change is
process-wise and other tests running in parallel might be affected.
Also, a somewhat complicated cleanup is needed.

Both issues are solved by using t.Setenv.

This commit also uses t.TempDir, t.Cleanup, and t.Helper when it makes
sense.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
Instead of using os.MkdirTemp in tests, use t.TempDir.

In a few places where the parent directory is already create by t.TempDir,
replace os.MkdirTemp with os.Mkdir.

While at it, make sure to not a leak opened file descriptor returned by
os.CreateTemp.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
@kolyshkin
Copy link
Contributor Author

Thanks, that seems fine here but as general thing there is some IMO unexpected behavior in ginkgo when mixing AfterEach() and DeferCleanup() (used behind the scenes by ginkgo here to register the cleanup functions for the tmpdir, etc...) ref containers/podman@3c0176b onsi/ginkgo#1360

I cannot see any case where this would matter here but I still would have a slight preference to migrate away from AfterEach() here and use DeferCleanup() directly where needed to perfom extra cleanup in the ginkgo tests

Done (as a separate third commit).

In Ginkgo, there might be some unexpected behavior when mising AfterEach
and DeferCleanup (see [1], [2]), so let's switch to DeferCleanup.

[1]: containers/podman@3c0176b2d0dd3
[2]: onsi/ginkgo#1360

Reported-by: Paul Holzinger <pholzing@redhat.com>
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
Copy link
Member

@Luap99 Luap99 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks

Copy link
Contributor

openshift-ci bot commented Apr 15, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: kolyshkin, Luap99, sourcery-ai[bot]

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@rhatdan
Copy link
Member

rhatdan commented Apr 15, 2025

/lgtm

@openshift-ci openshift-ci bot added the lgtm label Apr 15, 2025
@openshift-merge-bot openshift-merge-bot bot merged commit c0f75ea into containers:main Apr 15, 2025
15 checks passed
@Luap99
Copy link
Member

Luap99 commented Apr 23, 2025

FYI, this did indeed cause a test regression (flake). But only because the test was already badly written before: aaa527e

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants