2017-05-06 02:21:12 +03:00
/ *
2019-10-21 18:25:43 +03:00
Copyright 2019 The Vitess Authors .
2017-05-06 02:21:12 +03:00
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
* /
2014-10-22 19:05:42 +04:00
package topo
import (
"reflect"
"testing"
2015-08-04 22:44:19 +03:00
2016-05-24 18:34:17 +03:00
"golang.org/x/net/context"
2018-02-27 22:58:59 +03:00
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
2014-10-22 19:05:42 +04:00
)
// This file tests the shard related object functionnalities.
2014-10-22 22:34:38 +04:00
func TestAddCells ( t * testing . T ) {
var cells [ ] string
2014-10-22 19:05:42 +04:00
// no restriction + no restriction -> no restrictions
2014-10-22 22:34:38 +04:00
cells = addCells ( cells , nil )
if cells != nil {
2014-10-22 19:05:42 +04:00
t . Fatalf ( "addCells(no restriction)+no restriction should be no restriction" )
}
// no restriction + cells -> no restrictions
2014-10-22 22:34:38 +04:00
cells = addCells ( cells , [ ] string { "c1" , "c2" } )
if cells != nil {
2014-10-22 19:05:42 +04:00
t . Fatalf ( "addCells(no restriction)+restriction should be no restriction" )
}
// cells + no restriction -> no restrictions
2014-10-22 22:34:38 +04:00
cells = [ ] string { "c1" , "c2" }
cells = addCells ( cells , nil )
if cells != nil {
2014-10-22 19:05:42 +04:00
t . Fatalf ( "addCells(restriction)+no restriction should be no restriction" )
}
// cells + cells -> union
2014-10-22 22:34:38 +04:00
cells = [ ] string { "c1" , "c2" }
cells = addCells ( cells , [ ] string { "c2" , "c3" } )
if ! reflect . DeepEqual ( cells , [ ] string { "c1" , "c2" , "c3" } ) {
t . Fatalf ( "addCells(restriction)+restriction failed: got %v" , cells )
}
}
2019-02-09 01:57:19 +03:00
func TestRemoveCellsFromList ( t * testing . T ) {
var cells [ ] string
allCells := [ ] string { "first" , "second" , "third" }
// remove from empty list should return allCells - what we remove
cells = removeCellsFromList ( [ ] string { "second" } , allCells )
if ! reflect . DeepEqual ( cells , [ ] string { "first" , "third" } ) {
t . Fatalf ( "removeCells(full)-second failed: got %v" , allCells )
}
// removethe next two cells, should return empty list
cells = removeCellsFromList ( cells , [ ] string { "first" , "third" } )
if len ( cells ) != 0 {
t . Fatalf ( "removeCells(full)-first-third is not empty: %v" , cells )
}
}
2014-10-22 22:34:38 +04:00
func TestRemoveCells ( t * testing . T ) {
var cells [ ] string
allCells := [ ] string { "first" , "second" , "third" }
// remove from empty list should return allCells - what we remove
var emptyResult bool
cells , emptyResult = removeCells ( cells , [ ] string { "second" } , allCells )
if emptyResult || ! reflect . DeepEqual ( cells , [ ] string { "first" , "third" } ) {
t . Fatalf ( "removeCells(full)-second failed: got %v" , cells )
}
// removethe next two cells, should return empty list
cells , emptyResult = removeCells ( cells , [ ] string { "first" , "third" } , allCells )
if ! emptyResult {
t . Fatalf ( "removeCells(full)-first-third is not empty: %v" , cells )
2014-10-22 19:05:42 +04:00
}
}
2016-05-24 18:34:17 +03:00
func lockedKeyspaceContext ( keyspace string ) context . Context {
ctx := context . Background ( )
return context . WithValue ( ctx , locksKey , & locksInfo {
info : map [ string ] * lockInfo {
2017-11-22 22:13:58 +03:00
// An empty entry is good enough for this.
keyspace : { } ,
2016-05-24 18:34:17 +03:00
} ,
} )
}
2014-10-22 19:05:42 +04:00
func TestUpdateSourceBlacklistedTables ( t * testing . T ) {
2019-02-09 01:57:19 +03:00
si := NewShardInfo ( "ks" , "sh" , & topodatapb . Shard { } , nil )
2014-10-22 19:05:42 +04:00
2016-05-24 18:34:17 +03:00
// check we enforce the keyspace lock
ctx := context . Background ( )
2016-05-24 19:11:36 +03:00
if err := si . UpdateSourceBlacklistedTables ( ctx , topodatapb . TabletType_RDONLY , nil , false , nil ) ; err == nil || err . Error ( ) != "keyspace ks is not locked (no locksInfo)" {
2016-05-24 18:34:17 +03:00
t . Fatalf ( "unlocked keyspace produced wrong error: %v" , err )
}
ctx = lockedKeyspaceContext ( "ks" )
2014-10-22 19:05:42 +04:00
// add one cell
2016-05-24 19:11:36 +03:00
if err := si . UpdateSourceBlacklistedTables ( ctx , topodatapb . TabletType_RDONLY , [ ] string { "first" } , false , [ ] string { "t1" , "t2" } ) ; err != nil || ! reflect . DeepEqual ( si . TabletControls , [ ] * topodatapb . Shard_TabletControl {
2015-12-16 02:38:45 +03:00
{
2015-11-12 12:13:51 +03:00
TabletType : topodatapb . TabletType_RDONLY ,
2014-10-22 19:05:42 +04:00
Cells : [ ] string { "first" } ,
BlacklistedTables : [ ] string { "t1" , "t2" } ,
} ,
} ) {
t . Fatalf ( "one cell add failed: %v" , si )
}
// remove that cell, going back
2016-05-24 19:11:36 +03:00
if err := si . UpdateSourceBlacklistedTables ( ctx , topodatapb . TabletType_RDONLY , [ ] string { "first" } , true , nil ) ; err != nil || len ( si . TabletControls ) != 0 {
2014-10-22 19:05:42 +04:00
t . Fatalf ( "going back should have remove the record: %v" , si )
}
// re-add a cell, then another with different table list to
// make sure it fails
2016-05-24 19:11:36 +03:00
if err := si . UpdateSourceBlacklistedTables ( ctx , topodatapb . TabletType_RDONLY , [ ] string { "first" } , false , [ ] string { "t1" , "t2" } ) ; err != nil {
2014-10-22 19:05:42 +04:00
t . Fatalf ( "one cell add failed: %v" , si )
}
2016-05-24 19:11:36 +03:00
if err := si . UpdateSourceBlacklistedTables ( ctx , topodatapb . TabletType_RDONLY , [ ] string { "second" } , false , [ ] string { "t2" , "t3" } ) ; err == nil || err . Error ( ) != "trying to use two different sets of blacklisted tables for shard ks/sh: [t1 t2] and [t2 t3]" {
2014-10-22 19:05:42 +04:00
t . Fatalf ( "different table list should fail: %v" , err )
}
// add another cell, see the list grow
2016-05-24 19:11:36 +03:00
if err := si . UpdateSourceBlacklistedTables ( ctx , topodatapb . TabletType_RDONLY , [ ] string { "second" } , false , [ ] string { "t1" , "t2" } ) ; err != nil || ! reflect . DeepEqual ( si . TabletControls , [ ] * topodatapb . Shard_TabletControl {
2015-12-16 02:38:45 +03:00
{
2015-11-12 12:13:51 +03:00
TabletType : topodatapb . TabletType_RDONLY ,
2014-10-22 19:05:42 +04:00
Cells : [ ] string { "first" , "second" } ,
BlacklistedTables : [ ] string { "t1" , "t2" } ,
} ,
} ) {
t . Fatalf ( "second cell add failed: %v" , si )
}
// add all cells, see the list grow to all
2019-02-09 01:57:19 +03:00
if err := si . UpdateSourceBlacklistedTables ( ctx , topodatapb . TabletType_RDONLY , [ ] string { "first" , "second" , "third" } , false , [ ] string { "t1" , "t2" } ) ; err != nil || ! reflect . DeepEqual ( si . TabletControls , [ ] * topodatapb . Shard_TabletControl {
2015-12-16 02:38:45 +03:00
{
2015-11-12 12:13:51 +03:00
TabletType : topodatapb . TabletType_RDONLY ,
2019-02-09 01:57:19 +03:00
Cells : [ ] string { "first" , "second" , "third" } ,
2014-10-22 19:05:42 +04:00
BlacklistedTables : [ ] string { "t1" , "t2" } ,
} ,
} ) {
t . Fatalf ( "all cells add failed: %v" , si )
}
// remove one cell from the full list
2016-05-24 19:11:36 +03:00
if err := si . UpdateSourceBlacklistedTables ( ctx , topodatapb . TabletType_RDONLY , [ ] string { "second" } , true , [ ] string { "t1" , "t2" } ) ; err != nil || ! reflect . DeepEqual ( si . TabletControls , [ ] * topodatapb . Shard_TabletControl {
2015-12-16 02:38:45 +03:00
{
2015-11-12 12:13:51 +03:00
TabletType : topodatapb . TabletType_RDONLY ,
2014-10-22 19:05:42 +04:00
Cells : [ ] string { "first" , "third" } ,
BlacklistedTables : [ ] string { "t1" , "t2" } ,
} ,
} ) {
t . Fatalf ( "one cell removal from all failed: %v" , si )
}
}