vitess.io/vitess@v0.16.2/web/vtadmin/src/util/workflows.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 { vtadmin as pb } from '../proto/vtadmin'; 17 import { getStream, getStreams, getStreamTablets } from './workflows'; 18 19 describe('getStreams', () => { 20 const tests: { 21 name: string; 22 input: Parameters<typeof getStreams>; 23 expected: ReturnType<typeof getStreams>; 24 }[] = [ 25 { 26 name: 'should return a flat list of streams', 27 input: [ 28 pb.Workflow.create({ 29 workflow: { 30 shard_streams: { 31 '-80/us_east_1a-123456': { 32 streams: [ 33 { id: 1, shard: '-80' }, 34 { id: 2, shard: '-80' }, 35 ], 36 }, 37 '80-/us_east_1a-789012': { 38 streams: [ 39 { id: 1, shard: '80-' }, 40 { id: 2, shard: '80-' }, 41 ], 42 }, 43 }, 44 }, 45 }), 46 ], 47 expected: [ 48 { id: 1, shard: '-80' }, 49 { id: 2, shard: '-80' }, 50 { id: 1, shard: '80-' }, 51 { id: 2, shard: '80-' }, 52 ], 53 }, 54 { 55 name: 'should handle when shard streams undefined', 56 input: [pb.Workflow.create()], 57 expected: [], 58 }, 59 { 60 name: 'should handle null input', 61 input: [null], 62 expected: [], 63 }, 64 ]; 65 66 test.each(tests.map(Object.values))( 67 '%s', 68 (name: string, input: Parameters<typeof getStreams>, expected: ReturnType<typeof getStreams>) => { 69 expect(getStreams(...input)).toEqual(expected); 70 } 71 ); 72 }); 73 74 describe('getStream', () => { 75 const tests: { 76 name: string; 77 input: Parameters<typeof getStream>; 78 expected: ReturnType<typeof getStream>; 79 }[] = [ 80 { 81 name: 'should return the stream for the streamKey', 82 input: [ 83 pb.Workflow.create({ 84 workflow: { 85 shard_streams: { 86 '-80/us_east_1a-123456': { 87 streams: [ 88 { 89 id: 1, 90 shard: '-80', 91 tablet: { 92 cell: 'us_east_1a', 93 uid: 123456, 94 }, 95 }, 96 { 97 id: 2, 98 shard: '-80', 99 tablet: { 100 cell: 'us_east_1a', 101 uid: 123456, 102 }, 103 }, 104 ], 105 }, 106 '-80/us_east_1a-789012': { 107 streams: [ 108 { 109 id: 1, 110 shard: '-80', 111 tablet: { 112 cell: 'us_east_1a', 113 uid: 789012, 114 }, 115 }, 116 ], 117 }, 118 }, 119 }, 120 }), 121 'us_east_1a-123456/2', 122 ], 123 expected: { 124 id: 2, 125 shard: '-80', 126 tablet: { 127 cell: 'us_east_1a', 128 uid: 123456, 129 }, 130 }, 131 }, 132 { 133 name: 'should handle no matching stream in workflow', 134 input: [ 135 pb.Workflow.create({ 136 workflow: { 137 shard_streams: { 138 '-80/us_east_1a-123456': { 139 streams: [ 140 { 141 id: 1, 142 shard: '-80', 143 tablet: { 144 cell: 'us_east_1a', 145 uid: 123456, 146 }, 147 }, 148 ], 149 }, 150 }, 151 }, 152 }), 153 'us_east_1a-123456/2', 154 ], 155 expected: undefined, 156 }, 157 { 158 name: 'should handle undefined streamKey', 159 input: [ 160 pb.Workflow.create({ 161 workflow: { 162 shard_streams: { 163 '-80/us_east_1a-123456': { 164 streams: [ 165 { 166 id: 1, 167 shard: '-80', 168 tablet: { 169 cell: 'us_east_1a', 170 uid: 123456, 171 }, 172 }, 173 ], 174 }, 175 }, 176 }, 177 }), 178 undefined, 179 ], 180 expected: undefined, 181 }, 182 { 183 name: 'should handle undefined workflow', 184 input: [undefined, 'us_east_1a-123456/1'], 185 expected: undefined, 186 }, 187 ]; 188 189 test.each(tests.map(Object.values))( 190 '%s', 191 (name: string, input: Parameters<typeof getStream>, expected: ReturnType<typeof getStream>) => { 192 expect(getStream(...input)).toEqual(expected); 193 } 194 ); 195 }); 196 197 describe('getStreamTablets', () => { 198 const tests: { 199 name: string; 200 input: Parameters<typeof getStreamTablets>; 201 expected: ReturnType<typeof getStreamTablets>; 202 }[] = [ 203 { 204 name: 'should return a set of unique tablet aliases', 205 input: [ 206 pb.Workflow.create({ 207 workflow: { 208 shard_streams: { 209 '-80/us_east_1a-123456': { 210 streams: [ 211 { id: 1, shard: '-80', tablet: { cell: 'us_east_1a', uid: 123456 } }, 212 { id: 2, shard: '-80', tablet: { cell: 'us_east_1a', uid: 123456 } }, 213 ], 214 }, 215 '80-/us_east_1a-789012': { 216 streams: [{ id: 1, shard: '80-', tablet: { cell: 'us_east_1a', uid: 789012 } }], 217 }, 218 }, 219 }, 220 }), 221 ], 222 expected: ['us_east_1a-123456', 'us_east_1a-789012'], 223 }, 224 { 225 name: 'should handle empty workflow', 226 input: [pb.Workflow.create()], 227 expected: [], 228 }, 229 { 230 name: 'should handle null input', 231 input: [null], 232 expected: [], 233 }, 234 ]; 235 236 test.each(tests.map(Object.values))( 237 '%s', 238 (name: string, input: Parameters<typeof getStreamTablets>, expected: ReturnType<typeof getStreamTablets>) => { 239 expect(getStreamTablets(...input)).toEqual(expected); 240 } 241 ); 242 });