vitess.io/vitess@v0.16.2/go/vt/topo/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 topo
    18  
    19  import (
    20  	"strings"
    21  	"testing"
    22  
    23  	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
    24  )
    25  
    26  func TestValidateAlias(t *testing.T) {
    27  	table := []struct {
    28  		currentAliases map[string][]string
    29  		newAliasName   string
    30  		newAlias       []string
    31  		wantErrMsg     string
    32  	}{
    33  		{
    34  			currentAliases: map[string][]string{
    35  				"alias1": {"cell_a", "cell_b"},
    36  				"alias2": {"cell_c", "cell_d"},
    37  			},
    38  			newAliasName: "overlaps_alias1",
    39  			newAlias:     []string{"cell_x", "cell_b"},
    40  			wantErrMsg:   "alias1",
    41  		},
    42  		{
    43  			currentAliases: map[string][]string{
    44  				"alias1": {"cell_a", "cell_b"},
    45  				"alias2": {"cell_c", "cell_d"},
    46  			},
    47  			newAliasName: "overlaps_alias2",
    48  			newAlias:     []string{"cell_x", "cell_c"},
    49  			wantErrMsg:   "alias2",
    50  		},
    51  		{
    52  			currentAliases: map[string][]string{
    53  				"alias1": {"cell_a", "cell_b"},
    54  				"alias2": {"cell_c", "cell_d"},
    55  			},
    56  			newAliasName: "no_overlap",
    57  			newAlias:     []string{"cell_x", "cell_y"},
    58  			wantErrMsg:   "",
    59  		},
    60  		{
    61  			currentAliases: map[string][]string{
    62  				"overlaps_self": {"cell_a", "cell_b"},
    63  				"alias2":        {"cell_c", "cell_d"},
    64  			},
    65  			newAliasName: "overlaps_self",
    66  			newAlias:     []string{"cell_a", "cell_b", "cell_x"},
    67  			wantErrMsg:   "",
    68  		},
    69  	}
    70  
    71  	for _, test := range table {
    72  		currentAliases := map[string]*topodatapb.CellsAlias{}
    73  		for name, cells := range test.currentAliases {
    74  			currentAliases[name] = &topodatapb.CellsAlias{Cells: cells}
    75  		}
    76  		newAlias := &topodatapb.CellsAlias{Cells: test.newAlias}
    77  
    78  		gotErr := validateAlias(currentAliases, test.newAliasName, newAlias)
    79  		if test.wantErrMsg == "" {
    80  			// Expect success.
    81  			if gotErr != nil {
    82  				t.Errorf("validateAlias(%v) error = %q; want nil", test.newAliasName, gotErr.Error())
    83  			}
    84  		} else {
    85  			// Expect failure.
    86  			if gotErr == nil {
    87  				t.Errorf("validateAlias(%v) error = nil; want non-nil", test.newAliasName)
    88  			}
    89  			if got, want := gotErr.Error(), test.wantErrMsg; !strings.Contains(got, want) {
    90  				t.Errorf("validateAlias(%v) error = %q; want *%q*", test.newAliasName, got, want)
    91  			}
    92  		}
    93  	}
    94  }