From feaf5902f650f2326e1c41e82dfe28962f1ba46e Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Thu, 22 Dec 2016 13:25:02 -0800 Subject: [PATCH] Move builder cli helper functions to own pkg Signed-off-by: Tonis Tiigi --- builder/utils_test.go | 26 ------- cli/command/image/build.go | 14 ++-- .../command/image/build}/context.go | 7 +- .../command/image/build}/context_test.go | 78 ++++++++++++++++++- .../command/image/build}/context_unix.go | 2 +- .../command/image/build}/context_windows.go | 2 +- 6 files changed, 92 insertions(+), 37 deletions(-) rename {builder => cli/command/image/build}/context.go (98%) rename {builder => cli/command/image/build}/context_test.go (78%) rename {builder => cli/command/image/build}/context_unix.go (90%) rename {builder => cli/command/image/build}/context_windows.go (94%) diff --git a/builder/utils_test.go b/builder/utils_test.go index 1101ff1d1d..adc264539a 100644 --- a/builder/utils_test.go +++ b/builder/utils_test.go @@ -59,29 +59,3 @@ func createTestTempFile(t *testing.T, dir, filename, contents string, perm os.Fi return filePath } - -// chdir changes current working directory to dir. -// It returns a function which changes working directory back to the previous one. -// This function is meant to be executed as a deferred call. -// When an error occurs, it terminates the test. -func chdir(t *testing.T, dir string) func() { - workingDirectory, err := os.Getwd() - - if err != nil { - t.Fatalf("Error when retrieving working directory: %s", err) - } - - err = os.Chdir(dir) - - if err != nil { - t.Fatalf("Error when changing directory to %s: %s", dir, err) - } - - return func() { - err = os.Chdir(workingDirectory) - - if err != nil { - t.Fatalf("Error when changing back to working directory (%s): %s", workingDirectory, err) - } - } -} diff --git a/cli/command/image/build.go b/cli/command/image/build.go index e3e7ff2b02..f194659e08 100644 --- a/cli/command/image/build.go +++ b/cli/command/image/build.go @@ -16,10 +16,10 @@ import ( "github.com/docker/docker/api" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/container" - "github.com/docker/docker/builder" "github.com/docker/docker/builder/dockerignore" "github.com/docker/docker/cli" "github.com/docker/docker/cli/command" + "github.com/docker/docker/cli/command/image/build" "github.com/docker/docker/opts" "github.com/docker/docker/pkg/archive" "github.com/docker/docker/pkg/fileutils" @@ -29,7 +29,7 @@ import ( "github.com/docker/docker/pkg/urlutil" "github.com/docker/docker/reference" runconfigopts "github.com/docker/docker/runconfig/opts" - "github.com/docker/go-units" + units "github.com/docker/go-units" "github.com/spf13/cobra" ) @@ -156,13 +156,13 @@ func runBuild(dockerCli *command.DockerCli, options buildOptions) error { switch { case specifiedContext == "-": - buildCtx, relDockerfile, err = builder.GetContextFromReader(dockerCli.In(), options.dockerfileName) + buildCtx, relDockerfile, err = build.GetContextFromReader(dockerCli.In(), options.dockerfileName) case urlutil.IsGitURL(specifiedContext): - tempDir, relDockerfile, err = builder.GetContextFromGitURL(specifiedContext, options.dockerfileName) + tempDir, relDockerfile, err = build.GetContextFromGitURL(specifiedContext, options.dockerfileName) case urlutil.IsURL(specifiedContext): - buildCtx, relDockerfile, err = builder.GetContextFromURL(progBuff, specifiedContext, options.dockerfileName) + buildCtx, relDockerfile, err = build.GetContextFromURL(progBuff, specifiedContext, options.dockerfileName) default: - contextDir, relDockerfile, err = builder.GetContextFromLocalDir(specifiedContext, options.dockerfileName) + contextDir, relDockerfile, err = build.GetContextFromLocalDir(specifiedContext, options.dockerfileName) } if err != nil { @@ -198,7 +198,7 @@ func runBuild(dockerCli *command.DockerCli, options buildOptions) error { } } - if err := builder.ValidateContextDirectory(contextDir, excludes); err != nil { + if err := build.ValidateContextDirectory(contextDir, excludes); err != nil { return fmt.Errorf("Error checking context: '%s'.", err) } diff --git a/builder/context.go b/cli/command/image/build/context.go similarity index 98% rename from builder/context.go rename to cli/command/image/build/context.go index 600f42319b..86157c359d 100644 --- a/builder/context.go +++ b/cli/command/image/build/context.go @@ -1,4 +1,4 @@ -package builder +package build import ( "bufio" @@ -20,6 +20,11 @@ import ( "github.com/docker/docker/pkg/streamformatter" ) +const ( + // DefaultDockerfileName is the Default filename with Docker commands, read by docker build + DefaultDockerfileName string = "Dockerfile" +) + // ValidateContextDirectory checks if all the contents of the directory // can be read and returns an error if some files can't be read // symlinks which point to non-existing files don't trigger an error diff --git a/builder/context_test.go b/cli/command/image/build/context_test.go similarity index 78% rename from builder/context_test.go rename to cli/command/image/build/context_test.go index 27d29d79f4..afa04a4fcd 100644 --- a/builder/context_test.go +++ b/cli/command/image/build/context_test.go @@ -1,10 +1,11 @@ -package builder +package build import ( "archive/tar" "bytes" "io" "io/ioutil" + "os" "path/filepath" "runtime" "strings" @@ -13,6 +14,8 @@ import ( "github.com/docker/docker/pkg/archive" ) +const dockerfileContents = "FROM busybox" + var prepareEmpty = func(t *testing.T) (string, func()) { return "", func() {} } @@ -305,3 +308,76 @@ func TestValidateContextDirectoryWithOneFile(t *testing.T) { func TestValidateContextDirectoryWithOneFileExcludes(t *testing.T) { testValidateContextDirectory(t, prepareOneFile, []string{DefaultDockerfileName}) } + +// createTestTempDir creates a temporary directory for testing. +// It returns the created path and a cleanup function which is meant to be used as deferred call. +// When an error occurs, it terminates the test. +func createTestTempDir(t *testing.T, dir, prefix string) (string, func()) { + path, err := ioutil.TempDir(dir, prefix) + + if err != nil { + t.Fatalf("Error when creating directory %s with prefix %s: %s", dir, prefix, err) + } + + return path, func() { + err = os.RemoveAll(path) + + if err != nil { + t.Fatalf("Error when removing directory %s: %s", path, err) + } + } +} + +// createTestTempSubdir creates a temporary directory for testing. +// It returns the created path but doesn't provide a cleanup function, +// so createTestTempSubdir should be used only for creating temporary subdirectories +// whose parent directories are properly cleaned up. +// When an error occurs, it terminates the test. +func createTestTempSubdir(t *testing.T, dir, prefix string) string { + path, err := ioutil.TempDir(dir, prefix) + + if err != nil { + t.Fatalf("Error when creating directory %s with prefix %s: %s", dir, prefix, err) + } + + return path +} + +// createTestTempFile creates a temporary file within dir with specific contents and permissions. +// When an error occurs, it terminates the test +func createTestTempFile(t *testing.T, dir, filename, contents string, perm os.FileMode) string { + filePath := filepath.Join(dir, filename) + err := ioutil.WriteFile(filePath, []byte(contents), perm) + + if err != nil { + t.Fatalf("Error when creating %s file: %s", filename, err) + } + + return filePath +} + +// chdir changes current working directory to dir. +// It returns a function which changes working directory back to the previous one. +// This function is meant to be executed as a deferred call. +// When an error occurs, it terminates the test. +func chdir(t *testing.T, dir string) func() { + workingDirectory, err := os.Getwd() + + if err != nil { + t.Fatalf("Error when retrieving working directory: %s", err) + } + + err = os.Chdir(dir) + + if err != nil { + t.Fatalf("Error when changing directory to %s: %s", dir, err) + } + + return func() { + err = os.Chdir(workingDirectory) + + if err != nil { + t.Fatalf("Error when changing back to working directory (%s): %s", workingDirectory, err) + } + } +} diff --git a/builder/context_unix.go b/cli/command/image/build/context_unix.go similarity index 90% rename from builder/context_unix.go rename to cli/command/image/build/context_unix.go index d1f72e0573..cb2634f079 100644 --- a/builder/context_unix.go +++ b/cli/command/image/build/context_unix.go @@ -1,6 +1,6 @@ // +build !windows -package builder +package build import ( "path/filepath" diff --git a/builder/context_windows.go b/cli/command/image/build/context_windows.go similarity index 94% rename from builder/context_windows.go rename to cli/command/image/build/context_windows.go index b8ba2ba231..c577cfa7be 100644 --- a/builder/context_windows.go +++ b/cli/command/image/build/context_windows.go @@ -1,6 +1,6 @@ // +build windows -package builder +package build import ( "path/filepath"