From aa8004593dc3ad22dac2313ea96a5e7f5b5d4f9d Mon Sep 17 00:00:00 2001 From: Konovalov Maxim <43151027+KaymeKaydex@users.noreply.github.com> Date: Thu, 25 Apr 2024 19:08:24 +0300 Subject: [PATCH] feature/tests 1 (#19) * add more logs for provider * refactor static provider/integration tests & add tests & fix makefile tests * add tests for viper provider close --------- Co-authored-by: maxim-konovalov --- .gitignore | 5 ++- Makefile | 2 +- providers.go | 1 + providers/static/provider.go | 4 -- providers/static/provider_test.go | 65 +++++++++++++++++++++++++++ providers/viper/provider_test.go | 4 ++ providers_test.go | 51 +++++++++++++++++++++ tests/integration/integration_test.go | 8 +++- vshard_test.go | 2 +- 9 files changed, 133 insertions(+), 9 deletions(-) create mode 100644 providers_test.go diff --git a/.gitignore b/.gitignore index 1139c2e..0c753c7 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,7 @@ *.log # System -*.pid \ No newline at end of file +*.pid + +# Go output +*.out \ No newline at end of file diff --git a/Makefile b/Makefile index 9036322..412d7f4 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ TEST_TIMEOUT?=20s test: - go test $(go list ./... | grep -v /tests/) -parallel=10 -timeout=$(TEST_TIMEOUT) -coverprofile=coverage.out + go test ./... -parallel=10 -timeout=$(TEST_TIMEOUT) -coverprofile=coverage.out test/integration: @$(MAKE) -C ./tests/integration test diff --git a/providers.go b/providers.go index 3f30d4d..3cba0ce 100644 --- a/providers.go +++ b/providers.go @@ -43,6 +43,7 @@ func (e *StdoutLogger) Warn(ctx context.Context, msg string) { // Metrics +// MetricsProvider is an interface for passing library metrics to your prometheus/graphite and other metrics type MetricsProvider interface { CronDiscoveryEvent(ok bool, duration time.Duration, reason string) RetryOnCall(reason string) diff --git a/providers/static/provider.go b/providers/static/provider.go index 6c7ea8b..c6e0961 100644 --- a/providers/static/provider.go +++ b/providers/static/provider.go @@ -26,10 +26,6 @@ func NewProvider(rs map[vshardrouter.ReplicasetInfo][]vshardrouter.InstanceInfo) } func (p *Provider) Validate() error { - if len(p.rs) < 1 { - return fmt.Errorf("replicasets are empty") - } - for rs := range p.rs { // check replicaset name if rs.Name == "" { diff --git a/providers/static/provider_test.go b/providers/static/provider_test.go index f6006b7..85a2b34 100644 --- a/providers/static/provider_test.go +++ b/providers/static/provider_test.go @@ -1,8 +1,10 @@ package static import ( + "fmt" "testing" + "github.com/google/uuid" "github.com/stretchr/testify/require" vshardrouter "github.com/KaymeKaydex/go-vshard-router" @@ -14,6 +16,12 @@ func TestNewProvider(t *testing.T) { }{ {nil}, {make(map[vshardrouter.ReplicasetInfo][]vshardrouter.InstanceInfo)}, + {map[vshardrouter.ReplicasetInfo][]vshardrouter.InstanceInfo{ + vshardrouter.ReplicasetInfo{}: { + vshardrouter.InstanceInfo{}, + vshardrouter.InstanceInfo{}, + }, + }}, } for _, tc := range tCases { @@ -22,8 +30,65 @@ func TestNewProvider(t *testing.T) { require.Panics(t, func() { NewProvider(tc.Source) }) + return } + require.NotPanics(t, func() { + provider := NewProvider(tc.Source) + require.NotNil(t, provider) + }) + + }) + } +} + +func TestProvider_Validate(t *testing.T) { + tCases := []struct { + Name string + Source map[vshardrouter.ReplicasetInfo][]vshardrouter.InstanceInfo + IsErr bool + }{ + { + Name: "empty name", + Source: map[vshardrouter.ReplicasetInfo][]vshardrouter.InstanceInfo{ + vshardrouter.ReplicasetInfo{}: { + vshardrouter.InstanceInfo{}, + vshardrouter.InstanceInfo{}, + }, + }, + IsErr: true, + }, + { + Name: "no uuid", + Source: map[vshardrouter.ReplicasetInfo][]vshardrouter.InstanceInfo{ + vshardrouter.ReplicasetInfo{Name: "rs_1"}: { + vshardrouter.InstanceInfo{}, + vshardrouter.InstanceInfo{}, + }, + }, + IsErr: true, + }, + { + Name: "valid", + Source: map[vshardrouter.ReplicasetInfo][]vshardrouter.InstanceInfo{ + vshardrouter.ReplicasetInfo{Name: "rs_1", UUID: uuid.New()}: { + vshardrouter.InstanceInfo{}, + vshardrouter.InstanceInfo{}, + }, + }, + IsErr: false, + }, + } + + for _, tc := range tCases { + t.Run(fmt.Sprintf("is err: %v", tc.IsErr), func(t *testing.T) { + provider := NewProvider(tc.Source) + if tc.IsErr { + require.Error(t, provider.Validate()) + return + } + + require.NoError(t, provider.Validate()) }) } } diff --git a/providers/viper/provider_test.go b/providers/viper/provider_test.go index 9a252f8..ace0cac 100644 --- a/providers/viper/provider_test.go +++ b/providers/viper/provider_test.go @@ -9,6 +9,10 @@ import ( vprovider "github.com/KaymeKaydex/go-vshard-router/providers/viper" ) +func TestProvider_Close(t *testing.T) { + require.NotPanics(t, (&vprovider.Provider{}).Close) +} + func TestNewProviderNilPanic(t *testing.T) { require.Panics(t, func() { vprovider.NewProvider(nil) diff --git a/providers_test.go b/providers_test.go new file mode 100644 index 0000000..22b1c1a --- /dev/null +++ b/providers_test.go @@ -0,0 +1,51 @@ +package vshard_router_test + +import ( + "context" + "testing" + "time" + + "github.com/stretchr/testify/require" + + vshardrouter "github.com/KaymeKaydex/go-vshard-router" +) + +var ( + emptyMetrics = vshardrouter.EmptyMetrics{} + stdoutLogger = vshardrouter.StdoutLogger{} +) + +func TestEmptyMetrics_RetryOnCall(t *testing.T) { + require.NotPanics(t, func() { + emptyMetrics.RetryOnCall("") + }) +} + +func TestEmptyMetrics_RequestDuration(t *testing.T) { + require.NotPanics(t, func() { + emptyMetrics.RequestDuration(time.Second, false, false) + }) +} + +func TestEmptyMetrics_CronDiscoveryEvent(t *testing.T) { + require.NotPanics(t, func() { + emptyMetrics.CronDiscoveryEvent(false, time.Second, "") + }) +} + +func TestStdoutLogger(t *testing.T) { + ctx := context.TODO() + + require.NotPanics(t, func() { + stdoutLogger.Error(ctx, "") + }) + require.NotPanics(t, func() { + stdoutLogger.Info(ctx, "") + }) + require.NotPanics(t, func() { + stdoutLogger.Warn(ctx, "") + }) + require.NotPanics(t, func() { + stdoutLogger.Debug(ctx, "") + }) +} diff --git a/tests/integration/integration_test.go b/tests/integration/integration_test.go index 3211f6f..72124af 100644 --- a/tests/integration/integration_test.go +++ b/tests/integration/integration_test.go @@ -1,3 +1,6 @@ +//go:build integration +// +build integration + package integration import ( @@ -8,6 +11,7 @@ import ( "time" vshard_router "github.com/KaymeKaydex/go-vshard-router" + "github.com/KaymeKaydex/go-vshard-router/providers/static" "github.com/google/uuid" "github.com/stretchr/testify/assert" @@ -33,14 +37,14 @@ func TestReplicasetCall(t *testing.T) { User: "test", Password: "test", TotalBucketCount: 300, - Replicasets: map[vshard_router.ReplicasetInfo][]vshard_router.InstanceInfo{ + TopologyProvider: static.NewProvider(map[vshard_router.ReplicasetInfo][]vshard_router.InstanceInfo{ { Name: "replicaset-1", UUID: uuid.New(), }: { {Addr: server, UUID: uuid.New()}, }, - }, + }), }) if err != nil { log.Fatalf("create router err: %s", err.Error()) diff --git a/vshard_test.go b/vshard_test.go index 1e37dc4..a8b526a 100644 --- a/vshard_test.go +++ b/vshard_test.go @@ -26,7 +26,7 @@ func TestNewRouter_InvalidReplicasetUUID(t *testing.T) { TopologyProvider: static.NewProvider(map[vshard_router.ReplicasetInfo][]vshard_router.InstanceInfo{ vshard_router.ReplicasetInfo{ Name: "123", - }: []vshard_router.InstanceInfo{ + }: { {Addr: "first.internal:1212"}, }, }),