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 #1918

Merged
merged 1 commit into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 7 additions & 17 deletions pkg/commands/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,8 @@ var copyTests = []struct {
},
}

func setupTestTemp() string {
tempDir, err := ioutil.TempDir("", "")
if err != nil {
logrus.Fatalf("error creating temp dir %s", err)
}
func setupTestTemp(t *testing.T) string {
tempDir := t.TempDir()
logrus.Debugf("Tempdir: %s", tempDir)

srcPath, err := filepath.Abs("../../integration/context")
Expand Down Expand Up @@ -108,9 +105,9 @@ func setupTestTemp() string {
}

func Test_CachingCopyCommand_ExecuteCommand(t *testing.T) {
tempDir := setupTestTemp()
tempDir := setupTestTemp(t)

tarContent, err := prepareTarFixture([]string{"foo.txt"})
tarContent, err := prepareTarFixture(t, []string{"foo.txt"})
if err != nil {
t.Errorf("couldn't prepare tar fixture %v", err)
}
Expand Down Expand Up @@ -261,8 +258,7 @@ func Test_CachingCopyCommand_ExecuteCommand(t *testing.T) {
}

func TestCopyExecuteCmd(t *testing.T) {
tempDir := setupTestTemp()
defer os.RemoveAll(tempDir)
tempDir := setupTestTemp(t)

cfg := &v1.Config{
Cmd: nil,
Expand Down Expand Up @@ -336,10 +332,7 @@ func Test_resolveIfSymlink(t *testing.T) {
err error
}

tmpDir, err := ioutil.TempDir("", "copy-test")
if err != nil {
t.Error(err)
}
tmpDir := t.TempDir()

baseDir, err := ioutil.TempDir(tmpDir, "not-linked")
if err != nil {
Expand Down Expand Up @@ -394,10 +387,7 @@ func Test_resolveIfSymlink(t *testing.T) {

func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) {
setupDirs := func(t *testing.T) (string, string) {
testDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
testDir := t.TempDir()

dir := filepath.Join(testDir, "bar")

Expand Down
14 changes: 4 additions & 10 deletions pkg/commands/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,8 @@ func Test_addDefaultHOME(t *testing.T) {
}
}

func prepareTarFixture(fileNames []string) ([]byte, error) {
dir, err := ioutil.TempDir("/tmp", "tar-fixture")
if err != nil {
return nil, err
}
func prepareTarFixture(t *testing.T, fileNames []string) ([]byte, error) {
dir := t.TempDir()

content := `
Meow meow meow meow
Expand Down Expand Up @@ -174,7 +171,7 @@ meow meow meow meow
}

func Test_CachingRunCommand_ExecuteCommand(t *testing.T) {
tarContent, err := prepareTarFixture([]string{"foo.txt"})
tarContent, err := prepareTarFixture(t, []string{"foo.txt"})
if err != nil {
t.Errorf("couldn't prepare tar fixture %v", err)
}
Expand Down Expand Up @@ -318,10 +315,7 @@ func Test_CachingRunCommand_ExecuteCommand(t *testing.T) {
}

func TestSetWorkDirIfExists(t *testing.T) {
testDir, err := ioutil.TempDir("", "workdir")
if err != nil {
t.Error(err)
}
testDir := t.TempDir()
testutil.CheckDeepEqual(t, testDir, setWorkDirIfExists(testDir))
testutil.CheckDeepEqual(t, "", setWorkDirIfExists("doesnot-exists"))
}
45 changes: 10 additions & 35 deletions pkg/executor/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,15 +430,11 @@ func Test_filesToSave(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "")
tmpDir := t.TempDir()
original := config.RootDir
config.RootDir = tmpDir
if err != nil {
t.Errorf("error creating tmpdir: %s", err)
}
defer func() {
config.RootDir = original
os.RemoveAll(tmpDir)
}()

for _, f := range tt.files {
Expand Down Expand Up @@ -762,10 +758,7 @@ func Test_stageBuilder_build(t *testing.T) {
},
}

destDir, err := ioutil.TempDir("", "baz")
if err != nil {
t.Errorf("could not create temp dir %v", err)
}
destDir := t.TempDir()
return testcase{
description: "fake command cache enabled but key not in cache",
config: &v1.ConfigFile{Config: v1.Config{WorkingDir: destDir}},
Expand Down Expand Up @@ -795,10 +788,7 @@ func Test_stageBuilder_build(t *testing.T) {
},
}

destDir, err := ioutil.TempDir("", "baz")
if err != nil {
t.Errorf("could not create temp dir %v", err)
}
destDir := t.TempDir()
return testcase{
description: "fake command cache enabled and key in cache",
opts: &config.KanikoOptions{Cache: true},
Expand Down Expand Up @@ -831,10 +821,7 @@ func Test_stageBuilder_build(t *testing.T) {
},
}

destDir, err := ioutil.TempDir("", "baz")
if err != nil {
t.Errorf("could not create temp dir %v", err)
}
destDir := t.TempDir()
return testcase{
description: "fake command cache enabled with tar compression disabled and key in cache",
opts: &config.KanikoOptions{Cache: true, CompressedCaching: false},
Expand Down Expand Up @@ -932,10 +919,7 @@ COPY %s foo.txt
dir, filenames := tempDirAndFile(t)
filename := filenames[0]
tarContent := []byte{}
destDir, err := ioutil.TempDir("", "baz")
if err != nil {
t.Errorf("could not create temp dir %v", err)
}
destDir := t.TempDir()
filePath := filepath.Join(dir, filename)
ch := NewCompositeCache("", fmt.Sprintf("COPY %s foo.txt", filename))
ch.AddPath(filePath, util.FileContext{})
Expand Down Expand Up @@ -993,10 +977,7 @@ COPY %s foo.txt
filename := filenames[0]
tarContent := generateTar(t, filename)

destDir, err := ioutil.TempDir("", "baz")
if err != nil {
t.Errorf("could not create temp dir %v", err)
}
destDir := t.TempDir()
filePath := filepath.Join(dir, filename)

ch := NewCompositeCache("", "RUN foobar")
Expand Down Expand Up @@ -1070,18 +1051,15 @@ COPY %s bar.txt
filename := filenames[0]
tarContent := generateTar(t, filename)

destDir, err := ioutil.TempDir("", "baz")
if err != nil {
t.Errorf("could not create temp dir %v", err)
}
destDir := t.TempDir()

filePath := filepath.Join(dir, filename)

ch := NewCompositeCache("", fmt.Sprintf("COPY %s bar.txt", filename))
ch.AddPath(filePath, util.FileContext{})

// copy hash
_, err = ch.Hash()
_, err := ch.Hash()
if err != nil {
t.Errorf("couldn't create hash %v", err)
}
Expand Down Expand Up @@ -1372,13 +1350,10 @@ func getCommands(fileContext util.FileContext, cmds []instructions.Command, cach
func tempDirAndFile(t *testing.T) (string, []string) {
filenames := []string{"bar.txt"}

dir, err := ioutil.TempDir("", "foo")
if err != nil {
t.Errorf("could not create temp dir %v", err)
}
dir := t.TempDir()
for _, filename := range filenames {
filepath := filepath.Join(dir, filename)
err = ioutil.WriteFile(filepath, []byte(`meow`), 0777)
err := ioutil.WriteFile(filepath, []byte(`meow`), 0777)
if err != nil {
t.Errorf("could not create temp file %v", err)
}
Expand Down
87 changes: 20 additions & 67 deletions pkg/executor/composite_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ func Test_CompositeCache_Hash(t *testing.T) {
}

func Test_CompositeCache_AddPath_dir(t *testing.T) {
tmpDir, err := ioutil.TempDir("/tmp", "foo")
if err != nil {
t.Errorf("got error setting up test %v", err)
}
tmpDir := t.TempDir()

content := `meow meow meow`
if err := ioutil.WriteFile(filepath.Join(tmpDir, "foo.txt"), []byte(content), 0777); err != nil {
Expand Down Expand Up @@ -158,14 +155,10 @@ func createFilesystemStructure(root string, directories, files []string) error {
return nil
}

func setIgnoreContext(content string) (util.FileContext, error) {
func setIgnoreContext(t *testing.T, content string) (util.FileContext, error) {
var fileContext util.FileContext
dockerIgnoreDir, err := ioutil.TempDir("", "")
if err != nil {
return fileContext, err
}
defer os.RemoveAll(dockerIgnoreDir)
err = ioutil.WriteFile(dockerIgnoreDir+".dockerignore", []byte(content), 0644)
dockerIgnoreDir := t.TempDir()
err := ioutil.WriteFile(dockerIgnoreDir+".dockerignore", []byte(content), 0644)
if err != nil {
return fileContext, err
}
Expand Down Expand Up @@ -218,21 +211,13 @@ func Test_CompositeKey_AddPath_Works(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
testDir1, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("Error creating tempdir: %s", err)
}
defer os.RemoveAll(testDir1)
err = createFilesystemStructure(testDir1, test.directories, test.files)
testDir1 := t.TempDir()
err := createFilesystemStructure(testDir1, test.directories, test.files)
if err != nil {
t.Fatalf("Error creating filesytem structure: %s", err)
}

testDir2, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("Error creating tempdir: %s", err)
}
defer os.RemoveAll(testDir2)
testDir2 := t.TempDir()
err = createFilesystemStructure(testDir2, test.directories, test.files)
if err != nil {
t.Fatalf("Error creating filesytem structure: %s", err)
Expand Down Expand Up @@ -291,21 +276,13 @@ func Test_CompositeKey_AddPath_WithExtraFile_Works(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
testDir1, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("Error creating tempdir: %s", err)
}
defer os.RemoveAll(testDir1)
err = createFilesystemStructure(testDir1, test.directories, test.files)
testDir1 := t.TempDir()
err := createFilesystemStructure(testDir1, test.directories, test.files)
if err != nil {
t.Fatalf("Error creating filesytem structure: %s", err)
}

testDir2, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("Error creating tempdir: %s", err)
}
defer os.RemoveAll(testDir2)
testDir2 := t.TempDir()
err = createFilesystemStructure(testDir2, test.directories, test.files)
if err != nil {
t.Fatalf("Error creating filesytem structure: %s", err)
Expand Down Expand Up @@ -369,21 +346,13 @@ func Test_CompositeKey_AddPath_WithExtraDir_Works(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
testDir1, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("Error creating tempdir: %s", err)
}
defer os.RemoveAll(testDir1)
err = createFilesystemStructure(testDir1, test.directories, test.files)
testDir1 := t.TempDir()
err := createFilesystemStructure(testDir1, test.directories, test.files)
if err != nil {
t.Fatalf("Error creating filesytem structure: %s", err)
}

testDir2, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("Error creating tempdir: %s", err)
}
defer os.RemoveAll(testDir2)
testDir2 := t.TempDir()
err = createFilesystemStructure(testDir2, test.directories, test.files)
if err != nil {
t.Fatalf("Error creating filesytem structure: %s", err)
Expand Down Expand Up @@ -443,28 +412,20 @@ func Test_CompositeKey_AddPath_WithExtraFilIgnored_Works(t *testing.T) {
},
}

fileContext, err := setIgnoreContext("**/extra")
fileContext, err := setIgnoreContext(t, "**/extra")
if err != nil {
t.Fatalf("Error setting exlusion context: %s", err)
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
testDir1, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("Error creating tempdir: %s", err)
}
defer os.RemoveAll(testDir1)
testDir1 := t.TempDir()
err = createFilesystemStructure(testDir1, test.directories, test.files)
if err != nil {
t.Fatalf("Error creating filesytem structure: %s", err)
}

testDir2, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("Error creating tempdir: %s", err)
}
defer os.RemoveAll(testDir2)
testDir2 := t.TempDir()
err = createFilesystemStructure(testDir2, test.directories, test.files)
if err != nil {
t.Fatalf("Error creating filesytem structure: %s", err)
Expand Down Expand Up @@ -524,28 +485,20 @@ func Test_CompositeKey_AddPath_WithExtraDirIgnored_Works(t *testing.T) {
},
}

fileContext, err := setIgnoreContext("**/extra")
fileContext, err := setIgnoreContext(t, "**/extra")
if err != nil {
t.Fatalf("Error setting exlusion context: %s", err)
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
testDir1, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("Error creating tempdir: %s", err)
}
defer os.RemoveAll(testDir1)
err = createFilesystemStructure(testDir1, test.directories, test.files)
testDir1 := t.TempDir()
err := createFilesystemStructure(testDir1, test.directories, test.files)
if err != nil {
t.Fatalf("Error creating filesytem structure: %s", err)
}

testDir2, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("Error creating tempdir: %s", err)
}
defer os.RemoveAll(testDir2)
testDir2 := t.TempDir()
err = createFilesystemStructure(testDir2, test.directories, test.files)
if err != nil {
t.Fatalf("Error creating filesytem structure: %s", err)
Expand Down
5 changes: 1 addition & 4 deletions pkg/executor/copy_multistage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,7 @@ COPY --from=first copied another`)
}

func setupMultistageTests(t *testing.T) (string, func()) {
testDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
testDir := t.TempDir()

// Create workspace with files, dirs, and symlinks
// workspace tree:
Expand Down
Loading