From ca91abbed9d0297fc49d9dad8012749217e269d5 Mon Sep 17 00:00:00 2001 From: p4u1 Date: Fri, 22 Dec 2023 12:08:12 +0000 Subject: [PATCH] fix: correct append service name logic in Filters function (!396) This fixes a regression introduced by #395 Reviewed-on: https://git.coopcloud.tech/coop-cloud/abra/pulls/396 Co-authored-by: p4u1 Co-committed-by: p4u1 --- go.mod | 2 +- pkg/config/app.go | 4 +- pkg/config/app_test.go | 89 ++++++++++++++++++++++ pkg/config/testdir/filtertest.env | 2 + pkg/config/testdir/test-recipe/compose.yml | 6 ++ 5 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 pkg/config/testdir/filtertest.env create mode 100644 pkg/config/testdir/test-recipe/compose.yml diff --git a/go.mod b/go.mod index 5287ec3d..950841f8 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,7 @@ require ( github.com/docker/docker v24.0.7+incompatible github.com/docker/go-units v0.5.0 github.com/go-git/go-git/v5 v5.10.0 + github.com/google/go-cmp v0.5.9 github.com/moby/sys/signal v0.7.0 github.com/moby/term v0.5.0 github.com/olekukonko/tablewriter v0.0.5 @@ -47,7 +48,6 @@ require ( github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect github.com/golang/protobuf v1.5.3 // indirect - github.com/google/go-cmp v0.5.9 // indirect github.com/hashicorp/go-cleanhttp v0.5.2 // indirect github.com/imdario/mergo v0.3.12 // indirect github.com/inconshreveable/mousetrap v1.0.0 // indirect diff --git a/pkg/config/app.go b/pkg/config/app.go index a0d3e869..38b9ddc6 100644 --- a/pkg/config/app.go +++ b/pkg/config/app.go @@ -95,7 +95,9 @@ func (a App) Filters(appendServiceNames, exactMatch bool, services ...string) (f return filters, nil } - if appendServiceNames { + // When not appending the service name, just add one filter for the whole + // stack. + if !appendServiceNames { f := fmt.Sprintf("%s", a.StackName()) if exactMatch { f = fmt.Sprintf("^%s", f) diff --git a/pkg/config/app_test.go b/pkg/config/app_test.go index 94398a0c..0823f4e2 100644 --- a/pkg/config/app_test.go +++ b/pkg/config/app_test.go @@ -1,12 +1,15 @@ package config_test import ( + "encoding/json" "fmt" "reflect" "testing" "coopcloud.tech/abra/pkg/config" "coopcloud.tech/abra/pkg/recipe" + "github.com/docker/docker/api/types/filters" + "github.com/google/go-cmp/cmp" "github.com/stretchr/testify/assert" ) @@ -106,3 +109,89 @@ func TestGetComposeFilesError(t *testing.T) { } } } + +func TestFilters(t *testing.T) { + oldDir := config.RECIPES_DIR + config.RECIPES_DIR = "./testdir" + defer func() { + config.RECIPES_DIR = oldDir + }() + + app, err := config.NewApp(config.AppEnv{ + "DOMAIN": "test.example.com", + "RECIPE": "test-recipe", + }, "test_example_com", config.AppFile{ + Path: "./testdir/filtertest.end", + Server: "local", + }) + if err != nil { + t.Fatal(err) + } + + f, err := app.Filters(false, false) + if err != nil { + t.Error(err) + } + compareFilter(t, f, map[string]map[string]bool{ + "name": { + "test_example_com": true, + }, + }) + + f2, err := app.Filters(false, true) + if err != nil { + t.Error(err) + } + compareFilter(t, f2, map[string]map[string]bool{ + "name": { + "^test_example_com": true, + }, + }) + + f3, err := app.Filters(true, false) + if err != nil { + t.Error(err) + } + compareFilter(t, f3, map[string]map[string]bool{ + "name": { + "test_example_com_bar": true, + "test_example_com_foo": true, + }, + }) + + f4, err := app.Filters(true, true) + if err != nil { + t.Error(err) + } + compareFilter(t, f4, map[string]map[string]bool{ + "name": { + "^test_example_com_bar": true, + "^test_example_com_foo": true, + }, + }) + + f5, err := app.Filters(false, false, "foo") + if err != nil { + t.Error(err) + } + compareFilter(t, f5, map[string]map[string]bool{ + "name": { + "test_example_com_foo": true, + }, + }) +} + +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) + } +} diff --git a/pkg/config/testdir/filtertest.env b/pkg/config/testdir/filtertest.env new file mode 100644 index 00000000..9250f3b4 --- /dev/null +++ b/pkg/config/testdir/filtertest.env @@ -0,0 +1,2 @@ +RECIPE=test-recipe +DOMAIN=test.example.com diff --git a/pkg/config/testdir/test-recipe/compose.yml b/pkg/config/testdir/test-recipe/compose.yml new file mode 100644 index 00000000..8232eca0 --- /dev/null +++ b/pkg/config/testdir/test-recipe/compose.yml @@ -0,0 +1,6 @@ +version: "3.8" +services: + foo: + image: debian + bar: + image: debian