From 4a345a20a36ff069d8ba7dd07e231215cb821f48 Mon Sep 17 00:00:00 2001 From: Angel Montero Date: Wed, 22 Jan 2025 16:18:00 +0000 Subject: [PATCH] add integration test to ensure that the build int BUILD args can be used as part of a file --- integration/build_test.go | 34 +++++++++++++------ .../base/Dockerfile | 0 .../base/go.mod | 0 .../base/main.go | 0 .../child/Dockerfile | 0 .../skaffold.yaml | 0 .../single-artifact/Dockerfile | 5 +++ .../single-artifact/script-latest.sh | 1 + .../single-artifact/skaffold.yaml | 18 ++++++++++ 9 files changed, 47 insertions(+), 11 deletions(-) rename integration/testdata/docker-run-with-build-args/{ => artifact-with-dependency}/base/Dockerfile (100%) rename integration/testdata/docker-run-with-build-args/{ => artifact-with-dependency}/base/go.mod (100%) rename integration/testdata/docker-run-with-build-args/{ => artifact-with-dependency}/base/main.go (100%) rename integration/testdata/docker-run-with-build-args/{ => artifact-with-dependency}/child/Dockerfile (100%) rename integration/testdata/docker-run-with-build-args/{ => artifact-with-dependency}/skaffold.yaml (100%) create mode 100644 integration/testdata/docker-run-with-build-args/single-artifact/Dockerfile create mode 100755 integration/testdata/docker-run-with-build-args/single-artifact/script-latest.sh create mode 100644 integration/testdata/docker-run-with-build-args/single-artifact/skaffold.yaml diff --git a/integration/build_test.go b/integration/build_test.go index 23276c038c5..461454fec80 100644 --- a/integration/build_test.go +++ b/integration/build_test.go @@ -335,34 +335,46 @@ func failNowIfError(t Fataler, err error) { func TestRunWithDockerAndBuildArgs(t *testing.T) { tests := []struct { - description string - projectDir string - args []string + description string + projectDir string + skaffoldArgs []string + dockerRunArgs []string + wantOutput string }{ { - description: "IMAGE_REPO, IMAGE_TAG, and IMAGE_NAME are passed to the docker build args", - projectDir: "testdata/docker-run-with-build-args", - args: []string{"--kube-context", "default"}, + description: "IMAGE_REPO, IMAGE_TAG, and IMAGE_NAME are passed to Docker build as build args", + projectDir: "testdata/docker-run-with-build-args/artifact-with-dependency", + skaffoldArgs: []string{"--kube-context", "default"}, + dockerRunArgs: []string{"run", "child:latest"}, + wantOutput: "IMAGE_REPO: gcr.io/k8s-skaffold, IMAGE_NAME: skaffold, IMAGE_TAG:latest", + }, + { + description: "IMAGE_TAG can be used as a part of a filename in the Dockerfile", + projectDir: "testdata/docker-run-with-build-args/single-artifact", + skaffoldArgs: []string{"--kube-context", "default"}, + dockerRunArgs: []string{"run", "example:latest"}, + wantOutput: "HELLO WORLD", }, } for _, test := range tests { testutil.Run(t, test.description, func(t *testutil.T) { - skaffold.Build(test.args...).InDir(test.projectDir).Run(t.T) defer skaffold.Delete().InDir(test.projectDir).Run(t.T) + if err := skaffold.Build(test.skaffoldArgs...).InDir(test.projectDir).Run(t.T); err != nil { + t.Errorf("skaffold build args: %v working directory:%s returned unexpected error: %v", test.skaffoldArgs, test.projectDir, err) + } - expected := "IMAGE_REPO: gcr.io/k8s-skaffold, IMAGE_NAME: skaffold, IMAGE_TAG:latest" got := "" err := wait.PollImmediate(time.Millisecond*500, 1*time.Minute, func() (bool, error) { - out, _ := exec.Command("docker", "run", "child:latest").Output() + out, _ := exec.Command("docker", test.dockerRunArgs...).Output() t.Logf("Output:[%s]\n", out) got = strings.Trim(string(out), " \n") - return got == expected, nil + return got == test.wantOutput, nil }) if err != nil { - t.Errorf("docker run produced incorrect output, got:[%s], want:[%s], err: %v", got, expected, err) + t.Errorf("docker run produced incorrect output, got:[%s], want:[%s], err: %v", got, test.wantOutput, err) } failNowIfError(t, err) }) diff --git a/integration/testdata/docker-run-with-build-args/base/Dockerfile b/integration/testdata/docker-run-with-build-args/artifact-with-dependency/base/Dockerfile similarity index 100% rename from integration/testdata/docker-run-with-build-args/base/Dockerfile rename to integration/testdata/docker-run-with-build-args/artifact-with-dependency/base/Dockerfile diff --git a/integration/testdata/docker-run-with-build-args/base/go.mod b/integration/testdata/docker-run-with-build-args/artifact-with-dependency/base/go.mod similarity index 100% rename from integration/testdata/docker-run-with-build-args/base/go.mod rename to integration/testdata/docker-run-with-build-args/artifact-with-dependency/base/go.mod diff --git a/integration/testdata/docker-run-with-build-args/base/main.go b/integration/testdata/docker-run-with-build-args/artifact-with-dependency/base/main.go similarity index 100% rename from integration/testdata/docker-run-with-build-args/base/main.go rename to integration/testdata/docker-run-with-build-args/artifact-with-dependency/base/main.go diff --git a/integration/testdata/docker-run-with-build-args/child/Dockerfile b/integration/testdata/docker-run-with-build-args/artifact-with-dependency/child/Dockerfile similarity index 100% rename from integration/testdata/docker-run-with-build-args/child/Dockerfile rename to integration/testdata/docker-run-with-build-args/artifact-with-dependency/child/Dockerfile diff --git a/integration/testdata/docker-run-with-build-args/skaffold.yaml b/integration/testdata/docker-run-with-build-args/artifact-with-dependency/skaffold.yaml similarity index 100% rename from integration/testdata/docker-run-with-build-args/skaffold.yaml rename to integration/testdata/docker-run-with-build-args/artifact-with-dependency/skaffold.yaml diff --git a/integration/testdata/docker-run-with-build-args/single-artifact/Dockerfile b/integration/testdata/docker-run-with-build-args/single-artifact/Dockerfile new file mode 100644 index 00000000000..7daf2952870 --- /dev/null +++ b/integration/testdata/docker-run-with-build-args/single-artifact/Dockerfile @@ -0,0 +1,5 @@ +FROM busybox +ARG IMAGE_TAG +RUN echo $IMAGE_TAG +COPY "script-${IMAGE_TAG}.sh" script.sh +CMD ["/bin/sh","script.sh"] diff --git a/integration/testdata/docker-run-with-build-args/single-artifact/script-latest.sh b/integration/testdata/docker-run-with-build-args/single-artifact/script-latest.sh new file mode 100755 index 00000000000..dfc0eb82378 --- /dev/null +++ b/integration/testdata/docker-run-with-build-args/single-artifact/script-latest.sh @@ -0,0 +1 @@ +echo "HELLO WORLD" \ No newline at end of file diff --git a/integration/testdata/docker-run-with-build-args/single-artifact/skaffold.yaml b/integration/testdata/docker-run-with-build-args/single-artifact/skaffold.yaml new file mode 100644 index 00000000000..20ee43ce74a --- /dev/null +++ b/integration/testdata/docker-run-with-build-args/single-artifact/skaffold.yaml @@ -0,0 +1,18 @@ +apiVersion: skaffold/v4beta12 +kind: Config +build: + tagPolicy: + sha256: {} + local: + push: false + useDockerCLI: true + artifacts: + - image: example + docker: + dockerfile: Dockerfile + noCache: true + buildArgs: + IMAGE_TAG: '{{.IMAGE_TAG}}' +deploy: + docker: + images: [example]