vitess.io/vitess@v0.16.2/go/vt/topo/topotests/cells_aliases_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  	"reflect"
    22  	"sort"
    23  	"testing"
    24  
    25  	"vitess.io/vitess/go/vt/topo/memorytopo"
    26  
    27  	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
    28  )
    29  
    30  // This file tests the CellsAliases part of the topo.Server API.
    31  
    32  func TestCellsAliases(t *testing.T) {
    33  	// Create an alias
    34  
    35  	cell := "cell1"
    36  	ctx := context.Background()
    37  	ts := memorytopo.NewServer(cell)
    38  
    39  	if err := ts.CreateCellsAlias(ctx, "alias", &topodatapb.CellsAlias{Cells: []string{"cell1", "cell2"}}); err != nil {
    40  		t.Fatalf("CreateCellsAlias failed: %v", err)
    41  	}
    42  
    43  	if err := ts.CreateCellsAlias(ctx, "aliasb", &topodatapb.CellsAlias{Cells: []string{"cell3", "cell4"}}); err != nil {
    44  		t.Fatalf("CreateCellsAlias failed: %v", err)
    45  	}
    46  
    47  	aliases, err := ts.GetCellsAliases(ctx, true /*strongRead*/)
    48  	if err != nil {
    49  		t.Fatalf("GetCellsAliases failed: %v", err)
    50  	}
    51  
    52  	var aliasesName []string
    53  	for aliasName := range aliases {
    54  		aliasesName = append(aliasesName, aliasName)
    55  	}
    56  	sort.Strings(aliasesName)
    57  
    58  	if len(aliasesName) != 2 {
    59  		t.Fatalf("Expected to have 2 aliases. Got %v", len(aliasesName))
    60  
    61  	}
    62  
    63  	if aliasesName[0] != "alias" {
    64  		t.Fatalf("Expected alias name to be alias, got: %v", aliasesName[0])
    65  	}
    66  
    67  	if aliasesName[1] != "aliasb" {
    68  		t.Fatalf("Expected alias name to be aliasb, got: %v", aliasesName[0])
    69  	}
    70  
    71  	want := []string{"cell1", "cell2"}
    72  
    73  	if !reflect.DeepEqual(aliases[aliasesName[0]].Cells, want) {
    74  		t.Fatalf("Expected alias to be: %v, got %v", want, aliases[aliasesName[0]])
    75  	}
    76  
    77  	want = []string{"cell3", "cell4"}
    78  
    79  	if !reflect.DeepEqual(aliases[aliasesName[1]].Cells, want) {
    80  		t.Fatalf("Expected aliasb to be: %v, got %v", want, aliases[aliasesName[1]])
    81  	}
    82  
    83  	// Test update on non-existing object.
    84  
    85  	want = []string{"newcell"}
    86  
    87  	if err := ts.UpdateCellsAlias(ctx, "newalias", func(ca *topodatapb.CellsAlias) error {
    88  		ca.Cells = want
    89  		return nil
    90  	}); err != nil {
    91  		t.Fatalf("UpdateCellsAlias failed: %v", err)
    92  	}
    93  
    94  	aliases, err = ts.GetCellsAliases(ctx, true /*strongRead*/)
    95  	if err != nil {
    96  		t.Fatalf("GetCellsAliases failed: %v", err)
    97  	}
    98  
    99  	if !reflect.DeepEqual(aliases["newalias"].Cells, want) {
   100  		t.Fatalf("Expected newalias to be: %v, got %v", want, aliases["newalias"])
   101  	}
   102  
   103  	// Test update on existing object.
   104  
   105  	want = []string{"newcell2"}
   106  
   107  	if err := ts.UpdateCellsAlias(ctx, "newalias", func(ca *topodatapb.CellsAlias) error {
   108  		ca.Cells = want
   109  		return nil
   110  	}); err != nil {
   111  		t.Fatalf("UpdateCellsAlias failed: %v", err)
   112  	}
   113  
   114  	aliases, err = ts.GetCellsAliases(ctx, true /*strongRead*/)
   115  	if err != nil {
   116  		t.Fatalf("GetCellsAliases failed: %v", err)
   117  	}
   118  
   119  	if !reflect.DeepEqual(aliases["newalias"].Cells, want) {
   120  		t.Fatalf("Expected newalias to be: %v, got %v", want, aliases["newalias"])
   121  	}
   122  
   123  	// Test delete alias
   124  
   125  	if err := ts.DeleteCellsAlias(ctx, "newalias"); err != nil {
   126  		t.Fatalf("UpdateCellsAlias failed: %v", err)
   127  	}
   128  
   129  	aliases, err = ts.GetCellsAliases(ctx, true /*strongRead*/)
   130  	if err != nil {
   131  		t.Fatalf("GetCellsAliases failed: %v", err)
   132  	}
   133  
   134  	if aliases["newalias"] != nil {
   135  		t.Fatalf("Expected newalias to be: nil, got %v", aliases["newalias"])
   136  	}
   137  
   138  	// Create an alias that adds an overlapping cell is not supported
   139  	if err := ts.CreateCellsAlias(ctx, "invalid", &topodatapb.CellsAlias{Cells: []string{"cell1", "cell2"}}); err == nil {
   140  		t.Fatal("CreateCellsAlias should fail, got nil")
   141  	}
   142  
   143  	// Update an alias that adds an overlapping cell is not supported
   144  	if err := ts.UpdateCellsAlias(ctx, "aliasb", func(ca *topodatapb.CellsAlias) error {
   145  		ca.Cells = []string{"cell1"}
   146  		return nil
   147  	}); err == nil {
   148  		t.Fatalf("UpdateCellsAlias should fail, got nil")
   149  	}
   150  }