From 03b4e4b4e26a8cb2c88e57c6fd383b3c17da2752 Mon Sep 17 00:00:00 2001 From: decentral1se Date: Tue, 5 Sep 2023 11:12:04 +0200 Subject: [PATCH 1/7] feat: support abra testing mode --- pkg/config/env.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pkg/config/env.go b/pkg/config/env.go index e799f58b..c0358dbb 100644 --- a/pkg/config/env.go +++ b/pkg/config/env.go @@ -14,7 +14,17 @@ import ( "github.com/sirupsen/logrus" ) -var ABRA_DIR = os.ExpandEnv("$HOME/.abra") +// getBaseDir retrieves the Abra base directory. +func getBaseDir() string { + home := os.ExpandEnv("$HOME/.abra") + if _, exists := os.LookupEnv("ABRA_TEST"); exists { + home = os.ExpandEnv("$HOME/.abra_test") + logrus.Debugf("running under test mode as requested (ABRA_TEST)") + } + return home +} + +var ABRA_DIR = getBaseDir() var SERVERS_DIR = path.Join(ABRA_DIR, "servers") var RECIPES_DIR = path.Join(ABRA_DIR, "recipes") var VENDOR_DIR = path.Join(ABRA_DIR, "vendor") -- 2.47.2 From 617e8d94092991dabad1a516ba17e771b137ae99 Mon Sep 17 00:00:00 2001 From: decentral1se Date: Tue, 5 Sep 2023 11:12:21 +0200 Subject: [PATCH 2/7] test: write initial automatic integration tests --- tests/integration/dirs.bats | 39 +++++++++++++++++++++++++++++++++++ tests/integration/helpers.sh | 36 ++++++++++++++++++++++++++++++++ tests/integration/recipe.bats | 33 +++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 tests/integration/dirs.bats create mode 100644 tests/integration/helpers.sh create mode 100644 tests/integration/recipe.bats diff --git a/tests/integration/dirs.bats b/tests/integration/dirs.bats new file mode 100644 index 00000000..bc326652 --- /dev/null +++ b/tests/integration/dirs.bats @@ -0,0 +1,39 @@ +#!/usr/bin/env bash + +setup() { + DIR="$( cd "$( dirname "$BATS_TEST_FILENAME" )" >/dev/null 2>&1 && pwd )" + source "$DIR/helpers.sh" + _setup_env +} + +@test "ensure abra directories created" { + run $ABRA app ls + + # no servers yet, so will fail. however, it will run the required code which + # checks if it should create these base directories and that is what we want + assert_failure + + assert [ -d "$ABRA_TEST_DIR" ] + assert [ -d "$ABRA_TEST_DIR/servers" ] + assert [ -d "$ABRA_TEST_DIR/recipes" ] + assert [ -d "$ABRA_TEST_DIR/backups" ] + assert [ -d "$ABRA_TEST_DIR/vendor" ] + assert [ -d "$ABRA_TEST_DIR/catalogue" ] +} + +@test "ensure catalogue recipe is a git repository" { + run $ABRA app ls + + # no servers yet, so will fail. however, it will run the required code which + # checks if it should create these base directories and that is what we want + assert_failure + + assert_output --partial 'local recipe catalogue is missing' + + assert [ -d "$ABRA_TEST_DIR/catalogue" ] + assert [ -d "$ABRA_TEST_DIR/catalogue/.git" ] +} + +teardown(){ + _default_teardown +} diff --git a/tests/integration/helpers.sh b/tests/integration/helpers.sh new file mode 100644 index 00000000..19836a22 --- /dev/null +++ b/tests/integration/helpers.sh @@ -0,0 +1,36 @@ +#!/usr/bin/env bash + +_build_abra() { + if [[ ! -e "$ROOT/abra" ]]; then + cd "$ROOT" && make build-dev + return 0 + fi + return 0 +} + +_build_kadabra() { + if [[ ! -e "$ROOT/kadabra" ]]; then + cd "$ROOT" && make build-dev + return 0 + fi + return 0 +} + +_setup_env(){ + load '/usr/lib/bats/bats-support/load' + load '/usr/lib/bats/bats-assert/load' + + ROOT="$DIR/../.." + ABRA="$ROOT/abra" + KADABRA="$ROOT/kadabra" + + ABRA_TEST=true + ABRA_TEST_DIR="$HOME/.abra_test" + + _build_abra + _build_kadabra +} + +_default_teardown(){ + rm -rf "$ABRA_TEST_DIR" +} diff --git a/tests/integration/recipe.bats b/tests/integration/recipe.bats new file mode 100644 index 00000000..c8d1bcb9 --- /dev/null +++ b/tests/integration/recipe.bats @@ -0,0 +1,33 @@ +#!/usr/bin/env bash + +setup() { + DIR="$( cd "$( dirname "$BATS_TEST_FILENAME" )" >/dev/null 2>&1 && pwd )" + source "$DIR/helpers.sh" + _setup_env + + mkdir -p "$HOME/.abra_test/servers/example.com" + if ! grep -R -q "example.com" "$HOME/.docker"; then + docker context create --docker host=ssh://foo@example.com:222 example.com + fi +} + +@test "create new recipe" { + run $ABRA recipe new foobar + assert_success + assert_output --partial 'Your new foobar recipe has been created' + + run $ABRA app new foobar \ + --no-input \ + --server example.com \ + --domain foobar.example.com + assert_success + assert_output --partial 'A new foobar app has been created!' +} + +teardown() { + _default_teardown + + if grep -R -q "example.com" "$HOME/.docker"; then + docker context rm example.com + fi +} -- 2.47.2 From 5af3e9c32d09a304a4de0d3f5207f80fdd54552e Mon Sep 17 00:00:00 2001 From: decentral1se Date: Tue, 5 Sep 2023 11:12:39 +0200 Subject: [PATCH 3/7] fix: only load client if creating secrets --- cli/app/new.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cli/app/new.go b/cli/app/new.go index ac8e194c..9034b3bc 100644 --- a/cli/app/new.go +++ b/cli/app/new.go @@ -93,14 +93,14 @@ var appNewCommand = cli.Command{ logrus.Fatal(err) } - cl, err := client.New(internal.NewAppServer) - if err != nil { - logrus.Fatal(err) - } - var secrets AppSecrets var secretTable *jsontable.JSONTable if internal.Secrets { + cl, err := client.New(internal.NewAppServer) + if err != nil { + logrus.Fatal(err) + } + secrets, err := createSecrets(cl, sanitisedAppName) if err != nil { logrus.Fatal(err) -- 2.47.2 From 9a0437d116f14d7a19e31759ef72bfda9115f2af Mon Sep 17 00:00:00 2001 From: decentral1se Date: Tue, 5 Sep 2023 11:13:19 +0200 Subject: [PATCH 4/7] fix: clone catalogue on initial run --- cli/cli.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/cli/cli.go b/cli/cli.go index 51c2342c..f21ee2fe 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -16,6 +16,7 @@ import ( "coopcloud.tech/abra/cli/server" "coopcloud.tech/abra/pkg/autocomplete" "coopcloud.tech/abra/pkg/config" + "coopcloud.tech/abra/pkg/git" "coopcloud.tech/abra/pkg/web" "github.com/sirupsen/logrus" "github.com/urfave/cli" @@ -185,6 +186,14 @@ func newAbraApp(version, commit string) *cli.App { } } + if _, err := os.Stat(config.CATALOGUE_DIR); os.IsNotExist(err) { + url := fmt.Sprintf("%s/%s.git", config.REPOS_BASE_URL, config.CATALOGUE_JSON_REPO_NAME) + logrus.Warnf("local recipe catalogue is missing, retrieving now") + if err := git.Clone(config.CATALOGUE_DIR, url); err != nil { + logrus.Fatal(err) + } + } + logrus.Debugf("abra version %s, commit %s", version, commit) return nil -- 2.47.2 From fcd3c9fc4128447de82d9f0c72de463d7a185972 Mon Sep 17 00:00:00 2001 From: decentral1se Date: Tue, 5 Sep 2023 11:13:30 +0200 Subject: [PATCH 5/7] chore: don't join if nothing to join --- cli/cli.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cli/cli.go b/cli/cli.go index f21ee2fe..b38833f9 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -171,10 +171,10 @@ func newAbraApp(version, commit string) *cli.App { app.Before = func(c *cli.Context) error { paths := []string{ config.ABRA_DIR, - path.Join(config.SERVERS_DIR), - path.Join(config.RECIPES_DIR), - path.Join(config.VENDOR_DIR), - path.Join(config.BACKUP_DIR), + config.SERVERS_DIR, + config.RECIPES_DIR, + config.VENDOR_DIR, + config.BACKUP_DIR, } for _, path := range paths { -- 2.47.2 From 0ee13b427a22bf19650f2d5603cb54941d350aca Mon Sep 17 00:00:00 2001 From: decentral1se Date: Tue, 5 Sep 2023 11:46:18 +0200 Subject: [PATCH 6/7] fix: separate abra/kababra makefile targets --- Makefile | 5 ++++- tests/integration/helpers.sh | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index e9597ab0..3349a1b9 100644 --- a/Makefile +++ b/Makefile @@ -15,9 +15,12 @@ run: install: @go install -ldflags=$(LDFLAGS) $(ABRA) -build-dev: +build-abra: @go build -v -ldflags=$(LDFLAGS) $(ABRA) +build-kadabra: + @go build -v -ldflags=$(LDFLAGS) $(KADABRA) + build: @go build -v -ldflags=$(DIST_LDFLAGS) $(ABRA) @go build -v -ldflags=$(DIST_LDFLAGS) $(KADABRA) diff --git a/tests/integration/helpers.sh b/tests/integration/helpers.sh index 19836a22..e573b631 100644 --- a/tests/integration/helpers.sh +++ b/tests/integration/helpers.sh @@ -2,7 +2,7 @@ _build_abra() { if [[ ! -e "$ROOT/abra" ]]; then - cd "$ROOT" && make build-dev + cd "$ROOT" && make build-abra return 0 fi return 0 @@ -10,7 +10,7 @@ _build_abra() { _build_kadabra() { if [[ ! -e "$ROOT/kadabra" ]]; then - cd "$ROOT" && make build-dev + cd "$ROOT" && make build-kadabra return 0 fi return 0 -- 2.47.2 From 46eb5762d853429f8e0fb9a4aaef195714172d94 Mon Sep 17 00:00:00 2001 From: decentral1se Date: Tue, 5 Sep 2023 11:57:02 +0200 Subject: [PATCH 7/7] fix: overridable ABRA_DIR --- pkg/config/env.go | 5 ++--- tests/integration/dirs.bats | 32 ++++++++++++++++++++++---------- tests/integration/helpers.sh | 5 ++--- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/pkg/config/env.go b/pkg/config/env.go index c0358dbb..0874fb57 100644 --- a/pkg/config/env.go +++ b/pkg/config/env.go @@ -17,9 +17,8 @@ import ( // getBaseDir retrieves the Abra base directory. func getBaseDir() string { home := os.ExpandEnv("$HOME/.abra") - if _, exists := os.LookupEnv("ABRA_TEST"); exists { - home = os.ExpandEnv("$HOME/.abra_test") - logrus.Debugf("running under test mode as requested (ABRA_TEST)") + if customAbraDir, exists := os.LookupEnv("ABRA_DIR"); exists && customAbraDir != "" { + home = customAbraDir } return home } diff --git a/tests/integration/dirs.bats b/tests/integration/dirs.bats index bc326652..0ba86678 100644 --- a/tests/integration/dirs.bats +++ b/tests/integration/dirs.bats @@ -6,22 +6,34 @@ setup() { _setup_env } -@test "ensure abra directories created" { +@test "ABRA_DIR can be overriden" { + ABRA_DIR="$HOME/.abra_foo" + run $ABRA app ls # no servers yet, so will fail. however, it will run the required code which # checks if it should create these base directories and that is what we want assert_failure - assert [ -d "$ABRA_TEST_DIR" ] - assert [ -d "$ABRA_TEST_DIR/servers" ] - assert [ -d "$ABRA_TEST_DIR/recipes" ] - assert [ -d "$ABRA_TEST_DIR/backups" ] - assert [ -d "$ABRA_TEST_DIR/vendor" ] - assert [ -d "$ABRA_TEST_DIR/catalogue" ] + assert [ -d "$HOME/.abra_foo" ] } -@test "ensure catalogue recipe is a git repository" { +@test "abra directories created" { + run $ABRA app ls + + # no servers yet, so will fail. however, it will run the required code which + # checks if it should create these base directories and that is what we want + assert_failure + + assert [ -d "$ABRA_DIR" ] + assert [ -d "$ABRA_DIR/servers" ] + assert [ -d "$ABRA_DIR/recipes" ] + assert [ -d "$ABRA_DIR/backups" ] + assert [ -d "$ABRA_DIR/vendor" ] + assert [ -d "$ABRA_DIR/catalogue" ] +} + +@test "catalogue recipe is a git repository" { run $ABRA app ls # no servers yet, so will fail. however, it will run the required code which @@ -30,8 +42,8 @@ setup() { assert_output --partial 'local recipe catalogue is missing' - assert [ -d "$ABRA_TEST_DIR/catalogue" ] - assert [ -d "$ABRA_TEST_DIR/catalogue/.git" ] + assert [ -d "$ABRA_DIR/catalogue" ] + assert [ -d "$ABRA_DIR/catalogue/.git" ] } teardown(){ diff --git a/tests/integration/helpers.sh b/tests/integration/helpers.sh index e573b631..f3aa6ead 100644 --- a/tests/integration/helpers.sh +++ b/tests/integration/helpers.sh @@ -24,13 +24,12 @@ _setup_env(){ ABRA="$ROOT/abra" KADABRA="$ROOT/kadabra" - ABRA_TEST=true - ABRA_TEST_DIR="$HOME/.abra_test" + ABRA_DIR="$HOME/.abra_test" _build_abra _build_kadabra } _default_teardown(){ - rm -rf "$ABRA_TEST_DIR" + rm -rf "$ABRA_DIR" } -- 2.47.2