From 20dc74dbe5141bfb80f2feabc25711be38e2297f Mon Sep 17 00:00:00 2001 From: Alexander Shulyak Date: Sun, 28 Feb 2021 17:02:57 +0000 Subject: [PATCH] Target flag and test --- modules/docker/build.go | 7 +++++++ modules/docker/build_test.go | 19 +++++++++++++++++++ test/fixtures/docker/Dockerfile | 7 ++++++- 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/modules/docker/build.go b/modules/docker/build.go index c4965b8f3..c1025d2b9 100644 --- a/modules/docker/build.go +++ b/modules/docker/build.go @@ -15,6 +15,9 @@ type BuildOptions struct { // Build args to pass the 'docker build' command BuildArgs []string + // Target build arg to pass to the 'docker build' command + Target string + // Custom CLI options that will be passed as-is to the 'docker build' command. This is an "escape hatch" that allows // Terratest to not have to support every single command-line option offered by the 'docker build' command, and // solely focus on the most important ones. @@ -61,6 +64,10 @@ func formatDockerBuildArgs(path string, options *BuildOptions) ([]string, error) args = append(args, "--build-arg", arg) } + if len(options.Target) > 0 { + args = append(args, "--target", options.Target) + } + args = append(args, options.OtherOptions...) args = append(args, path) diff --git a/modules/docker/build_test.go b/modules/docker/build_test.go index 085c9769e..396ef7bd6 100644 --- a/modules/docker/build_test.go +++ b/modules/docker/build_test.go @@ -23,3 +23,22 @@ func TestBuild(t *testing.T) { out := Run(t, tag, &RunOptions{Remove: true}) require.Contains(t, out, text) } + +func TestBuildWithTarget(t *testing.T) { + t.Parallel() + + tag := "gruntwork-io/test-image:target1" + text := "Hello, World!" + text1 := "Hello, World! This is build target 1!" + + options := &BuildOptions{ + Tags: []string{tag}, + BuildArgs: []string{fmt.Sprintf("text=%s", text), fmt.Sprintf("text1=%s", text1)}, + Target: "step1", + } + + Build(t, "../../test/fixtures/docker", options) + + out := Run(t, tag, &RunOptions{Remove: true}) + require.Contains(t, out, text1) +} diff --git a/test/fixtures/docker/Dockerfile b/test/fixtures/docker/Dockerfile index 89fe0d17f..ce19d2fdd 100644 --- a/test/fixtures/docker/Dockerfile +++ b/test/fixtures/docker/Dockerfile @@ -1,5 +1,10 @@ # A "Hello, World" Docker image used in automated tests for the docker.Build command. -FROM alpine:3.7 +FROM alpine:3.7 as step1 +ARG text1 +RUN echo $text1 > text.txt +CMD ["cat", "text.txt"] + +FROM step1 ARG text RUN echo $text > text.txt CMD ["cat", "text.txt"] \ No newline at end of file