Skip to content

Commit

Permalink
add integration test to ensure that the build int BUILD args can be u…
Browse files Browse the repository at this point in the history
…sed as part of a file
  • Loading branch information
alphanota committed Jan 22, 2025
1 parent 8c4e2e4 commit 4a345a2
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 11 deletions.
34 changes: 23 additions & 11 deletions integration/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
Original file line number Diff line number Diff line change
@@ -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"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
echo "HELLO WORLD"
Original file line number Diff line number Diff line change
@@ -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]

0 comments on commit 4a345a2

Please sign in to comment.