From 41fa496052fa219cb6d3d6b9e818ccc88265b5d5 Mon Sep 17 00:00:00 2001 From: yankomissarov Date: Mon, 10 Mar 2025 11:11:57 +0300 Subject: [PATCH] MSP-5013 fix test --- cmd/migrate/go.mod | 2 +- cmd/migrate/go.sum | 4 ++-- go.mod | 2 +- go.sum | 4 ++-- repository/variable.go | 9 ++++++++- tests/acceptance_test.go | 18 +++++++++++++----- tests/variable_test.go | 23 +++++++++++++++-------- 7 files changed, 42 insertions(+), 20 deletions(-) diff --git a/cmd/migrate/go.mod b/cmd/migrate/go.mod index 007032c..2ed6d6d 100644 --- a/cmd/migrate/go.mod +++ b/cmd/migrate/go.mod @@ -11,7 +11,7 @@ require ( github.com/mattn/go-sqlite3 v1.14.24 github.com/pkg/errors v0.9.1 github.com/pressly/goose/v3 v3.24.1 - github.com/txix-open/isp-kit v1.43.0 + github.com/txix-open/isp-kit v1.44.0 isp-config-service v0.0.0-00010101000000-000000000000 ) diff --git a/cmd/migrate/go.sum b/cmd/migrate/go.sum index c2cd302..549da31 100644 --- a/cmd/migrate/go.sum +++ b/cmd/migrate/go.sum @@ -143,8 +143,8 @@ github.com/txix-open/bellows v1.2.0 h1:CXv8nQaZtB/micraeRilYyj/gtfv+bqBgP5aPYQgj github.com/txix-open/bellows v1.2.0/go.mod h1:qbKCy+RTgD30Qpw1fyb3y3jp5Y9mGhLLxgae1l0W92o= github.com/txix-open/etp/v4 v4.0.1 h1:5VSYgGsvnMz0tvUHsjVWFETI0qWBTCF1O3ARCHaCIgA= github.com/txix-open/etp/v4 v4.0.1/go.mod h1:FIu7TUDdwcgmtmF6tFTV1bGA2fRY/iVaPQdsGzvqYc8= -github.com/txix-open/isp-kit v1.43.0 h1:fVVa8WNGSShtK935ypOAN1eQCY7Y4bqr17A3JCbp6mA= -github.com/txix-open/isp-kit v1.43.0/go.mod h1:BHRF3AYRcQtEtNvoQZXxTVj/CYOVMKhOcCD3n8CdsxE= +github.com/txix-open/isp-kit v1.44.0 h1:ao16vgZ/cdbO4sZWYXD4VcJQ+984IQvb/A80mEjU504= +github.com/txix-open/isp-kit v1.44.0/go.mod h1:BHRF3AYRcQtEtNvoQZXxTVj/CYOVMKhOcCD3n8CdsxE= github.com/txix-open/validator/v10 v10.0.0-20240401163703-e83541b40f99 h1:Z0Sqf+U+Sc6hfIXtRCIZlbJprkCjwMFbS4ZbEPQ44To= github.com/txix-open/validator/v10 v10.0.0-20240401163703-e83541b40f99/go.mod h1:pA0NYmhL0uEkkxj55S+S7MZU4e2QIt46x9XSrzrWc3c= go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA= diff --git a/go.mod b/go.mod index 9571da7..989737b 100644 --- a/go.mod +++ b/go.mod @@ -21,7 +21,7 @@ require ( github.com/stretchr/testify v1.10.0 github.com/tidwall/gjson v1.18.0 github.com/txix-open/etp/v4 v4.0.1 - github.com/txix-open/isp-kit v1.43.0 + github.com/txix-open/isp-kit v1.44.0 github.com/xeipuuv/gojsonschema v1.2.0 golang.org/x/sync v0.12.0 google.golang.org/grpc v1.71.0 diff --git a/go.sum b/go.sum index bcb5a5f..8a49e09 100644 --- a/go.sum +++ b/go.sum @@ -266,8 +266,8 @@ github.com/txix-open/etp/v4 v4.0.1 h1:5VSYgGsvnMz0tvUHsjVWFETI0qWBTCF1O3ARCHaCIg github.com/txix-open/etp/v4 v4.0.1/go.mod h1:FIu7TUDdwcgmtmF6tFTV1bGA2fRY/iVaPQdsGzvqYc8= github.com/txix-open/grmq v1.7.0 h1:nwenrlxEjXpK0EBCa/WbIxK+y0mF2pZxNKbheFjQVsI= github.com/txix-open/grmq v1.7.0/go.mod h1:DAfbV4jLyxQBLG8OYJVy8kCbhucdlu/C/wl+nUDc9p8= -github.com/txix-open/isp-kit v1.43.0 h1:fVVa8WNGSShtK935ypOAN1eQCY7Y4bqr17A3JCbp6mA= -github.com/txix-open/isp-kit v1.43.0/go.mod h1:BHRF3AYRcQtEtNvoQZXxTVj/CYOVMKhOcCD3n8CdsxE= +github.com/txix-open/isp-kit v1.44.0 h1:ao16vgZ/cdbO4sZWYXD4VcJQ+984IQvb/A80mEjU504= +github.com/txix-open/isp-kit v1.44.0/go.mod h1:BHRF3AYRcQtEtNvoQZXxTVj/CYOVMKhOcCD3n8CdsxE= github.com/txix-open/jsonschema v1.2.0 h1:B8TrdSsPhDvYv67oi/LVqf7/+PuqFlMsNwUzGeHfl1s= github.com/txix-open/jsonschema v1.2.0/go.mod h1:l8YDZ1nvJrw6uxWowSVOxCV/ebiMJyapffW87ZEqH00= github.com/txix-open/validator/v10 v10.0.0-20240401163703-e83541b40f99 h1:Z0Sqf+U+Sc6hfIXtRCIZlbJprkCjwMFbS4ZbEPQ44To= diff --git a/repository/variable.go b/repository/variable.go index 3bb463f..a935e35 100644 --- a/repository/variable.go +++ b/repository/variable.go @@ -9,6 +9,7 @@ import ( "isp-config-service/entity" "isp-config-service/middlewares/sql_metrics" "isp-config-service/service/rqlite/db" + "strings" ) type Variable struct { @@ -68,6 +69,9 @@ func (r Variable) Insert(ctx context.Context, variable entity.Variable) error { _, err = r.db.Exec(ctx, query, args...) if err != nil { + if strings.Contains(err.Error(), "UNIQUE constraint failed") { + return entity.ErrVariableAlreadyExists + } return errors.WithMessagef(err, "exec: %s", query) } @@ -110,7 +114,10 @@ func (r Variable) Upsert(ctx context.Context, variables []entity.Variable) error Columns("name", "description", "type", "value"). Values(variable.Name, variable.Description, variable.Type, variable.Value). Suffix(`on conflict (name) do update - set description = excluded.description, type = excluded.type, value = excluded.value, updated_at = unixepoch()`, + set description = excluded.description, + type = excluded.type, + value = excluded.value, + updated_at = unixepoch()`, ).ToSql() if err != nil { return errors.WithMessage(err, "build query") diff --git a/tests/acceptance_test.go b/tests/acceptance_test.go index 1dfbc04..924b1cc 100644 --- a/tests/acceptance_test.go +++ b/tests/acceptance_test.go @@ -25,14 +25,14 @@ func TestAcceptance(t *testing.T) { require := require.New(t) logger := setupTest(t) - clientA1 := newClusterClient("A", "10.2.9.1", logger) + clientA1 := newClusterClient(t, "A", "10.2.9.1", logger) go func() { handler := cluster.NewEventHandler() err := clientA1.Run(context.Background(), handler) require.NoError(err) //nolint:testifylint }() - clientA2 := newClusterClient("A", "10.2.9.2", logger) + clientA2 := newClusterClient(t, "A", "10.2.9.2", logger) clientA2Ctx, cancelClient2 := context.WithCancel(context.Background()) go func() { handler := cluster.NewEventHandler() @@ -41,7 +41,7 @@ func TestAcceptance(t *testing.T) { }() time.Sleep(2 * time.Second) - clientB := newClusterClient("B", "10.2.9.2", logger) + clientB := newClusterClient(t, "B", "10.2.9.2", logger) eventHandler := newClusterEventHandler() go func() { handler := cluster.NewEventHandler(). @@ -119,26 +119,30 @@ type testModuleRemoteConfig struct { } func newClusterClient( + t *testing.T, moduleName string, host string, logger log.Logger, ) *cluster.Client { - return newClusterClientWith(moduleName, host, testModuleRemoteConfig{}, []byte("{}"), logger) + t.Helper() + return newClusterClientWith(t, moduleName, host, testModuleRemoteConfig{}, []byte("{}"), logger) } func newClusterClientWith( + t *testing.T, moduleName string, host string, config any, defaultRemoteConfig []byte, logger log.Logger, ) *cluster.Client { + t.Helper() schema := schema.NewGenerator().Generate(config) schemaData, err := json.Marshal(schema) if err != nil { panic(err) } - return cluster.NewClient(cluster.ModuleInfo{ + cli := cluster.NewClient(cluster.ModuleInfo{ ModuleName: moduleName, ModuleVersion: "1.0.0", LibVersion: "1.0.0", @@ -152,6 +156,10 @@ func newClusterClientWith( Schema: schemaData, Config: defaultRemoteConfig, }, []string{"127.0.0.1:9001"}, logger) + t.Cleanup(func() { + _ = cli.Close() + }) + return cli } //nolint:ireturn diff --git a/tests/variable_test.go b/tests/variable_test.go index 2104cd6..0336151 100644 --- a/tests/variable_test.go +++ b/tests/variable_test.go @@ -34,14 +34,20 @@ func TestVariableAcceptance(t *testing.T) { require.NoError(err) apiCli.Upgrade([]string{"127.0.0.1:9002"}) varAValue := fake.It[string]() + varA := domain.CreateVariableRequest{ + Name: "varA", + Description: fake.It[string](), + Type: "TEXT", + Value: varAValue, + } err = apiCli.Invoke("config/variable/create"). - JsonRequestBody(domain.CreateVariableRequest{ - Name: "varA", - Description: fake.It[string](), - Type: "TEXT", - Value: varAValue, - }).Do(context.Background()) + JsonRequestBody(varA). + Do(context.Background()) require.NoError(err) + err = apiCli.Invoke("config/variable/create"). + JsonRequestBody(varA). + Do(context.Background()) + require.Error(err) varBValue := fake.It[int]() err = apiCli.Invoke("config/variable/upsert"). @@ -56,6 +62,7 @@ func TestVariableAcceptance(t *testing.T) { eventHandler := newClusterEventHandler() clientA1 := newClusterClientWith( + t, "A", "10.2.9.1", configWithVars{}, @@ -63,7 +70,7 @@ func TestVariableAcceptance(t *testing.T) { logger, ) go func() { - handler := cluster.NewEventHandler().RemoteConfigReceiver(eventHandler) + handler := cluster.NewEventHandler().RemoteConfigReceiverWithTimeout(eventHandler, 5*time.Second) err := clientA1.Run(context.Background(), handler) require.NoError(err) //nolint:testifylint }() @@ -109,7 +116,7 @@ func TestVariableAcceptance(t *testing.T) { require.Len(allResponse, 2) require.EqualValues(varAValue, allResponse[0].Value) require.EqualValues("TEXT", allResponse[0].Type) - require.Empty(allResponse[1].Value) + require.Empty(allResponse[1].Value) // secret } func unmarshalConfig(require *require.Assertions, configData []byte) configWithVars {