vitess.io/vitess@v0.16.2/go/vt/topotools/routing_rules_test.go (about) 1 /* 2 Copyright 2021 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package topotools 18 19 import ( 20 "context" 21 "errors" 22 "testing" 23 24 "github.com/stretchr/testify/assert" 25 "github.com/stretchr/testify/require" 26 27 "vitess.io/vitess/go/vt/topo/memorytopo" 28 ) 29 30 func TestRoutingRulesRoundTrip(t *testing.T) { 31 ctx := context.Background() 32 ts := memorytopo.NewServer("zone1") 33 34 rules := map[string][]string{ 35 "t1": {"t2", "t3"}, 36 "t4": {"t5"}, 37 } 38 39 err := SaveRoutingRules(ctx, ts, rules) 40 require.NoError(t, err, "could not save routing rules to topo %v", rules) 41 42 roundtripRules, err := GetRoutingRules(ctx, ts) 43 require.NoError(t, err, "could not fetch routing rules from topo") 44 45 assert.Equal(t, rules, roundtripRules) 46 } 47 48 func TestRoutingRulesErrors(t *testing.T) { 49 ctx := context.Background() 50 ts, factory := memorytopo.NewServerAndFactory("zone1") 51 factory.SetError(errors.New("topo failure for testing")) 52 53 t.Run("GetRoutingRules error", func(t *testing.T) { 54 55 rules, err := GetRoutingRules(ctx, ts) 56 assert.Error(t, err, "expected error from GetRoutingRules, got rules=%v", rules) 57 }) 58 59 t.Run("SaveRoutingRules error", func(t *testing.T) { 60 rules := map[string][]string{ 61 "t1": {"t2", "t3"}, 62 "t4": {"t5"}, 63 } 64 65 err := SaveRoutingRules(ctx, ts, rules) 66 assert.Error(t, err, "expected error from GetRoutingRules, got rules=%v", rules) 67 }) 68 } 69 70 func TestShardRoutingRulesRoundTrip(t *testing.T) { 71 ctx := context.Background() 72 ts := memorytopo.NewServer("zone1") 73 74 srr := map[string]string{ 75 "ks1.shard1": "ks2", 76 "ks3.shard2": "ks4", 77 } 78 79 err := SaveShardRoutingRules(ctx, ts, srr) 80 require.NoError(t, err, "could not save shard routing rules to topo %v", err) 81 82 roundtripRules, err := GetShardRoutingRules(ctx, ts) 83 require.NoError(t, err, "could not fetch shard routing rules from topo: %v", err) 84 85 assert.Equal(t, srr, roundtripRules) 86 }