diff --git a/pkg/commands/add.go b/pkg/commands/add.go index 769d050dd..7a3d6164b 100644 --- a/pkg/commands/add.go +++ b/pkg/commands/add.go @@ -19,12 +19,11 @@ package commands import ( "path/filepath" + v1 "github.com/google/go-containerregistry/pkg/v1" "github.com/moby/buildkit/frontend/dockerfile/instructions" "github.com/GoogleContainerTools/kaniko/pkg/dockerfile" - "github.com/google/go-containerregistry/pkg/v1" - "github.com/GoogleContainerTools/kaniko/pkg/util" "github.com/sirupsen/logrus" ) diff --git a/pkg/commands/arg.go b/pkg/commands/arg.go index e17767338..598e59a50 100644 --- a/pkg/commands/arg.go +++ b/pkg/commands/arg.go @@ -19,7 +19,7 @@ package commands import ( "github.com/GoogleContainerTools/kaniko/pkg/dockerfile" "github.com/GoogleContainerTools/kaniko/pkg/util" - "github.com/google/go-containerregistry/pkg/v1" + v1 "github.com/google/go-containerregistry/pkg/v1" "github.com/moby/buildkit/frontend/dockerfile/instructions" ) diff --git a/pkg/commands/base_command.go b/pkg/commands/base_command.go index 3d2cd8fad..ddbfd650b 100644 --- a/pkg/commands/base_command.go +++ b/pkg/commands/base_command.go @@ -18,7 +18,7 @@ package commands import ( "github.com/GoogleContainerTools/kaniko/pkg/dockerfile" - "github.com/google/go-containerregistry/pkg/v1" + v1 "github.com/google/go-containerregistry/pkg/v1" ) type BaseCommand struct { diff --git a/pkg/commands/cmd.go b/pkg/commands/cmd.go index 87714430b..ff94de509 100644 --- a/pkg/commands/cmd.go +++ b/pkg/commands/cmd.go @@ -20,8 +20,8 @@ import ( "strings" "github.com/GoogleContainerTools/kaniko/pkg/dockerfile" + v1 "github.com/google/go-containerregistry/pkg/v1" - "github.com/google/go-containerregistry/pkg/v1" "github.com/moby/buildkit/frontend/dockerfile/instructions" ) diff --git a/pkg/commands/cmd_test.go b/pkg/commands/cmd_test.go index b56786ad1..182e9707c 100644 --- a/pkg/commands/cmd_test.go +++ b/pkg/commands/cmd_test.go @@ -19,7 +19,7 @@ import ( "testing" "github.com/GoogleContainerTools/kaniko/testutil" - "github.com/google/go-containerregistry/pkg/v1" + v1 "github.com/google/go-containerregistry/pkg/v1" "github.com/moby/buildkit/frontend/dockerfile/instructions" ) diff --git a/pkg/commands/commands.go b/pkg/commands/commands.go index 2615138be..a2ea7788d 100644 --- a/pkg/commands/commands.go +++ b/pkg/commands/commands.go @@ -18,7 +18,7 @@ package commands import ( "github.com/GoogleContainerTools/kaniko/pkg/dockerfile" - "github.com/google/go-containerregistry/pkg/v1" + v1 "github.com/google/go-containerregistry/pkg/v1" "github.com/moby/buildkit/frontend/dockerfile/instructions" "github.com/pkg/errors" "github.com/sirupsen/logrus" diff --git a/pkg/commands/copy.go b/pkg/commands/copy.go index 9af5ce3fd..c8ec11427 100644 --- a/pkg/commands/copy.go +++ b/pkg/commands/copy.go @@ -27,7 +27,7 @@ import ( "github.com/GoogleContainerTools/kaniko/pkg/dockerfile" "github.com/GoogleContainerTools/kaniko/pkg/util" - "github.com/google/go-containerregistry/pkg/v1" + v1 "github.com/google/go-containerregistry/pkg/v1" ) type CopyCommand struct { diff --git a/pkg/commands/copy_test.go b/pkg/commands/copy_test.go index d99e756aa..0519f6682 100644 --- a/pkg/commands/copy_test.go +++ b/pkg/commands/copy_test.go @@ -16,41 +16,103 @@ limitations under the License. package commands import ( + "io/ioutil" + "os" + "path/filepath" "testing" + "github.com/GoogleContainerTools/kaniko/pkg/dockerfile" "github.com/GoogleContainerTools/kaniko/testutil" - "github.com/google/go-containerregistry/pkg/v1" + v1 "github.com/google/go-containerregistry/pkg/v1" "github.com/moby/buildkit/frontend/dockerfile/instructions" + "github.com/sirupsen/logrus" ) var copyTests = []struct { - SourcesAndDest []string - expectedDest []string + name string + SourcesAndDest []string + expectedDest []string }{ { - SourcesAndDest: []string{"/usr/bin/bash", "/tmp"}, - expectedDest: []string{"/tmp/bash"}, + name: "copy foo into tempCopyExecuteTest/", + SourcesAndDest: []string{"foo", "tempCopyExecuteTest/"}, + expectedDest: []string{"foo"}, }, { - SourcesAndDest: []string{"/usr/bin/bash", "/tmp/"}, - expectedDest: []string{"/tmp/bash"}, + name: "copy foo into tempCopyExecuteTest", + SourcesAndDest: []string{"foo", "tempCopyExecuteTest"}, + expectedDest: []string{"tempCopyExecuteTest"}, }, } func TestCopyExecuteCmd(t *testing.T) { - cfg := &v1.Config{ - Cmd: nil, + Cmd: nil, + Env: []string{}, + WorkingDir: "../../integration/context/", } for _, test := range copyTests { - cmd := CopyCommand{ - cmd: &instructions.CopyCommand{ - SourcesAndDest: instructions.SourcesAndDest{ - SourcesAndDest: test.SourcesAndDest, - }, - } - err := cmd.ExecuteCommand(cfg, nil) - testutil.CheckErrorAndDeepEqual(t, false, err, test.expectedDest, cfg.WorkingDir) + t.Run(test.name, func(t *testing.T) { + dirList := []string{} + + logrus.Infof("Running test: %v", test.name) + + cmd := CopyCommand{ + cmd: &instructions.CopyCommand{ + SourcesAndDest: test.SourcesAndDest, + }, + buildcontext: "../../integration/context/", + } + + buildArgs := copySetUpBuildArgs() + dest := cfg.WorkingDir + test.SourcesAndDest[len(test.SourcesAndDest)-1] + + os.RemoveAll(dest) + + err := cmd.ExecuteCommand(cfg, buildArgs) + + fi, ferr := os.Open(dest) + if ferr != nil { + t.Error() + } + defer fi.Close() + fstat, ferr := fi.Stat() + if ferr != nil { + t.Error() + } + if fstat.IsDir() { + files, ferr := ioutil.ReadDir(dest) + if ferr != nil { + t.Error() + } + for _, file := range files { + logrus.Infof("file: %v", file.Name()) + dirList = append(dirList, file.Name()) + } + } else { + dirList = append(dirList, filepath.Base(dest)) + } + //dir, err := os.Getwd() + // logrus.Infof("CWD: %v", dir) + // logrus.Infof("test.SourcesAndDest: %v", test.SourcesAndDest) + logrus.Infof("dest: %v", dest) + logrus.Infof("test.expectedDest: %v", test.expectedDest) + logrus.Infof("dirList: %v", dirList) + + testutil.CheckErrorAndDeepEqual(t, false, err, test.expectedDest, dirList) + os.RemoveAll(dest) + }) } } + +func copySetUpBuildArgs() *dockerfile.BuildArgs { + buildArgs := dockerfile.NewBuildArgs([]string{ + "buildArg1=foo", + "buildArg2=foo2", + }) + buildArgs.AddArg("buildArg1", nil) + d := "default" + buildArgs.AddArg("buildArg2", &d) + return buildArgs +} diff --git a/pkg/commands/entrypoint.go b/pkg/commands/entrypoint.go index 23f349347..83964d4fa 100644 --- a/pkg/commands/entrypoint.go +++ b/pkg/commands/entrypoint.go @@ -20,8 +20,8 @@ import ( "strings" "github.com/GoogleContainerTools/kaniko/pkg/dockerfile" + v1 "github.com/google/go-containerregistry/pkg/v1" - "github.com/google/go-containerregistry/pkg/v1" "github.com/moby/buildkit/frontend/dockerfile/instructions" ) diff --git a/pkg/commands/entrypoint_test.go b/pkg/commands/entrypoint_test.go index 5343d67fe..190c7055c 100644 --- a/pkg/commands/entrypoint_test.go +++ b/pkg/commands/entrypoint_test.go @@ -19,7 +19,7 @@ import ( "testing" "github.com/GoogleContainerTools/kaniko/testutil" - "github.com/google/go-containerregistry/pkg/v1" + v1 "github.com/google/go-containerregistry/pkg/v1" "github.com/moby/buildkit/frontend/dockerfile/instructions" ) diff --git a/pkg/commands/env.go b/pkg/commands/env.go index 5286f7260..2be271490 100644 --- a/pkg/commands/env.go +++ b/pkg/commands/env.go @@ -18,9 +18,9 @@ package commands import ( "github.com/GoogleContainerTools/kaniko/pkg/dockerfile" + v1 "github.com/google/go-containerregistry/pkg/v1" "github.com/GoogleContainerTools/kaniko/pkg/util" - "github.com/google/go-containerregistry/pkg/v1" "github.com/moby/buildkit/frontend/dockerfile/instructions" ) diff --git a/pkg/commands/env_test.go b/pkg/commands/env_test.go index d4870bb1e..ccada538c 100644 --- a/pkg/commands/env_test.go +++ b/pkg/commands/env_test.go @@ -20,7 +20,7 @@ import ( "github.com/GoogleContainerTools/kaniko/pkg/dockerfile" "github.com/GoogleContainerTools/kaniko/testutil" - "github.com/google/go-containerregistry/pkg/v1" + v1 "github.com/google/go-containerregistry/pkg/v1" "github.com/moby/buildkit/frontend/dockerfile/instructions" ) diff --git a/pkg/commands/expose.go b/pkg/commands/expose.go index 9d56ee3ea..0aacbb6cf 100644 --- a/pkg/commands/expose.go +++ b/pkg/commands/expose.go @@ -21,9 +21,9 @@ import ( "strings" "github.com/GoogleContainerTools/kaniko/pkg/dockerfile" + v1 "github.com/google/go-containerregistry/pkg/v1" "github.com/GoogleContainerTools/kaniko/pkg/util" - "github.com/google/go-containerregistry/pkg/v1" "github.com/moby/buildkit/frontend/dockerfile/instructions" "github.com/sirupsen/logrus" ) diff --git a/pkg/commands/expose_test.go b/pkg/commands/expose_test.go index 02ad17576..fa1f9520a 100644 --- a/pkg/commands/expose_test.go +++ b/pkg/commands/expose_test.go @@ -20,9 +20,9 @@ import ( "testing" "github.com/GoogleContainerTools/kaniko/pkg/dockerfile" + v1 "github.com/google/go-containerregistry/pkg/v1" "github.com/GoogleContainerTools/kaniko/testutil" - "github.com/google/go-containerregistry/pkg/v1" "github.com/moby/buildkit/frontend/dockerfile/instructions" ) diff --git a/pkg/commands/healthcheck.go b/pkg/commands/healthcheck.go index 610d9c935..af40ba652 100644 --- a/pkg/commands/healthcheck.go +++ b/pkg/commands/healthcheck.go @@ -18,7 +18,7 @@ package commands import ( "github.com/GoogleContainerTools/kaniko/pkg/dockerfile" - "github.com/google/go-containerregistry/pkg/v1" + v1 "github.com/google/go-containerregistry/pkg/v1" "github.com/moby/buildkit/frontend/dockerfile/instructions" ) diff --git a/pkg/commands/label.go b/pkg/commands/label.go index c8790824b..7568886c0 100644 --- a/pkg/commands/label.go +++ b/pkg/commands/label.go @@ -18,9 +18,9 @@ package commands import ( "github.com/GoogleContainerTools/kaniko/pkg/dockerfile" + v1 "github.com/google/go-containerregistry/pkg/v1" "github.com/GoogleContainerTools/kaniko/pkg/util" - "github.com/google/go-containerregistry/pkg/v1" "github.com/moby/buildkit/frontend/dockerfile/instructions" "github.com/sirupsen/logrus" ) diff --git a/pkg/commands/label_test.go b/pkg/commands/label_test.go index da2cfbaea..644746311 100644 --- a/pkg/commands/label_test.go +++ b/pkg/commands/label_test.go @@ -21,7 +21,7 @@ import ( "github.com/GoogleContainerTools/kaniko/pkg/dockerfile" "github.com/GoogleContainerTools/kaniko/testutil" - "github.com/google/go-containerregistry/pkg/v1" + v1 "github.com/google/go-containerregistry/pkg/v1" "github.com/moby/buildkit/frontend/dockerfile/instructions" ) diff --git a/pkg/commands/onbuild.go b/pkg/commands/onbuild.go index 0d36afe4b..74c6ad01e 100644 --- a/pkg/commands/onbuild.go +++ b/pkg/commands/onbuild.go @@ -18,7 +18,7 @@ package commands import ( "github.com/GoogleContainerTools/kaniko/pkg/dockerfile" - "github.com/google/go-containerregistry/pkg/v1" + v1 "github.com/google/go-containerregistry/pkg/v1" "github.com/moby/buildkit/frontend/dockerfile/instructions" "github.com/sirupsen/logrus" ) diff --git a/pkg/commands/onbuild_test.go b/pkg/commands/onbuild_test.go index 10ee9ba26..e2d5d940d 100644 --- a/pkg/commands/onbuild_test.go +++ b/pkg/commands/onbuild_test.go @@ -20,9 +20,9 @@ import ( "testing" "github.com/GoogleContainerTools/kaniko/pkg/dockerfile" + v1 "github.com/google/go-containerregistry/pkg/v1" "github.com/GoogleContainerTools/kaniko/testutil" - "github.com/google/go-containerregistry/pkg/v1" "github.com/moby/buildkit/frontend/dockerfile/instructions" ) diff --git a/pkg/util/fs_util.go b/pkg/util/fs_util.go index 4abfcd4a4..b9dffacc4 100644 --- a/pkg/util/fs_util.go +++ b/pkg/util/fs_util.go @@ -32,7 +32,7 @@ import ( "github.com/GoogleContainerTools/kaniko/pkg/constants" "github.com/docker/docker/builder/dockerignore" "github.com/docker/docker/pkg/fileutils" - "github.com/google/go-containerregistry/pkg/v1" + v1 "github.com/google/go-containerregistry/pkg/v1" "github.com/pkg/errors" "github.com/sirupsen/logrus" ) @@ -188,9 +188,12 @@ func extractFile(dest string, hdr *tar.Header, tr io.Reader) error { switch hdr.Typeflag { case tar.TypeReg: logrus.Debugf("creating file %s", path) - // It's possible a file is in the tar before its directory. - if _, err := os.Stat(dir); os.IsNotExist(err) { + // It's possible a file is in the tar before its directory, + // or a file was copied over a directory prior to now + fi, err := os.Stat(dir) + if os.IsNotExist(err) || !fi.IsDir() { logrus.Debugf("base %s for file %s does not exist. Creating.", base, path) + os.RemoveAll(dir) if err := os.MkdirAll(dir, 0755); err != nil { return err } diff --git a/pkg/util/image_util.go b/pkg/util/image_util.go index 0978a6fef..01dc12fae 100644 --- a/pkg/util/image_util.go +++ b/pkg/util/image_util.go @@ -28,7 +28,7 @@ import ( "github.com/GoogleContainerTools/kaniko/pkg/creds" "github.com/google/go-containerregistry/pkg/name" - "github.com/google/go-containerregistry/pkg/v1" + v1 "github.com/google/go-containerregistry/pkg/v1" "github.com/google/go-containerregistry/pkg/v1/empty" "github.com/google/go-containerregistry/pkg/v1/remote" "github.com/google/go-containerregistry/pkg/v1/tarball"