vitess.io/vitess@v0.16.2/go/vt/topo/topotests/tablet_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 topotests
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"google.golang.org/protobuf/proto"
    24  
    25  	"vitess.io/vitess/go/vt/topo/memorytopo"
    26  
    27  	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
    28  )
    29  
    30  // This file contains tests for the tablet.go file.
    31  
    32  // TestCreateTablet tests all the logic in the topo.CreateTablet method.
    33  func TestCreateTablet(t *testing.T) {
    34  	cell := "cell1"
    35  	keyspace := "ks1"
    36  	shard := "shard1"
    37  	ctx := context.Background()
    38  	ts := memorytopo.NewServer(cell)
    39  
    40  	// Create a tablet.
    41  	alias := &topodatapb.TabletAlias{
    42  		Cell: cell,
    43  		Uid:  1,
    44  	}
    45  	tablet := &topodatapb.Tablet{
    46  		Keyspace: keyspace,
    47  		Shard:    shard,
    48  		Alias:    alias,
    49  	}
    50  	if err := ts.CreateTablet(ctx, tablet); err != nil {
    51  		t.Fatalf("CreateTablet failed: %v", err)
    52  	}
    53  
    54  	// Get the tablet, make sure it's good. Also check ShardReplication.
    55  	ti, err := ts.GetTablet(ctx, alias)
    56  	if err != nil || !proto.Equal(ti.Tablet, tablet) {
    57  		t.Fatalf("Created Tablet doesn't match: %v %v", ti, err)
    58  	}
    59  	sri, err := ts.GetShardReplication(ctx, cell, keyspace, shard)
    60  	if err != nil || len(sri.Nodes) != 1 || !proto.Equal(sri.Nodes[0].TabletAlias, alias) {
    61  		t.Fatalf("Created ShardReplication doesn't match: %v %v", sri, err)
    62  	}
    63  }