fix: warn instead of error when COPY wildcard does not match any files (#3127)

* when using wildcard hitting ResolveEnvAndWildcards ignore error when there is no file match

* adding unit test

* fix unit test wrong check in expected

* fix unit test for make sure there is no file copied

* copy with wildcard warning and success when no match file

* fix Test_IsSrcsValid since its changes the behaviour
This commit is contained in:
Prima Adi Pradana 2024-05-14 12:38:51 +07:00 committed by GitHub
parent 423d20cee2
commit 34905d62e2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 53 additions and 3 deletions

View File

@ -450,8 +450,9 @@ func Test_CopyEnvAndWildcards(t *testing.T) {
return testDir, filepath.Base(dir) return testDir, filepath.Base(dir)
} }
testDir, srcDir := setupDirs(t)
t.Run("copy sources into a dir defined in env variable", func(t *testing.T) { t.Run("copy sources into a dir defined in env variable", func(t *testing.T) {
testDir, srcDir := setupDirs(t)
defer os.RemoveAll(testDir) defer os.RemoveAll(testDir)
expected, err := readDirectory(filepath.Join(testDir, srcDir)) expected, err := readDirectory(filepath.Join(testDir, srcDir))
if err != nil { if err != nil {
@ -487,6 +488,44 @@ func Test_CopyEnvAndWildcards(t *testing.T) {
testutil.CheckDeepEqual(t, expected[i].Name(), f.Name()) testutil.CheckDeepEqual(t, expected[i].Name(), f.Name())
testutil.CheckDeepEqual(t, expected[i].Mode(), f.Mode()) testutil.CheckDeepEqual(t, expected[i].Mode(), f.Mode())
} }
})
t.Run("copy sources into a dir defined in env variable with no file found", func(t *testing.T) {
testDir, srcDir := setupDirs(t)
defer os.RemoveAll(testDir)
targetPath := filepath.Join(testDir, "target") + "/"
cmd := CopyCommand{
cmd: &instructions.CopyCommand{
//should only dam and bam be copied
SourcesAndDest: instructions.SourcesAndDest{SourcePaths: []string{srcDir + "/tam[s]"}, DestPath: "$TARGET_PATH"},
},
fileContext: util.FileContext{Root: testDir},
}
cfg := &v1.Config{
Cmd: nil,
Env: []string{"TARGET_PATH=" + targetPath},
WorkingDir: testDir,
}
err := cmd.ExecuteCommand(cfg, dockerfile.NewBuildArgs([]string{}))
if err != nil {
t.Fatal(err)
}
testutil.CheckNoError(t, err)
actual, err := readDirectory(targetPath)
//check it should error out since no files are copied and targetPath is not created
if err == nil {
t.Fatal("expected error no dirrectory but got nil")
}
//actual should empty since no files are copied
testutil.CheckDeepEqual(t, 0, len(actual))
}) })
} }

View File

@ -286,8 +286,10 @@ func IsSrcsValid(srcsAndDest instructions.SourcesAndDest, resolvedSources []stri
totalFiles++ totalFiles++
} }
} }
// ignore the case where whildcards and there are no files to copy
if totalFiles == 0 { if totalFiles == 0 {
return errors.New("copy failed: no source files specified") // using log warning instead of return errors.New("copy failed: no source files specified")
logrus.Warn("No files to copy")
} }
// If there are wildcards, and the destination is a file, there must be exactly one file to copy over, // If there are wildcards, and the destination is a file, there must be exactly one file to copy over,
// Otherwise, return an error // Otherwise, return an error

View File

@ -489,7 +489,16 @@ var isSrcValidTests = []struct {
"ignore/baz", "ignore/baz",
"ignore/bar", "ignore/bar",
}, },
shouldErr: true, shouldErr: false,
},
{
name: "copy two srcs, wildcard and no file match, to file",
srcsAndDest: []string{
"ignore/ba[s]",
"dest",
},
resolvedSources: []string{},
shouldErr: false,
}, },
} }