From 20ed6a8082b9e5dd988f3af98a0c13366d64c57d Mon Sep 17 00:00:00 2001 From: decentral1se Date: Wed, 8 Jan 2025 14:05:15 +0100 Subject: [PATCH 1/3] fix: app new from chaos changes See https://git.coopcloud.tech/toolshed/abra/issues/471 --- cli/app/new.go | 69 +++++++++++++++++++++++----------- tests/integration/app_new.bats | 51 +++++++++++++++++++++++++ 2 files changed, 98 insertions(+), 22 deletions(-) diff --git a/cli/app/new.go b/cli/app/new.go index 773187c1..daa818a7 100644 --- a/cli/app/new.go +++ b/cli/app/new.go @@ -75,9 +75,34 @@ var AppNewCommand = &cobra.Command{ chaosVersion := config.CHAOS_DEFAULT if internal.Chaos { - recipeVersion = chaosVersion + var err error + chaosVersion, err = recipe.ChaosVersion() + if err != nil { + log.Fatal(err) + } - if !internal.Offline { + // NOTE(d1): rely on tags as there is no recipe.EnvVersion yet because + // the app has not been fully created. we rely on the local git state of + // the repository + tags, err := recipe.Tags() + if err != nil { + log.Fatal(err) + } + + internal.SortVersionsDesc(tags) + + if len(tags) == 0 { + // NOTE(d1): this is a new recipe with no released versions + recipeVersion = config.UNKNOWN_DEFAULT + } else { + recipeVersion = tags[len(tags)-1] + } + + if err := recipe.IsDirty(); err != nil { + log.Fatal(err) + } + + if !internal.Offline && !recipe.Dirty { if err := recipe.EnsureUpToDate(); err != nil { log.Fatal(err) } @@ -88,29 +113,29 @@ var AppNewCommand = &cobra.Command{ if err := recipe.EnsureIsClean(); err != nil { log.Fatal(err) } - } - var recipeVersions recipePkg.RecipeVersions - if recipeVersion == "" { - var err error - recipeVersions, _, err = recipe.GetRecipeVersions() - if err != nil { - log.Fatal(err) - } - } - - if len(recipeVersions) > 0 { - latest := recipeVersions[len(recipeVersions)-1] - for tag := range latest { - recipeVersion = tag + var recipeVersions recipePkg.RecipeVersions + if recipeVersion == "" { + var err error + recipeVersions, _, err = recipe.GetRecipeVersions() + if err != nil { + log.Fatal(err) + } } - if _, err := recipe.EnsureVersion(recipeVersion); err != nil { - log.Fatal(err) - } - } else { - if err := recipe.EnsureLatest(); err != nil { - log.Fatal(err) + if len(recipeVersions) > 0 { + latest := recipeVersions[len(recipeVersions)-1] + for tag := range latest { + recipeVersion = tag + } + + if _, err := recipe.EnsureVersion(recipeVersion); err != nil { + log.Fatal(err) + } + } else { + if err := recipe.EnsureLatest(); err != nil { + log.Fatal(err) + } } } diff --git a/tests/integration/app_new.bats b/tests/integration/app_new.bats index 503cf4da..f55f2052 100644 --- a/tests/integration/app_new.bats +++ b/tests/integration/app_new.bats @@ -20,6 +20,10 @@ setup(){ teardown(){ _rm_app _reset_recipe + _reset_tags + + run rm -rf "$ABRA_DIR/recipes/$TEST_RECIPE/foo" + assert_not_exists "$ABRA_DIR/recipes/$TEST_RECIPE/foo" } @test "create new app" { @@ -185,3 +189,50 @@ teardown(){ assert_success assert_output --partial 'test_pass_one' } + +# bats test_tags=slow +@test "app new from chaos recipe" { + currentHash=$(_get_current_hash) + latestRelease=$(_latest_release) + + run bash -c "echo foo >> $ABRA_DIR/recipes/$TEST_RECIPE/foo" + assert_success + assert_exists "$ABRA_DIR/recipes/$TEST_RECIPE/foo" + + run $ABRA app new "$TEST_RECIPE" \ + --no-input \ + --server "$TEST_SERVER" \ + --domain "$TEST_APP_DOMAIN" \ + --secrets \ + --chaos + assert_success + assert_exists "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env" + assert_output --partial "version: $latestRelease" + assert_output --partial "chaos: ${currentHash:0:8}" + + assert_exists "$ABRA_DIR/recipes/$TEST_RECIPE/foo" + assert_equal "$(_git_status)" "?? foo" +} + +# bats test_tags=slow +@test "app new, no releases, from chaos recipe" { + _remove_tags + + run bash -c "echo foo >> $ABRA_DIR/recipes/$TEST_RECIPE/foo" + assert_success + assert_exists "$ABRA_DIR/recipes/$TEST_RECIPE/foo" + + run $ABRA app new "$TEST_RECIPE" \ + --no-input \ + --server "$TEST_SERVER" \ + --domain "$TEST_APP_DOMAIN" \ + --secrets \ + --chaos + assert_success + assert_exists "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env" + assert_output --partial "version: unknown" + assert_output --partial "chaos: ${currentHash:0:8}" + + assert_exists "$ABRA_DIR/recipes/$TEST_RECIPE/foo" + assert_equal "$(_git_status)" "?? foo" +} -- 2.49.0 From 485a03dc7efe32ffa37169d461a566f605c39eb0 Mon Sep 17 00:00:00 2001 From: decentral1se Date: Wed, 8 Jan 2025 14:18:50 +0100 Subject: [PATCH 2/3] test: ensure unstaged changes preserved --- tests/integration/app_new.bats | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/integration/app_new.bats b/tests/integration/app_new.bats index f55f2052..da7e5b10 100644 --- a/tests/integration/app_new.bats +++ b/tests/integration/app_new.bats @@ -98,9 +98,6 @@ teardown(){ assert_success assert_output --partial 'foo' - assert_exists "$ABRA_DIR/recipes/$TEST_RECIPE/foo" - assert_equal "$(_git_status)" "?? foo" - run $ABRA app new "$TEST_RECIPE" \ --no-input \ --server "$TEST_SERVER" \ @@ -108,6 +105,9 @@ teardown(){ assert_failure assert_output --partial 'locally unstaged changes' + assert_exists "$ABRA_DIR/recipes/$TEST_RECIPE/foo" + assert_equal "$(_git_status)" "?? foo" + run rm -rf "$ABRA_DIR/recipes/$TEST_RECIPE/foo" assert_not_exists "$ABRA_DIR/recipes/$TEST_RECIPE/foo" } @@ -121,9 +121,6 @@ teardown(){ assert_success assert_output --partial 'foo' - assert_exists "$ABRA_DIR/recipes/$TEST_RECIPE/foo" - assert_equal "$(_git_status)" "?? foo" - run $ABRA app new "$TEST_RECIPE" \ --no-input \ --chaos \ @@ -132,6 +129,9 @@ teardown(){ assert_success assert_exists "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env" + assert_exists "$ABRA_DIR/recipes/$TEST_RECIPE/foo" + assert_equal "$(_git_status)" "?? foo" + run rm -rf "$ABRA_DIR/recipes/$TEST_RECIPE/foo" assert_not_exists "$ABRA_DIR/recipes/$TEST_RECIPE/foo" } -- 2.49.0 From 699791c653c2f450cd6eb22a6745857dca3af201 Mon Sep 17 00:00:00 2001 From: decentral1se Date: Wed, 8 Jan 2025 14:19:01 +0100 Subject: [PATCH 3/3] test: ensure .env version written --- cli/app/new.go | 11 ++++++++++- tests/integration/app_new.bats | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/cli/app/new.go b/cli/app/new.go index daa818a7..ecd231c8 100644 --- a/cli/app/new.go +++ b/cli/app/new.go @@ -236,7 +236,16 @@ var AppNewCommand = &cobra.Command{ log.Fatal(err) } - if err := app.WriteRecipeVersion(recipeVersion, false); err != nil { + if err := app.Recipe.IsDirty(); err != nil { + log.Fatal(err) + } + + toWriteVersion := recipeVersion + if internal.Chaos || app.Recipe.Dirty { + toWriteVersion = chaosVersion + } + + if err := app.WriteRecipeVersion(toWriteVersion, false); err != nil { log.Fatalf("writing recipe version failed: %s", err) } }, diff --git a/tests/integration/app_new.bats b/tests/integration/app_new.bats index da7e5b10..6899270e 100644 --- a/tests/integration/app_new.bats +++ b/tests/integration/app_new.bats @@ -177,6 +177,8 @@ teardown(){ # bats test_tags=slow @test "generate secrets" { + latestRelease=$(_latest_release) + run $ABRA app new "$TEST_RECIPE" \ --no-input \ --server "$TEST_SERVER" \ @@ -188,6 +190,10 @@ teardown(){ run $ABRA app secret ls "$TEST_APP_DOMAIN" assert_success assert_output --partial 'test_pass_one' + + run grep -q "TYPE=$TEST_RECIPE:${latestRelease}" \ + "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env" + assert_success } # bats test_tags=slow @@ -212,10 +218,15 @@ teardown(){ assert_exists "$ABRA_DIR/recipes/$TEST_RECIPE/foo" assert_equal "$(_git_status)" "?? foo" + + run grep -q "TYPE=$TEST_RECIPE:${currentHash:0:8}+U" \ + "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env" + assert_success } # bats test_tags=slow @test "app new, no releases, from chaos recipe" { + currentHash=$(_get_current_hash) _remove_tags run bash -c "echo foo >> $ABRA_DIR/recipes/$TEST_RECIPE/foo" @@ -235,4 +246,8 @@ teardown(){ assert_exists "$ABRA_DIR/recipes/$TEST_RECIPE/foo" assert_equal "$(_git_status)" "?? foo" + + run grep -q "TYPE=$TEST_RECIPE:${currentHash:0:8}+U" \ + "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env" + assert_success } -- 2.49.0