MSP-5013 unmarshal fix
This commit is contained in:
1 parent
7852772e9d
commit
c501354be1
3 files changed
+59
-7
No files matched your search
@@ -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
|
||||
|
||||
@@ -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")
|
||||
|
||||
+57
-5
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user