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:
parent
b1a0d57a65
commit
b433ddd6bb
|
|
@ -20,6 +20,7 @@ import (
|
||||||
"archive/tar"
|
"archive/tar"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"io/fs"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"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) {
|
t.Run("copy file to a dir", func(t *testing.T) {
|
||||||
testDir, srcDir := setupDirs(t)
|
testDir, srcDir := setupDirs(t)
|
||||||
defer os.RemoveAll(testDir)
|
defer os.RemoveAll(testDir)
|
||||||
|
|
|
||||||
|
|
@ -663,14 +663,14 @@ func CopyDir(src, dest string, context FileContext, uid, gid int64) ([]string, e
|
||||||
var copiedFiles []string
|
var copiedFiles []string
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
fullPath := filepath.Join(src, file)
|
fullPath := filepath.Join(src, file)
|
||||||
fi, err := os.Lstat(fullPath)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Wrap(err, "copying dir")
|
|
||||||
}
|
|
||||||
if context.ExcludesFile(fullPath) {
|
if context.ExcludesFile(fullPath) {
|
||||||
logrus.Debugf("%s found in .dockerignore, ignoring", src)
|
logrus.Debugf("%s found in .dockerignore, ignoring", src)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
fi, err := os.Lstat(fullPath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "copying dir")
|
||||||
|
}
|
||||||
destPath := filepath.Join(dest, file)
|
destPath := filepath.Join(dest, file)
|
||||||
if fi.IsDir() {
|
if fi.IsDir() {
|
||||||
logrus.Tracef("Creating directory %s", destPath)
|
logrus.Tracef("Creating directory %s", destPath)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue