Skip to content

Commit

Permalink
Add tests and correct panics for invalid topology config from viper
Browse files Browse the repository at this point in the history
  • Loading branch information
maksim.konovalov committed Jan 23, 2025
1 parent 91e5d7a commit f01c756
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ TESTS:
- Added etcd v2 provider tests.
- Add test for degraded cluster (TestDegradedCluster).
- Add more tests for etcd v3 viper provider.
- Add tests and correct panics for invalid topology config from viper.

## v2.0.1

Expand Down
13 changes: 7 additions & 6 deletions providers/viper/moonlibs/convert.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package moonlibs

import (
"fmt"
"log"

"github.com/google/uuid"
vshardrouter "github.com/tarantool/go-vshard-router/v2"
)

func (cfg *Config) Convert() map[vshardrouter.ReplicasetInfo][]vshardrouter.InstanceInfo {
func (cfg *Config) Convert() (map[vshardrouter.ReplicasetInfo][]vshardrouter.InstanceInfo, error) {
if cfg.Topology.Instances == nil {
panic("instances is nil")
return nil, fmt.Errorf("no topology instances found")
}

if cfg.Topology.Clusters == nil {
panic("clusters is nil")
return nil, fmt.Errorf("no topology clusters found")
}

// prepare vshard router config
Expand All @@ -22,7 +23,7 @@ func (cfg *Config) Convert() map[vshardrouter.ReplicasetInfo][]vshardrouter.Inst
for rsName, rs := range cfg.Topology.Clusters {
rsUUID, err := uuid.Parse(rs.ReplicasetUUID)
if err != nil {
panic("Can't parse replicaset uuid: %s")
return nil, fmt.Errorf("invalid topology replicaset UUID: %s", rs.ReplicasetUUID)
}

rsInstances := make([]vshardrouter.InstanceInfo, 0)
Expand All @@ -36,7 +37,7 @@ func (cfg *Config) Convert() map[vshardrouter.ReplicasetInfo][]vshardrouter.Inst
if err != nil {
log.Printf("Can't parse replicaset uuid: %s", err)

panic(err)
return nil, fmt.Errorf("invalid topology instance UUID: %s", instInfo.Box.InstanceUUID)
}

rsInstances = append(rsInstances, vshardrouter.InstanceInfo{
Expand All @@ -51,5 +52,5 @@ func (cfg *Config) Convert() map[vshardrouter.ReplicasetInfo][]vshardrouter.Inst
}] = rsInstances
}

return vshardRouterTopology
return vshardRouterTopology, nil
}
7 changes: 5 additions & 2 deletions providers/viper/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const (
)

type Convertable interface {
Convert() map[vshardrouter.ReplicasetInfo][]vshardrouter.InstanceInfo
Convert() (map[vshardrouter.ReplicasetInfo][]vshardrouter.InstanceInfo, error)
}

func NewProvider(ctx context.Context, v *srcviper.Viper, cfgType ConfigType) *Provider {
Expand All @@ -53,7 +53,10 @@ func NewProvider(ctx context.Context, v *srcviper.Viper, cfgType ConfigType) *Pr
panic(err)
}

resultMap := cfg.Convert()
resultMap, err := cfg.Convert()
if err != nil {
panic(err)
}

return &Provider{ctx: ctx, v: v, rs: resultMap}
}
Expand Down
49 changes: 41 additions & 8 deletions providers/viper/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,50 @@ func TestNewProvider_ETCD3(t *testing.T) {
require.NoError(t, err)
require.NotEmpty(t, getResponse.Kvs[0].Value)

etcdViper := viper.New()
err = etcdViper.AddRemoteProvider("etcd3", "http://127.0.0.1:2379", key)
require.NoError(t, err)
t.Run("ok reads config", func(t *testing.T) {
etcdViper := viper.New()
err = etcdViper.AddRemoteProvider("etcd3", "http://127.0.0.1:2379", key)
require.NoError(t, err)

etcdViper.SetConfigType("yaml")
err = etcdViper.ReadRemoteConfig()
require.NoError(t, err)
etcdViper.SetConfigType("yaml")
err = etcdViper.ReadRemoteConfig()
require.NoError(t, err)

provider := vprovider.NewProvider(ctx, etcdViper, vprovider.ConfigTypeTarantool3)
provider := vprovider.NewProvider(ctx, etcdViper, vprovider.ConfigTypeTarantool3)
anyProviderValidation(t, provider)
})

anyProviderValidation(t, provider)
t.Run("invalid path", func(t *testing.T) {
etcdViper := viper.New()

err = etcdViper.AddRemoteProvider("etcd3", "http://127.0.0.1:2379", "/some-invalid-path")
require.NoError(t, err)

etcdViper.SetConfigType("yaml")

err = etcdViper.ReadRemoteConfig()
require.Error(t, err, "path not found error")
})

t.Run("invalid config panics", func(t *testing.T) {
etcdViper := viper.New()

emptyPath := "/some-empty-path"

_, err := kv.Put(ctx, emptyPath, "")
require.NoError(t, err)

err = etcdViper.AddRemoteProvider("etcd3", "http://127.0.0.1:2379", "/some-empty-path")
require.NoError(t, err)

etcdViper.SetConfigType("yaml")
err = etcdViper.ReadRemoteConfig()
require.NoError(t, err)

require.Panics(t, func() {
vprovider.NewProvider(ctx, etcdViper, vprovider.ConfigTypeTarantool3)
})
})
}

func anyProviderValidation(t testing.TB, provider *vprovider.Provider) {
Expand Down
14 changes: 12 additions & 2 deletions providers/viper/tarantool3/convert.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
package tarantool3

import (
"fmt"

vshardrouter "github.com/tarantool/go-vshard-router/v2"
)

func (cfg *Config) Convert() map[vshardrouter.ReplicasetInfo][]vshardrouter.InstanceInfo {
func (cfg *Config) Convert() (map[vshardrouter.ReplicasetInfo][]vshardrouter.InstanceInfo, error) {
if cfg.Groups.Storages == nil {
return nil, fmt.Errorf("cant get groups storage from etcd")
}

if cfg.Groups.Storages.Replicasets == nil {
return nil, fmt.Errorf("cant get storage replicasets from etcd")
}

m := make(map[vshardrouter.ReplicasetInfo][]vshardrouter.InstanceInfo)

for rsName, rs := range cfg.Groups.Storages.Replicasets {
Expand All @@ -21,5 +31,5 @@ func (cfg *Config) Convert() map[vshardrouter.ReplicasetInfo][]vshardrouter.Inst
m[rsInfo] = instances
}

return m
return m, nil
}

0 comments on commit f01c756

Please sign in to comment.