Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up workdir by always returning an empty filelist (rather than a… #557

Merged
merged 1 commit into from
Feb 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions pkg/commands/workdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
61 changes: 44 additions & 17 deletions pkg/commands/workdir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.
package commands

import (
"os"
"testing"

"github.com/GoogleContainerTools/kaniko/pkg/dockerfile"
Expand All @@ -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{
Expand All @@ -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)
}
}