From 8b8e158664d1436f289cb71dbaf72e4b0a5cea27 Mon Sep 17 00:00:00 2001 From: decentral1se Date: Wed, 17 Jul 2024 13:22:14 +0200 Subject: [PATCH] test: int suite fixes --- cli/app/deploy.go | 2 +- cli/app/secret.go | 1 + cli/recipe/version.go | 25 ++++++++++--------- pkg/app/app.go | 13 +++++++++- pkg/formatter/formatter.go | 6 ++++- pkg/recipe/git.go | 13 +++++++--- tests/integration/app_check.bats | 15 ++++++----- tests/integration/app_cmd.bats | 15 ++++++----- tests/integration/app_deploy.bats | 16 ++++++------ .../app_deploy_remote_recipes.bats | 3 +-- tests/integration/app_list.bats | 12 ++++----- tests/integration/app_new.bats | 3 +-- tests/integration/app_ps.bats | 4 +-- tests/integration/app_rollback.bats | 10 ++++---- tests/integration/app_secret.bats | 8 +++--- tests/integration/app_secret_env_version.bats | 3 ++- tests/integration/app_upgrade.bats | 15 ++++------- tests/integration/helpers/recipe.bash | 18 ++++++++++++- tests/integration/recipe_version.bats | 11 +++++--- tests/integration/server_list.bats | 1 - 20 files changed, 115 insertions(+), 79 deletions(-) diff --git a/cli/app/deploy.go b/cli/app/deploy.go index e3e6e2c75..7802dd0c3 100644 --- a/cli/app/deploy.go +++ b/cli/app/deploy.go @@ -64,7 +64,7 @@ EXAMPLE: app.Recipe.Version = specificVersion } - if specificVersion == "" && app.Recipe.Version != "" { + if specificVersion == "" && app.Recipe.Version != "" && !internal.Chaos { log.Debugf("retrieved %s as version from env file", app.Recipe.Version) specificVersion = app.Recipe.Version } diff --git a/cli/app/secret.go b/cli/app/secret.go index 5d4a09c7e..29beca786 100644 --- a/cli/app/secret.go +++ b/cli/app/secret.go @@ -160,6 +160,7 @@ var appSecretInsertCommand = cli.Command{ internal.PassFlag, internal.FileFlag, internal.TrimFlag, + internal.ChaosFlag, }, Before: internal.SubCommandBefore, ArgsUsage: " ", diff --git a/cli/recipe/version.go b/cli/recipe/version.go index 0f59d7fd5..ff7d6649f 100644 --- a/cli/recipe/version.go +++ b/cli/recipe/version.go @@ -61,7 +61,6 @@ var recipeVersionCommand = cli.Command{ log.Fatalf("%s has no published versions?", recipe.Name) } - var allRows [][]string for i := len(recipeMeta.Versions) - 1; i >= 0; i-- { table, err := formatter.CreateTable() if err != nil { @@ -71,6 +70,7 @@ var recipeVersionCommand = cli.Command{ table.Headers("SERVICE", "NAME", "TAG") for version, meta := range recipeMeta.Versions[i] { + var allRows [][]string var rows [][]string for service, serviceMeta := range meta { @@ -86,6 +86,18 @@ var recipeVersionCommand = cli.Command{ fmt.Println(table) log.Infof("VERSION: %s", version) fmt.Println() + continue + } + + if internal.MachineReadable { + sort.Slice(allRows, sortServiceByName(allRows)) + headers := []string{"VERSION", "SERVICE", "NAME", "TAG"} + out, err := formatter.ToJSON(headers, allRows) + if err != nil { + log.Fatal("unable to render to JSON: %s", err) + } + fmt.Println(out) + continue } } } @@ -96,17 +108,6 @@ var recipeVersionCommand = cli.Command{ } } - if internal.MachineReadable { - sort.Slice(allRows, sortServiceByName(allRows)) - headers := []string{"VERSION", "SERVICE", "NAME", "TAG"} - out, err := formatter.ToJSON(headers, allRows) - if err != nil { - log.Fatal("unable to render to JSON: %s", err) - } - fmt.Println(out) - return nil - } - return nil }, } diff --git a/pkg/app/app.go b/pkg/app/app.go index ccd63aae4..b11047aee 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -576,6 +576,7 @@ func (a App) WriteRecipeVersion(version string) error { } defer file.Close() + skipped := false scanner := bufio.NewScanner(file) lines := []string{} for scanner.Scan() { @@ -590,6 +591,12 @@ func (a App) WriteRecipeVersion(version string) error { continue } + if strings.Contains(line, version) { + skipped = true + lines = append(lines, line) + continue + } + splitted := strings.Split(line, ":") line = fmt.Sprintf("%s:%s", splitted[0], version) lines = append(lines, line) @@ -603,7 +610,11 @@ func (a App) WriteRecipeVersion(version string) error { log.Fatal(err) } - log.Infof("version %s saved to %s.env", version, a.Domain) + if !skipped { + log.Infof("version %s saved to %s.env", version, a.Domain) + } else { + log.Debugf("skipping version %s write as already exists in %s.env", version, a.Domain) + } return nil } diff --git a/pkg/formatter/formatter.go b/pkg/formatter/formatter.go index ca9c5961a..61de2c888 100644 --- a/pkg/formatter/formatter.go +++ b/pkg/formatter/formatter.go @@ -69,7 +69,7 @@ func ToJSON(headers []string, rows [][]string) (string, error) { buff.Write([]byte("[")) - for _, row := range rows { + for idx, row := range rows { payload := make(map[string]string) for idx, header := range headers { @@ -82,6 +82,10 @@ func ToJSON(headers []string, rows [][]string) (string, error) { } buff.Write(serialized) + + if idx < (len(rows) - 1) { + buff.Write([]byte(",")) + } } buff.Write([]byte("]")) diff --git a/pkg/recipe/git.go b/pkg/recipe/git.go index 69067ad45..7a695fd31 100644 --- a/pkg/recipe/git.go +++ b/pkg/recipe/git.go @@ -26,21 +26,26 @@ func (r Recipe) Ensure(chaos bool, offline bool) error { if err := r.EnsureIsClean(); err != nil { return err } + if !offline { if err := r.EnsureUpToDate(); err != nil { log.Fatal(err) } } + if r.Version != "" { log.Debugf("ensuring version %s", r.Version) if _, err := r.EnsureVersion(r.Version); err != nil { return err } - } else { - if err := r.EnsureLatest(); err != nil { - return err - } + + return nil } + + if err := r.EnsureLatest(); err != nil { + return err + } + return nil } diff --git a/tests/integration/app_check.bats b/tests/integration/app_check.bats index d1ee7b8d3..b5be82eb6 100644 --- a/tests/integration/app_check.bats +++ b/tests/integration/app_check.bats @@ -20,6 +20,7 @@ setup(){ teardown(){ _reset_recipe + _reset_tags } @test "validate app argument" { @@ -82,19 +83,17 @@ teardown(){ } @test "ensure recipe not up to date if --offline" { - wantHash=$(_get_n_hash 1) + _ensure_env_version "0.1.0+1.20.0" + latestRelease=$(_latest_release) - run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" reset --hard HEAD~1 + run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -d "$latestRelease" assert_success - assert_equal $(_get_current_hash) "$wantHash" - - # NOTE(d1): we can't quite tell if this will fail or not in the future, so, - # since it isn't an important part of what we're testing here, we don't check - # it + # NOTE(d1): don't assert success because it might flake run $ABRA app check "$TEST_APP_DOMAIN" --offline - assert_equal $(_get_current_hash) "$wantHash" + run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -l + refute_output --partial "$latestRelease" } @test "error if missing .env.sample" { diff --git a/tests/integration/app_cmd.bats b/tests/integration/app_cmd.bats index 9df0efbc9..673063d48 100644 --- a/tests/integration/app_cmd.bats +++ b/tests/integration/app_cmd.bats @@ -19,8 +19,9 @@ setup(){ } teardown(){ - _undeploy_app _reset_recipe + _reset_tags + _undeploy_app } # bats test_tags=slow @@ -105,20 +106,18 @@ test_cmd_export" } @test "ensure recipe not up to date if --offline" { - wantHash=$(_get_n_hash 3) + _ensure_env_version "0.1.0+1.20.0" + latestRelease=$(_latest_release) - run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" reset --hard HEAD~3 + run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -d "$latestRelease" assert_success - assert_equal $(_get_current_hash) "$wantHash" - run $ABRA app cmd --local --offline "$TEST_APP_DOMAIN" test_cmd assert_success assert_output --partial 'baz' - assert_equal $(_get_current_hash) $wantHash - - _reset_recipe "$TEST_RECIPE" + run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -l + refute_output --partial "$latestRelease" } @test "error if missing arguments without passing --local" { diff --git a/tests/integration/app_deploy.bats b/tests/integration/app_deploy.bats index c9f7c1123..a76016f12 100644 --- a/tests/integration/app_deploy.bats +++ b/tests/integration/app_deploy.bats @@ -87,19 +87,18 @@ teardown(){ # bats test_tags=slow @test "ensure recipe not up to date if --offline" { - wantHash=$(_get_n_hash 3) + _ensure_env_version "0.1.0+1.20.0" + latestRelease=$(_latest_release) - run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" reset --hard HEAD~3 + run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -d "$latestRelease" assert_success - assert_equal $(_get_current_hash) "$wantHash" - - # NOTE(d1): need to use --chaos to force same commit run $ABRA app deploy "$TEST_APP_DOMAIN" \ - --no-input --no-converge-checks --chaos --offline + --no-input --no-converge-checks --offline assert_success - assert_equal $(_get_current_hash) "$wantHash" + run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -l + refute_output --partial "$latestRelease" } # bats test_tags=slow @@ -107,6 +106,7 @@ teardown(){ latestCommit="$(git -C "$ABRA_DIR/recipes/$TEST_RECIPE" rev-parse --short HEAD)" _remove_tags + _wipe_env_version # NOTE(d1): need to pass --offline to stop tags being pulled again run $ABRA app deploy "$TEST_APP_DOMAIN" \ @@ -126,6 +126,8 @@ teardown(){ assert_equal $(_get_current_hash) "$wantHash" + _wipe_env_version + run $ABRA app deploy "$TEST_APP_DOMAIN" \ --no-input --no-converge-checks --chaos assert_success diff --git a/tests/integration/app_deploy_remote_recipes.bats b/tests/integration/app_deploy_remote_recipes.bats index c45819c0e..0e006ce04 100644 --- a/tests/integration/app_deploy_remote_recipes.bats +++ b/tests/integration/app_deploy_remote_recipes.bats @@ -67,8 +67,7 @@ teardown(){ run $ABRA app deploy "$TEST_APP_DOMAIN" --no-input --no-converge-checks assert_success - latestRelease=$(git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -l | tail -n 1) - run grep -q "TYPE=git.coopcloud.tech\/coop-cloud\/abra-test-recipe:$latestRelease" \ + run grep -q "TYPE=git.coopcloud.tech\/coop-cloud\/abra-test-recipe:$(_latest_release)" \ "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env" assert_success } diff --git a/tests/integration/app_list.bats b/tests/integration/app_list.bats index 9067581cc..61c09e92a 100644 --- a/tests/integration/app_list.bats +++ b/tests/integration/app_list.bats @@ -62,8 +62,8 @@ teardown(){ run $ABRA app ls --server foo.com assert_success - refute_output --partial "server: $TEST_SERVER |" - assert_output --partial "server: foo.com |" + refute_output --partial "SERVER: $TEST_SERVER" + assert_output --partial "SERVER: foo.com" run rm -rf "$ABRA_DIR/servers/foo.com" assert_success @@ -97,8 +97,8 @@ teardown(){ @test "server stats are correct" { run $ABRA app ls assert_success - assert_output --partial "server: $TEST_SERVER" - assert_output --partial "total apps: 1" + assert_output --partial "SERVER: $TEST_SERVER" + assert_output --partial "TOTAL APPS: 1" run mkdir -p "$ABRA_DIR/servers/foo.com" assert_success @@ -113,8 +113,8 @@ teardown(){ assert_success assert_output --partial "$TEST_SERVER" assert_output --partial "foo.com" - assert_output --partial "total servers: 2" - assert_output --partial "total apps: 2" + assert_output --partial "TOTAL SERVERS: 2" + assert_output --partial "TOTAL APPS: 2" run rm -rf "$ABRA_DIR/servers/foo.com" assert_success diff --git a/tests/integration/app_new.bats b/tests/integration/app_new.bats index d88b7404a..9dc773427 100644 --- a/tests/integration/app_new.bats +++ b/tests/integration/app_new.bats @@ -40,8 +40,7 @@ teardown(){ _get_current_hash assert_equal "$headHash" "$currentHash" - latestRelease=$(git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -l | tail -n 1) - run grep -q "TYPE=$TEST_RECIPE:$latestRelease" \ + run grep -q "TYPE=$TEST_RECIPE:$(_latest_release)" \ "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env" assert_success } diff --git a/tests/integration/app_ps.bats b/tests/integration/app_ps.bats index 0e3f962ca..b0f64f51d 100644 --- a/tests/integration/app_ps.bats +++ b/tests/integration/app_ps.bats @@ -41,13 +41,11 @@ teardown(){ @test "show ps report" { _deploy_app - latestRelease=$(git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -l | tail -n 1) - run $ABRA app ps "$TEST_APP_DOMAIN" assert_success assert_output --partial 'app' assert_output --partial 'healthy' - assert_output --partial "$latestRelease" + assert_output --partial $(_latest_release) assert_output --partial 'false' # not a chaos deploy } diff --git a/tests/integration/app_rollback.bats b/tests/integration/app_rollback.bats index f1ec824da..55be3dcab 100644 --- a/tests/integration/app_rollback.bats +++ b/tests/integration/app_rollback.bats @@ -58,18 +58,18 @@ teardown(){ } @test "ensure recipe not up to date if --offline" { - wantHash=$(_get_n_hash 3) + _ensure_env_version "0.1.0+1.20.0" + latestRelease=$(_latest_release) - run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" reset --hard HEAD~3 + run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -d "$latestRelease" assert_success - assert_equal $(_get_current_hash) "$wantHash" - run $ABRA app rollback "$TEST_APP_DOMAIN" \ --no-input --no-converge-checks --offline assert_failure - assert_equal $(_get_current_hash) $wantHash + run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -l + refute_output --partial "$latestRelease" } @test "error if not already deployed" { diff --git a/tests/integration/app_secret.bats b/tests/integration/app_secret.bats index 5e2d55f3b..ffc38588f 100644 --- a/tests/integration/app_secret.bats +++ b/tests/integration/app_secret.bats @@ -217,7 +217,8 @@ teardown(){ run bash -c "echo bar >> $ABRA_DIR/recipes/$TEST_RECIPE/foo" run $ABRA app secret insert \ - --file "$TEST_APP_DOMAIN" test_pass_one v1 "$ABRA_DIR/recipes/$TEST_RECIPE/foo" + --chaos \ + --file "$TEST_APP_DOMAIN" test_pass_one v1 "$ABRA_DIR/recipes/$TEST_RECIPE/foo" assert_success assert_output --partial 'successfully stored on server' @@ -317,9 +318,10 @@ teardown(){ run $ABRA app secret generate "$TEST_APP_DOMAIN" --all assert_success - run $ABRA app secret ls "$TEST_APP_DOMAIN" --machine + run bash -c '$ABRA app secret ls "$TEST_APP_DOMAIN" --machine \ + | jq -r ".[] | select(.name==\"test_pass_two\") | .version"' assert_success - assert_output --partial '"created-on-server":"true"' + assert_output --partial 'v1' } @test "ls: bail if unstaged changes and no --chaos" { diff --git a/tests/integration/app_secret_env_version.bats b/tests/integration/app_secret_env_version.bats index de35e0401..2a137d97e 100644 --- a/tests/integration/app_secret_env_version.bats +++ b/tests/integration/app_secret_env_version.bats @@ -64,8 +64,8 @@ teardown(){ run sed -i 's/TYPE=abra-test-recipe:.*/TYPE=abra-test-recipe:0.2.0+1.21.0/g' \ "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env" - assert_success + run $ABRA app secret generate "$TEST_APP_DOMAIN" --all assert_success @@ -80,6 +80,7 @@ teardown(){ run sed -i 's/TYPE=abra-test-recipe:.*/TYPE=abra-test-recipe:0.2.0+1.21.0/g' \ "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env" + assert_success run $ABRA app secret generate "$TEST_APP_DOMAIN" --all assert_success diff --git a/tests/integration/app_upgrade.bats b/tests/integration/app_upgrade.bats index 4bbfd96c5..532243017 100644 --- a/tests/integration/app_upgrade.bats +++ b/tests/integration/app_upgrade.bats @@ -18,8 +18,9 @@ setup(){ } teardown(){ - _undeploy_app _reset_recipe + _reset_app + _undeploy_app } @test "validate app argument" { @@ -123,11 +124,9 @@ teardown(){ assert_success assert_output --partial '0.1.0+1.20.0' - latestRelease=$(git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -l | tail -n 1) - run $ABRA app upgrade "$TEST_APP_DOMAIN" --no-input --no-converge-checks assert_success - assert_output --partial "$latestRelease" + assert_output --partial "$(_latest_release)" } # bats test_tags=slow @@ -136,11 +135,9 @@ teardown(){ assert_success assert_output --partial '0.1.1+1.20.2' - latestRelease=$(git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -l | tail -n 1) - run $ABRA app upgrade "$TEST_APP_DOMAIN" --no-input --no-converge-checks assert_success - assert_output --partial "$latestRelease" + assert_output --partial "$(_latest_release)" assert_output --partial 'release notes baz' # 0.2.0+1.21.0 refute_output --partial 'release notes bar' # 0.1.1+1.20.2 } @@ -164,11 +161,9 @@ teardown(){ assert_success assert_output --partial '0.1.0+1.20.0' - latestRelease=$(git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -l | tail -n 1) - run $ABRA app upgrade "$TEST_APP_DOMAIN" --no-input --no-converge-checks assert_success - assert_output --partial "$latestRelease" + assert_output --partial "$(_latest_release)" assert_output --partial 'release notes bar' # 0.1.1+1.20.2 assert_output --partial 'release notes baz' # 0.2.0+1.21.0 } diff --git a/tests/integration/helpers/recipe.bash b/tests/integration/helpers/recipe.bash index d59b90fd6..2a9bd80d6 100644 --- a/tests/integration/helpers/recipe.bash +++ b/tests/integration/helpers/recipe.bash @@ -1,5 +1,9 @@ #!/usr/bin/env bash +_latest_release(){ + echo $(git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -l | tail -n 1) +} + _fetch_recipe() { if [[ ! -d "$ABRA_DIR/recipes/$TEST_RECIPE" ]]; then run mkdir -p "$ABRA_DIR/recipes" @@ -19,7 +23,7 @@ _reset_recipe(){ } _ensure_latest_version(){ - latestRelease=$(git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -l | tail -n 1) + latestRelease=$(_latest_release) if [ ! $latestRelease = "$1" ]; then echo "expected latest recipe version of '$1', saw: $latestRelease" @@ -33,3 +37,15 @@ _ensure_catalogue(){ assert_success fi } + +_ensure_env_version(){ + run sed -i 's/TYPE=abra-test-recipe:.*/TYPE=abra-test-recipe:$1/g' \ + "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env" + assert_success +} + +_wipe_env_version(){ + run sed -i 's/TYPE=abra-test-recipe:.*/TYPE=abra-test-recipe/g' \ + "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env" + assert_success +} diff --git a/tests/integration/recipe_version.bats b/tests/integration/recipe_version.bats index ee5840ff0..38f1ccb9a 100644 --- a/tests/integration/recipe_version.bats +++ b/tests/integration/recipe_version.bats @@ -1,10 +1,17 @@ #!/usr/bin/env bash +setup_file(){ + load "$PWD/tests/integration/helpers/common" + _common_setup + _ensure_catalogue +} + setup() { load "$PWD/tests/integration/helpers/common" _common_setup } + @test "recipe versions" { run $ABRA recipe versions gitea assert_success @@ -12,11 +19,9 @@ setup() { } @test "local tags used if no catalogue entry" { - latestRelease=$(git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -l | tail -n 1) - run $ABRA recipe versions "$TEST_RECIPE" assert_success - assert_output --partial "$latestRelease" + assert_output --partial "$(_latest_release)" } @test "versions listed in correct order" { diff --git a/tests/integration/server_list.bats b/tests/integration/server_list.bats index 3387edc62..6cb37e80c 100644 --- a/tests/integration/server_list.bats +++ b/tests/integration/server_list.bats @@ -66,7 +66,6 @@ teardown(){ fi output=$("$ABRA" server ls --machine) - run diff \ <(jq -S "." <(echo "$output")) \ <(jq -S "." <(echo '[{"host":"local","name":"default"}]'))