diff --git a/cli/recipe/lint.go b/cli/recipe/lint.go index dfeba1a2..a01d3ecc 100644 --- a/cli/recipe/lint.go +++ b/cli/recipe/lint.go @@ -7,6 +7,7 @@ import ( "coopcloud.tech/abra/pkg/autocomplete" "coopcloud.tech/abra/pkg/formatter" "coopcloud.tech/abra/pkg/lint" + recipePkg "coopcloud.tech/abra/pkg/recipe" "github.com/sirupsen/logrus" "github.com/urfave/cli" ) @@ -21,12 +22,33 @@ var recipeLintCommand = cli.Command{ internal.OnlyErrorFlag, internal.OfflineFlag, internal.NoInputFlag, + internal.ChaosFlag, }, Before: internal.SubCommandBefore, BashComplete: autocomplete.RecipeNameComplete, Action: func(c *cli.Context) error { recipe := internal.ValidateRecipe(c) + if err := recipePkg.EnsureExists(recipe.Name); err != nil { + logrus.Fatal(err) + } + + if !internal.Chaos { + if err := recipePkg.EnsureIsClean(recipe.Name); err != nil { + logrus.Fatal(err) + } + + if !internal.Offline { + if err := recipePkg.EnsureUpToDate(recipe.Name); err != nil { + logrus.Fatal(err) + } + } + + if err := recipePkg.EnsureLatest(recipe.Name); err != nil { + logrus.Fatal(err) + } + } + tableCol := []string{"ref", "rule", "severity", "satisfied", "skipped", "resolve"} table := formatter.CreateTable(tableCol) diff --git a/tests/integration/recipe_lint.bats b/tests/integration/recipe_lint.bats index 63a3e824..094495d1 100644 --- a/tests/integration/recipe_lint.bats +++ b/tests/integration/recipe_lint.bats @@ -11,25 +11,51 @@ setup() { assert_output --partial 'compose config has expected version' } -@test "recipe lint warns on error" { +@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 lint "$TEST_RECIPE" assert_success - refute_output --partial 'watch out, some critical errors are present' - run sed -i '/traefik.enable=.*/d' "$ABRA_DIR/recipes/$TEST_RECIPE/compose.yml" - assert_success - - run $ABRA recipe lint "$TEST_RECIPE" - assert_success --partial 'watch out, some critical errors are present' - - _checkout_recipe "$TEST_RECIPE" + assert_exists "$ABRA_DIR/recipes/$TEST_RECIPE" } -@test "recipe lint uses latest commit" { - _fetch_recipe "$TEST_RECIPE" +@test "bail if unstaged changes and no --chaos" { + run bash -c "echo foo >> $ABRA_DIR/recipes/$TEST_RECIPE/foo" + assert_success + assert_exists "$ABRA_DIR/recipes/$TEST_RECIPE/foo" - latestCommit="$(git -C "$ABRA_DIR/recipes/$TEST_RECIPE" rev-parse --short HEAD)" + run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" status + assert_success + assert_output --partial 'foo' + run $ABRA recipe lint "$TEST_RECIPE" + 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 "do not bail if unstaged changes and --chaos" { + 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 lint "$TEST_RECIPE" --chaos + assert_success + + run rm -rf "$ABRA_DIR/recipes/$TEST_RECIPE/foo" + assert_not_exists "$ABRA_DIR/recipes/$TEST_RECIPE/foo" +} + +@test "ensure recipe up to date if no --offline" { run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" reset --hard HEAD~3 assert_success @@ -39,6 +65,23 @@ setup() { run $ABRA recipe lint "$TEST_RECIPE" assert_success + run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" status + refute_output --partial 'behind 3' +} + +@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 + + run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" status + assert_output --partial 'behind 3' + + run $ABRA recipe lint "$TEST_RECIPE" --offline + assert_success + run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" status assert_output --partial 'behind 3' @@ -48,3 +91,17 @@ setup() { run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" status refute_output --partial 'behind 3' } + +@test "recipe lint warns on error" { + run $ABRA recipe lint "$TEST_RECIPE" + assert_success + refute_output --partial 'watch out, some critical errors are present' + + run sed -i '/traefik.enable=.*/d' "$ABRA_DIR/recipes/$TEST_RECIPE/compose.yml" + assert_success + + run $ABRA recipe lint "$TEST_RECIPE" --chaos + assert_success --partial 'watch out, some critical errors are present' + + _checkout_recipe "$TEST_RECIPE" +}