diff --git a/pkg/commands/workdir.go b/pkg/commands/workdir.go index abc1f9c065..179a6d4c8e 100644 --- a/pkg/commands/workdir.go +++ b/pkg/commands/workdir.go @@ -34,6 +34,9 @@ type WorkdirCommand struct { snapshotFiles []string } +// For testing +var mkdir = os.MkdirAll + func (w *WorkdirCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.BuildArgs) error { logrus.Info("cmd: workdir") workdirPath := w.cmd.Path @@ -50,10 +53,11 @@ func (w *WorkdirCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile logrus.Infof("Changed working directory to %s", config.WorkingDir) // Only create and snapshot the dir if it didn't exist already + w.snapshotFiles = []string{} if _, err := os.Stat(config.WorkingDir); os.IsNotExist(err) { logrus.Infof("Creating directory %s", config.WorkingDir) - w.snapshotFiles = []string{config.WorkingDir} - return os.MkdirAll(config.WorkingDir, 0755) + w.snapshotFiles = append(w.snapshotFiles, config.WorkingDir) + return mkdir(config.WorkingDir, 0755) } return nil } diff --git a/pkg/commands/workdir_test.go b/pkg/commands/workdir_test.go index 5b16a8840f..be67098fa1 100644 --- a/pkg/commands/workdir_test.go +++ b/pkg/commands/workdir_test.go @@ -16,6 +16,7 @@ limitations under the License. package commands import ( + "os" "testing" "github.com/GoogleContainerTools/kaniko/pkg/dockerfile" @@ -30,41 +31,66 @@ import ( // This is needed to make sure WorkingDir handles paths correctly // For example, if WORKDIR specifies a non-absolute path, it should be appended to the current WORKDIR var workdirTests = []struct { - path string - expectedPath string + path string + expectedPath string + snapshotFiles []string }{ { - path: "/a", - expectedPath: "/a", + path: "/a", + expectedPath: "/a", + snapshotFiles: []string{"/a"}, }, { - path: "b", - expectedPath: "/a/b", + path: "b", + expectedPath: "/a/b", + snapshotFiles: []string{"/a/b"}, }, { - path: "c", - expectedPath: "/a/b/c", + path: "c", + expectedPath: "/a/b/c", + snapshotFiles: []string{"/a/b/c"}, }, { - path: "/d", - expectedPath: "/d", + path: "/d", + expectedPath: "/d", + snapshotFiles: []string{"/d"}, }, { - path: "$path", - expectedPath: "/d/usr", + path: "$path", + expectedPath: "/d/usr", + snapshotFiles: []string{"/d/usr"}, }, { - path: "$home", - expectedPath: "/root", + path: "$home", + expectedPath: "/root", + snapshotFiles: []string{}, }, { - path: "$path/$home", - expectedPath: "/root/usr/root", + path: "/foo/$path/$home", + expectedPath: "/foo/usr/root", + snapshotFiles: []string{"/foo/usr/root"}, + }, + { + path: "/tmp", + expectedPath: "/tmp", + snapshotFiles: []string{}, }, } +// For testing +func mockDir(p string, fi os.FileMode) error { + return nil +} func TestWorkdirCommand(t *testing.T) { + // Mock out mkdir for testing. + oldMkdir := mkdir + mkdir = mockDir + + defer func() { + mkdir = oldMkdir + }() + cfg := &v1.Config{ WorkingDir: "/", Env: []string{ @@ -78,10 +104,11 @@ func TestWorkdirCommand(t *testing.T) { cmd: &instructions.WorkdirCommand{ Path: test.path, }, - snapshotFiles: []string{}, + snapshotFiles: nil, } buildArgs := dockerfile.NewBuildArgs([]string{}) cmd.ExecuteCommand(cfg, buildArgs) testutil.CheckErrorAndDeepEqual(t, false, nil, test.expectedPath, cfg.WorkingDir) + testutil.CheckErrorAndDeepEqual(t, false, nil, test.snapshotFiles, cmd.snapshotFiles) } }