From d88b478503f969c2b6b72992bf2327d7fad6dc22 Mon Sep 17 00:00:00 2001 From: decentral1se Date: Sun, 29 Mar 2026 19:57:19 +0200 Subject: [PATCH] test: unit tests clean up themselves See https://git.coopcloud.tech/toolshed/abra/issues/792 --- .drone.yml | 5 +- Makefile | 2 +- pkg/app/app_test.go | 85 ++++++++++++---- pkg/app/compose_test.go | 7 +- pkg/config/abra_test.go | 15 +-- pkg/envfile/envfile_test.go | 96 +++++++++++++------ pkg/git/clone_test.go | 38 +++++++- pkg/recipe/git_test.go | 10 +- pkg/recipe/recipe_test.go | 23 ++--- pkg/test/test.go | 94 ++++++++++-------- tests/resources/test_recipe/.env.sample | 9 +- tests/resources/test_recipe/compose.yml | 1 + .../test_server/test_app.example.com.env | 9 +- 13 files changed, 271 insertions(+), 123 deletions(-) diff --git a/.drone.yml b/.drone.yml index d6f16f14..b8d42c16 100644 --- a/.drone.yml +++ b/.drone.yml @@ -40,11 +40,8 @@ steps: - name: make test image: golang:1.24 environment: - ABRA_DIR: $HOME/.abra - CATL_URL: https://git.coopcloud.tech/toolshed/recipes-catalogue-json.git + ABRA_DIR: /root/.abra_test commands: - - mkdir -p $HOME/.abra - - git clone $CATL_URL $HOME/.abra/catalogue - make test depends_on: - make check diff --git a/Makefile b/Makefile index bf1a2d00..4476e1ac 100644 --- a/Makefile +++ b/Makefile @@ -40,7 +40,7 @@ check: (echo "gofmt: formatting issue - run 'make format' to resolve" && exit 1) test: - @go test ./... -cover -v + @go test ./... -cover -v -p 1 find-tests: @find . -name "*_test.go" diff --git a/pkg/app/app_test.go b/pkg/app/app_test.go index a9de601c..bf3c00c2 100644 --- a/pkg/app/app_test.go +++ b/pkg/app/app_test.go @@ -10,46 +10,83 @@ import ( "coopcloud.tech/abra/pkg/config" "coopcloud.tech/abra/pkg/envfile" "coopcloud.tech/abra/pkg/recipe" - testPkg "coopcloud.tech/abra/pkg/test" + recipePkg "coopcloud.tech/abra/pkg/recipe" + "coopcloud.tech/abra/pkg/test" "github.com/docker/docker/api/types/filters" "github.com/google/go-cmp/cmp" "github.com/stretchr/testify/assert" ) +var ( + expectedAppEnv = envfile.AppEnv{ + "DOMAIN": test.AppName, + "RECIPE": test.RecipeName, + } + + expectedApp = appPkg.App{ + Name: test.AppName, + Recipe: recipePkg.Get(expectedAppEnv["RECIPE"]), + Domain: expectedAppEnv["DOMAIN"], + Env: expectedAppEnv, + Path: expectedAppFile.Path, + Server: expectedAppFile.Server, + } + + expectedAppFile = appPkg.AppFile{ + Path: test.AppEnvPath, + Server: test.ServerName, + } + + expectedAppFiles = map[string]appPkg.AppFile{ + test.AppName: expectedAppFile, + } +) + func TestNewApp(t *testing.T) { - app, err := appPkg.NewApp(testPkg.ExpectedAppEnv, testPkg.AppName, testPkg.ExpectedAppFile) + app, err := appPkg.NewApp(expectedAppEnv, test.AppName, expectedAppFile) if err != nil { t.Fatal(err) } - if !reflect.DeepEqual(app, testPkg.ExpectedApp) { - t.Fatalf("did not get expected app type. Expected: %s; Got: %s", app, testPkg.ExpectedApp) + + if !reflect.DeepEqual(app, expectedApp) { + t.Fatalf("did not get expected app type. Expected: %s; Got: %s", app, expectedApp) } } func TestReadAppEnvFile(t *testing.T) { - app, err := appPkg.ReadAppEnvFile(testPkg.ExpectedAppFile, testPkg.AppName) + test.Setup() + t.Cleanup(func() { test.Teardown() }) + + app, err := appPkg.ReadAppEnvFile(expectedAppFile, test.AppName) if err != nil { t.Fatal(err) } - if !reflect.DeepEqual(app, testPkg.ExpectedApp) { - t.Fatalf("did not get expected app type. Expected: %s; Got: %s", app, testPkg.ExpectedApp) + + if !reflect.DeepEqual(app, expectedApp) { + t.Fatalf("did not get expected app type. Expected: %s; Got: %s", app, expectedApp) } } func TestGetApp(t *testing.T) { - app, err := appPkg.GetApp(testPkg.ExpectedAppFiles, testPkg.AppName) + test.Setup() + t.Cleanup(func() { test.Teardown() }) + + app, err := appPkg.GetApp(expectedAppFiles, test.AppName) if err != nil { t.Fatal(err) } - if !reflect.DeepEqual(app, testPkg.ExpectedApp) { - t.Fatalf("did not get expected app type. Expected: %s; Got: %s", app, testPkg.ExpectedApp) + + if !reflect.DeepEqual(app, expectedApp) { + t.Fatalf("did not get expected app type. Expected: %s; Got: %s", app, expectedApp) } } func TestGetComposeFiles(t *testing.T) { - r := recipe.Get("abra-test-recipe") - err := r.EnsureExists() - if err != nil { + test.Setup() + t.Cleanup(func() { test.Teardown() }) + + r := recipe.Get(test.AbraTestRecipe) + if err := r.EnsureExists(); err != nil { t.Fatal(err) } @@ -94,7 +131,10 @@ func TestGetComposeFiles(t *testing.T) { } func TestGetComposeFilesError(t *testing.T) { - r := recipe.Get("abra-test-recipe") + test.Setup() + t.Cleanup(func() { test.Teardown() }) + + r := recipe.Get(test.AbraTestRecipe) err := r.EnsureExists() if err != nil { t.Fatal(err) @@ -186,26 +226,32 @@ func TestFilters(t *testing.T) { func compareFilter(t *testing.T, f1 filters.Args, f2 map[string]map[string]bool) { t.Helper() + j1, err := f1.MarshalJSON() if err != nil { t.Error(err) } + j2, err := json.Marshal(f2) if err != nil { t.Error(err) } + if diff := cmp.Diff(string(j2), string(j1)); diff != "" { t.Errorf("filters mismatch (-want +got):\n%s", diff) } } func TestWriteRecipeVersionOverwrite(t *testing.T) { - app, err := appPkg.GetApp(testPkg.ExpectedAppFiles, testPkg.AppName) + test.Setup() + t.Cleanup(func() { test.Teardown() }) + + app, err := appPkg.GetApp(expectedAppFiles, test.AppName) if err != nil { t.Fatal(err) } - defer t.Cleanup(func() { + t.Cleanup(func() { if err := app.WipeRecipeVersion(); err != nil { t.Fatal(err) } @@ -217,7 +263,7 @@ func TestWriteRecipeVersionOverwrite(t *testing.T) { t.Fatal(err) } - app, err = appPkg.GetApp(testPkg.ExpectedAppFiles, testPkg.AppName) + app, err = appPkg.GetApp(expectedAppFiles, test.AppName) if err != nil { t.Fatal(err) } @@ -226,7 +272,10 @@ func TestWriteRecipeVersionOverwrite(t *testing.T) { } func TestWriteRecipeVersionUnknown(t *testing.T) { - app, err := appPkg.GetApp(testPkg.ExpectedAppFiles, testPkg.AppName) + test.Setup() + t.Cleanup(func() { test.Teardown() }) + + app, err := appPkg.GetApp(expectedAppFiles, test.AppName) if err != nil { t.Fatal(err) } diff --git a/pkg/app/compose_test.go b/pkg/app/compose_test.go index f66e7565..81eeee36 100644 --- a/pkg/app/compose_test.go +++ b/pkg/app/compose_test.go @@ -4,6 +4,7 @@ import ( "testing" appPkg "coopcloud.tech/abra/pkg/app" + "coopcloud.tech/abra/pkg/test" testPkg "coopcloud.tech/abra/pkg/test" stack "coopcloud.tech/abra/pkg/upstream/stack" @@ -11,8 +12,8 @@ import ( ) func TestGetTimeoutFromLabel(t *testing.T) { - testPkg.MkServerAppRecipe() - defer testPkg.RmServerAppRecipe() + test.Setup() + t.Cleanup(func() { test.Teardown() }) tests := []struct { configuredTimeout string @@ -25,7 +26,7 @@ func TestGetTimeoutFromLabel(t *testing.T) { } for _, test := range tests { - app, err := appPkg.GetApp(testPkg.ExpectedAppFiles, testPkg.AppName) + app, err := appPkg.GetApp(expectedAppFiles, testPkg.AppName) if err != nil { t.Fatal(err) } diff --git a/pkg/config/abra_test.go b/pkg/config/abra_test.go index 77b1fb2a..eca2595f 100644 --- a/pkg/config/abra_test.go +++ b/pkg/config/abra_test.go @@ -10,8 +10,9 @@ import ( func TestFindAbraConfig(t *testing.T) { wd, err := os.Getwd() if err != nil { - log.Fatal(err) + t.Fatal(err) } + tests := []struct { Dir string Config string @@ -51,8 +52,9 @@ func TestFindAbraConfig(t *testing.T) { func TestLoadAbraConfigGetAbraDir(t *testing.T) { wd, err := os.Getwd() if err != nil { - log.Fatal(err) + t.Fatal(err) } + t.Setenv("ABRA_DIR", "") t.Run("default", func(t *testing.T) { @@ -67,7 +69,7 @@ func TestLoadAbraConfigGetAbraDir(t *testing.T) { t.Cleanup(func() { os.Chdir(wd) }) err = os.Chdir(filepath.Join(wd, "testdata/abraconfig1")) if err != nil { - log.Fatal(err) + t.Fatal(err) } cfg := LoadAbraConfig() @@ -81,7 +83,7 @@ func TestLoadAbraConfigGetAbraDir(t *testing.T) { t.Cleanup(func() { os.Chdir(wd) }) err := os.Chdir(filepath.Join(wd, "testdata/abraconfig2")) if err != nil { - log.Fatal(err) + t.Fatal(err) } cfg := LoadAbraConfig() @@ -104,8 +106,9 @@ func TestLoadAbraConfigGetAbraDir(t *testing.T) { func TestLoadAbraConfigServersDir(t *testing.T) { wd, err := os.Getwd() if err != nil { - log.Fatal(err) + t.Fatal(err) } + t.Setenv("ABRA_DIR", "") t.Run("default", func(t *testing.T) { @@ -120,7 +123,7 @@ func TestLoadAbraConfigServersDir(t *testing.T) { t.Cleanup(func() { os.Chdir(wd) }) err = os.Chdir(filepath.Join(wd, "testdata/abraconfig1")) if err != nil { - log.Fatal(err) + t.Fatal(err) } cfg := LoadAbraConfig() diff --git a/pkg/envfile/envfile_test.go b/pkg/envfile/envfile_test.go index 9ae75da6..168c06be 100644 --- a/pkg/envfile/envfile_test.go +++ b/pkg/envfile/envfile_test.go @@ -10,48 +10,73 @@ import ( "coopcloud.tech/abra/pkg/config" "coopcloud.tech/abra/pkg/envfile" "coopcloud.tech/abra/pkg/recipe" - testPkg "coopcloud.tech/abra/pkg/test" + "coopcloud.tech/abra/pkg/test" "github.com/stretchr/testify/assert" ) +var ( + expectedAppEnv = envfile.AppEnv{ + "DOMAIN": test.AppName, + "RECIPE": test.RecipeName, + } + + expectedAppFile = appPkg.AppFile{ + Path: test.AppEnvPath, + Server: test.ServerName, + } + + expectedAppFiles = map[string]appPkg.AppFile{ + test.AppName: expectedAppFile, + } +) + func TestGetAllFoldersInDirectory(t *testing.T) { - folders, err := config.GetAllFoldersInDirectory(testPkg.TestDir) + folders, err := config.GetAllFoldersInDirectory(test.TestDir) if err != nil { t.Fatal(err) } - if !reflect.DeepEqual(folders, testPkg.TFolders) { - t.Fatalf("did not get expected folders. Expected: (%s), Got: (%s)", strings.Join(testPkg.TFolders, ","), strings.Join(folders, ",")) + + if !reflect.DeepEqual(folders, test.TFolders) { + t.Fatalf("did not get expected folders. Expected: (%s), Got: (%s)", strings.Join(test.TFolders, ","), strings.Join(folders, ",")) } } func TestGetAllFilesInDirectory(t *testing.T) { - files, err := config.GetAllFilesInDirectory(testPkg.TestDir) + files, err := config.GetAllFilesInDirectory(test.TestDir) if err != nil { t.Fatal(err) } + var fileNames []string for _, file := range files { fileNames = append(fileNames, file.Name()) } - if !reflect.DeepEqual(fileNames, testPkg.TFiles) { - t.Fatalf("did not get expected files. Expected: (%s), Got: (%s)", strings.Join(testPkg.TFiles, ","), strings.Join(fileNames, ",")) + + if !reflect.DeepEqual(fileNames, test.TFiles) { + t.Fatalf("did not get expected files. Expected: (%s), Got: (%s)", strings.Join(test.TFiles, ","), strings.Join(fileNames, ",")) } } func TestReadEnv(t *testing.T) { - env, err := envfile.ReadEnv(testPkg.ExpectedAppFile.Path) + test.Setup() + t.Cleanup(func() { test.Teardown() }) + + env, err := envfile.ReadEnv(expectedAppFile.Path) if err != nil { t.Fatal(err) } - if !reflect.DeepEqual(env, testPkg.ExpectedAppEnv) { + + if !reflect.DeepEqual(env, expectedAppEnv) { t.Fatal("did not get expected application settings") } } func TestReadAbraShEnvVars(t *testing.T) { - r := recipe.Get("abra-test-recipe") - err := r.EnsureExists() - if err != nil { + test.Setup() + t.Cleanup(func() { test.Teardown() }) + + r := recipe.Get(test.AbraTestRecipe) + if err := r.EnsureExists(); err != nil { t.Fatal(err) } @@ -78,9 +103,11 @@ func TestReadAbraShEnvVars(t *testing.T) { } func TestReadAbraShCmdNames(t *testing.T) { - r := recipe.Get("abra-test-recipe") - err := r.EnsureExists() - if err != nil { + test.Setup() + t.Cleanup(func() { test.Teardown() }) + + r := recipe.Get(test.AbraTestRecipe) + if err := r.EnsureExists(); err != nil { t.Fatal(err) } @@ -102,9 +129,11 @@ func TestReadAbraShCmdNames(t *testing.T) { } func TestCheckEnv(t *testing.T) { - r := recipe.Get("abra-test-recipe") - err := r.EnsureExists() - if err != nil { + test.Setup() + t.Cleanup(func() { test.Teardown() }) + + r := recipe.Get(test.AbraTestRecipe) + if err := r.EnsureExists(); err != nil { t.Fatal(err) } @@ -135,9 +164,11 @@ func TestCheckEnv(t *testing.T) { } func TestCheckEnvError(t *testing.T) { - r := recipe.Get("abra-test-recipe") - err := r.EnsureExists() - if err != nil { + test.Setup() + t.Cleanup(func() { test.Teardown() }) + + r := recipe.Get(test.AbraTestRecipe) + if err := r.EnsureExists(); err != nil { t.Fatal(err) } @@ -170,9 +201,11 @@ func TestCheckEnvError(t *testing.T) { } func TestEnvVarCommentsRemoved(t *testing.T) { - r := recipe.Get("abra-test-recipe") - err := r.EnsureExists() - if err != nil { + test.Setup() + t.Cleanup(func() { test.Teardown() }) + + r := recipe.Get(test.AbraTestRecipe) + if err := r.EnsureExists(); err != nil { t.Fatal(err) } @@ -201,9 +234,11 @@ func TestEnvVarCommentsRemoved(t *testing.T) { } func TestEnvVarModifiersIncluded(t *testing.T) { - r := recipe.Get("abra-test-recipe") - err := r.EnsureExists() - if err != nil { + test.Setup() + t.Cleanup(func() { test.Teardown() }) + + r := recipe.Get(test.AbraTestRecipe) + if err := r.EnsureExists(); err != nil { t.Fatal(err) } @@ -225,7 +260,10 @@ func TestEnvVarModifiersIncluded(t *testing.T) { } func TestNoOverwriteNonVersionEnvVars(t *testing.T) { - app, err := appPkg.GetApp(testPkg.ExpectedAppFiles, testPkg.AppName) + test.Setup() + t.Cleanup(func() { test.Teardown() }) + + app, err := appPkg.GetApp(expectedAppFiles, test.AppName) if err != nil { t.Fatal(err) } @@ -234,7 +272,7 @@ func TestNoOverwriteNonVersionEnvVars(t *testing.T) { t.Fatal(err) } - app, err = appPkg.GetApp(testPkg.ExpectedAppFiles, testPkg.AppName) + app, err = appPkg.GetApp(expectedAppFiles, test.AppName) if err != nil { t.Fatal(err) } diff --git a/pkg/git/clone_test.go b/pkg/git/clone_test.go index 80c859eb..380024a4 100644 --- a/pkg/git/clone_test.go +++ b/pkg/git/clone_test.go @@ -8,11 +8,44 @@ import ( "testing" "coopcloud.tech/abra/pkg/config" + "coopcloud.tech/abra/pkg/log" ) +func setup() { + teardown() + + if err := os.Mkdir(os.ExpandEnv("$ABRA_DIR"), 0764); err != nil { + if !os.IsExist(err) { + log.Fatal(err) + } + } + + if err := os.Mkdir(os.ExpandEnv("$ABRA_DIR/recipes"), 0764); err != nil { + if !os.IsExist(err) { + log.Fatal(err) + } + } +} + +func teardown() { + abraDir := os.ExpandEnv("$ABRA_DIR") + if abraDir == fmt.Sprintf("%s/.abra", os.ExpandEnv("$HOME")) { + log.Fatal("set $ABRA_DIR before running the test suite") + } + + if err := os.RemoveAll(abraDir); err != nil { + log.Fatal(err) + } +} + func TestClone(t *testing.T) { + setup() + t.Cleanup(func() { teardown() }) + dir := path.Join(config.RECIPES_DIR, "gitea") - os.RemoveAll(dir) + if err := os.RemoveAll(dir); err != nil { + t.Fatal(err) + } gitURL := fmt.Sprintf("%s/%s.git", config.REPOS_BASE_URL, "gitea") if err := Clone(dir, gitURL); err != nil { @@ -25,6 +58,9 @@ func TestClone(t *testing.T) { } func TestCancelGitClone(t *testing.T) { + setup() + t.Cleanup(func() { teardown() }) + dir := path.Join(config.RECIPES_DIR, "gitea") os.RemoveAll(dir) diff --git a/pkg/recipe/git_test.go b/pkg/recipe/git_test.go index 32197b83..0f54f12b 100644 --- a/pkg/recipe/git_test.go +++ b/pkg/recipe/git_test.go @@ -5,12 +5,15 @@ import ( "path/filepath" "testing" + "coopcloud.tech/abra/pkg/test" "github.com/stretchr/testify/assert" ) func TestIsDirty(t *testing.T) { - r := Get("abra-test-recipe") + test.Setup() + t.Cleanup(func() { test.Teardown() }) + r := Get(test.RecipeName) if err := r.EnsureExists(); err != nil { t.Fatal(err) } @@ -22,10 +25,9 @@ func TestIsDirty(t *testing.T) { if err != nil { t.Fatal(err) } + defer f.Close() - defer t.Cleanup(func() { - os.Remove(fpath) - }) + t.Cleanup(func() { os.Remove(fpath) }) dirty, err := r.IsDirty() if err != nil { diff --git a/pkg/recipe/recipe_test.go b/pkg/recipe/recipe_test.go index 57a6e6ba..9f3c5748 100644 --- a/pkg/recipe/recipe_test.go +++ b/pkg/recipe/recipe_test.go @@ -5,6 +5,7 @@ import ( "testing" "coopcloud.tech/abra/pkg/config" + "coopcloud.tech/abra/pkg/test" "github.com/google/go-cmp/cmp" "github.com/stretchr/testify/assert" @@ -101,24 +102,24 @@ func TestGet(t *testing.T) { } func TestGetVersionLabelLocalDoesNotUseTimeoutLabel(t *testing.T) { - r := Get("traefik") + test.Setup() + t.Cleanup(func() { test.Teardown() }) + + r := Get(test.RecipeName) if err := r.EnsureExists(); err != nil { t.Fatal(err) } - for i := 1; i < 50; i++ { + timeout := "120" + if err := test.AddEnv("TIMEOUT", timeout); err != nil { + t.Fatal(err) + } + + for i := 1; i < 3; i++ { label, err := r.GetVersionLabelLocal() if err != nil { t.Fatal(err) } - - // NOTE(d1): this is potentially quite a brittle unit test as it needs to - // hardcode the default timeout label to ensure that the label parser never - // returns it. hopefully this won't fail too often! if you're here because - // of a failure, just update the `defaultTimeoutLabel` value & permalink - // below - // https://git.coopcloud.tech/coop-cloud/traefik/src/commit/ac3a47fe8ca3ef92db84f64cfedfbb348000faee/.env.sample#L2 - defaultTimeoutLabel := "300" - assert.NotEqual(t, label, defaultTimeoutLabel) + assert.NotEqual(t, label, timeout) } } diff --git a/pkg/test/test.go b/pkg/test/test.go index d8768c4d..302934f6 100644 --- a/pkg/test/test.go +++ b/pkg/test/test.go @@ -2,57 +2,50 @@ package test import ( "fmt" + "log" "os" "path" - appPkg "coopcloud.tech/abra/pkg/app" - "coopcloud.tech/abra/pkg/envfile" - "coopcloud.tech/abra/pkg/log" - "coopcloud.tech/abra/pkg/recipe" + gitPkg "coopcloud.tech/abra/pkg/git" + "git.coopcloud.tech/toolshed/godotenv" ) var ( AppName = "test_app.example.com" ServerName = "test_server" - TFiles = []string{"bar.env", "foo.env"} - TFolders = []string{"dir1", "dir2"} - TestServer = os.ExpandEnv("$PWD/../../tests/resources/test_server") - TestDir = os.ExpandEnv("$PWD/../../tests/resources/test_dir") + RecipeName = "test_recipe" - ExpectedAppEnv = envfile.AppEnv{ - "DOMAIN": "test_app.example.com", - "RECIPE": "test_recipe", - } + TFiles = []string{"bar.env", "foo.env"} + TFolders = []string{"dir1", "dir2"} - ExpectedApp = appPkg.App{ - Name: AppName, - Recipe: recipe.Get(ExpectedAppEnv["RECIPE"]), - Domain: ExpectedAppEnv["DOMAIN"], - Env: ExpectedAppEnv, - Path: ExpectedAppFile.Path, - Server: ExpectedAppFile.Server, - } + ServerDir = os.ExpandEnv("$ABRA_DIR/servers/test_server") + RecipeDir = os.ExpandEnv("$ABRA_DIR/recipes/test_recipe") + TestDir = os.ExpandEnv("$PWD/../../tests/resources/test_dir") - ExpectedAppFile = appPkg.AppFile{ - Path: path.Join(TestServer, fmt.Sprintf("%s.env", AppName)), - Server: ServerName, - } + AppEnvPath = path.Join(ServerDir, fmt.Sprintf("%s.env", AppName)) - ExpectedAppFiles = map[string]appPkg.AppFile{ - AppName: ExpectedAppFile, - } + AbraTestRecipe = "abra-test-recipe" ) -func RmServerAppRecipe() { - testAppLink := os.ExpandEnv("$ABRA_DIR/servers/test_server") - os.Remove(testAppLink) +func Teardown() { + abraDir := os.ExpandEnv("$ABRA_DIR") + if abraDir == fmt.Sprintf("%s/.abra", os.ExpandEnv("$HOME")) { + log.Fatal("set $ABRA_DIR before running the test suite") + } - testRecipeLink := os.ExpandEnv("$ABRA_DIR/recipes/test_recipe") - os.Remove(testRecipeLink) + if err := os.RemoveAll(abraDir); err != nil { + log.Fatal(err) + } } -func MkServerAppRecipe() { - RmServerAppRecipe() +func Setup() { + Teardown() + + if err := os.Mkdir(os.ExpandEnv("$ABRA_DIR"), 0764); err != nil { + if !os.IsExist(err) { + log.Fatal(err) + } + } if err := os.Mkdir(os.ExpandEnv("$ABRA_DIR/servers"), 0700); err != nil { if !os.IsExist(err) { @@ -66,15 +59,36 @@ func MkServerAppRecipe() { } } - testAppDir := os.ExpandEnv("$PWD/../../tests/resources/test_server") - testAppLink := os.ExpandEnv("$ABRA_DIR/servers/test_server") - if err := os.Symlink(testAppDir, testAppLink); err != nil { + serverSrcDir := os.ExpandEnv("$PWD/../../tests/resources/test_server") + serverDestDir := os.ExpandEnv("$ABRA_DIR/servers/test_server") + if err := os.CopyFS(serverDestDir, os.DirFS(serverSrcDir)); err != nil { log.Fatal(err) } - testRecipeDir := os.ExpandEnv("$PWD/../../tests/resources/test_recipe") - testRecipeLink := os.ExpandEnv("$ABRA_DIR/recipes/test_recipe") - if err := os.Symlink(testRecipeDir, testRecipeLink); err != nil { + recipeSrcDir := os.ExpandEnv("$PWD/../../tests/resources/test_recipe") + recipeDestDir := os.ExpandEnv("$ABRA_DIR/recipes/test_recipe") + if err := os.CopyFS(recipeDestDir, os.DirFS(recipeSrcDir)); err != nil { + log.Fatal(err) + } + + if err := gitPkg.Init(recipeDestDir, true, "tester", "helo@coopcloud.tech"); err != nil { log.Fatal(err) } } + +func AddEnv(envKey, envValue string) error { + filePath := os.ExpandEnv(fmt.Sprintf("$ABRA_DIR/servers/%s/%s.env", ServerName, AppName)) + + envVars, _, err := godotenv.Read(filePath) + if err != nil { + return err + } + + envVars[envKey] = envValue + + if err := godotenv.Write(envVars, filePath); err != nil { + return err + } + + return nil +} diff --git a/tests/resources/test_recipe/.env.sample b/tests/resources/test_recipe/.env.sample index f2f4dcb2..4dd1cdcd 100644 --- a/tests/resources/test_recipe/.env.sample +++ b/tests/resources/test_recipe/.env.sample @@ -1,6 +1,9 @@ RECIPE=test_recipe DOMAIN=test_app.example.com -# NOTE(d1): ensure commented out TIMEOUT doesn't get included -# see TestReadEnv in ./pkg/envfile -# TIMEOUT=120 +# +# NOTE(d1): for new changes, you *MUST* also update ../test_server/test_app.example.com.env +# + +# NOTE(d1): TestReadEnv +# FOO=BAR diff --git a/tests/resources/test_recipe/compose.yml b/tests/resources/test_recipe/compose.yml index 8d8ff83a..638fd142 100644 --- a/tests/resources/test_recipe/compose.yml +++ b/tests/resources/test_recipe/compose.yml @@ -8,6 +8,7 @@ services: - proxy deploy: labels: + - "coop-cloud.${STACK_NAME}.version=0.1.0+0.1.0" - "coop-cloud.${STACK_NAME}.timeout=${TIMEOUT}" networks: diff --git a/tests/resources/test_server/test_app.example.com.env b/tests/resources/test_server/test_app.example.com.env index dd74c315..ab781496 100644 --- a/tests/resources/test_server/test_app.example.com.env +++ b/tests/resources/test_server/test_app.example.com.env @@ -1,6 +1,9 @@ RECIPE=test_recipe DOMAIN=test_app.example.com -# NOTE(d1): ensure commented out TIMEOUT doesn't get included -# see TestReadEnv in ./pkg/envfile -# TIMEOUT=120 \ No newline at end of file +# +# NOTE(d1): for new changes, you *MUST* also update ../test_recipe/.env.sample +# + +# NOTE(d1): TestReadEnv +# FOO=BAR -- 2.49.0