From ec3c39aea06f63ca4ea4ac343c31c9958e1f05ae Mon Sep 17 00:00:00 2001 From: guillaume Date: Tue, 23 Nov 2021 18:33:28 +0100 Subject: [PATCH 1/4] Implement http.#Wait to wait for a URL to be ready. It creates a dependency in the Cue tree Signed-off-by: guillaume --- stdlib/.dagger/env/http/values.yaml | 8 +++- stdlib/cue.mod/pkg/.gitignore | 3 +- stdlib/http/http.cue | 68 +++++++++++++++++++++++++++++ stdlib/http/tests/http.cue | 45 ++++++++++++++++--- stdlib/universe.bats | 4 +- 5 files changed, 118 insertions(+), 10 deletions(-) diff --git a/stdlib/.dagger/env/http/values.yaml b/stdlib/.dagger/env/http/values.yaml index 76b6c328..7ef0dfa9 100644 --- a/stdlib/.dagger/env/http/values.yaml +++ b/stdlib/.dagger/env/http/values.yaml @@ -1,6 +1,10 @@ plan: package: ./http/tests name: http +inputs: + TestDockersocket: + socket: + unix: /var/run/docker.sock sops: kms: [] gcp_kms: [] @@ -16,8 +20,8 @@ sops: NHkxc1VXWHBwZFN5WFNxamdPaGZLM3MKaZra2gWvKl7+pXSihV70/mQYel9697z2 eBSMuRPuZSjrg3JCBLhXTdIgUYxbMgWhILLpN5UqhTBVboUlRsURQA== -----END AGE ENCRYPTED FILE----- - lastmodified: "2021-10-19T11:50:42Z" - mac: ENC[AES256_GCM,data:iG/JWnCk8UpThscQBqd9l7R7k1Xae1ED7WbYqPQmK5GRXhb7hIZGENnjn1aARnFjlCtEob6WvutWaZe7q1P90ZCirwXVBGewAeHchHqFA0KmUORRdtjMkZjnlRLMDKYu6CeNkaVK1al+B87CPlCBKz5UMeL9gnvug0k298AxWLY=,iv:RZEtSdbkxwgq5xEucnC+qyD2RCMk1DuaShjCh5Ihe5M=,tag:swW4R7ni0zoxOrc9lrV9Ww==,type:str] + lastmodified: "2021-11-23T17:30:31Z" + mac: ENC[AES256_GCM,data:R82VFQG+vtFoH/H4R3TGfFzcmEBLRWv81/pfbYIoWGTH3iFmVgfiCXuqF2O4suhG6mlmiGVDCFBBGPfFvOIBPSwbAqOoNQrM+pXPk3nrm3KY6RLSiByZX3yS1G2HHzGSbQVDuxlTBGyXhpJZs1ZgysZguHUqtunbrjSpD4+iWE8=,iv:LhTwB1QKXCXz9tu03d5NHIv/Jwrgg3LXQYafXykFz/o=,tag:h0mgHTyfxflAuv/uYuRibw==,type:str] pgp: [] encrypted_suffix: secret version: 3.7.1 diff --git a/stdlib/cue.mod/pkg/.gitignore b/stdlib/cue.mod/pkg/.gitignore index a572e9ee..2d4dc1ae 100644 --- a/stdlib/cue.mod/pkg/.gitignore +++ b/stdlib/cue.mod/pkg/.gitignore @@ -1,2 +1,3 @@ -# dagger universe +# generated by dagger alpha.dagger.io +dagger.lock diff --git a/stdlib/http/http.cue b/stdlib/http/http.cue index d5b99145..b7b7f898 100644 --- a/stdlib/http/http.cue +++ b/stdlib/http/http.cue @@ -6,6 +6,7 @@ import ( "alpha.dagger.io/alpine" "alpha.dagger.io/dagger" + "alpha.dagger.io/dagger/op" "alpha.dagger.io/os" ) @@ -84,3 +85,70 @@ import ( "statusCode": strconv.Atoi(statusCode) } } + +// URL listener +// Creates a dependency on URL +#Wait: { + // URL to listen + url: string + + // Waiting time between checks (sec.) + interval: int | *30 + + // Max amount of retries + retries: int | *3 + + // Max initialization time (sec.) + startPeriod: int | *0 + + // Time until timeout (sec.) + timeout: int | *30 + + #up: [ + op.#Load & { + from: alpine.#Image & { + package: bash: "=~5.1" + package: curl: true + } + }, + op.#Exec & { + args: ["/bin/bash", "-c", + #""" + # (f: str -> int) + nb_retries=$(($NB_RETRIES+0)) + starting_period=$(($START_PERIOD+0)) + + status="0" + SECONDS=0 + # START_PERIOD implementation + while [ $SECONDS -lt $starting_period ]; do + status="$(curl --connect-timeout 1 -s -o /dev/null -w ''%{http_code}'' $HEALTH_URL)" + if [ "$status" == "200" ]; then + exit 0; + fi + sleep 1; + done + + # TIMEOUT, INTERVAL, INTERVAL implementation + for ((i=0;i/dev/null test "$STATUS" = "200" """# +} \ No newline at end of file diff --git a/stdlib/universe.bats b/stdlib/universe.bats index 86dbeec2..acac71eb 100644 --- a/stdlib/universe.bats +++ b/stdlib/universe.bats @@ -24,7 +24,9 @@ setup() { } @test "http" { - dagger -e http up + dagger -e http up + CONTAINER=$(docker container ls -q --filter "name=daggerci-test-wait-*") + docker stop "$CONTAINER" && docker rm "$CONTAINER" } @test "js/yarn" { From 6ce8731d698dabc94f5f35ba40c8806036c69221 Mon Sep 17 00:00:00 2001 From: guillaume Date: Tue, 23 Nov 2021 19:22:19 +0100 Subject: [PATCH 2/4] Fix infinite loop issue on container healthcheck Signed-off-by: guillaume --- stdlib/universe.bats | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/stdlib/universe.bats b/stdlib/universe.bats index acac71eb..485ab84c 100644 --- a/stdlib/universe.bats +++ b/stdlib/universe.bats @@ -88,7 +88,8 @@ setup() { @test "docker run: ports" { dagger -e docker-run-ports up CONTAINER=$(docker container ls -q --filter "name=daggerci-test-ports-*") - until docker inspect --format "{{json .State.Status }}" "$CONTAINER" | grep -m 1 "running"; do sleep 1 ; done + SECONDS=0 + while [[ "$(docker inspect --format '{{json .State.Status }}' todoapp | grep -m 1 'running')" != "running" && $SECONDS -lt 45 ]]; do sleep 1 ; done run curl -f -LI http://localhost:8080 assert_output --partial '200 OK' docker stop "$CONTAINER" && docker rm "$CONTAINER" From 84952b1725ce576f30a998904216506a39ffe9ff Mon Sep 17 00:00:00 2001 From: guillaume Date: Tue, 23 Nov 2021 19:22:59 +0100 Subject: [PATCH 3/4] Add tests for http.#Wait Signed-off-by: guillaume --- stdlib/.dagger/env/docker-pull/values.yaml | 6 ++--- .../sanity-check/plan/cue.mod/pkg/.gitignore | 3 ++- stdlib/http/http.cue | 12 ++++----- stdlib/http/tests/http.cue | 27 +++++++++---------- 4 files changed, 23 insertions(+), 25 deletions(-) diff --git a/stdlib/.dagger/env/docker-pull/values.yaml b/stdlib/.dagger/env/docker-pull/values.yaml index 75138a21..1ddc3303 100644 --- a/stdlib/.dagger/env/docker-pull/values.yaml +++ b/stdlib/.dagger/env/docker-pull/values.yaml @@ -3,7 +3,7 @@ plan: name: docker-pull inputs: ref: - text: docker.io/daggerio/ci-test:pncdyzkdemof@sha256:b92cbbfef6b952befc38812cd88cf5c4c1012f6df2891595c226f56cc053334e + text: docker.io/daggerio/ci-test:tyrgcrtlasex@sha256:3b308dec2f3d4c563b9c71ce4ddc660e476b899d06fa37f3b9fef350c1062a1f sops: kms: [] gcp_kms: [] @@ -19,8 +19,8 @@ sops: SG1raUVNTzZIWDltV1pOS3hySHlJeWcKg3blmstOGcxtPww513+mAEA0MWOXwNAT 5ngRvG6MraW3g9dhIuUYOwjuJyz1Z07/DBEocSxnjSyw45ZCkM1/9Q== -----END AGE ENCRYPTED FILE----- - lastmodified: "2021-08-31T10:10:02Z" - mac: ENC[AES256_GCM,data:30qNlAVLJunPEboTzeIxcsZ06LcLiDiXXJLVqHE328hcezcOYGsvhlYTiGEzxtAsv78Mwxw54oSbiFZmCKoew9bTZFUyb6FcFVk4GG8z2I8pn7FkZlcnEknWinVf9Tc/h5R/g4/BBGzsBf2dr4fx4ADewwO2z1Df/8wdup0PD4E=,iv:KJcMdpLCfSU1LvvPMXitSPzm0JPwrDWdLncdvVFngNk=,tag:X2/D+RhEnyizZHXJWYnmmg==,type:str] + lastmodified: "2021-11-23T18:15:32Z" + mac: ENC[AES256_GCM,data:+HpG1ZDND1Japu1hLgTVKF6CfEUbYp+sEBpv0WtsCHcHGYeYVkZT8ZJKajWlz8uFfikpKv76FkBw8XqKVXyPZYTQWuo9VvmPTF5HajiVE6kP5Ax3/5Sh7ecwfGdbqE3mQoj+jaQE8NBnGbCBljij/xFYW63fIaurqsyPWTQl2YI=,iv:fBPseUNueHO4ZUnlwDljfAwUvi4UyjGysXMTCmoAgYw=,tag:Pg4dGX4/05SahATlE6zrsA==,type:str] pgp: [] encrypted_suffix: secret version: 3.7.1 diff --git a/stdlib/.dagger/env/sanity-check/plan/cue.mod/pkg/.gitignore b/stdlib/.dagger/env/sanity-check/plan/cue.mod/pkg/.gitignore index a572e9ee..2d4dc1ae 100644 --- a/stdlib/.dagger/env/sanity-check/plan/cue.mod/pkg/.gitignore +++ b/stdlib/.dagger/env/sanity-check/plan/cue.mod/pkg/.gitignore @@ -1,2 +1,3 @@ -# dagger universe +# generated by dagger alpha.dagger.io +dagger.lock diff --git a/stdlib/http/http.cue b/stdlib/http/http.cue index b7b7f898..2628324f 100644 --- a/stdlib/http/http.cue +++ b/stdlib/http/http.cue @@ -94,7 +94,7 @@ import ( // Waiting time between checks (sec.) interval: int | *30 - + // Max amount of retries retries: int | *3 @@ -143,12 +143,12 @@ import ( ] always: true env: { - HEALTH_URL: url - INTERVAL: "\(interval)" - NB_RETRIES: "\(retries)" + HEALTH_URL: url + INTERVAL: "\(interval)" + NB_RETRIES: "\(retries)" START_PERIOD: "\(startPeriod)" - TIMEOUT: "\(timeout)" + TIMEOUT: "\(timeout)" } }, ] -} \ No newline at end of file +} diff --git a/stdlib/http/tests/http.cue b/stdlib/http/tests/http.cue index e91b2736..01de831b 100644 --- a/stdlib/http/tests/http.cue +++ b/stdlib/http/tests/http.cue @@ -8,7 +8,6 @@ import ( "alpha.dagger.io/random" ) - TestDockersocket: dagger.#Stream & dagger.#Input TestSuffix: random.#String & { @@ -27,17 +26,15 @@ Testhealth: #Wait & { url: "http://localhost:8080/" } -TestWait: { - query: os.#Container & { - image: alpine.#Image & { - package: bash: "=~5.1" - package: curl: true - } - command: #""" - test "$(curl -L --fail --silent --show-error --write-out "%{http_code}" "$URL" -o /dev/null)" = "200" - """# - env: URL: Testhealth.url +TestWait: query: os.#Container & { + image: alpine.#Image & { + package: bash: "=~5.1" + package: curl: true } + command: #""" + test "$(curl -L --fail --silent --show-error --write-out "%{http_code}" "$URL" -o /dev/null)" = "200" + """# + env: URL: Testhealth.url } TestRequest: { @@ -59,7 +56,7 @@ TestRequest: { command: #Command } #Command: #""" - cat /content.json | grep -q nginx >/dev/null - test "$STATUS" = "200" - """# -} \ No newline at end of file + cat /content.json | grep -q nginx >/dev/null + test "$STATUS" = "200" + """# +} From 475e47de09eb1293baf86b3d970aaac2d2c2f697 Mon Sep 17 00:00:00 2001 From: guillaume Date: Tue, 23 Nov 2021 19:26:02 +0100 Subject: [PATCH 4/4] Modify getting started doc to integrate http.#Wait, fix doc dependency issue Signed-off-by: guillaume --- .../tests/getting-started/plans/local/local.cue | 9 ++++++++- docs/learn/tests/getting-started/plans/todoapp.cue | 2 +- docs/reference/http.md | 12 ++++++++++++ stdlib/http/tests/http.cue | 4 ++-- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/docs/learn/tests/getting-started/plans/local/local.cue b/docs/learn/tests/getting-started/plans/local/local.cue index a9460987..63ed78d3 100644 --- a/docs/learn/tests/getting-started/plans/local/local.cue +++ b/docs/learn/tests/getting-started/plans/local/local.cue @@ -3,6 +3,7 @@ package todoapp import ( "alpha.dagger.io/dagger" "alpha.dagger.io/docker" + "alpha.dagger.io/http" ) // docker local socket @@ -24,10 +25,16 @@ registry: docker.#Run & { socket: dockerSocket } +// As we pushed the registry to our local docker +// we need to wait for the container to be up +wait: http.#Wait & { + url: "localhost:5042" +} + // push to our local registry // this concrete value satisfies the string constraint // we defined in the previous file -push: target: "localhost:5042/todoapp" +push: target: "\(wait.url)/todoapp" // Application URL appURL: "http://localhost:8080/" & dagger.#Output diff --git a/docs/learn/tests/getting-started/plans/todoapp.cue b/docs/learn/tests/getting-started/plans/todoapp.cue index 8fa8b0cd..e3812196 100644 --- a/docs/learn/tests/getting-started/plans/todoapp.cue +++ b/docs/learn/tests/getting-started/plans/todoapp.cue @@ -2,9 +2,9 @@ package todoapp import ( "alpha.dagger.io/dagger" - "alpha.dagger.io/os" "alpha.dagger.io/docker" "alpha.dagger.io/js/yarn" + "alpha.dagger.io/os" ) // Build the source code using Yarn diff --git a/docs/reference/http.md b/docs/reference/http.md index 71d6e0b9..41c83cd6 100644 --- a/docs/reference/http.md +++ b/docs/reference/http.md @@ -72,3 +72,15 @@ _No input._ | ------------- |:-------------: |:-------------: | |*statusCode* | `string` |- | |*body* | `string` |- | + +## http.#Wait + +URL listener Creates a dependency on URL + +### http.#Wait Inputs + +_No input._ + +### http.#Wait Outputs + +_No output._ diff --git a/stdlib/http/tests/http.cue b/stdlib/http/tests/http.cue index 01de831b..a512b633 100644 --- a/stdlib/http/tests/http.cue +++ b/stdlib/http/tests/http.cue @@ -18,12 +18,12 @@ TestRun: docker.#Run & { name: "daggerci-test-wait-\(TestSuffix.out)" ref: "nginx" socket: TestDockersocket - ports: ["8080:80"] + ports: ["8088:80"] } // Waits for TestRun to finish initializing Testhealth: #Wait & { - url: "http://localhost:8080/" + url: "http://localhost:8088/" } TestWait: query: os.#Container & {