diff --git a/.golangci.yml b/.golangci.yml index bf47957..a2de546 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -35,6 +35,7 @@ linters: - gomoddirectives #требует rqlite - tagliatelle - gomnd + - paralleltest linters-settings: funlen: lines: 80 diff --git a/go.mod b/go.mod index c4c20f9..9571da7 100644 --- a/go.mod +++ b/go.mod @@ -39,6 +39,7 @@ require ( github.com/fatih/color v1.18.0 // indirect github.com/gabriel-vasile/mimetype v1.4.8 // indirect github.com/getsentry/sentry-go v0.31.1 // indirect + github.com/go-faker/faker/v4 v4.5.0 // indirect github.com/go-logr/logr v1.4.2 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/go-playground/locales v0.14.1 // indirect diff --git a/go.sum b/go.sum index cab7089..bcb5a5f 100644 --- a/go.sum +++ b/go.sum @@ -43,6 +43,8 @@ github.com/getsentry/sentry-go v0.31.1 h1:ELVc0h7gwyhnXHDouXkhqTFSO5oslsRDk0++ey github.com/getsentry/sentry-go v0.31.1/go.mod h1:CYNcMMz73YigoHljQRG+qPF+eMq8gG72XcGN/p71BAY= github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA= github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og= +github.com/go-faker/faker/v4 v4.5.0 h1:ARzAY2XoOL9tOUK+KSecUQzyXQsUaZHefjyF8x6YFHc= +github.com/go-faker/faker/v4 v4.5.0/go.mod h1:p3oq1GRjG2PZ7yqeFFfQI20Xm61DoBDlCA8RiSyZ48M= github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY= diff --git a/repository/variable.go b/repository/variable.go index 65ad391..3bb463f 100644 --- a/repository/variable.go +++ b/repository/variable.go @@ -36,7 +36,10 @@ func (r Variable) All(ctx context.Context) ([]entity.Variable, error) { func (r Variable) GetByName(ctx context.Context, name string) (*entity.Variable, error) { ctx = sql_metrics.OperationLabelToContext(ctx, "Variable.GetByName") - query, args, err := squirrel.Select().From(Table("variable")).Where(squirrel.Eq{"name": name}).ToSql() + query, args, err := squirrel.Select("*"). + From(Table("variable")). + Where(squirrel.Eq{"name": name}). + ToSql() if err != nil { return nil, errors.WithMessage(err, "build query") } diff --git a/tests/acceptance_test.go b/tests/acceptance_test.go index 0e6fd37..1dfbc04 100644 --- a/tests/acceptance_test.go +++ b/tests/acceptance_test.go @@ -23,26 +23,7 @@ import ( //nolint:funlen func TestAcceptance(t *testing.T) { require := require.New(t) - - t.Setenv("APP_CONFIG_PATH", "../conf/config.yml") - t.Setenv("DefaultRemoteConfigPath", "../conf/default_remote_config.json") - boot := bootstrap.New("1.0.0", conf.Remote{}, nil) - boot.MigrationsDir = "../migrations" - dataPath := dataDir(t) - boot.App.Config().Set("rqlite.DataPath", dataPath) - logger := boot.App.Logger() - - startup, err := startup.New(boot) - require.NoError(err) - t.Cleanup(func() { - for _, closer := range startup.Closers() { - err := closer.Close() - require.NoError(err) - } - }) - err = startup.Run(context.Background()) - require.NoError(err) - time.Sleep(1 * time.Second) + logger := setupTest(t) clientA1 := newClusterClient("A", "10.2.9.1", logger) go func() { @@ -142,7 +123,17 @@ func newClusterClient( host string, logger log.Logger, ) *cluster.Client { - schema := schema.NewGenerator().Generate(testModuleRemoteConfig{}) + return newClusterClientWith(moduleName, host, testModuleRemoteConfig{}, []byte("{}"), logger) +} + +func newClusterClientWith( + moduleName string, + host string, + config any, + defaultRemoteConfig []byte, + logger log.Logger, +) *cluster.Client { + schema := schema.NewGenerator().Generate(config) schemaData, err := json.Marshal(schema) if err != nil { panic(err) @@ -159,6 +150,34 @@ func newClusterClient( }, cluster.ConfigData{ Version: "1.0.0", Schema: schemaData, - Config: []byte(`{}`), + Config: defaultRemoteConfig, }, []string{"127.0.0.1:9001"}, logger) } + +//nolint:ireturn +func setupTest(t *testing.T) log.Logger { + t.Helper() + require := require.New(t) + + t.Setenv("APP_CONFIG_PATH", "../conf/config.yml") + t.Setenv("DefaultRemoteConfigPath", "../conf/default_remote_config.json") + boot := bootstrap.New("1.0.0", conf.Remote{}, nil) + boot.MigrationsDir = "../migrations" + dataPath := dataDir(t) + boot.App.Config().Set("rqlite.DataPath", dataPath) + logger := boot.App.Logger() + + startup, err := startup.New(boot) + require.NoError(err) + t.Cleanup(func() { + for _, closer := range startup.Closers() { + err := closer.Close() + require.NoError(err) + } + }) + err = startup.Run(context.Background()) + require.NoError(err) + time.Sleep(1 * time.Second) + + return logger +} diff --git a/tests/variable_test.go b/tests/variable_test.go new file mode 100644 index 0000000..3a8cef3 --- /dev/null +++ b/tests/variable_test.go @@ -0,0 +1,67 @@ +package tests_test + +import ( + "context" + "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/test/fake" + "isp-config-service/domain" + "strconv" + "testing" + "time" +) + +const defaultRemoteConfigsWithVariables = ` +{ + "varA": "_Var({{ .varA }})", + "varB": "_ToInt(_Var({{ .varB }}))" +} +` + +type configWithVars struct { + VarA string + VarB int +} + +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"}) + err = apiCli.Invoke("config/variable/create"). + JsonRequestBody(domain.CreateVariableRequest{ + Name: "varA", + Description: fake.It[string](), + Type: "TEXT", + Value: fake.It[string](), + }).Do(context.Background()) + require.NoError(err) + + err = apiCli.Invoke("config/variable/upsert"). + JsonRequestBody([]domain.UpsertVariableRequest{{ + Name: "varB", + Description: "test", + Type: "SECRET", + Value: strconv.Itoa(fake.It[int]()), + }}). + Do(context.Background()) + require.NoError(err) + + clientA1 := newClusterClientWith( + "A", + "10.2.9.1", + configWithVars{}, + []byte(defaultRemoteConfigsWithVariables), + logger) + go func() { + handler := cluster.NewEventHandler() + err := clientA1.Run(context.Background(), handler) + require.NoError(err) //nolint:testifylint + }() + time.Sleep(3 * time.Second) + + time.Sleep(5 * time.Second) +}