diff --git a/examples/docker/main.cue b/examples/docker/main.cue index 8be803a6..485ff08f 100644 --- a/examples/docker/main.cue +++ b/examples/docker/main.cue @@ -9,6 +9,6 @@ import ( source: dagger.#Artifact // Container image -container: docker.#ImageFromSource & { +container: docker.#Build & { "source": source } diff --git a/stdlib/docker/docker.cue b/stdlib/docker/docker.cue index 15c04efd..63763e7e 100644 --- a/stdlib/docker/docker.cue +++ b/stdlib/docker/docker.cue @@ -27,7 +27,20 @@ import ( ] } -// FIXME: #Push +// Push a docker image +#Push: { + // Remote ref (example: "index.docker.io/alpine:latest") + ref: string + + // Image + source: dagger.#Artifact + + #up: [ + op.#Load & {from: source}, + op.#PushContainer & {"ref": ref}, + ] +} + // FIXME: #Run // Build a Docker image from the provided Dockerfile contents diff --git a/tests/examples.bats b/tests/examples.bats index 67890d85..665354e7 100644 --- a/tests/examples.bats +++ b/tests/examples.bats @@ -17,4 +17,4 @@ setup() { run curl -sS "$deployUrl" assert_success assert_output --partial "Todo App" -} +} \ No newline at end of file diff --git a/tests/helpers.bash b/tests/helpers.bash index 00f94b44..ec22da5b 100644 --- a/tests/helpers.bash +++ b/tests/helpers.bash @@ -19,6 +19,11 @@ skip_unless_secrets_available() { sops exec-file "$inputFile" echo > /dev/null 2>&1 || skip "$inputFile cannot be decrypted" } +skip_unless_file_exist() { + local inputFile="$1" + test -f "$inputFile" || skip "$inputFile does not exist" +} + skip_unless_local_kube() { if [ -f ~/.kube/config ] && grep -q "user: kind-kind" ~/.kube/config &> /dev/null && grep -q "127.0.0.1" ~/.kube/config &> /dev/null; then echo "Kubernetes available" diff --git a/tests/stdlib.bats b/tests/stdlib.bats index 16be89de..b2fc37c5 100644 --- a/tests/stdlib.bats +++ b/tests/stdlib.bats @@ -60,6 +60,24 @@ setup() { "$DAGGER" compute "$TESTDIR"/stdlib/aws/ecr --input-yaml "$TESTDIR"/stdlib/aws/inputs.yaml } +@test "stdlib: docker-build" { + skip_unless_file_exist "$TESTDIR"/stdlib/docker/build/Dockerfile + + "$DAGGER" compute "$TESTDIR"/stdlib/docker/build/ --input-dir source="$TESTDIR"/stdlib/docker/build +} + +@test "stdlib: docker-dockerfile" { + "$DAGGER" compute "$TESTDIR"/stdlib/docker/dockerfile/ --input-dir source="$TESTDIR"/stdlib/docker/dockerfile/testdata +} + +@test "stdlib: docker-push-and-pull" { + skip_unless_secrets_available "$TESTDIR"/stdlib/docker/push-pull/inputs.yaml + + # check that they succeed with the credentials + run "$DAGGER" compute --input-yaml "$TESTDIR"/stdlib/docker/push-pull/inputs.yaml --input-dir source="$TESTDIR"/stdlib/docker/push-pull/testdata "$TESTDIR"/stdlib/docker/push-pull/ + assert_success +} + @test "stdlib: terraform" { skip_unless_secrets_available "$TESTDIR"/stdlib/aws/inputs.yaml diff --git a/tests/stdlib/docker/build/Dockerfile b/tests/stdlib/docker/build/Dockerfile new file mode 100644 index 00000000..987ce9be --- /dev/null +++ b/tests/stdlib/docker/build/Dockerfile @@ -0,0 +1,2 @@ +FROM alpine +RUN echo test >> /test.txt \ No newline at end of file diff --git a/tests/stdlib/docker/build/build.cue b/tests/stdlib/docker/build/build.cue new file mode 100644 index 00000000..58601432 --- /dev/null +++ b/tests/stdlib/docker/build/build.cue @@ -0,0 +1,31 @@ +package docker + +import ( + "dagger.io/dagger" + "dagger.io/dagger/op" + "dagger.io/docker" +) + +// Build a Docker image from source, using included Dockerfile +source: dagger.#Artifact + +TestBuild: { + image: docker.#Build & { + "source": source + } + + verify: #up: [ + op.#Load & { + from: image + }, + + op.#Exec & { + always: true + args: [ + "sh", "-c", """ + grep -q "test" /test.txt + """, + ] + }, + ] +} diff --git a/tests/stdlib/docker/dockerfile/dockerfile.cue b/tests/stdlib/docker/dockerfile/dockerfile.cue new file mode 100644 index 00000000..7f982ab7 --- /dev/null +++ b/tests/stdlib/docker/dockerfile/dockerfile.cue @@ -0,0 +1,34 @@ +package docker + +import ( + "dagger.io/dagger" + "dagger.io/dagger/op" + "dagger.io/docker" +) + +source: dagger.#Artifact + +TestImageFromDockerfile: { + image: docker.#ImageFromDockerfile & { + dockerfile: """ + FROM alpine + COPY test.txt /test.txt + """ + context: source + } + + verify: #up: [ + op.#Load & { + from: image + }, + + op.#Exec & { + always: true + args: [ + "sh", "-c", """ + grep -q "test" /test.txt + """, + ] + }, + ] +} diff --git a/tests/stdlib/docker/dockerfile/testdata/test.txt b/tests/stdlib/docker/dockerfile/testdata/test.txt new file mode 100644 index 00000000..30d74d25 --- /dev/null +++ b/tests/stdlib/docker/dockerfile/testdata/test.txt @@ -0,0 +1 @@ +test \ No newline at end of file diff --git a/tests/stdlib/docker/push-pull/inputs.yaml b/tests/stdlib/docker/push-pull/inputs.yaml new file mode 100644 index 00000000..f054b2cf --- /dev/null +++ b/tests/stdlib/docker/push-pull/inputs.yaml @@ -0,0 +1,34 @@ +registry: + username: ENC[AES256_GCM,data:8AH6p9WHidanCA==,iv:ezThCQJv+bVBf8SdfSa2HFoP+eu6IZMPl5xvMOGDcps=,tag:mzR7xTKeQNDvkyd2Dm3AKw==,type:str] + secret: ENC[AES256_GCM,data:GtuaBAhFBw2JFaeuOm6mUr3m1j5fvCJjcWAzjsdU2xASFxwO,iv:YAXcRzBoemmef5PBdAOBa5acNPo4BoKH7Ngud/CWYfA=,tag:MCCUCOSutjRCI92raYrxdg==,type:str] +sops: + kms: [] + gcp_kms: [] + azure_kv: [] + hc_vault: [] + age: [] + lastmodified: "2021-04-27T00:59:33Z" + mac: ENC[AES256_GCM,data:qk+oo4m5OpfuQ+R3pZUuvn+gqAk15OAJzOULrlYqt1FIDRk/Q5ah5QpIbVxeP1EDVyuY/V/E0ZngRlSV7Dyx6Cp/moMd8AFBHNgnTB+Lq+NmZ9HR1QMOxpbMpJmUGn7MqQ1Ys4wy0p2q2Y2+TuUpKwmRGJbGVYEVmqvV5OT3jhc=,iv:QsUFa2GVzy6iqqLXRz8HascQZPIIzKBhxHdlabov02k=,tag:7lk63FeXsOlTCgfmWd7zrg==,type:str] + pgp: + - created_at: "2021-03-18T22:59:59Z" + enc: | + -----BEGIN PGP MESSAGE----- + + hQIMAzqVY590vudzAQ//etnfnpfCo9rAkctR+Fwg/7VdVL3Rov+6gnyjUnoN1BS1 + 8jnBF/86AZ7uK89dTcTZCsK1hKPxeYg1kJTKpA+zfDORupzTWcMrRyjwNk5wQ2Vg + N1adUwFsBQpk8WptpsU/ro6+3yH+Nn35begs6hP2fH/EQ9XOxw5gY0kp0AFjGaKJ + tRZVrr3f2hpLESo6LILRO97UXZiGcwTn5onslECL92260cU1nqEQp+ESK7XrdYIG + 99oM3eXEraKw4WuQDaDE6U135aUl6vIJWD1JZzyr3RW3+5O9pn5rpN3Wc0TbDR6+ + 9Fs/TjuA1h5eJzbt+lkA74BtxPOBv9O7HJnWJpXjiG0VUGHdFXoq5Tr5Ol68RQxa + BWe7IfTO6FHN0xOl1dY7cn5jtf+xlFjL86s9OkrJUFa9lbQx8L/QPCeA2Xiu4tpW + +wTSel13k8Uv/JSGgLwSohW6N4XTQYdxPkO+a1V08adwFBXaGgqxfg0rNehcS5fp + y3TEq84cOlBsaI+rYpnOTPEajtYWfTe8WFf+lBOn1vZ9EiupjZtefGX2MIWPXoaK + kVBgRvzjp4/BY68yRvdi5sZFd2nakl+DOXzouuFbzsOkxL3o9FA9aCVsXtFqqzSG + Hvq4ZJ5ivXf6vQf+s7Tgc4qxW2CQwIPZVkHhQossrWgtkQ4WDAyzfhF0YuhEnpLS + XgGNLr82LMVmempaJd7GfAR2nwGnLUTYny1KoiW/1ie6DPwLZBX/UxPOplaS5wYH + Xd3gV3smg5xZ7/rfvzKTzJ1a5yH6D3xI05UtnUWdqojONcXS9NS+P7RArngJwSs= + =m0OS + -----END PGP MESSAGE----- + fp: 6CB37404020B5F0A0B41B5BB225EBAB0B936AC65 + unencrypted_suffix: _unencrypted + version: 3.7.1 diff --git a/tests/stdlib/docker/push-pull/push-pull.cue b/tests/stdlib/docker/push-pull/push-pull.cue new file mode 100644 index 00000000..6dfee310 --- /dev/null +++ b/tests/stdlib/docker/push-pull/push-pull.cue @@ -0,0 +1,74 @@ +package docker + +import ( + "dagger.io/dagger" + "dagger.io/dagger/op" + "dagger.io/alpine" + "dagger.io/docker" +) + +source: dagger.#Artifact + +registry: { + username: string + secret: dagger.#Secret +} + +TestPushAndPull: { + // Generate a random number + random: { + string + #up: [ + op.#Load & {from: alpine.#Image}, + op.#Exec & { + args: ["sh", "-c", "cat /dev/urandom | tr -dc 'a-z' | fold -w 10 | head -n 1 | tr -d '\n' > /rand"] + }, + op.#Export & { + source: "/rand" + }, + ] + } + + ref: "daggerio/ci-test:\(random)" + + // Create image + image: docker.#ImageFromDockerfile & { + dockerfile: """ + FROM alpine + COPY test.txt /test.txt + """ + context: source + } + + // Login + login: #up: [ + op.#DockerLogin & { + registry + }, + ] + + // Push image + push: docker.#Push & { + "ref": ref + source: image + } + + // Push image + pull: docker.#Pull & { + from: push.ref + } + + // Check the content + verify: #up: [ + op.#Load & {from: alpine.#Image}, + op.#Exec & { + always: true + args: [ + "sh", "-c", """ + grep -q "test" /src/test.txt + """, + ] + mount: "/src": from: pull + }, + ] +} diff --git a/tests/stdlib/docker/push-pull/testdata/test.txt b/tests/stdlib/docker/push-pull/testdata/test.txt new file mode 100644 index 00000000..30d74d25 --- /dev/null +++ b/tests/stdlib/docker/push-pull/testdata/test.txt @@ -0,0 +1 @@ +test \ No newline at end of file