@@ -37,14 +37,14 @@ import (
37
37
"testing"
38
38
"time"
39
39
40
- "google.golang.org/protobuf/encoding/protojson"
41
-
42
40
"github.com/stretchr/testify/require"
41
+ "google.golang.org/protobuf/encoding/protojson"
43
42
44
43
"vitess.io/vitess/go/vt/log"
44
+ "vitess.io/vitess/go/vt/proto/vtctldata"
45
+
45
46
binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata"
46
47
vschemapb "vitess.io/vitess/go/vt/proto/vschema"
47
- "vitess.io/vitess/go/vt/proto/vtctldata"
48
48
)
49
49
50
50
type tenantMigrationStatus int
71
71
chNotSetup , chNotCreated , chInProgress , chSwitched , chCompleted chan int64
72
72
// counters to keep track of the number of tenants in each state
73
73
numSetup , numInProgress , numSwitched , numCompleted atomic.Int64
74
+
75
+ emptyKeyspaceRoutingRules = & vschemapb.KeyspaceRoutingRules {}
74
76
)
75
77
76
78
// multiTenantMigration manages the migration of multiple tenants to a single target keyspace.
@@ -193,6 +195,7 @@ func TestMultiTenantSimple(t *testing.T) {
193
195
}
194
196
195
197
require .Zero (t , len (getKeyspaceRoutingRules (t , vc ).Rules ))
198
+
196
199
mt .Create ()
197
200
confirmKeyspacesRoutedTo (t , sourceKeyspace , "s1" , "t1" , nil )
198
201
validateKeyspaceRoutingRules (t , vc , initialRules )
@@ -223,6 +226,41 @@ func TestMultiTenantSimple(t *testing.T) {
223
226
log .Infof ("Migration completed, total rows in target: %d" , actualRowsInserted )
224
227
require .Equal (t , lastIndex , int64 (actualRowsInserted ))
225
228
229
+ t .Run ("Test ApplyKeyspaceRoutingRules" , func (t * testing.T ) {
230
+ // First set of rules
231
+ applyKeyspaceRoutingRules (t , initialRules )
232
+
233
+ updatedRules := & vschemapb.KeyspaceRoutingRules {
234
+ Rules : []* vschemapb.KeyspaceRoutingRule {
235
+ {FromKeyspace : "s1" , ToKeyspace : "mt" },
236
+ {FromKeyspace : "s1@rdonly" , ToKeyspace : "mt" },
237
+ {FromKeyspace : "s1@replica" , ToKeyspace : "mt" },
238
+ },
239
+ }
240
+ // Update the rules
241
+ applyKeyspaceRoutingRules (t , updatedRules )
242
+ // Update with the same rules
243
+ applyKeyspaceRoutingRules (t , updatedRules )
244
+ // Remove the rules
245
+ applyKeyspaceRoutingRules (t , emptyKeyspaceRoutingRules )
246
+ // Test setting empty rules again
247
+ applyKeyspaceRoutingRules (t , emptyKeyspaceRoutingRules )
248
+ })
249
+ }
250
+
251
+ func applyKeyspaceRoutingRules (t * testing.T , newRules * vschemapb.KeyspaceRoutingRules ) {
252
+ var rulesJSON []byte
253
+ var err error
254
+ require .NotNil (t , newRules )
255
+ rulesJSON , err = json .Marshal (newRules )
256
+ require .NoError (t , err )
257
+ output , err := vc .VtctldClient .ExecuteCommandWithOutput ("ApplyKeyspaceRoutingRules" , "--rules" , string (rulesJSON ))
258
+ require .NoError (t , err )
259
+
260
+ response := & vtctldata.ApplyKeyspaceRoutingRulesResponse {}
261
+ err = json .Unmarshal ([]byte (output ), response )
262
+ require .NoError (t , err )
263
+ require .ElementsMatch (t , newRules .Rules , response .GetKeyspaceRoutingRules ().Rules )
226
264
}
227
265
228
266
func confirmOnlyReadsSwitched (t * testing.T ) {
@@ -251,10 +289,10 @@ func confirmOnlyWritesSwitched(t *testing.T) {
251
289
validateKeyspaceRoutingRules (t , vc , rules )
252
290
}
253
291
254
- // TestMultiTenantSimpleSharded tests a single tenant migration to a sharded target. The aim is to test
292
+ // TestMultiTenantSharded tests a single tenant migration to a sharded target. The aim is to test
255
293
// the specification of the target shards in all the MoveTables subcommands, including creating only one stream
256
294
// for a tenant on the shard to which this tenant id will be routed, using the specified Vindex.
257
- func TestMultiTenantSimpleSharded (t * testing.T ) {
295
+ func TestMultiTenantSharded (t * testing.T ) {
258
296
setSidecarDBName ("_vt" )
259
297
// Don't create RDONLY tablets to reduce number of tablets created to reduce resource requirements for the test.
260
298
origDefaultRdonly := defaultRdonly
0 commit comments