MSP-5579 add test for autodiscovery functionality
This commit is contained in:
1 parent
1482636ad8
commit
de85ffa726
3 files changed
+125
-3
No files matched your search
@@ -1,3 +1,4 @@
|
||||
// nolint:ireturn
|
||||
package tests_test
|
||||
|
||||
import (
|
||||
@@ -125,7 +126,7 @@ func newClusterClient(
|
||||
logger log.Logger,
|
||||
) *cluster.Client {
|
||||
t.Helper()
|
||||
return newClusterClientWith(t, moduleName, host, testModuleRemoteConfig{}, []byte("{}"), logger)
|
||||
return newClusterClientWith(t, moduleName, host, testModuleRemoteConfig{}, []byte("{}"), nil, logger)
|
||||
}
|
||||
|
||||
func newClusterClientWith(
|
||||
@@ -134,6 +135,7 @@ func newClusterClientWith(
|
||||
host string,
|
||||
config any,
|
||||
defaultRemoteConfig []byte,
|
||||
metricsAd *cluster.MetricsAutodiscovery,
|
||||
logger log.Logger,
|
||||
) *cluster.Client {
|
||||
t.Helper()
|
||||
@@ -150,7 +152,8 @@ func newClusterClientWith(
|
||||
IP: host,
|
||||
Port: "9999",
|
||||
},
|
||||
Endpoints: nil,
|
||||
Endpoints: nil,
|
||||
MetricsAutodiscovery: metricsAd,
|
||||
}, cluster.ConfigData{
|
||||
Version: "1.0.0",
|
||||
Schema: schemaData,
|
||||
@@ -162,7 +165,6 @@ func newClusterClientWith(
|
||||
return cli
|
||||
}
|
||||
|
||||
//nolint:ireturn
|
||||
func setupTest(t *testing.T) log.Logger {
|
||||
t.Helper()
|
||||
require := require.New(t)
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
package tests_test
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"strconv"
|
||||
"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/http/httpclix"
|
||||
"github.com/txix-open/isp-kit/log"
|
||||
"github.com/txix-open/isp-kit/test/fake"
|
||||
"isp-config-service/conf"
|
||||
"isp-config-service/domain"
|
||||
"isp-config-service/service/startup"
|
||||
)
|
||||
|
||||
// nolint:paralleltest
|
||||
func TestMetricsAutodiscovery(t *testing.T) {
|
||||
require, logger := setupTestWithAppRun(t)
|
||||
|
||||
cli := httpclix.DefaultWithBalancer([]string{"127.0.0.1:9006"})
|
||||
resp := make(domain.AutodiscoveryResponse, 0)
|
||||
err := cli.Get("/internal/metrics/autodiscovery").
|
||||
JsonResponseBody(&resp).
|
||||
StatusCodeToError().
|
||||
DoWithoutResponse(t.Context())
|
||||
require.NoError(err)
|
||||
|
||||
configTarget := domain.PrometheusTargets{
|
||||
Targets: []string{"isp-config-service:9006"},
|
||||
Labels: map[string]string{
|
||||
"app": "isp-config-service",
|
||||
"__metrics_path__": "/internal/metrics",
|
||||
},
|
||||
}
|
||||
require.Equal(domain.AutodiscoveryResponse{configTarget}, resp)
|
||||
|
||||
const (
|
||||
iterCount = 8
|
||||
chanceToMetricsAd = 0.5
|
||||
)
|
||||
expected := make(domain.AutodiscoveryResponse, 0, iterCount+1)
|
||||
for i := range iterCount {
|
||||
metricsAd := generateMetricsAdWithChance(chanceToMetricsAd)
|
||||
if metricsAd != nil {
|
||||
expected = append(expected, domain.PrometheusTargets{
|
||||
Targets: []string{metricsAd.Address},
|
||||
Labels: metricsAd.Labels,
|
||||
})
|
||||
}
|
||||
clusterCli := newClusterClientWith(
|
||||
t,
|
||||
strconv.Itoa(i),
|
||||
"10.2.9.1",
|
||||
testModuleRemoteConfig{},
|
||||
[]byte("{}"),
|
||||
metricsAd,
|
||||
logger,
|
||||
)
|
||||
go func() {
|
||||
err := clusterCli.Run(t.Context(), cluster.NewEventHandler())
|
||||
require.NoError(err) // nolint:testifylint
|
||||
}()
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
expected = append(expected, configTarget)
|
||||
|
||||
resp = make(domain.AutodiscoveryResponse, 0)
|
||||
err = cli.Get("/internal/metrics/autodiscovery").
|
||||
JsonResponseBody(&resp).
|
||||
StatusCodeToError().
|
||||
DoWithoutResponse(t.Context())
|
||||
require.NoError(err)
|
||||
require.Equal(expected, resp)
|
||||
}
|
||||
|
||||
func generateMetricsAdWithChance(chance float64) *cluster.MetricsAutodiscovery {
|
||||
if rand.Float64() > chance { // nolint:gosec
|
||||
return nil
|
||||
}
|
||||
return fake.It[*cluster.MetricsAutodiscovery]()
|
||||
}
|
||||
|
||||
// nolint:ireturn
|
||||
func setupTestWithAppRun(t *testing.T) (*require.Assertions, 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(t.Context())
|
||||
require.NoError(err)
|
||||
|
||||
go func() {
|
||||
err = boot.App.Run()
|
||||
require.NoError(err) // nolint:testifylint
|
||||
}()
|
||||
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
return require, logger
|
||||
}
|
||||
@@ -69,6 +69,7 @@ func TestVariableAcceptance(t *testing.T) {
|
||||
"10.2.9.1",
|
||||
configWithVars{},
|
||||
[]byte(defaultRemoteConfigsWithVariables),
|
||||
nil,
|
||||
logger,
|
||||
)
|
||||
go func() {
|
||||
|
||||
Reference in new issue
Block a user