vitess.io/vitess@v0.16.2/go/vt/topotools/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 topotools
    18  
    19  import (
    20  	"testing"
    21  
    22  	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
    23  )
    24  
    25  func TestCheckOwnership(t *testing.T) {
    26  	type testCase struct {
    27  		oldTablet, newTablet *topodatapb.Tablet
    28  		wantError            bool
    29  	}
    30  	table := []testCase{
    31  		{
    32  			oldTablet: &topodatapb.Tablet{
    33  				Hostname: "host1",
    34  				PortMap:  map[string]int32{"vt": 123, "grpc": 555},
    35  			},
    36  			newTablet: &topodatapb.Tablet{
    37  				Hostname: "host1",
    38  				PortMap:  map[string]int32{"vt": 123, "grpc": 222},
    39  			},
    40  			wantError: false,
    41  		},
    42  		{
    43  			oldTablet: &topodatapb.Tablet{
    44  				Hostname: "host2",
    45  				PortMap:  map[string]int32{"vt": 123},
    46  			},
    47  			newTablet: &topodatapb.Tablet{
    48  				Hostname: "host1",
    49  				PortMap:  map[string]int32{"vt": 123},
    50  			},
    51  			wantError: true,
    52  		},
    53  		{
    54  			oldTablet: &topodatapb.Tablet{
    55  				Hostname: "host1",
    56  				PortMap:  map[string]int32{"vt": 123},
    57  			},
    58  			newTablet: &topodatapb.Tablet{
    59  				Hostname: "host1",
    60  				PortMap:  map[string]int32{"vt": 321},
    61  			},
    62  			wantError: true,
    63  		},
    64  		{
    65  			newTablet: &topodatapb.Tablet{
    66  				Hostname: "host1",
    67  				PortMap:  map[string]int32{"vt": 123},
    68  			},
    69  			wantError: true,
    70  		},
    71  		{
    72  			oldTablet: &topodatapb.Tablet{
    73  				PortMap: map[string]int32{"vt": 123},
    74  			},
    75  			newTablet: &topodatapb.Tablet{
    76  				Hostname: "host1",
    77  				PortMap:  map[string]int32{"vt": 123},
    78  			},
    79  			wantError: true,
    80  		},
    81  		{
    82  			oldTablet: &topodatapb.Tablet{
    83  				Hostname: "host1",
    84  			},
    85  			newTablet: &topodatapb.Tablet{
    86  				Hostname: "host1",
    87  				PortMap:  map[string]int32{"vt": 123},
    88  			},
    89  			wantError: true,
    90  		},
    91  	}
    92  
    93  	for i, tc := range table {
    94  		gotError := CheckOwnership(tc.oldTablet, tc.newTablet) != nil
    95  		if gotError != tc.wantError {
    96  			t.Errorf("[%v]: got error = %v, want error = %v", i, gotError, tc.wantError)
    97  		}
    98  	}
    99  }
   100  
   101  type identTestCase struct {
   102  	tablet *topodatapb.Tablet
   103  	ident  string
   104  }
   105  
   106  func TestIdent(t *testing.T) {
   107  	tests := []identTestCase{
   108  		{
   109  			tablet: &topodatapb.Tablet{
   110  				Keyspace: "ks1",
   111  				Alias: &topodatapb.TabletAlias{
   112  					Cell: "cell",
   113  					Uid:  1,
   114  				},
   115  				Hostname: "host1",
   116  			},
   117  			ident: "cell-1 (host1)",
   118  		},
   119  		{
   120  			tablet: &topodatapb.Tablet{
   121  				Keyspace: "ks1",
   122  				Alias: &topodatapb.TabletAlias{
   123  					Cell: "cell",
   124  					Uid:  1,
   125  				},
   126  				Hostname: "host1",
   127  				Tags: map[string]string{
   128  					"tag1": "val1",
   129  				},
   130  			},
   131  			ident: "cell-1 (host1 tag1=val1)",
   132  		},
   133  	}
   134  
   135  	for _, test := range tests {
   136  		got := TabletIdent(test.tablet)
   137  		if got != test.ident {
   138  			t.Errorf("TabletIdent mismatch: got %s want %s", got, test.ident)
   139  		}
   140  	}
   141  }