mirror of
https://github.com/aljazceru/kata-containers.git
synced 2025-12-18 23:04:20 +01:00
test: use T.TempDir to create temporary test directory
The directory created by `T.TempDir` is automatically removed when the test and all its subtests complete. This commit also updates the unit test advice to use `T.TempDir` to create temporary directory in tests. Fixes: #3924 Reference: https://pkg.go.dev/testing#T.TempDir Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
@@ -458,12 +458,8 @@ func TestMkdirAllWithInheritedOwnerSuccessful(t *testing.T) {
|
||||
t.Skip("Test disabled as requires root user")
|
||||
}
|
||||
assert := assert.New(t)
|
||||
tmpDir1, err := os.MkdirTemp("", "test")
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(tmpDir1)
|
||||
tmpDir2, err := os.MkdirTemp("", "test")
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(tmpDir2)
|
||||
tmpDir1 := t.TempDir()
|
||||
tmpDir2 := t.TempDir()
|
||||
|
||||
testCases := []struct {
|
||||
before func(rootDir string, uid, gid int)
|
||||
@@ -474,7 +470,7 @@ func TestMkdirAllWithInheritedOwnerSuccessful(t *testing.T) {
|
||||
}{
|
||||
{
|
||||
before: func(rootDir string, uid, gid int) {
|
||||
err = syscall.Chown(rootDir, uid, gid)
|
||||
err := syscall.Chown(rootDir, uid, gid)
|
||||
assert.NoError(err)
|
||||
},
|
||||
rootDir: tmpDir1,
|
||||
@@ -485,7 +481,7 @@ func TestMkdirAllWithInheritedOwnerSuccessful(t *testing.T) {
|
||||
{
|
||||
before: func(rootDir string, uid, gid int) {
|
||||
// remove the tmpDir2 so the MkdirAllWithInheritedOwner() call creates them from /tmp
|
||||
err = os.RemoveAll(tmpDir2)
|
||||
err := os.RemoveAll(tmpDir2)
|
||||
assert.NoError(err)
|
||||
},
|
||||
rootDir: tmpDir2,
|
||||
@@ -502,8 +498,10 @@ func TestMkdirAllWithInheritedOwnerSuccessful(t *testing.T) {
|
||||
|
||||
err := MkdirAllWithInheritedOwner(tc.targetDir, 0700)
|
||||
assert.NoError(err)
|
||||
// remove the first parent "/tmp" from the assertion as it's owned by root
|
||||
for _, p := range getAllParentPaths(tc.targetDir)[1:] {
|
||||
// tmpDir1: /tmp/TestMkdirAllWithInheritedOwnerSuccessful/001
|
||||
// tmpDir2: /tmp/TestMkdirAllWithInheritedOwnerSuccessful/002
|
||||
// remove the first two parent "/tmp/TestMkdirAllWithInheritedOwnerSuccessful" from the assertion as it's owned by root
|
||||
for _, p := range getAllParentPaths(tc.targetDir)[2:] {
|
||||
info, err := os.Stat(p)
|
||||
assert.NoError(err)
|
||||
assert.True(info.IsDir())
|
||||
@@ -520,12 +518,10 @@ func TestChownToParent(t *testing.T) {
|
||||
t.Skip("Test disabled as requires root user")
|
||||
}
|
||||
assert := assert.New(t)
|
||||
rootDir, err := os.MkdirTemp("", "root")
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(rootDir)
|
||||
rootDir := t.TempDir()
|
||||
uid := 1234
|
||||
gid := 5678
|
||||
err = syscall.Chown(rootDir, uid, gid)
|
||||
err := syscall.Chown(rootDir, uid, gid)
|
||||
assert.NoError(err)
|
||||
|
||||
targetDir := path.Join(rootDir, "foo")
|
||||
|
||||
Reference in New Issue
Block a user