From 9c20c45453e5ba89bdc58eb5a0d2af2660cf7352 Mon Sep 17 00:00:00 2001 From: Tom Chauveau Date: Fri, 18 Jun 2021 19:15:48 +0200 Subject: [PATCH] Use sandbox in tests Signed-off-by: Tom Chauveau --- .../.dagger/env/kubernetes-helm/plan/helm.cue | 2 +- stdlib/helpers.bash | 12 +++++++ stdlib/universe.bats | 35 ++++++++++++------- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/stdlib/.dagger/env/kubernetes-helm/plan/helm.cue b/stdlib/.dagger/env/kubernetes-helm/plan/helm.cue index 6d1c5485..ab06eb13 100644 --- a/stdlib/.dagger/env/kubernetes-helm/plan/helm.cue +++ b/stdlib/.dagger/env/kubernetes-helm/plan/helm.cue @@ -39,7 +39,7 @@ TestHelmRepoChart: { seed: "repo" } - // Deploy chart + // Deploy remote chart deploy: helm.#Chart & { name: "dagger-test-repository-\(suffix.out)" namespace: "dagger-test" diff --git a/stdlib/helpers.bash b/stdlib/helpers.bash index 0bddf106..f2f4b4c7 100644 --- a/stdlib/helpers.bash +++ b/stdlib/helpers.bash @@ -39,12 +39,24 @@ dagger() { # copy_to_sandbox myenv # dagger input secret -w "$DAGGER_SANDBOX" -e myenv "temporary change" # dagger up -w "$DAGGER_SANDBOX" -e myenv +# +# To use testdata directory in tests, add the package name as second flag +# Usage: +# copy_to_sandbox myenv mypackage copy_to_sandbox() { local name="$1" local source="$DAGGER_WORKSPACE"/.dagger/env/"$name" local target="$DAGGER_SANDBOX"/.dagger/env/"$name" cp -a "$source" "$target" + + if [ -d "$2" ]; then + local package="$2" + local source_package="$DAGGER_WORKSPACE"/"$package" + local target_package="$DAGGER_SANDBOX"/ + + cp -a "$source_package" "$target_package" + fi } # Check if there is a local kubernetes cluster. diff --git a/stdlib/universe.bats b/stdlib/universe.bats index 16681b0e..66d6a965 100644 --- a/stdlib/universe.bats +++ b/stdlib/universe.bats @@ -64,13 +64,16 @@ setup() { @test "kubernetes: deployment" { skip_unless_local_kube - # Set kubeconfig - dagger -e kubernetes-deployment input text TestKubeconfig -f "$HOME"/.kube/config + # Copy deployment to sandbox + copy_to_sandbox kubernetes-deployment - dagger -e kubernetes-deployment up + # Set kubeconfig + dagger -w "$DAGGER_SANDBOX" -e kubernetes-deployment input text TestKubeconfig -f "$HOME"/.kube/config + + dagger -w "$DAGGER_SANDBOX" -e kubernetes-deployment up # Unset kubeconfig - dagger -e kubernetes-deployment input unset TestKubeconfig + dagger -w "$DAGGER_SANDBOX" -e kubernetes-deployment input unset TestKubeconfig } @test "kubernetes: kustomize" { @@ -80,13 +83,16 @@ setup() { @test "kubernetes: helm" { skip_unless_local_kube - # Set kubeconfig - dagger -e kubernetes-helm input text TestKubeconfig -f "$HOME"/.kube/config + # Copy deployment to sandbox + copy_to_sandbox kubernetes-helm kubernetes - dagger -e kubernetes-helm up + # Set kubeconfig + dagger -w "$DAGGER_SANDBOX" -e kubernetes-helm input text TestKubeconfig -f "$HOME"/.kube/config + + dagger -w "$DAGGER_SANDBOX" -e kubernetes-helm up # Unset kubeconfig - dagger -e kubernetes-helm input unset TestKubeconfig + dagger -w "$DAGGER_SANDBOX" -e kubernetes-helm input unset TestKubeconfig } @test "google cloud: gcr" { @@ -102,23 +108,26 @@ setup() { run dagger -e terraform up assert_failure + # Copy deployment to sandbox + copy_to_sandbox terraform terraform + # Add the var and try again - run dagger -e terraform input text TestTerraform.apply.tfvars.input "42" - run dagger -e terraform up + run dagger -w "$DAGGER_SANDBOX" -e terraform input text TestTerraform.apply.tfvars.input "42" + run dagger -w "$DAGGER_SANDBOX" -e terraform up assert_success # ensure the tfvar was passed correctly - run dagger query -e terraform TestTerraform.apply.output.input.value -f text + run dagger -w "$DAGGER_SANDBOX" query -e terraform TestTerraform.apply.output.input.value -f text assert_success assert_output "42" # ensure the random value is always the same # this proves we're effectively using the s3 backend - run dagger query -e terraform TestTerraform.apply.output.random.value -f json + run dagger -w "$DAGGER_SANDBOX" query -e terraform TestTerraform.apply.output.random.value -f json assert_success assert_output "36" # Unset input - run dagger -e terraform input unset TestTerraform.apply.tfvars.input + run dagger -w "$DAGGER_SANDBOX" -e terraform input unset TestTerraform.apply.tfvars.input assert_success } \ No newline at end of file