-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
test: use T.TempDir
to create temporary test directory
#12853
Conversation
This commit replaces `ioutil.TempDir` with `t.TempDir` in tests. The directory created by `t.TempDir` is automatically removed when the test and all its subtests complete. Prior to this commit, temporary directory created using `ioutil.TempDir` needs to be removed manually by calling `os.RemoveAll`, which is omitted in some tests. The error handling boilerplate e.g. defer func() { if err := os.RemoveAll(dir); err != nil { t.Fatal(err) } } is also tedious, but `t.TempDir` handles this for us nicely. Reference: https://pkg.go.dev/testing#T.TempDir Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
t.TempDir fails to perform the cleanup properly because the folder is still in use testing.go:967: TempDir RemoveAll cleanup: unlinkat /tmp/TestConsul_Integration2837567823/002/191a6f1a-5371-cf7c-da38-220fe85d10e5/web/secrets: device or resource busy Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this PR @Juneezee!
I checked the golang changelog and t.Tempdir
was introduced in go1.15, so this is safe for us to backport as well. I've left two questions about additional changes you've included beyond that.
t.Cleanup(func() { | ||
require.NoError(lm.Stop()) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The commit message for the changes in this file was only:
Can you explain what was happening here? If you think there's a Windows-specific issue here it may actually be a Windows-specific bug! Keeping the PR to a single topic will also make it easier for us to backport it to earlier versions to make future bug fix backports easier.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a Windows-specific issue, but not a bug. You can see the full test log here: https://app.circleci.com/pipelines/github/hashicorp/nomad/23525/workflows/812a1d80-37b1-4251-8fc4-b7f192a7bf20/jobs/250935/tests#failed-test-0
The problem here is that Windows does not allow removing an open file (golang/go#50510 (comment)). .So we have to close os.File
properly, which is what the two t.Cleanup
below are doing
t.Cleanup(func() {
require.NoError(stdout.Close())
})
t.Cleanup(func() {
require.NoError(stderr.Close())
})
t.Cleanup(func() { | ||
r.NoError(allocDir.Destroy()) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to change the semantics of the test. Why do we need to Destroy
this dir instead of relying on the cleanup function to remove it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The full test log: https://app.circleci.com/pipelines/github/hashicorp/nomad/23525/workflows/812a1d80-37b1-4251-8fc4-b7f192a7bf20/jobs/250947/tests#failed-test-0
At the middle of the logs,
2022-05-03T07:40:32.419Z [WARN] taskrunner/script_check_hook.go:381: task_runner.task_hook.script_checks: updating check failed: task=web error="Unexpected response code: 500 (CheckID \"_nomad-check-fe290e29ae5ee2da59e3bdbaf221c7b18e766e4f\" does not have associated TTL)"
testing.go:967: TempDir RemoveAll cleanup: unlinkat /tmp/TestConsul_Integration1745651562/002/7f3f8cc1-961a-f83a-7daf-1554fd2cec19/web/secrets: device or resource busy
bootstrap = true: do not enable unless necessary
==> Starting Consul agent...
Version: '1.8.3'
Node ID: '2d6af774-8e9b-b8c8-cbe5-557349a513fc'
Node name: 'node-2d6af774-8e9b-b8c8-cbe5-557349a513fc'
Datacenter: 'dc1' (Segment: '<all>')
Server: true (Bootstrap: true)
Client Addr: [127.0.0.1] (HTTP: 22260, HTTPS: 22261, gRPC: -1, DNS: 22259)
Cluster Addr: 127.0.0.1 (LAN: 22262, WAN: 22263)
Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false, Auto-Encrypt-TLS: false
==> Log data will now stream in as it occurs:
t.TempDir
failed to perform the cleanup because the child folder is still being used. allocDir.Destroy()
is required here to unmount the directory
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, thanks for following up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Hey @Juneezee just a heads up that we're in the middle of cutting the GA from our release candidate and that'll be shipping shortly. So I'm going to hold off on merging this until we've shipped the GA. Thanks again for contributing this! |
This is awesome, thanks @Juneezee ! |
I'm going to lock this pull request because it has been closed for 120 days ⏳. This helps our maintainers find and focus on the active contributions. |
A testing cleanup.
This pull request replaces
ioutil.TempDir
witht.TempDir
. We can use theT.TempDir
function from thetesting
package to create temporary directory. The directory created byT.TempDir
is automatically removed when the test and all its subtests complete.This saves us at least 2 lines (error check, and cleanup) on every instance, or in some cases adds cleanup that we forgot.
Reference: https://pkg.go.dev/testing#T.TempDir
Related PR in Terraform: hashicorp/terraform#30803