From 26b2ef869764b30463add5b40f5b06dbeaea11e7 Mon Sep 17 00:00:00 2001 From: Tom Chauveau Date: Fri, 28 Jan 2022 13:37:47 +0100 Subject: [PATCH 1/2] Add tests on `docker.#Build` definition - Simple build test - Multi step to build a go binary Signed-off-by: Vasek - Tom C --- .../docker/test/build-multi-steps.cue | 49 +++++++++++++++++++ .../docker/test/build-simple.cue | 38 ++++++++++++++ .../docker/test/docker.bats | 3 ++ .../docker/test/testdata/go.mod | 3 ++ .../docker/test/testdata/main.go | 7 +++ 5 files changed, 100 insertions(+) create mode 100644 pkg/universe.dagger.io/docker/test/build-multi-steps.cue create mode 100644 pkg/universe.dagger.io/docker/test/build-simple.cue create mode 100644 pkg/universe.dagger.io/docker/test/testdata/go.mod create mode 100644 pkg/universe.dagger.io/docker/test/testdata/main.go diff --git a/pkg/universe.dagger.io/docker/test/build-multi-steps.cue b/pkg/universe.dagger.io/docker/test/build-multi-steps.cue new file mode 100644 index 00000000..b4567b57 --- /dev/null +++ b/pkg/universe.dagger.io/docker/test/build-multi-steps.cue @@ -0,0 +1,49 @@ +package test + +import ( + "dagger.io/dagger" + "dagger.io/dagger/engine" + "universe.dagger.io/docker" +) + +// This test verify that we can correctly build an image +// using docker.#Build with multiple steps executed during +// the building process +dagger.#Plan & { + inputs: directories: testdata: path: "./testdata" + + actions: { + image: docker.#Build & { + steps: [ + docker.#Pull & { + source: "golang:1.17-alpine" + }, + docker.#Copy & { + contents: inputs.directories.testdata.contents + dest: "/input" + }, + docker.#Run & { + script: """ + # FIXME remove that line when #1517 is merged + export PATH=/go/bin:/usr/local/go/bin:$PATH + go build -o hello ./main.go + mv hello /bin + """ + workdir: "/input" + }, + docker.#Run & { + script: """ + hello >> /test.txt + """ + }, + ] + } + + verify: engine.#ReadFile & { + input: image.output.rootfs + path: "/test.txt" + } & { + contents: "hello world" + } + } +} diff --git a/pkg/universe.dagger.io/docker/test/build-simple.cue b/pkg/universe.dagger.io/docker/test/build-simple.cue new file mode 100644 index 00000000..fb72d510 --- /dev/null +++ b/pkg/universe.dagger.io/docker/test/build-simple.cue @@ -0,0 +1,38 @@ +package test + +import ( + "dagger.io/dagger" + "dagger.io/dagger/engine" + "universe.dagger.io/docker" +) + +// This test verify that we can correctly build a simplistic image +// using docker.#Build +dagger.#Plan & { + #alpineImage: "index.docker.io/alpine:3.15.0@sha256:21a3deaa0d32a8057914f36584b5288d2e5ecc984380bc0118285c70fa8c9300" + + #testValue: "hello world" + + actions: { + image: docker.#Build & { + steps: [ + docker.#Pull & { + source: #alpineImage + }, + docker.#Run & { + script: """ + echo -n $TEST >> /test.txt + """ + env: TEST: #testValue + }, + ] + } + + verify: engine.#ReadFile & { + input: image.output.rootfs + path: "/test.txt" + } & { + contents: #testValue + } + } +} diff --git a/pkg/universe.dagger.io/docker/test/docker.bats b/pkg/universe.dagger.io/docker/test/docker.bats index 8b7abe8c..d8414a1d 100644 --- a/pkg/universe.dagger.io/docker/test/docker.bats +++ b/pkg/universe.dagger.io/docker/test/docker.bats @@ -5,6 +5,9 @@ setup() { } @test "docker.#Build" { + dagger up ./build-simple.cue + dagger up ./build-multi-steps.cue + dagger up ./nested-build-test.cue # FIXME: this is currently broken diff --git a/pkg/universe.dagger.io/docker/test/testdata/go.mod b/pkg/universe.dagger.io/docker/test/testdata/go.mod new file mode 100644 index 00000000..9bacf229 --- /dev/null +++ b/pkg/universe.dagger.io/docker/test/testdata/go.mod @@ -0,0 +1,3 @@ +module test.com + +go 1.17 diff --git a/pkg/universe.dagger.io/docker/test/testdata/main.go b/pkg/universe.dagger.io/docker/test/testdata/main.go new file mode 100644 index 00000000..75b323ab --- /dev/null +++ b/pkg/universe.dagger.io/docker/test/testdata/main.go @@ -0,0 +1,7 @@ +package main + +import "fmt" + +func main() { + fmt.Printf("hello world") +} From 32b48f7dce82b2b03dd71f1b994aa622c253586c Mon Sep 17 00:00:00 2001 From: Vasek - Tom C Date: Fri, 28 Jan 2022 21:23:20 +0100 Subject: [PATCH 2/2] Optimize tests with simpler workflow Signed-off-by: Vasek - Tom C --- .../docker/test/build-multi-steps.cue | 24 +++++++------------ .../docker/test/build-simple.cue | 7 ++---- .../docker/test/testdata/go.mod | 3 --- .../docker/test/testdata/main.go | 7 ------ 4 files changed, 11 insertions(+), 30 deletions(-) delete mode 100644 pkg/universe.dagger.io/docker/test/testdata/go.mod delete mode 100644 pkg/universe.dagger.io/docker/test/testdata/main.go diff --git a/pkg/universe.dagger.io/docker/test/build-multi-steps.cue b/pkg/universe.dagger.io/docker/test/build-multi-steps.cue index b4567b57..a1dae986 100644 --- a/pkg/universe.dagger.io/docker/test/build-multi-steps.cue +++ b/pkg/universe.dagger.io/docker/test/build-multi-steps.cue @@ -4,36 +4,30 @@ import ( "dagger.io/dagger" "dagger.io/dagger/engine" "universe.dagger.io/docker" + "universe.dagger.io/alpine" ) // This test verify that we can correctly build an image // using docker.#Build with multiple steps executed during // the building process dagger.#Plan & { - inputs: directories: testdata: path: "./testdata" - actions: { image: docker.#Build & { steps: [ - docker.#Pull & { - source: "golang:1.17-alpine" - }, - docker.#Copy & { - contents: inputs.directories.testdata.contents - dest: "/input" - }, + alpine.#Build, docker.#Run & { script: """ - # FIXME remove that line when #1517 is merged - export PATH=/go/bin:/usr/local/go/bin:$PATH - go build -o hello ./main.go - mv hello /bin + echo -n hello > /bar.txt """ - workdir: "/input" }, docker.#Run & { script: """ - hello >> /test.txt + echo -n $(cat /bar.txt) world > /foo.txt + """ + }, + docker.#Run & { + script: """ + echo -n $(cat /foo.txt) >> /test.txt """ }, ] diff --git a/pkg/universe.dagger.io/docker/test/build-simple.cue b/pkg/universe.dagger.io/docker/test/build-simple.cue index fb72d510..3a019291 100644 --- a/pkg/universe.dagger.io/docker/test/build-simple.cue +++ b/pkg/universe.dagger.io/docker/test/build-simple.cue @@ -4,21 +4,18 @@ import ( "dagger.io/dagger" "dagger.io/dagger/engine" "universe.dagger.io/docker" + "universe.dagger.io/alpine" ) // This test verify that we can correctly build a simplistic image // using docker.#Build dagger.#Plan & { - #alpineImage: "index.docker.io/alpine:3.15.0@sha256:21a3deaa0d32a8057914f36584b5288d2e5ecc984380bc0118285c70fa8c9300" - #testValue: "hello world" actions: { image: docker.#Build & { steps: [ - docker.#Pull & { - source: #alpineImage - }, + alpine.#Build, docker.#Run & { script: """ echo -n $TEST >> /test.txt diff --git a/pkg/universe.dagger.io/docker/test/testdata/go.mod b/pkg/universe.dagger.io/docker/test/testdata/go.mod deleted file mode 100644 index 9bacf229..00000000 --- a/pkg/universe.dagger.io/docker/test/testdata/go.mod +++ /dev/null @@ -1,3 +0,0 @@ -module test.com - -go 1.17 diff --git a/pkg/universe.dagger.io/docker/test/testdata/main.go b/pkg/universe.dagger.io/docker/test/testdata/main.go deleted file mode 100644 index 75b323ab..00000000 --- a/pkg/universe.dagger.io/docker/test/testdata/main.go +++ /dev/null @@ -1,7 +0,0 @@ -package main - -import "fmt" - -func main() { - fmt.Printf("hello world") -}