Fix COPY fails when multiple files are copied to path specified in ENV (#3034)
* Fixed unsolved ENV variable used as dest of COPY * added unit test for copy cmd using env var as dest * remove comment
This commit is contained in:
parent
8148159c30
commit
ba433abdb6
|
|
@ -426,6 +426,70 @@ func Test_resolveIfSymlink(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_CopyEnvAndWildcards(t *testing.T) {
|
||||||
|
setupDirs := func(t *testing.T) (string, string) {
|
||||||
|
testDir := t.TempDir()
|
||||||
|
|
||||||
|
dir := filepath.Join(testDir, "bar")
|
||||||
|
|
||||||
|
if err := os.MkdirAll(dir, 0777); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
file := filepath.Join(dir, "bam.txt")
|
||||||
|
|
||||||
|
if err := os.WriteFile(file, []byte("meow"), 0777); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
targetPath := filepath.Join(dir, "dam.txt")
|
||||||
|
if err := os.WriteFile(targetPath, []byte("woof"), 0777); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := os.Symlink("dam.txt", filepath.Join(dir, "sym.link")); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return testDir, filepath.Base(dir)
|
||||||
|
}
|
||||||
|
testDir, srcDir := setupDirs(t)
|
||||||
|
t.Run("copy sources into a dir defined in env variable", func(t *testing.T) {
|
||||||
|
defer os.RemoveAll(testDir)
|
||||||
|
expected, err := readDirectory(filepath.Join(testDir, srcDir))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
targetPath := filepath.Join(testDir, "target") + "/"
|
||||||
|
|
||||||
|
cmd := CopyCommand{
|
||||||
|
cmd: &instructions.CopyCommand{
|
||||||
|
SourcesAndDest: instructions.SourcesAndDest{SourcePaths: []string{srcDir + "/*"}, 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)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
for i, f := range actual {
|
||||||
|
testutil.CheckDeepEqual(t, expected[i].Name(), f.Name())
|
||||||
|
testutil.CheckDeepEqual(t, expected[i].Mode(), f.Mode())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) {
|
func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) {
|
||||||
setupDirs := func(t *testing.T) (string, string) {
|
setupDirs := func(t *testing.T) (string, string) {
|
||||||
testDir := t.TempDir()
|
testDir := t.TempDir()
|
||||||
|
|
|
||||||
|
|
@ -99,6 +99,7 @@ func ResolveEnvAndWildcards(sd instructions.SourcesAndDest, fileContext FileCont
|
||||||
return nil, "", errors.Wrap(err, "failed to resolve environment for dest path")
|
return nil, "", errors.Wrap(err, "failed to resolve environment for dest path")
|
||||||
}
|
}
|
||||||
dest := dests[0]
|
dest := dests[0]
|
||||||
|
sd.DestPath = dest
|
||||||
// Resolve wildcards and get a list of resolved sources
|
// Resolve wildcards and get a list of resolved sources
|
||||||
srcs, err := ResolveSources(resolvedEnvs, fileContext.Root)
|
srcs, err := ResolveSources(resolvedEnvs, fileContext.Root)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue