From ce7b4733d746701980e7095c4f6a3f15eeef4557 Mon Sep 17 00:00:00 2001 From: decentral1se Date: Sat, 23 Sep 2023 23:19:49 +0200 Subject: [PATCH] test: tag/git helpers & refactor [ci skip] --- tests/integration/app_backup.bats | 4 +++ tests/integration/app_check.bats | 11 ++----- tests/integration/app_cmd.bats | 11 ++----- tests/integration/app_deploy.bats | 42 ++++----------------------- tests/integration/app_rollback.bats | 6 +--- tests/integration/helpers/git.bash | 31 ++++++++++++++++---- tests/integration/helpers/recipe.bash | 17 ++++++----- tests/integration/recipe_release.bats | 5 +--- 8 files changed, 52 insertions(+), 75 deletions(-) diff --git a/tests/integration/app_backup.bats b/tests/integration/app_backup.bats index a91d6e13..abd3f697 100644 --- a/tests/integration/app_backup.bats +++ b/tests/integration/app_backup.bats @@ -94,6 +94,8 @@ teardown(){ run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" status refute_output --partial 'behind 3' + + _reset_recipe "$TEST_RECIPE" } @test "ensure recipe not up to date if --offline" { @@ -118,6 +120,8 @@ teardown(){ run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" status refute_output --partial 'behind 3' + + _reset_recipe "$TEST_RECIPE" } @test "detect backup labels" { diff --git a/tests/integration/app_check.bats b/tests/integration/app_check.bats index 712f2ca3..1982016d 100644 --- a/tests/integration/app_check.bats +++ b/tests/integration/app_check.bats @@ -75,12 +75,11 @@ setup(){ run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" status refute_output --partial 'behind 3' + + _reset_recipe "$TEST_RECIPE" } @test "ensure recipe not up to date if --offline" { - latestCommit="$(git -C "$ABRA_DIR/recipes/$TEST_RECIPE" rev-parse --short HEAD)" - refute [ -z "$latestCommit" ]; - run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" reset --hard HEAD~3 assert_success @@ -93,11 +92,7 @@ setup(){ run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" status assert_output --partial 'behind 3' - run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" checkout "$latestCommit" - assert_success - - run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" status - refute_output --partial 'behind 3' + _reset_recipe "$TEST_RECIPE" } @test "error if missing .env.sample" { diff --git a/tests/integration/app_cmd.bats b/tests/integration/app_cmd.bats index 3dd50ee3..cd3d53e1 100644 --- a/tests/integration/app_cmd.bats +++ b/tests/integration/app_cmd.bats @@ -85,12 +85,11 @@ teardown(){ run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" status refute_output --partial 'behind 3' + + _reset_recipe "$TEST_RECIPE" } @test "ensure recipe not up to date if --offline" { - latestCommit="$(git -C "$ABRA_DIR/recipes/$TEST_RECIPE" rev-parse --short HEAD)" - refute [ -z "$latestCommit" ]; - run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" reset --hard HEAD~3 assert_success @@ -104,11 +103,7 @@ teardown(){ run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" status assert_output --partial 'behind 3' - run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" checkout "$latestCommit" - assert_success - - run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" status - refute_output --partial 'behind 3' + _reset_recipe "$TEST_RECIPE" } @test "error if missing arguments without passing --local" { diff --git a/tests/integration/app_deploy.bats b/tests/integration/app_deploy.bats index edee25d9..3ba3f485 100644 --- a/tests/integration/app_deploy.bats +++ b/tests/integration/app_deploy.bats @@ -86,12 +86,7 @@ teardown(){ run $ABRA app deploy "$TEST_APP_DOMAIN" --no-input --no-converge-checks assert_success - # NOTE(d1): nuke it to ensure clean git state - run rm -rf "$ABRA_DIR/recipes/$TEST_RECIPE" - assert_success - assert_not_exists "$ABRA_DIR/recipes/$TEST_RECIPE" - _fetch_recipe "$TEST_RECIPE" - + _reset_recipe "$TEST_RECIPE" _undeploy_app } @@ -112,30 +107,14 @@ teardown(){ assert_output --partial 'behind 3' _undeploy_app - - # NOTE(d1): nuke it to ensure clean git state - run rm -rf "$ABRA_DIR/recipes/$TEST_RECIPE" - assert_success - assert_not_exists "$ABRA_DIR/recipes/$TEST_RECIPE" - _fetch_recipe "$TEST_RECIPE" + _reset_recipe "$TEST_RECIPE" } # bats test_tags=slow @test "deploy latest commit if no published versions and no --chaos" { latestCommit="$(git -C "$ABRA_DIR/recipes/$TEST_RECIPE" rev-parse --short HEAD)" - run bash -c 'git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -l | wc -l' - assert_success - refute_output '0' - - run bash -c 'git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -l | \ - xargs -I{} git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -d {}' - assert_success - assert_output --partial 'Deleted tag' - - run bash -c 'git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -l | wc -l' - assert_success - assert_output '0' + _remove_tags # NOTE(d1): need to pass --offline to stop tags being pulled again run $ABRA app deploy "$TEST_APP_DOMAIN" \ @@ -146,13 +125,7 @@ teardown(){ refute_output --partial 'chaos' _undeploy_app - - run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" fetch --all - assert_success - - run bash -c 'git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -l | wc -l' - assert_success - refute_output '0' + _reset_tags } # bats test_tags=slow @@ -175,12 +148,7 @@ teardown(){ assert_output --partial 'chaos' _undeploy_app - - # NOTE(d1): nuke it to ensure clean git state - run rm -rf "$ABRA_DIR/recipes/$TEST_RECIPE" - assert_success - assert_not_exists "$ABRA_DIR/recipes/$TEST_RECIPE" - _fetch_recipe "$TEST_RECIPE" + _reset_recipe } # bats test_tags=slow diff --git a/tests/integration/app_rollback.bats b/tests/integration/app_rollback.bats index b3601741..e71ac190 100644 --- a/tests/integration/app_rollback.bats +++ b/tests/integration/app_rollback.bats @@ -78,11 +78,7 @@ teardown(){ run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" checkout "$latestCommit" assert_success - # NOTE(d1): nuke it to ensure clean git state - run rm -rf "$ABRA_DIR/recipes/$TEST_RECIPE" - assert_success - assert_not_exists "$ABRA_DIR/recipes/$TEST_RECIPE" - _fetch_recipe "$TEST_RECIPE" + _reset_recipe "$TEST_RECIPE" } @test "bail if unstaged changes and no --chaos" { diff --git a/tests/integration/helpers/git.bash b/tests/integration/helpers/git.bash index a8f41f4d..f2654944 100644 --- a/tests/integration/helpers/git.bash +++ b/tests/integration/helpers/git.bash @@ -1,11 +1,30 @@ #!/usr/bin/env bash _checkout_recipe() { - if [[ -z "$1" ]]; then - echo 'forgot to pass argument to function?' - exit 1 - fi - - run git -C "$ABRA_DIR/recipes/$1" checkout . + run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" checkout . assert_success } + +_remove_tags(){ + run bash -c 'git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -l | wc -l' + assert_success + refute_output '0' + + run bash -c 'git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -l | \ + xargs -I{} git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -d {}' + assert_success + assert_output --partial 'Deleted tag' + + run bash -c 'git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -l | wc -l' + assert_success + assert_output '0' +} + +_reset_tags() { + run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" fetch --all + assert_success + + run bash -c 'git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -l | wc -l' + assert_success + refute_output '0' +} diff --git a/tests/integration/helpers/recipe.bash b/tests/integration/helpers/recipe.bash index 4341f65c..5b5ec3f6 100644 --- a/tests/integration/helpers/recipe.bash +++ b/tests/integration/helpers/recipe.bash @@ -1,16 +1,19 @@ #!/usr/bin/env bash _fetch_recipe() { - if [[ -z "$1" ]]; then - echo 'forgot to pass argument to function?' - exit 1 - fi - - if [[ ! -d "$ABRA_DIR/recipes/$1" ]]; then + if [[ ! -d "$ABRA_DIR/recipes/$TEST_RECIPE" ]]; then run mkdir -p "$ABRA_DIR/recipes" assert_success - run git clone "https://git.coopcloud.tech/coop-cloud/$1" "$ABRA_DIR/recipes/$1" + run git clone "https://git.coopcloud.tech/coop-cloud/$TEST_RECIPE" "$ABRA_DIR/recipes/$TEST_RECIPE" assert_success fi } + +_reset_recipe(){ + run rm -rf "$ABRA_DIR/recipes/$TEST_RECIPE" + assert_success + assert_not_exists "$ABRA_DIR/recipes/$TEST_RECIPE" + + _fetch_recipe +} diff --git a/tests/integration/recipe_release.bats b/tests/integration/recipe_release.bats index 67c45bc9..6fe78305 100644 --- a/tests/integration/recipe_release.bats +++ b/tests/integration/recipe_release.bats @@ -100,8 +100,5 @@ setup(){ assert_output --partial '0.1.0+1.20.0' assert_output --regexp '0\.2\.0\+1\.2.*' - # NOTE(d1): nuke it since we can't clean up the tag - run rm -rf "$ABRA_DIR/recipes/$TEST_RECIPE" - assert_success - assert_not_exists "$ABRA_DIR/recipes/$TEST_RECIPE" + _reset_recipe "$TEST_RECIPE" }