From e7a738211f8eb658d549c4b29f81658ba4b4c0a7 Mon Sep 17 00:00:00 2001 From: Tom Chauveau Date: Mon, 30 Aug 2021 16:06:39 +0200 Subject: [PATCH] Improve test policy for examples Signed-off-by: Tom Chauveau --- docs/learn/tests/dev-cue-package/script.sh | 2 +- docs/learn/tests/doc.bats | 167 +++++++++++---------- docs/learn/tests/helpers.bash | 16 +- 3 files changed, 96 insertions(+), 89 deletions(-) diff --git a/docs/learn/tests/dev-cue-package/script.sh b/docs/learn/tests/dev-cue-package/script.sh index 3862188c..03aae599 100644 --- a/docs/learn/tests/dev-cue-package/script.sh +++ b/docs/learn/tests/dev-cue-package/script.sh @@ -1,6 +1,6 @@ #!/bin/bash -mkdir test +mkdir -p test cat > test/source.cue << EOF package test diff --git a/docs/learn/tests/doc.bats b/docs/learn/tests/doc.bats index 99a4a148..519e6a81 100644 --- a/docs/learn/tests/doc.bats +++ b/docs/learn/tests/doc.bats @@ -9,70 +9,73 @@ setup() { # Test 1003-get-started @test "doc-1003-get-started" { - setup_example_sandbox "doc" + setup_example_sandbox # Set examples private key - ./import-tutorial-key.sh + "$DAGGER_SANDBOX"/import-tutorial-key.sh # Collect url - dagger up - url=$(dagger query -f text url) + dagger -w "$DAGGER_SANDBOX" up + url=$(dagger -w "$DAGGER_SANDBOX" query -f text url) # More commands - dagger list - ls -l ./s3 - dagger input list + dagger -w "$DAGGER_SANDBOX" list + ls -l "$DAGGER_SANDBOX"/s3 + dagger -w "$DAGGER_SANDBOX" input list # Check output - run curl $url + run curl "$url" assert_output --partial "My Todo app" } @test "doc-1004-first-env" { - setup_example_sandbox "doc" + setup_example_sandbox # Follow tutorial - mkdir multibucket - cp $CODEBLOC_SRC/multibucket/source.cue multibucket - cp $CODEBLOC_SRC/multibucket/yarn.cue multibucket - cp $CODEBLOC_SRC/multibucket/netlify.cue multibucket + mkdir -p "$DAGGER_SANDBOX"/multibucket + cp "$DAGGER_WORKSPACE"/multibucket/source.cue "$DAGGER_SANDBOX"/multibucket + cp "$DAGGER_WORKSPACE"/multibucket/yarn.cue "$DAGGER_SANDBOX"/multibucket + cp "$DAGGER_WORKSPACE"/multibucket/netlify.cue "$DAGGER_SANDBOX"/multibucket - dagger doc alpha.dagger.io/netlify - dagger doc alpha.dagger.io/js/yarn + dagger -w "$DAGGER_SANDBOX" doc alpha.dagger.io/netlify + dagger -w "$DAGGER_SANDBOX" doc alpha.dagger.io/js/yarn # Initialize new env - dagger new 'multibucket' -p ./multibucket + dagger -w "$DAGGER_SANDBOX" new 'multibucket' -p "$DAGGER_SANDBOX"/multibucket # Copy corresponding env - cp -r $CODEBLOC_SRC/.dagger/env/multibucket .dagger/env/ + cp -r "$DAGGER_WORKSPACE"/.dagger/env/multibucket "$DAGGER_SANDBOX"/.dagger/env/ + # Add missing src input - dagger -e multibucket input dir src . + dagger -w "$DAGGER_SANDBOX" -e multibucket input dir src "$DAGGER_SANDBOX" # Run test - dagger -e multibucket up - url=$(dagger -e multibucket query -f text site.netlify.deployUrl) + dagger -w "$DAGGER_SANDBOX" -e multibucket up + url=$(dagger -w "$DAGGER_SANDBOX" -e multibucket query -f text site.netlify.deployUrl) - # Check output : - run curl $url + # Check output + run curl "$url" assert_output --partial "./static/css/main.9149988f.chunk.css" } @test "doc-1006-google-cloud-run" { - setup_example_sandbox "doc" + setup_example_sandbox - mkdir gcpcloudrun - cp $CODEBLOC_SRC/gcpcloudrun/source.cue gcpcloudrun + # Follow tutorial + mkdir -p "$DAGGER_SANDBOX"/gcpcloudrun + cp "$DAGGER_WORKSPACE"/gcpcloudrun/source.cue "$DAGGER_SANDBOX"/gcpcloudrun # Initialize new env - dagger new 'gcpcloudrun' -p gcpcloudrun + dagger -w "$DAGGER_SANDBOX" new 'gcpcloudrun' -p "$DAGGER_SANDBOX"/gcpcloudrun # Copy corresponding env - cp -r $CODEBLOC_SRC/.dagger/env/gcpcloudrun .dagger/env/ + cp -r "$DAGGER_WORKSPACE"/.dagger/env/gcpcloudrun "$DAGGER_SANDBOX"/.dagger/env/ + # Add missing src input - dagger -e gcpcloudrun input dir src . + dagger -w "$DAGGER_SANDBOX" -e gcpcloudrun input dir src "$DAGGER_SANDBOX" # Run test - run dagger -e gcpcloudrun up + run dagger -w "$DAGGER_SANDBOX" -e gcpcloudrun up assert_success } @@ -140,7 +143,7 @@ setup() { dagger -w "$DAGGER_SANDBOX" -e kube-aws-basic up #################### DEPLOYMENT #################### - # Copy deployment to sandbox + # Copy deployment to sandbox copy_to_sandbox kube-aws-deployment kube-aws # Up deployment @@ -177,93 +180,103 @@ setup() { @test "doc-1008-aws-cloudformation" { skip_unless_local_localstack - setup_example_sandbox "doc" + setup_example_sandbox ### Create a basic plan ## Construct - mkdir cloudformation - cp $CODEBLOC_SRC/cloudformation/template.cue cloudformation + mkdir -p "$DAGGER_SANDBOX"/cloudformation + cp "$DAGGER_WORKSPACE"/cloudformation/template.cue "$DAGGER_SANDBOX"/cloudformation # Cloudformation relay - dagger doc alpha.dagger.io/aws/cloudformation - cp $CODEBLOC_SRC/cloudformation/source-begin.cue cloudformation/source.cue + dagger -w "$DAGGER_SANDBOX" doc alpha.dagger.io/aws/cloudformation + cp "$DAGGER_WORKSPACE"/cloudformation/source-begin.cue "$DAGGER_SANDBOX"/cloudformation/source.cue # Initialize new env - dagger new 'cloudformation' -p cloudformation + dagger -w "$DAGGER_SANDBOX" new 'cloudformation' -p "$DAGGER_SANDBOX"/cloudformation # Finish template setup - cp $CODEBLOC_SRC/cloudformation/source-end.cue cloudformation/source.cue + cp "$DAGGER_WORKSPACE"/cloudformation/source-end.cue "$DAGGER_SANDBOX"/cloudformation/source.cue + # Copy corresponding env - cp -r $CODEBLOC_SRC/.dagger/env/cloudformation .dagger/env/ + cp -r "$DAGGER_WORKSPACE"/.dagger/env/cloudformation "$DAGGER_SANDBOX"/.dagger/env/ # Run test - dagger -e cloudformation up - stackName=$(dagger -e cloudformation query cfnStackName -f text) + dagger -w "$DAGGER_SANDBOX" -e cloudformation up + stackName=$(dagger -w "$DAGGER_SANDBOX" -e cloudformation query cfnStackName -f text) ## Cleanup # Place back empty source - cp $CODEBLOC_SRC/cloudformation/source-begin.cue cloudformation/source.cue - cp $CODEBLOC_SRC/cloudformation/deletion.cue cloudformation/deletion.cue + cp "$DAGGER_WORKSPACE"/cloudformation/source-begin.cue "$DAGGER_SANDBOX"/cloudformation/source.cue + cp "$DAGGER_WORKSPACE"/cloudformation/deletion.cue "$DAGGER_SANDBOX"/cloudformation/deletion.cue # Prepare and run cloudformation cleanup - dagger -e cloudformation input text stackRemoval.stackName $stackName - dagger -e cloudformation up + dagger -w "$DAGGER_SANDBOX" -e cloudformation input text stackRemoval.stackName "$stackName" + dagger -w "$DAGGER_SANDBOX" -e cloudformation up ### Template part ## Create convert.cue - cp $CODEBLOC_SRC/cloudformation/template/convert.cue cloudformation/convert.cue - rm cloudformation/source.cue cloudformation/deletion.cue + cp "$DAGGER_WORKSPACE"/cloudformation/template/convert.cue "$DAGGER_SANDBOX"/cloudformation/convert.cue + rm "$DAGGER_SANDBOX"/cloudformation/source.cue "$DAGGER_SANDBOX"/cloudformation/deletion.cue ## Retrieve Unmarshalled JSON - dagger query -e cloudformation s3Template + dagger -w "$DAGGER_SANDBOX" query -e cloudformation s3Template ## Remove convert.cue - rm cloudformation/convert.cue - ## Store the output - cp $CODEBLOC_SRC/cloudformation/template/template-begin.cue cloudformation/template.cue - # Inspect conf - dagger query -e cloudformation template -f text + rm "$DAGGER_SANDBOX"/cloudformation/convert.cue - cp $CODEBLOC_SRC/cloudformation/template/deployment.cue cloudformation/deployment.cue - cp $CODEBLOC_SRC/cloudformation/template/template-end.cue cloudformation/template.cue - cp $CODEBLOC_SRC/cloudformation/source-end.cue cloudformation/source.cue + ## Store the output + cp "$DAGGER_WORKSPACE"/cloudformation/template/template-begin.cue "$DAGGER_SANDBOX"/cloudformation/template.cue + + # Inspect conf + dagger -w "$DAGGER_SANDBOX" query -e cloudformation template -f text + + cp "$DAGGER_WORKSPACE"/cloudformation/template/deployment.cue "$DAGGER_SANDBOX"/cloudformation/deployment.cue + cp "$DAGGER_WORKSPACE"/cloudformation/template/template-end.cue "$DAGGER_SANDBOX"/cloudformation/template.cue + cp "$DAGGER_WORKSPACE"/cloudformation/source-end.cue "$DAGGER_SANDBOX"/cloudformation/source.cue # Deploy again - dagger -e cloudformation query template -f text - dagger -e cloudformation up - dagger -e cloudformation output list + dagger -w "$DAGGER_SANDBOX" -e cloudformation query template -f text + dagger -w "$DAGGER_SANDBOX" -e cloudformation up + dagger -w "$DAGGER_SANDBOX" -e cloudformation output list ## Cleanup again - stackName=$(dagger -e cloudformation query cfnStackName -f text) - rm -rf cloudformation/* + stackName=$(dagger -w "$DAGGER_SANDBOX" -e cloudformation query cfnStackName -f text) + rm -rf "$DAGGER_SANDBOX"/cloudformation/* + # Place back empty source - cp $CODEBLOC_SRC/cloudformation/source-begin.cue cloudformation/source.cue - cp $CODEBLOC_SRC/cloudformation/deletion.cue cloudformation/deletion.cue + cp "$DAGGER_WORKSPACE"/cloudformation/source-begin.cue "$DAGGER_SANDBOX"/cloudformation/source.cue + cp "$DAGGER_WORKSPACE"/cloudformation/deletion.cue "$DAGGER_SANDBOX"/cloudformation/deletion.cue + # Prepare and run cloudformation cleanup - dagger -e cloudformation input text stackRemoval.stackName $stackName - dagger -e cloudformation up + dagger -w "$DAGGER_SANDBOX" -e cloudformation input text stackRemoval.stackName "$stackName" + dagger -w "$DAGGER_SANDBOX" -e cloudformation up } @test "doc-1010-dev-cue-package" { - setup_example_sandbox "" - # Initializing workspace - mkdir workspace - cd workspace + mkdir -p "$DAGGER_SANDBOX"/workspace # Writing package - dagger init - mkdir -p cue.mod/pkg/github.com/tjovicic/gcpcloudrun - cp $CODEBLOC_SRC/dev-cue-package/source.cue cue.mod/pkg/github.com/tjovicic/gcpcloudrun/source.cue - cp $CODEBLOC_SRC/dev-cue-package/script.sh . + # dagger init # The sandbox is already init + mkdir -p "$DAGGER_SANDBOX"/cue.mod/pkg/github.com/tjovicic/gcpcloudrun + cp "$DAGGER_WORKSPACE"/dev-cue-package/source.cue "$DAGGER_SANDBOX"/cue.mod/pkg/github.com/tjovicic/gcpcloudrun/source.cue + cp "$DAGGER_WORKSPACE"/dev-cue-package/script.sh "$DAGGER_SANDBOX"/workspace/script.sh # We remove the last line of the script, as bats cannot expand dagger # to dagger() bats helper func inside bash files - sed '$d' < script.sh > tmpFile ; mv tmpFile script.sh + sed '$d' <"$DAGGER_SANDBOX"/workspace/script.sh >"$DAGGER_SANDBOX"/tmpFile + mv "$DAGGER_SANDBOX"/tmpFile "$DAGGER_SANDBOX"/workspace/script.sh + + chmod +x "$DAGGER_SANDBOX"/workspace/script.sh + "$DAGGER_SANDBOX"/workspace/script.sh + + # Sync file from documentation + rsync -a test "$DAGGER_SANDBOX" - chmod +x script.sh - ./script.sh # Command removed from script.sh above - dagger new staging -p ./test - run dagger up -e staging + dagger -w "$DAGGER_SANDBOX" new staging -p "$DAGGER_SANDBOX"/test + run dagger up -w "$DAGGER_SANDBOX" -e staging assert_output --partial "input=run.gcpConfig.serviceKey" + + # Clean script.sh output + rm -rf ./test } diff --git a/docs/learn/tests/helpers.bash b/docs/learn/tests/helpers.bash index b471179f..a59efdc7 100644 --- a/docs/learn/tests/helpers.bash +++ b/docs/learn/tests/helpers.bash @@ -34,21 +34,15 @@ dagger() { "${DAGGER}" "$@" } -# dagger helper to run doc examples in clean environment +# Setup sandbox for dagger example +# It clones the example repository and update Sandbox to setup_example_sandbox() { - # Tell Dagger not to use DAGGER WORKSPACE env var - unset DAGGER_WORKSPACE + git -C "$DAGGER_SANDBOX" clone https://github.com/dagger/examples - export CODEBLOC_SRC="$(pwd)" - local tmpdir=$(mktemp -d) - cd $tmpdir - if [ "$1" = "doc" ]; then - git clone https://github.com/dagger/examples - cd examples/todoapp - cue mod init - fi + export DAGGER_SANDBOX="$DAGGER_SANDBOX"/examples/todoapp } + # copy an environment from the current workspace to the sandbox. # # this is needed if the test requires altering inputs without dirtying the