Implement acceptance test

This commit is contained in:
d1slike committed 2024-05-14 19:21:19 +03:00
1 parent 3b34468842
commit 125013ba0a
6 files changed
+216 -17

No files matched your search

+2 -2
View File
@@ -3,10 +3,10 @@ configServiceAddress:
port: 9001
grpcOuterAddress:
ip: 127.0.0.1
port: 9001
port: 9002
grpcInnerAddress:
ip: 0.0.0.0
port: 9001
port: 9002
moduleName: isp-config-service
infraServerPort: 9006
+1
View File
@@ -10,6 +10,7 @@ require (
github.com/prometheus/client_golang v1.19.0
github.com/rqlite/gorqlite v0.0.0-20240227123050-397b03f02418
github.com/rqlite/rqlite/v8 v8.23.4
github.com/stretchr/testify v1.9.0
github.com/tidwall/gjson v1.17.1
github.com/txix-open/etp/v3 v3.1.0
github.com/txix-open/isp-kit v1.31.2
+11 -11
View File
@@ -59,47 +59,47 @@ func HttpHandler(etpSrv *etp.Server) http.Handler {
func endpointDescriptors(c Controllers) []cluster.EndpointDescriptor {
return []cluster.EndpointDescriptor{{
Path: "module/get_modules_info",
Path: "config/module/get_modules_info",
Inner: true,
Handler: c.ModuleApi.GetModulesAggregatedInfo,
}, {
Path: "module/delete_module",
Path: "config/module/delete_module",
Inner: true,
Handler: c.ModuleApi.DeleteModule,
}, {
Path: "config/get_active_config_by_module_name",
Path: "config/config/get_active_config_by_module_name",
Inner: true,
Handler: c.ConfigApi.GetActiveConfigByModuleName,
}, {
Path: "config/get_configs_by_module_id",
Path: "config/config/get_configs_by_module_id",
Inner: true,
Handler: c.ConfigApi.GetConfigsByModuleId,
}, {
Path: "config/create_update_config",
Path: "config/config/create_update_config",
Inner: true,
Handler: c.ConfigApi.CreateUpdateConfig,
}, {
Path: "config/get_config_by_id",
Path: "config/config/get_config_by_id",
Inner: true,
Handler: c.ConfigApi.GetConfigById,
}, {
Path: "config/mark_config_as_active",
Path: "config/config/mark_config_as_active",
Inner: true,
Handler: c.ConfigApi.MarkConfigAsActive,
}, {
Path: "config/delete_config",
Path: "config/config/delete_config",
Inner: true,
Handler: c.ConfigApi.DeleteConfigs,
}, {
Path: "config/get_all_version",
Path: "config/config/get_all_version",
Inner: true,
Handler: c.ConfigHistoryApi.GetAllVersion,
}, {
Path: "config/delete_version",
Path: "config/config/delete_version",
Inner: true,
Handler: c.ConfigHistoryApi.DeleteConfigVersion,
}, {
Path: "schema/get_by_module_id",
Path: "config/schema/get_by_module_id",
Inner: true,
Handler: c.ConfigSchemaApi.SchemaByModuleId,
}}
+1 -4
View File
@@ -427,13 +427,10 @@ type BuildInfo struct {
// ParseFlags parses the command line, and returns the configuration.
func ParseFlags(name, desc string, build *BuildInfo) (*Config, error) {
if flag.Parsed() {
return nil, fmt.Errorf("command-line flags already parsed")
}
config := &Config{}
showVersion := false
fs := flag.NewFlagSet(name, flag.ExitOnError)
fs := flag.NewFlagSet(name, flag.ContinueOnError)
fs.StringVar(&config.NodeID, "node-id", "", "Unique ID for node. If not set, set to advertised Raft address")
fs.StringVar(&config.HTTPAddr, HTTPAddrFlag, "localhost:4001", "HTTP server bind address. To enable HTTPS, set X.509 certificate and key")
+1
View File
@@ -174,6 +174,7 @@ func (s Service) NotifyRoutingChanged(ctx context.Context) error {
}
func (s Service) notifyRoutingChanged(ctx context.Context, event string, conns []*etp.Conn) error {
ctx = db.NoneConsistency().ToContext(ctx)
backends, err := s.backendRepo.All(ctx)
if err != nil {
return errors.WithMessage(err, "get all backends")
+200
View File
@@ -0,0 +1,200 @@
package tests_test
import (
"context"
"crypto/rand"
"encoding/hex"
"os"
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/txix-open/isp-kit/bootstrap"
"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/log"
"github.com/txix-open/isp-kit/rc/schema"
"isp-config-service/conf"
"isp-config-service/domain"
"isp-config-service/service/startup"
)
type clusterEventHandler struct {
receivedConfigs [][]byte
receivedHosts [][]string
receivedRoutes []cluster.RoutingConfig
lock sync.Locker
}
func newClusterEventHandler() *clusterEventHandler {
return &clusterEventHandler{
lock: &sync.Mutex{},
}
}
func (c *clusterEventHandler) ReceiveConfig(ctx context.Context, remoteConfig []byte) error {
c.lock.Lock()
defer c.lock.Unlock()
c.receivedConfigs = append(c.receivedConfigs, remoteConfig)
return nil
}
func (c *clusterEventHandler) Upgrade(hosts []string) {
c.lock.Lock()
defer c.lock.Unlock()
c.receivedHosts = append(c.receivedHosts, hosts)
}
func (c *clusterEventHandler) ReceiveRoutes(ctx context.Context, routes cluster.RoutingConfig) error {
c.lock.Lock()
defer c.lock.Unlock()
c.receivedRoutes = append(c.receivedRoutes, routes)
return nil
}
func TestAcceptance(t *testing.T) {
t.Parallel()
require := require.New(t)
err := os.Setenv("APP_CONFIG_PATH", "../conf/config.yml")
require.NoError(err)
err = os.Setenv("DefaultRemoteConfigPath", "../conf/default_remote_config.json")
require.NoError(err)
boot := bootstrap.New("1.0.0", conf.Remote{}, nil)
boot.App.Logger().SetLevel(log.DebugLevel)
boot.MigrationsDir = "../migrations"
dataPath := dataDir(t)
boot.App.Config().Set("rqlite.DataPath", dataPath)
logger := boot.App.Logger()
startup := startup.New(boot)
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)
clientA1 := newClusterClient("A", "10.2.9.1", logger)
go func() {
handler := cluster.NewEventHandler()
err := clientA1.Run(context.Background(), handler)
require.NoError(err)
}()
clientA2 := newClusterClient("A", "10.2.9.2", logger)
clientA2Ctx, cancelClient2 := context.WithCancel(context.Background())
go func() {
handler := cluster.NewEventHandler()
err := clientA2.Run(clientA2Ctx, handler)
require.NoError(err)
}()
time.Sleep(2 * time.Second)
clientB := newClusterClient("B", "10.2.9.2", logger)
eventHandler := newClusterEventHandler()
go func() {
handler := cluster.NewEventHandler().
RemoteConfigReceiver(eventHandler).
RequireModule("A", eventHandler).
RoutesReceiver(eventHandler)
err := clientB.Run(context.Background(), handler)
require.NoError(err)
}()
time.Sleep(2 * time.Second)
apiCli, err := client.Default()
require.NoError(err)
apiCli.Upgrade([]string{"127.0.0.1:9002"})
activeConfig := domain.Config{}
err = apiCli.Invoke("config/config/get_active_config_by_module_name").
JsonRequestBody(domain.GetByModuleNameRequest{ModuleName: "B"}).
JsonResponseBody(&activeConfig).
Do(context.Background())
require.NoError(err)
updateConfigReq := domain.CreateUpdateConfigRequest{
Id: activeConfig.Id,
Name: activeConfig.Name,
ModuleId: activeConfig.ModuleId,
Version: activeConfig.Version,
Data: []byte(`{"someString": "value"}`),
}
err = apiCli.Invoke("config/config/create_update_config").
JsonRequestBody(updateConfigReq).
Do(context.Background())
require.NoError(err)
cancelClient2()
err = clientA2.Close()
require.NoError(err)
time.Sleep(2 * time.Second)
require.Len(eventHandler.receivedConfigs, 2)
require.EqualValues([]byte("{}"), eventHandler.receivedConfigs[0])
require.Len(eventHandler.receivedHosts, 2)
require.EqualValues([]string{"10.2.9.1:9999", "10.2.9.2:9999"}, eventHandler.receivedHosts[0])
require.EqualValues([]string{"10.2.9.1:9999"}, eventHandler.receivedHosts[1])
require.Len(eventHandler.receivedRoutes, 3)
statusResponse := make([]domain.ModuleInfo, 0)
err = apiCli.Invoke("config/module/get_modules_info").
JsonResponseBody(&statusResponse).
Do(context.Background())
require.NoError(err)
require.Len(statusResponse, 3)
require.EqualValues("A", statusResponse[0].Name)
require.Len(statusResponse[0].Status, 1)
require.EqualValues("B", statusResponse[1].Name)
require.Len(statusResponse[1].Status, 1)
require.NotEmpty(statusResponse[1].ConfigSchema)
}
func dataDir(t *testing.T) string {
t.Helper()
data := make([]byte, 6)
_, _ = rand.Read(data)
dir := hex.EncodeToString(data)
t.Cleanup(func() {
_ = os.RemoveAll(dir)
})
return dir
}
type testModuleRemoteConfig struct {
SomeString string `validate:"required"`
}
func newClusterClient(
moduleName string,
host string,
logger log.Logger,
) *cluster.Client {
schema := schema.NewGenerator().Generate(testModuleRemoteConfig{})
schemaData, err := json.Marshal(schema)
if err != nil {
panic(err)
}
return cluster.NewClient(cluster.ModuleInfo{
ModuleName: moduleName,
ModuleVersion: "1.0.0",
LibVersion: "1.0.0",
GrpcOuterAddress: cluster.AddressConfiguration{
IP: host,
Port: "9999",
},
Endpoints: nil,
}, cluster.ConfigData{
Version: "1.0.0",
Schema: schemaData,
Config: []byte(`{}`),
}, []string{"127.0.0.1:9001"}, logger)
}