vitess.io/vitess@v0.16.2/go/vt/topo/helpers/copy_test.go (about)

     1  /*
     2  Copyright 2019 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 helpers
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/require"
    23  
    24  	"context"
    25  
    26  	"vitess.io/vitess/go/vt/topo"
    27  	"vitess.io/vitess/go/vt/topo/memorytopo"
    28  
    29  	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
    30  	vschemapb "vitess.io/vitess/go/vt/proto/vschema"
    31  )
    32  
    33  func createSetup(ctx context.Context, t *testing.T) (*topo.Server, *topo.Server) {
    34  	// Create a source and destination TS. They will have
    35  	// different generations, so we test using the Version for
    36  	// both works as expected.
    37  	fromTS := memorytopo.NewServer("test_cell")
    38  	toTS := memorytopo.NewServer("test_cell")
    39  
    40  	// create a keyspace and a couple tablets
    41  	if err := fromTS.CreateKeyspace(ctx, "test_keyspace", &topodatapb.Keyspace{}); err != nil {
    42  		t.Fatalf("cannot create keyspace: %v", err)
    43  	}
    44  	if err := fromTS.CreateShard(ctx, "test_keyspace", "0"); err != nil {
    45  		t.Fatalf("cannot create shard: %v", err)
    46  	}
    47  	tablet1 := &topodatapb.Tablet{
    48  		Alias: &topodatapb.TabletAlias{
    49  			Cell: "test_cell",
    50  			Uid:  123,
    51  		},
    52  		Hostname:      "primaryhost",
    53  		MysqlHostname: "primaryhost",
    54  		PortMap: map[string]int32{
    55  			"vt":   8101,
    56  			"gprc": 8102,
    57  		},
    58  		Keyspace:       "test_keyspace",
    59  		Shard:          "0",
    60  		Type:           topodatapb.TabletType_PRIMARY,
    61  		DbNameOverride: "",
    62  		KeyRange:       nil,
    63  	}
    64  	tablet1.MysqlPort = 3306
    65  	if err := fromTS.CreateTablet(ctx, tablet1); err != nil {
    66  		t.Fatalf("cannot create primary tablet: %v", err)
    67  	}
    68  	tablet2 := &topodatapb.Tablet{
    69  		Alias: &topodatapb.TabletAlias{
    70  			Cell: "test_cell",
    71  			Uid:  234,
    72  		},
    73  		PortMap: map[string]int32{
    74  			"vt":   8101,
    75  			"grpc": 8102,
    76  		},
    77  		Hostname:      "replicahost",
    78  		MysqlHostname: "replicahost",
    79  
    80  		Keyspace:       "test_keyspace",
    81  		Shard:          "0",
    82  		Type:           topodatapb.TabletType_REPLICA,
    83  		DbNameOverride: "",
    84  		KeyRange:       nil,
    85  	}
    86  	tablet2.MysqlPort = 3306
    87  	err := fromTS.CreateTablet(ctx, tablet2)
    88  	require.NoError(t, err, "cannot create tablet: %v", tablet2)
    89  
    90  	rr := &vschemapb.RoutingRules{
    91  		Rules: []*vschemapb.RoutingRule{{
    92  			FromTable: "t1",
    93  			ToTables:  []string{"t2", "t3"},
    94  		}},
    95  	}
    96  	if err := fromTS.SaveRoutingRules(ctx, rr); err != nil {
    97  		t.Fatalf("cannot save routing rules: %v", err)
    98  	}
    99  
   100  	return fromTS, toTS
   101  }
   102  
   103  func TestBasic(t *testing.T) {
   104  	ctx := context.Background()
   105  	fromTS, toTS := createSetup(ctx, t)
   106  
   107  	// check keyspace copy
   108  	CopyKeyspaces(ctx, fromTS, toTS)
   109  	keyspaces, err := toTS.GetKeyspaces(ctx)
   110  	if err != nil {
   111  		t.Fatalf("toTS.GetKeyspaces failed: %v", err)
   112  	}
   113  	if len(keyspaces) != 1 || keyspaces[0] != "test_keyspace" {
   114  		t.Fatalf("unexpected keyspaces: %v", keyspaces)
   115  	}
   116  	CopyKeyspaces(ctx, fromTS, toTS)
   117  
   118  	// check shard copy
   119  	CopyShards(ctx, fromTS, toTS)
   120  	shards, err := toTS.GetShardNames(ctx, "test_keyspace")
   121  	if err != nil {
   122  		t.Fatalf("toTS.GetShardNames failed: %v", err)
   123  	}
   124  	if len(shards) != 1 || shards[0] != "0" {
   125  		t.Fatalf("unexpected shards: %v", shards)
   126  	}
   127  	CopyShards(ctx, fromTS, toTS)
   128  
   129  	// check ShardReplication copy
   130  	_, err = fromTS.GetShardReplication(ctx, "test_cell", "test_keyspace", "0")
   131  	if err != nil {
   132  		t.Fatalf("fromTS.GetShardReplication failed: %v", err)
   133  	}
   134  	CopyShardReplications(ctx, fromTS, toTS)
   135  	sr, err := toTS.GetShardReplication(ctx, "test_cell", "test_keyspace", "0")
   136  	if err != nil {
   137  		t.Fatalf("toTS.GetShardReplication failed: %v", err)
   138  	}
   139  	if len(sr.Nodes) != 2 {
   140  		t.Fatalf("unexpected ShardReplication: %v", sr)
   141  	}
   142  
   143  	// check ShardReplications is idempotent
   144  	CopyShardReplications(ctx, fromTS, toTS)
   145  	sr, err = toTS.GetShardReplication(ctx, "test_cell", "test_keyspace", "0")
   146  	if err != nil {
   147  		t.Fatalf("toTS.GetShardReplication failed: %v", err)
   148  	}
   149  	if len(sr.Nodes) != 2 {
   150  		t.Fatalf("unexpected ShardReplication after second copy: %v", sr)
   151  	}
   152  
   153  	// check tablet copy
   154  	CopyTablets(ctx, fromTS, toTS)
   155  	tablets, err := toTS.GetTabletAliasesByCell(ctx, "test_cell")
   156  	if err != nil {
   157  		t.Fatalf("toTS.GetTabletsByCell failed: %v", err)
   158  	}
   159  	if len(tablets) != 2 || tablets[0].Uid != 123 || tablets[1].Uid != 234 {
   160  		t.Fatalf("unexpected tablets: %v", tablets)
   161  	}
   162  	CopyTablets(ctx, fromTS, toTS)
   163  }