vitess.io/vitess@v0.16.2/go/vt/topo/topoproto/destination_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 topoproto
    18  
    19  import (
    20  	"encoding/hex"
    21  	"reflect"
    22  	"testing"
    23  
    24  	"vitess.io/vitess/go/vt/key"
    25  
    26  	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
    27  )
    28  
    29  func TestParseDestination(t *testing.T) {
    30  	tenHexBytes, _ := hex.DecodeString("10")
    31  	twentyHexBytes, _ := hex.DecodeString("20")
    32  
    33  	testcases := []struct {
    34  		targetString string
    35  		dest         key.Destination
    36  		keyspace     string
    37  		tabletType   topodatapb.TabletType
    38  	}{{
    39  		targetString: "ks[10-20]@primary",
    40  		keyspace:     "ks",
    41  		tabletType:   topodatapb.TabletType_PRIMARY,
    42  		dest:         key.DestinationExactKeyRange{KeyRange: &topodatapb.KeyRange{Start: tenHexBytes, End: twentyHexBytes}},
    43  	}, {
    44  		targetString: "ks[-]@primary",
    45  		keyspace:     "ks",
    46  		tabletType:   topodatapb.TabletType_PRIMARY,
    47  		dest:         key.DestinationExactKeyRange{KeyRange: &topodatapb.KeyRange{}},
    48  	}, {
    49  		targetString: "ks[deadbeef]@primary",
    50  		keyspace:     "ks",
    51  		tabletType:   topodatapb.TabletType_PRIMARY,
    52  		dest:         key.DestinationKeyspaceID([]byte("\xde\xad\xbe\xef")),
    53  	}, {
    54  		targetString: "ks[10-]@primary",
    55  		keyspace:     "ks",
    56  		tabletType:   topodatapb.TabletType_PRIMARY,
    57  		dest:         key.DestinationExactKeyRange{KeyRange: &topodatapb.KeyRange{Start: tenHexBytes}},
    58  	}, {
    59  		targetString: "ks[-20]@primary",
    60  		keyspace:     "ks",
    61  		tabletType:   topodatapb.TabletType_PRIMARY,
    62  		dest:         key.DestinationExactKeyRange{KeyRange: &topodatapb.KeyRange{End: twentyHexBytes}},
    63  	}, {
    64  		targetString: "ks:-80@primary",
    65  		keyspace:     "ks",
    66  		tabletType:   topodatapb.TabletType_PRIMARY,
    67  		dest:         key.DestinationShard("-80"),
    68  	}, {
    69  		targetString: ":-80@primary",
    70  		keyspace:     "",
    71  		tabletType:   topodatapb.TabletType_PRIMARY,
    72  		dest:         key.DestinationShard("-80"),
    73  	}, {
    74  		targetString: "@primary",
    75  		keyspace:     "",
    76  		tabletType:   topodatapb.TabletType_PRIMARY,
    77  	}, {
    78  		targetString: "@replica",
    79  		keyspace:     "",
    80  		tabletType:   topodatapb.TabletType_REPLICA,
    81  	}, {
    82  		targetString: "ks",
    83  		keyspace:     "ks",
    84  		tabletType:   topodatapb.TabletType_PRIMARY,
    85  	}, {
    86  		targetString: "ks/-80",
    87  		keyspace:     "ks",
    88  		dest:         key.DestinationShard("-80"),
    89  		tabletType:   topodatapb.TabletType_PRIMARY,
    90  	}}
    91  
    92  	for _, tcase := range testcases {
    93  		if targetKeyspace, targetTabletType, targetDest, _ := ParseDestination(tcase.targetString, topodatapb.TabletType_PRIMARY); !reflect.DeepEqual(targetDest, tcase.dest) || targetKeyspace != tcase.keyspace || targetTabletType != tcase.tabletType {
    94  			t.Errorf("ParseDestination(%s) - got: (%v, %v, %v), want (%v, %v, %v)",
    95  				tcase.targetString,
    96  				targetDest,
    97  				targetKeyspace,
    98  				targetTabletType,
    99  				tcase.dest,
   100  				tcase.keyspace,
   101  				tcase.tabletType,
   102  			)
   103  		}
   104  	}
   105  
   106  	_, _, _, err := ParseDestination("ks[20-40-60]", topodatapb.TabletType_PRIMARY)
   107  	want := "single keyrange expected in 20-40-60"
   108  	if err == nil || err.Error() != want {
   109  		t.Errorf("executorExec error: %v, want %s", err, want)
   110  	}
   111  
   112  	_, _, _, err = ParseDestination("ks[--60]", topodatapb.TabletType_PRIMARY)
   113  	want = "malformed spec: MinKey/MaxKey cannot be in the middle of the spec: \"--60\""
   114  	if err == nil || err.Error() != want {
   115  		t.Errorf("executorExec error: %v, want %s", err, want)
   116  	}
   117  
   118  	_, _, _, err = ParseDestination("ks[qrnqorrs]@primary", topodatapb.TabletType_PRIMARY)
   119  	want = "expected valid hex in keyspace id qrnqorrs"
   120  	if err == nil || err.Error() != want {
   121  		t.Errorf("executorExec error: %v, want %s", err, want)
   122  	}
   123  }