vitess.io/vitess@v0.16.2/web/vtadmin/src/util/keyspaces.test.ts (about) 1 /** 2 * Copyright 2021 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 import { getShardsByState, getShardSortRange, ShardRange, ShardsByState, ShardState } from './keyspaces'; 17 import { vtadmin as pb } from '../proto/vtadmin'; 18 19 describe('getShardsByState', () => { 20 const tests: { 21 name: string; 22 keyspace: pb.Keyspace | null | undefined; 23 expected: ShardsByState; 24 }[] = [ 25 { 26 name: 'should return shards by state', 27 keyspace: pb.Keyspace.create({ 28 shards: { 29 '-80': { 30 name: '-80', 31 shard: { is_primary_serving: true }, 32 }, 33 '-': { 34 name: '-', 35 shard: { is_primary_serving: false }, 36 }, 37 '80-': { 38 name: '80-', 39 shard: { is_primary_serving: true }, 40 }, 41 }, 42 }), 43 expected: { 44 [ShardState.serving]: [ 45 { 46 name: '-80', 47 shard: { is_primary_serving: true }, 48 }, 49 { 50 name: '80-', 51 shard: { is_primary_serving: true }, 52 }, 53 ], 54 [ShardState.nonserving]: [ 55 { 56 name: '-', 57 shard: { is_primary_serving: false }, 58 }, 59 ], 60 }, 61 }, 62 { 63 name: 'should handle shard states without any shards', 64 keyspace: pb.Keyspace.create({ 65 shards: { 66 '-': { 67 name: '-', 68 shard: { is_primary_serving: true }, 69 }, 70 }, 71 }), 72 expected: { 73 [ShardState.serving]: [ 74 { 75 name: '-', 76 shard: { is_primary_serving: true }, 77 }, 78 ], 79 [ShardState.nonserving]: [], 80 }, 81 }, 82 { 83 name: 'should handle keyspaces without any shards', 84 keyspace: pb.Keyspace.create(), 85 expected: { 86 [ShardState.serving]: [], 87 [ShardState.nonserving]: [], 88 }, 89 }, 90 { 91 name: 'should handle empty input', 92 keyspace: null, 93 expected: { 94 [ShardState.serving]: [], 95 [ShardState.nonserving]: [], 96 }, 97 }, 98 ]; 99 100 test.each(tests.map(Object.values))('%s', (name: string, keyspace: pb.Keyspace, expected: ShardsByState) => { 101 const result = getShardsByState(keyspace); 102 expect(result).toEqual(expected); 103 }); 104 }); 105 106 describe('getShardSortRange', () => { 107 const tests: { 108 shardName: string; 109 expected: ShardRange; 110 }[] = [ 111 { 112 shardName: '0', 113 expected: { start: Number.MIN_VALUE, end: Number.MAX_VALUE }, 114 }, 115 { 116 shardName: '-', 117 expected: { start: Number.MIN_VALUE, end: Number.MAX_VALUE }, 118 }, 119 { 120 shardName: '-40', 121 expected: { start: Number.MIN_VALUE, end: 64 }, 122 }, 123 { 124 shardName: '40-80', 125 expected: { start: 64, end: 128 }, 126 }, 127 { 128 shardName: '80-c0', 129 expected: { start: 128, end: 192 }, 130 }, 131 { 132 shardName: 'c0-', 133 expected: { start: 192, end: Number.MAX_VALUE }, 134 }, 135 { 136 shardName: 'c0-', 137 expected: { start: 192, end: Number.MAX_VALUE }, 138 }, 139 ]; 140 141 test.each(tests.map(Object.values))('%s', (shardName: string, expected: ShardRange) => { 142 const result = getShardSortRange(shardName); 143 expect(result).toEqual(expected); 144 }); 145 146 it('handles invalid shard names', () => { 147 ['nope', '--', '', '40--'].forEach((s) => { 148 expect(() => { 149 getShardSortRange(s); 150 }).toThrow(`could not parse sortable range from shard ${s}`); 151 }); 152 }); 153 });