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

test: use T.TempDir to create temporary test directory #151

Merged
merged 2 commits into from
May 19, 2022
Merged

test: use T.TempDir to create temporary test directory #151

merged 2 commits into from
May 19, 2022

Conversation

Juneezee
Copy link
Contributor

Ⅰ. Describe what this PR does

A testing cleanup.

This pull request replaces ioutil.TempDir with t.TempDir. We can use the T.TempDir function from the testing package to create temporary directory. The directory created by T.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

Ⅱ. Does this pull request fix one issue?

NONE

Ⅲ. Describe how to verify it

All tests should continue to pass. No leftover temporary directories when the tests finish.

Ⅳ. Special notes for reviews

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>
@codecov-commenter
Copy link

codecov-commenter commented May 18, 2022

Codecov Report

Merging #151 (10ed0d1) into main (0cf1616) will increase coverage by 0.01%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main     #151      +/-   ##
==========================================
+ Coverage   56.89%   56.90%   +0.01%     
==========================================
  Files          86       86              
  Lines        7887     7882       -5     
==========================================
- Hits         4487     4485       -2     
+ Misses       3006     3004       -2     
+ Partials      394      393       -1     
Flag Coverage Δ
unittests 56.90% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
pkg/util/system/util_test_tool.go 54.11% <100.00%> (+0.78%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0cf1616...10ed0d1. Read the comment docs.

Copy link
Member

@jasonliu747 jasonliu747 left a comment

Choose a reason for hiding this comment

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

This is actually a very good suggestion!
/lgtm
/approve

@@ -805,8 +804,7 @@ func TestCgroupResourceReconcile_calculateResources(t *testing.T) {
assert.NoError(t, err)
defer func() { stop <- struct{}{} }()

helper := system.NewFileTestUtil(t)
defer helper.Cleanup()
_ = system.NewFileTestUtil(t)
Copy link
Member

Choose a reason for hiding this comment

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

can we remove the equation?


helper := system.NewFileTestUtil(t)
defer helper.Cleanup()
_ = system.NewFileTestUtil(t)
Copy link
Member

Choose a reason for hiding this comment

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

can we remove the equation

func NewFileTestUtil(t *testing.T) *FileTestUtil {
tempDir, err := ioutil.TempDir("/tmp", "koordlet_test")
tempDir := t.TempDir()
Copy link
Member

Choose a reason for hiding this comment

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

It seems (*FileTestUtil).Cleanup() is useless with this patch and its references are removed.
Can we remove the Cleanup() since its work would be done implicitly?

Copy link
Member

Choose a reason for hiding this comment

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

+1

NewFileTestUtil now uses t.TempDir() to create temporary directory so
manual cleanup is no longer necessary.

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
@koordinator-bot koordinator-bot bot removed the lgtm label May 19, 2022
@Juneezee Juneezee requested a review from saintube May 19, 2022 02:19
Copy link
Member

@jasonliu747 jasonliu747 left a comment

Choose a reason for hiding this comment

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

/lgtm

@koordinator-bot koordinator-bot bot added the lgtm label May 19, 2022
Copy link
Member

@saintube saintube left a comment

Choose a reason for hiding this comment

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

/lgtm

@hormes
Copy link
Member

hormes commented May 19, 2022

/approve

@koordinator-bot
Copy link

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: hormes, jasonliu747, saintube

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

@koordinator-bot koordinator-bot bot merged commit 889465a into koordinator-sh:main May 19, 2022
@jasonliu747
Copy link
Member

jasonliu747 commented May 19, 2022

@Juneezee Hi, when I ran runtime_test.go and docker_runtime_test.go locally on my Mac, it would throw an error something like this: Unix socket path "/var/folders/t3/fm5gy2413t304ksnq2_jd91h0000gn/T/Test_GetRuntimeHandlertest_have_dockerRuntime_but_need_containerd3848019481/001/var/run/docker.sock" is too long.

Did it ever happen to you?

@Juneezee
Copy link
Contributor Author

@jasonliu747 No, I don't get this error on my Linux machine. This is probably a Mac specific issue, see https://unix.stackexchange.com/a/367012/376279

@jasonliu747
Copy link
Member

@jasonliu747 No, I don't get this error on my Linux machine. This is probably a Mac specific issue, see https://unix.stackexchange.com/a/367012/376279

Could you try to fix this? I believe majority of programmer are using Mac for development.

@Juneezee
Copy link
Contributor Author

@jasonliu747 PR #156 fixes the issue.

I think the only way to tell t.TempDir() to use /tmp on MacOS is to set the environment variable $TMPDIR to empty so that it uses /tmp (https://github.com/golang/go/blob/128279e5034ca29bad4320eef81a8abd5b40ea7e/src/os/file.go#L379-L390). This might do more harm than good and break other tests.

Another way is to use ioutil.TempDir and specify /tmp explicitly, just like in #156.

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.

None yet

5 participants