From c501354be16ba39d33a2652bddd683770ab6d0b9 Mon Sep 17 00:00:00 2001 From: yankomissarov Date: Thu, 6 Mar 2025 15:40:54 +0300 Subject: [PATCH] MSP-5013 unmarshal fix --- migrations/20250303125608_add_variables.sql | 2 +- service/variable/service.go | 2 +- tests/variable_test.go | 62 +++++++++++++++++++-- 3 files changed, 59 insertions(+), 7 deletions(-) diff --git a/migrations/20250303125608_add_variables.sql b/migrations/20250303125608_add_variables.sql index 144972d..ce4d7d6 100644 --- a/migrations/20250303125608_add_variables.sql +++ b/migrations/20250303125608_add_variables.sql @@ -7,7 +7,7 @@ create table isp_config_service__variable type text not null, value text not null, created_at integer not null default (unixepoch()), - update_at integer not null default (unixepoch()) + updated_at integer not null default (unixepoch()) ); create table isp_config_service__config_has_variable diff --git a/service/variable/service.go b/service/variable/service.go index b60efca..09bff1b 100644 --- a/service/variable/service.go +++ b/service/variable/service.go @@ -200,7 +200,7 @@ func (s Service) RenderConfig(ctx context.Context, input []byte) ([]byte, error) if err != nil { return nil, errors.WithMessage(err, "parse template") } - out := bytes.NewBuffer(make([]byte, len(input))) + out := bytes.NewBuffer(make([]byte, 0, len(input))) err = tt.Execute(out, varsMap) if err != nil { return nil, errors.WithMessage(err, "execute template") diff --git a/tests/variable_test.go b/tests/variable_test.go index 3a8cef3..38a465c 100644 --- a/tests/variable_test.go +++ b/tests/variable_test.go @@ -5,6 +5,7 @@ import ( "github.com/stretchr/testify/require" "github.com/txix-open/isp-kit/cluster" "github.com/txix-open/isp-kit/grpc/client" + "github.com/txix-open/isp-kit/json" "github.com/txix-open/isp-kit/test/fake" "isp-config-service/domain" "strconv" @@ -31,37 +32,88 @@ func TestVariableAcceptance(t *testing.T) { apiCli, err := client.Default() require.NoError(err) apiCli.Upgrade([]string{"127.0.0.1:9002"}) + varAValue := fake.It[string]() err = apiCli.Invoke("config/variable/create"). JsonRequestBody(domain.CreateVariableRequest{ Name: "varA", Description: fake.It[string](), Type: "TEXT", - Value: fake.It[string](), + Value: varAValue, }).Do(context.Background()) require.NoError(err) + varBValue := fake.It[int]() err = apiCli.Invoke("config/variable/upsert"). JsonRequestBody([]domain.UpsertVariableRequest{{ Name: "varB", Description: "test", Type: "SECRET", - Value: strconv.Itoa(fake.It[int]()), + Value: strconv.Itoa(varBValue), }}). Do(context.Background()) require.NoError(err) + eventHandler := newClusterEventHandler() clientA1 := newClusterClientWith( "A", "10.2.9.1", configWithVars{}, []byte(defaultRemoteConfigsWithVariables), - logger) + logger, + ) go func() { - handler := cluster.NewEventHandler() + handler := cluster.NewEventHandler().RemoteConfigReceiver(eventHandler) err := clientA1.Run(context.Background(), handler) require.NoError(err) //nolint:testifylint }() time.Sleep(3 * time.Second) - time.Sleep(5 * time.Second) + firstConfig := unmarshalConfig(require, eventHandler.ReceivedConfigs()[0]) + expected := configWithVars{ + VarA: varAValue, + VarB: varBValue, + } + require.EqualValues(expected, firstConfig) + + err = apiCli.Invoke("config/variable/delete"). + JsonRequestBody(domain.VariableByNameRequest{Name: "varB"}). + Do(context.Background()) + require.Error(err) + + varBValue = fake.It[int]() + err = apiCli.Invoke("config/variable/upsert"). + JsonRequestBody([]domain.UpsertVariableRequest{{ + Name: "varB", + Description: "test", + Type: "SECRET", + Value: strconv.Itoa(varBValue), + }}). + Do(context.Background()) + require.NoError(err) + time.Sleep(3 * time.Second) + + secondConfig := unmarshalConfig(require, eventHandler.ReceivedConfigs()[1]) + expected = configWithVars{ + VarA: varAValue, + VarB: varBValue, + } + require.EqualValues(expected, secondConfig) + + allResponse := []domain.Variable{} + err = apiCli.Invoke("config/variable/all"). + JsonResponseBody(&allResponse). + Do(context.Background()) + require.NoError(err) + + require.Len(allResponse, 2) + require.EqualValues(varAValue, allResponse[0].Value) + require.EqualValues("TEXT", allResponse[0].Type) + require.Empty(allResponse[1].Value) +} + +func unmarshalConfig(require *require.Assertions, configData []byte) configWithVars { + firstConfig := configWithVars{} + err := json.Unmarshal(configData, &firstConfig) + require.NoError(err) + return firstConfig }