2025-03-24 15:30:34: merge [main:7a1d8822489f02be3b825bb1224de48bab9c1657];
This commit is contained in:
1 parent
4a95b4b578
commit
22baffabfb
36 files changed
+1827
-381
No files matched your search
@@ -0,0 +1,130 @@
|
||||
package tests_test
|
||||
|
||||
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"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
const defaultRemoteConfigsWithVariables = `
|
||||
{
|
||||
"varA": "_Var(varA)",
|
||||
"varB": "_ToInt(_Var(varB))",
|
||||
"varC": "{{ msp_pgsql_name }}"
|
||||
}
|
||||
`
|
||||
|
||||
type configWithVars struct {
|
||||
VarA string
|
||||
VarB int
|
||||
VarC string
|
||||
}
|
||||
|
||||
// nolint:funlen
|
||||
func TestVariableAcceptance(t *testing.T) {
|
||||
require := require.New(t)
|
||||
logger := setupTest(t)
|
||||
|
||||
apiCli, err := client.Default()
|
||||
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(varA).
|
||||
Do(t.Context())
|
||||
require.NoError(err)
|
||||
err = apiCli.Invoke("config/variable/create").
|
||||
JsonRequestBody(varA).
|
||||
Do(t.Context())
|
||||
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(t.Context())
|
||||
require.NoError(err)
|
||||
|
||||
eventHandler := newClusterEventHandler()
|
||||
clientA1 := newClusterClientWith(
|
||||
t,
|
||||
"A",
|
||||
"10.2.9.1",
|
||||
configWithVars{},
|
||||
[]byte(defaultRemoteConfigsWithVariables),
|
||||
logger,
|
||||
)
|
||||
go func() {
|
||||
handler := cluster.NewEventHandler().RemoteConfigReceiverWithTimeout(eventHandler, 5*time.Second)
|
||||
err := clientA1.Run(t.Context(), handler)
|
||||
require.NoError(err) //nolint:testifylint
|
||||
}()
|
||||
time.Sleep(3 * time.Second)
|
||||
|
||||
firstConfig := unmarshalConfig(require, eventHandler.ReceivedConfigs()[0])
|
||||
expected := configWithVars{
|
||||
VarA: varAValue,
|
||||
VarB: varBValue,
|
||||
VarC: "{{ msp_pgsql_name }}",
|
||||
}
|
||||
require.EqualValues(expected, firstConfig)
|
||||
|
||||
err = apiCli.Invoke("config/variable/delete").
|
||||
JsonRequestBody(domain.VariableByNameRequest{Name: "varB"}).
|
||||
Do(t.Context())
|
||||
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(t.Context())
|
||||
require.NoError(err)
|
||||
time.Sleep(3 * time.Second)
|
||||
|
||||
secondConfig := unmarshalConfig(require, eventHandler.ReceivedConfigs()[1])
|
||||
expected = configWithVars{
|
||||
VarA: varAValue,
|
||||
VarB: varBValue,
|
||||
VarC: "{{ msp_pgsql_name }}",
|
||||
}
|
||||
require.EqualValues(expected, secondConfig)
|
||||
|
||||
allResponse := []domain.Variable{}
|
||||
err = apiCli.Invoke("config/variable/all").
|
||||
JsonResponseBody(&allResponse).
|
||||
Do(t.Context())
|
||||
require.NoError(err)
|
||||
|
||||
require.Len(allResponse, 2)
|
||||
require.EqualValues(varAValue, allResponse[0].Value)
|
||||
require.EqualValues("TEXT", allResponse[0].Type)
|
||||
require.Empty(allResponse[1].Value) // secret
|
||||
}
|
||||
|
||||
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