From 76035e003e3989c83f90da856d68fa8eb99b1d03 Mon Sep 17 00:00:00 2001 From: decentral1se Date: Thu, 21 Sep 2023 10:36:53 +0200 Subject: [PATCH] fix: recipe workflow with integration tests --- cli/recipe/release.go | 8 -- cli/recipe/sync.go | 3 + cli/recipe/upgrade.go | 9 +- tests/integration/recipe_release.bats | 114 ++++++++++++++++++++++---- tests/integration/recipe_sync.bats | 100 +++++++++++++++++++--- tests/integration/recipe_upgrade.bats | 101 ++++++++++++++++++++++- 6 files changed, 295 insertions(+), 40 deletions(-) diff --git a/cli/recipe/release.go b/cli/recipe/release.go index 7d25d93c..3ce13ac6 100644 --- a/cli/recipe/release.go +++ b/cli/recipe/release.go @@ -61,14 +61,6 @@ your SSH keys configured on your account. Action: func(c *cli.Context) error { recipe := internal.ValidateRecipe(c) - if err := recipePkg.EnsureUpToDate(recipe.Name); err != nil { - logrus.Fatal(err) - } - - if err := recipePkg.EnsureLatest(recipe.Name); err != nil { - logrus.Fatal(err) - } - imagesTmp, err := getImageVersions(recipe) if err != nil { logrus.Fatal(err) diff --git a/cli/recipe/sync.go b/cli/recipe/sync.go index 0bd52fc9..1085a193 100644 --- a/cli/recipe/sync.go +++ b/cli/recipe/sync.go @@ -64,6 +64,9 @@ local file system. nextTag := c.Args().Get(1) if len(tags) == 0 && nextTag == "" { logrus.Warnf("no git tags found for %s", recipe.Name) + if internal.NoInput { + logrus.Fatalf("unable to continue, input required for initial version") + } fmt.Println(fmt.Sprintf(` The following options are two types of initial semantic version that you can pick for %s that will be published in the recipe catalogue. This follows the diff --git a/cli/recipe/upgrade.go b/cli/recipe/upgrade.go index fe7bbf83..aa258f16 100644 --- a/cli/recipe/upgrade.go +++ b/cli/recipe/upgrade.go @@ -66,13 +66,20 @@ You may invoke this command in "wizard" mode and be prompted for input: internal.MajorFlag, internal.MachineReadableFlag, internal.AllTagsFlag, - internal.OfflineFlag, }, Before: internal.SubCommandBefore, BashComplete: autocomplete.RecipeNameComplete, Action: func(c *cli.Context) error { recipe := internal.ValidateRecipe(c) + if err := recipePkg.EnsureIsClean(recipe.Name); err != nil { + logrus.Fatal(err) + } + + if err := recipePkg.EnsureExists(recipe.Name); err != nil { + logrus.Fatal(err) + } + if err := recipePkg.EnsureUpToDate(recipe.Name); err != nil { logrus.Fatal(err) } diff --git a/tests/integration/recipe_release.bats b/tests/integration/recipe_release.bats index 451b7182..67c45bc9 100644 --- a/tests/integration/recipe_release.bats +++ b/tests/integration/recipe_release.bats @@ -1,25 +1,107 @@ #!/usr/bin/env bash -setup() { +setup_file(){ + load "$PWD/tests/integration/helpers/common" + _common_setup + _add_server + _new_app +} + +teardown_file(){ + _rm_server +} + +setup(){ load "$PWD/tests/integration/helpers/common" _common_setup } -# bats test_tags=slow -@test "pull in latest changes" { - run $ABRA recipe fetch matrix-synapse - assert_success - - run git -C "$ABRA_DIR/recipes/matrix-synapse" reset --hard HEAD~3 - assert_success - - run git -C "$ABRA_DIR/recipes/matrix-synapse" status - assert_success - assert_output --partial 'behind 3' - - run $ABRA recipe release matrix-synapse --no-input +@test "validate recipe argument" { + run $ABRA recipe release --no-input assert_failure + assert_output --partial 'no recipe name provided' - run git -C "$ABRA_DIR/recipes/matrix-synapse" status - refute_output --partial 'behind 3' + run $ABRA recipe release DOESNTEXIST --no-input + assert_failure + assert_output --partial 'unable to validate recipe' +} + +@test "release patch bump" { + run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag --list + assert_success + assert_output '0.1.0+1.20.0' + + run bash -c 'cat "$ABRA_DIR/recipes/$TEST_RECIPE/compose.yml" | grep -q "0.1.0+1.20.0"' + assert_success + + run $ABRA recipe upgrade "$TEST_RECIPE" --no-input --patch + assert_success + + run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" diff + assert_success + assert_output --partial 'image: nginx:1.20.2' + + run $ABRA recipe sync "$TEST_RECIPE" --no-input --patch + assert_success + assert_output --partial 'synced label' + + run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" diff + assert_success + assert_output --partial 'coop-cloud.${STACK_NAME}.version=0.1.1+1.20.2' + + run $ABRA recipe release "$TEST_RECIPE" --no-input --patch + assert_success + assert_output --partial 'no -p/--publish passed, not publishing' + + run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag --list + assert_success + assert_output --partial '0.1.0+1.20.0' + assert_output --partial '0.1.1+1.20.2' + + _checkout_recipe "$TEST_RECIPE" + + run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" reset --hard HEAD~1 + assert_success + + run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -d '0.1.1+1.20.2' + assert_success + assert_output --partial "Deleted tag '0.1.1+1.20.2'" +} + +@test "release minor bump" { + run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag --list + assert_success + assert_output '0.1.0+1.20.0' + + run bash -c 'grep -q "0.1.0+1.20.0" "$ABRA_DIR/recipes/$TEST_RECIPE/compose.yml"' + assert_success + + run $ABRA recipe upgrade "$TEST_RECIPE" --no-input --minor + assert_success + + run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" diff + assert_success + assert_output --regexp 'image: nginx:1.2.*' + + run $ABRA recipe sync "$TEST_RECIPE" --no-input --minor + assert_success + assert_output --partial 'synced label' + + run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" diff + assert_success + assert_output --regexp 'coop-cloud\.\$\{STACK_NAME\}\.version=0\.2\.0\+1\.2.*' + + run $ABRA recipe release "$TEST_RECIPE" --no-input --minor + assert_success + assert_output --partial 'no -p/--publish passed, not publishing' + + run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag --list + assert_success + 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" } diff --git a/tests/integration/recipe_sync.bats b/tests/integration/recipe_sync.bats index 241a2fbe..a4d338ee 100644 --- a/tests/integration/recipe_sync.bats +++ b/tests/integration/recipe_sync.bats @@ -1,39 +1,113 @@ #!/usr/bin/env bash -setup() { +setup_file(){ + load "$PWD/tests/integration/helpers/common" + _common_setup + _add_server + _new_app +} + +teardown_file(){ + _rm_server +} + +setup(){ load "$PWD/tests/integration/helpers/common" _common_setup } +@test "validate recipe argument" { + run $ABRA recipe sync --no-input + assert_failure + assert_output --partial 'no recipe name provided' + + run $ABRA recipe sync DOESNTEXIST --no-input + assert_failure + assert_output --partial 'unable to validate recipe' +} + @test "allow unstaged changes" { - run $ABRA recipe fetch matrix-synapse + run echo "unstaged changes" >> "$ABRA_DIR/recipes/$TEST_RECIPE/foo" assert_success - run echo "unstaged changes" >> "$ABRA_DIR/recipes/matrix-synapse/foo" - assert_success - - run git -C "$ABRA_DIR/recipes/matrix-synapse" status + run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" status assert_success assert_output --partial 'foo' - run $ABRA recipe sync matrix-synapse --patch + run $ABRA recipe sync "$TEST_RECIPE" --no-input --patch assert_success - run rm -rf "$ABRA_DIR/recipes/matrix-synapse/foo" + run rm -rf "$ABRA_DIR/recipes/$TEST_RECIPE/foo" assert_success - assert_not_exists "$ABRA_DIR/recipes/matrix-synapse/foo" + assert_not_exists "$ABRA_DIR/recipes/$TEST_RECIPE/foo" + + _checkout_recipe "$TEST_RECIPE" } @test "detect unstaged label changes" { - run $ABRA recipe fetch matrix-synapse + run $ABRA recipe fetch "$TEST_RECIPE" assert_success - run $ABRA recipe sync matrix-synapse --patch + run $ABRA recipe sync "$TEST_RECIPE" --patch assert_success - run $ABRA recipe sync matrix-synapse --patch + run $ABRA recipe sync "$TEST_RECIPE" --patch assert_success assert_output --partial 'is already set, nothing to do?' + + _checkout_recipe "$TEST_RECIPE" } -# TODO(d1): implement +@test "sync patch label bump" { + run bash -c 'cat "$ABRA_DIR/recipes/$TEST_RECIPE/compose.yml" | grep -q "0.1.0+1.20.0"' + assert_success + + run $ABRA recipe upgrade "$TEST_RECIPE" --no-input --patch + assert_success + + run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" diff + assert_success + assert_output --partial 'image: nginx:1.20.2' + + run $ABRA recipe sync "$TEST_RECIPE" --no-input --patch + assert_success + + run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" diff + assert_success + assert_output --partial 'coop-cloud.${STACK_NAME}.version=0.1.1+1.20.2' + + _checkout_recipe "$TEST_RECIPE" +} + +@test "sync minor label bump" { + run $ABRA recipe upgrade "$TEST_RECIPE" --no-input --minor + assert_success + + run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" diff + assert_success + assert_output --regexp 'image: nginx:1.2.*' + + run $ABRA recipe sync "$TEST_RECIPE" --no-input --minor + assert_success + + run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" diff + assert_success + assert_output --regexp 'coop-cloud\.\$\{STACK_NAME\}\.version=0\.2\.0\+1\.2.*' + + _checkout_recipe "$TEST_RECIPE" +} + +@test "error if --no-input and no initial version" { + run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -d '0.1.0+1.20.0' + assert_success + assert_output --partial "Deleted tag '0.1.0+1.20.0'" + + run $ABRA recipe sync "$TEST_RECIPE" --no-input --patch + assert_failure + assert_output --partial 'unable to continue' + assert_output --partial 'initial version' + + run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" fetch -a + assert_success + assert_output --partial '[new tag]' +} diff --git a/tests/integration/recipe_upgrade.bats b/tests/integration/recipe_upgrade.bats index 627b24e1..6e5bbcf0 100644 --- a/tests/integration/recipe_upgrade.bats +++ b/tests/integration/recipe_upgrade.bats @@ -1,8 +1,105 @@ #!/usr/bin/env bash -setup() { +setup_file(){ + load "$PWD/tests/integration/helpers/common" + _common_setup + _add_server + _new_app +} + +teardown_file(){ + _rm_server +} + +setup(){ load "$PWD/tests/integration/helpers/common" _common_setup } -# TODO(d1): implement +@test "validate recipe argument" { + run $ABRA recipe upgrade --no-input + assert_failure + assert_output --partial 'no recipe name provided' + + run $ABRA recipe upgrade DOESNTEXIST --no-input + assert_failure + assert_output --partial 'unable to validate recipe' +} + +@test "retrieve recipe if missing" { + run rm -rf "$ABRA_DIR/recipes/$TEST_RECIPE" + assert_success + assert_not_exists "$ABRA_DIR/recipes/$TEST_RECIPE" + + run $ABRA recipe upgrade "$TEST_RECIPE" --no-input + assert_success + assert_output --partial 'can upgrade service: app' + + assert_exists "$ABRA_DIR/recipes/$TEST_RECIPE" +} + +@test "error if unstaged changes" { + run bash -c "echo foo >> $ABRA_DIR/recipes/$TEST_RECIPE/foo" + assert_success + assert_exists "$ABRA_DIR/recipes/$TEST_RECIPE/foo" + + run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" status + assert_success + assert_output --partial 'foo' + + run $ABRA recipe upgrade "$TEST_RECIPE" --no-input + assert_failure + assert_output --partial 'locally unstaged changes' + + run rm -rf "$ABRA_DIR/recipes/$TEST_RECIPE/foo" + assert_not_exists "$ABRA_DIR/recipes/$TEST_RECIPE/foo" +} + +@test "ensure up to date" { + run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" reset --hard HEAD~3 + assert_success + + run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" status + assert_output --partial 'behind 3' + + run $ABRA recipe upgrade "$TEST_RECIPE" --no-input + assert_success + assert_output --partial 'can upgrade service: app' + + run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" status + refute_output --partial 'behind 3' +} + +@test "only one of -x/y/z flags" { + run $ABRA recipe upgrade "$TEST_RECIPE" --no-input -x -y + assert_failure + assert_output --partial 'you can only use one of' +} + +@test "upgrade patch" { + run bash -c 'cat "$ABRA_DIR/recipes/$TEST_RECIPE/compose.yml" | grep -q "0.1.0+1.20.0"' + assert_success + + run $ABRA recipe upgrade "$TEST_RECIPE" --no-input --patch + assert_success + + run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" diff + assert_success + assert_output --partial 'image: nginx:1.20.2' + + _checkout_recipe "$TEST_RECIPE" +} + +@test "upgrade minor" { + run bash -c 'cat "$ABRA_DIR/recipes/$TEST_RECIPE/compose.yml" | grep -q "0.1.0+1.20.0"' + assert_success + + run $ABRA recipe upgrade "$TEST_RECIPE" --no-input --minor + assert_success + + run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" diff + assert_success + assert_output --regexp 'image: nginx:1.2.*' + + _checkout_recipe "$TEST_RECIPE" +}