fix: fix `COPY` command error due to missing but ignored files (#2812)

Fixes https://github.com/GoogleContainerTools/kaniko/issues/1598.

This commit puts `context.ExcludesFile` before `os.Lstat` to avoid the `COPY` command error due to missing but ignored files.
This commit is contained in:
Quan Zhang 2023-10-31 16:29:23 -04:00 committed by GitHub
parent b1a0d57a65
commit b433ddd6bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 4 deletions

View File

@ -20,6 +20,7 @@ import (
"archive/tar"
"fmt"
"io"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
@ -461,6 +462,60 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) {
}
})
t.Run("copy dir to another dir - with ignored files", func(t *testing.T) {
testDir, srcDir := setupDirs(t)
defer os.RemoveAll(testDir)
ignoredFile := "bam.txt"
srcFiles, err := ioutil.ReadDir(filepath.Join(testDir, srcDir))
if err != nil {
t.Fatal(err)
}
expected := map[string]fs.FileInfo{}
for _, sf := range srcFiles {
if sf.Name() == ignoredFile {
continue
}
expected[sf.Name()] = sf
}
cmd := CopyCommand{
cmd: &instructions.CopyCommand{
SourcesAndDest: instructions.SourcesAndDest{SourcePaths: []string{srcDir}, DestPath: "dest"},
},
fileContext: util.FileContext{
Root: testDir,
ExcludedFiles: []string{filepath.Join(srcDir, ignoredFile)}},
}
cfg := &v1.Config{
Cmd: nil,
Env: []string{},
WorkingDir: testDir,
}
err = cmd.ExecuteCommand(cfg, dockerfile.NewBuildArgs([]string{}))
if err != nil {
t.Fatal(err)
}
testutil.CheckNoError(t, err)
// Check if "dest" dir exists with contents of srcDir
actual, err := ioutil.ReadDir(filepath.Join(testDir, "dest"))
if err != nil {
t.Fatal(err)
}
if len(actual) != len(expected) {
t.Errorf("%v files are expected to be copied, but got %v", len(expected), len(actual))
}
for _, f := range actual {
if f.Name() == ignoredFile {
t.Errorf("file %v is expected to be ignored, but copied", f.Name())
}
testutil.CheckDeepEqual(t, expected[f.Name()].Name(), f.Name())
testutil.CheckDeepEqual(t, expected[f.Name()].Mode(), f.Mode())
}
})
t.Run("copy file to a dir", func(t *testing.T) {
testDir, srcDir := setupDirs(t)
defer os.RemoveAll(testDir)

View File

@ -663,14 +663,14 @@ func CopyDir(src, dest string, context FileContext, uid, gid int64) ([]string, e
var copiedFiles []string
for _, file := range files {
fullPath := filepath.Join(src, file)
fi, err := os.Lstat(fullPath)
if err != nil {
return nil, errors.Wrap(err, "copying dir")
}
if context.ExcludesFile(fullPath) {
logrus.Debugf("%s found in .dockerignore, ignoring", src)
continue
}
fi, err := os.Lstat(fullPath)
if err != nil {
return nil, errors.Wrap(err, "copying dir")
}
destPath := filepath.Join(dest, file)
if fi.IsDir() {
logrus.Tracef("Creating directory %s", destPath)