-
Notifications
You must be signed in to change notification settings - Fork 374
unit-test: cleaning up stale files under /tmp #2399
Conversation
os.Exit will skip all deferred instructions. So we should reconstruct TestMain to leave all setup-related code in setup(), and all cleanup-related code in shutdown(). Fixes: kata-containers#2398 Signed-off-by: Penny Zheng <penny.zheng@arm.com>
Normally, ioutil.TempDir will create a new temporary dir under /tmp. And we should do cleaning up after ioutil.TempDir(). Fixes: kata-containers#2398 Signed-off-by: Penny Zheng <penny.zheng@arm.com>
/test |
Codecov Report
@@ Coverage Diff @@
## master #2399 +/- ##
==========================================
+ Coverage 50.07% 50.99% +0.92%
==========================================
Files 112 112
Lines 16178 16148 -30
==========================================
+ Hits 8101 8235 +134
+ Misses 7063 6897 -166
- Partials 1014 1016 +2 |
ioutil.TempFile creates a new temporary file in the directory dir. It is the caller's responsibility to remove the file when no longer needed. Fixes: kata-containers#2398 Signed-off-by: Penny Zheng <penny.zheng@arm.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 @Pennyzct - lgtm - nice work!
setup() | ||
rt := m.Run() | ||
shutdown() | ||
os.Exit(rt) |
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.
OOI, in case you have never seen this - we check for no uses of os.Exit()
in the main codebase in the CI:
https://github.com/kata-containers/runtime/blob/master/.ci/go-no-os-exit.sh
(I think that is a feature we've had for a very very long time and is little seen, so just raising here for general awareness).
but, maybe that does not apply to test code.
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.
Hi~ @grahamwhaley Thanks for the notification. ;).
I'm afraid that it's not applicable for go unit test code, referring from Go.Doc:
TestMain runs in the main goroutine and can do whatever setup and teardown
is necessary around a call to m.Run.
It should then call os.Exit with the result of m.Run.
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 @Pennyzct
Thanks @Pennyzct . acrn test changes look fine to me. |
/test |
firecracker CI failed irrelevantly. All the other are green~~~ |
I've found a few unit tests which were generating stale files under
/tmp
- Using
defer
in TestMain.os.Exit will skip all deferred instructions.
So we should reconstruct TestMain to leave all setup-related code in setup(), and all cleanup-related code in shutdown().
- Missing deleting what
ioutil.TempDir()
createsNormally,
ioutil.TempDir
will create a new temporary dir under/tmp
.- Missing deleting what
ioutil.TempFile()
createsioutil.TempFile
creates a new temporary file in the directory/tmp
.It is the caller's responsibility to remove the file when no longer needed.