go.etcd.io/etcd@v3.3.27+incompatible/etcdctl/ctlv3/command/watch_command_test.go (about) 1 // Copyright 2017 The etcd Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package command 16 17 import ( 18 "reflect" 19 "testing" 20 ) 21 22 func Test_parseWatchArgs(t *testing.T) { 23 tt := []struct { 24 osArgs []string // raw arguments to "watch" command 25 commandArgs []string // arguments after "spf13/cobra" preprocessing 26 envKey, envRange string 27 interactive bool 28 29 interactiveWatchPrefix bool 30 interactiveWatchRev int64 31 interactiveWatchPrevKey bool 32 33 watchArgs []string 34 execArgs []string 35 err error 36 }{ 37 { 38 osArgs: []string{"./bin/etcdctl", "watch", "foo", "bar"}, 39 commandArgs: []string{"foo", "bar"}, 40 interactive: false, 41 watchArgs: []string{"foo", "bar"}, 42 execArgs: nil, 43 err: nil, 44 }, 45 { 46 osArgs: []string{"./bin/etcdctl", "watch", "foo", "bar", "--"}, 47 commandArgs: []string{"foo", "bar"}, 48 interactive: false, 49 watchArgs: nil, 50 execArgs: nil, 51 err: errBadArgsNumSeparator, 52 }, 53 { 54 osArgs: []string{"./bin/etcdctl", "watch"}, 55 commandArgs: nil, 56 envKey: "foo", 57 envRange: "bar", 58 interactive: false, 59 watchArgs: []string{"foo", "bar"}, 60 execArgs: nil, 61 err: nil, 62 }, 63 { 64 osArgs: []string{"./bin/etcdctl", "watch", "foo"}, 65 commandArgs: []string{"foo"}, 66 envKey: "foo", 67 envRange: "", 68 interactive: false, 69 watchArgs: nil, 70 execArgs: nil, 71 err: errBadArgsNumConflictEnv, 72 }, 73 { 74 osArgs: []string{"./bin/etcdctl", "watch", "foo", "bar"}, 75 commandArgs: []string{"foo", "bar"}, 76 envKey: "foo", 77 envRange: "", 78 interactive: false, 79 watchArgs: nil, 80 execArgs: nil, 81 err: errBadArgsNumConflictEnv, 82 }, 83 { 84 osArgs: []string{"./bin/etcdctl", "watch", "foo", "bar"}, 85 commandArgs: []string{"foo", "bar"}, 86 envKey: "foo", 87 envRange: "bar", 88 interactive: false, 89 watchArgs: nil, 90 execArgs: nil, 91 err: errBadArgsNumConflictEnv, 92 }, 93 { 94 osArgs: []string{"./bin/etcdctl", "watch", "foo"}, 95 commandArgs: []string{"foo"}, 96 interactive: false, 97 watchArgs: []string{"foo"}, 98 execArgs: nil, 99 err: nil, 100 }, 101 { 102 osArgs: []string{"./bin/etcdctl", "watch"}, 103 commandArgs: nil, 104 envKey: "foo", 105 interactive: false, 106 watchArgs: []string{"foo"}, 107 execArgs: nil, 108 err: nil, 109 }, 110 { 111 osArgs: []string{"./bin/etcdctl", "watch", "--rev", "1", "foo"}, 112 commandArgs: []string{"foo"}, 113 interactive: false, 114 watchArgs: []string{"foo"}, 115 execArgs: nil, 116 err: nil, 117 }, 118 { 119 osArgs: []string{"./bin/etcdctl", "watch", "--rev", "1", "foo"}, 120 commandArgs: []string{"foo"}, 121 envKey: "foo", 122 interactive: false, 123 watchArgs: nil, 124 execArgs: nil, 125 err: errBadArgsNumConflictEnv, 126 }, 127 { 128 osArgs: []string{"./bin/etcdctl", "watch", "--rev", "1"}, 129 commandArgs: nil, 130 envKey: "foo", 131 interactive: false, 132 watchArgs: []string{"foo"}, 133 execArgs: nil, 134 err: nil, 135 }, 136 { 137 osArgs: []string{"./bin/etcdctl", "watch", "foo", "--rev", "1"}, 138 commandArgs: []string{"foo"}, 139 interactive: false, 140 watchArgs: []string{"foo"}, 141 execArgs: nil, 142 err: nil, 143 }, 144 { 145 osArgs: []string{"./bin/etcdctl", "watch", "foo", "--", "echo", "Hello", "World"}, 146 commandArgs: []string{"foo", "echo", "Hello", "World"}, 147 interactive: false, 148 watchArgs: []string{"foo"}, 149 execArgs: []string{"echo", "Hello", "World"}, 150 err: nil, 151 }, 152 { 153 osArgs: []string{"./bin/etcdctl", "watch", "foo", "--", "echo", "watch", "event", "received"}, 154 commandArgs: []string{"foo", "echo", "watch", "event", "received"}, 155 interactive: false, 156 watchArgs: []string{"foo"}, 157 execArgs: []string{"echo", "watch", "event", "received"}, 158 err: nil, 159 }, 160 { 161 osArgs: []string{"./bin/etcdctl", "watch", "foo", "--rev", "1", "--", "echo", "Hello", "World"}, 162 commandArgs: []string{"foo", "echo", "Hello", "World"}, 163 interactive: false, 164 watchArgs: []string{"foo"}, 165 execArgs: []string{"echo", "Hello", "World"}, 166 err: nil, 167 }, 168 { 169 osArgs: []string{"./bin/etcdctl", "watch", "foo", "--rev", "1", "--", "echo", "watch", "event", "received"}, 170 commandArgs: []string{"foo", "echo", "watch", "event", "received"}, 171 interactive: false, 172 watchArgs: []string{"foo"}, 173 execArgs: []string{"echo", "watch", "event", "received"}, 174 err: nil, 175 }, 176 { 177 osArgs: []string{"./bin/etcdctl", "watch", "--rev", "1", "foo", "--", "echo", "watch", "event", "received"}, 178 commandArgs: []string{"foo", "echo", "watch", "event", "received"}, 179 interactive: false, 180 watchArgs: []string{"foo"}, 181 execArgs: []string{"echo", "watch", "event", "received"}, 182 err: nil, 183 }, 184 { 185 osArgs: []string{"./bin/etcdctl", "watch", "foo", "bar", "--", "echo", "Hello", "World"}, 186 commandArgs: []string{"foo", "bar", "echo", "Hello", "World"}, 187 interactive: false, 188 watchArgs: []string{"foo", "bar"}, 189 execArgs: []string{"echo", "Hello", "World"}, 190 err: nil, 191 }, 192 { 193 osArgs: []string{"./bin/etcdctl", "watch", "--rev", "1", "foo", "bar", "--", "echo", "Hello", "World"}, 194 commandArgs: []string{"foo", "bar", "echo", "Hello", "World"}, 195 interactive: false, 196 watchArgs: []string{"foo", "bar"}, 197 execArgs: []string{"echo", "Hello", "World"}, 198 err: nil, 199 }, 200 { 201 osArgs: []string{"./bin/etcdctl", "watch", "foo", "--rev", "1", "bar", "--", "echo", "Hello", "World"}, 202 commandArgs: []string{"foo", "bar", "echo", "Hello", "World"}, 203 interactive: false, 204 watchArgs: []string{"foo", "bar"}, 205 execArgs: []string{"echo", "Hello", "World"}, 206 err: nil, 207 }, 208 { 209 osArgs: []string{"./bin/etcdctl", "watch", "foo", "bar", "--rev", "1", "--", "echo", "Hello", "World"}, 210 commandArgs: []string{"foo", "bar", "echo", "Hello", "World"}, 211 interactive: false, 212 watchArgs: []string{"foo", "bar"}, 213 execArgs: []string{"echo", "Hello", "World"}, 214 err: nil, 215 }, 216 { 217 osArgs: []string{"./bin/etcdctl", "watch", "foo", "bar", "--rev", "1", "--", "echo", "watch", "event", "received"}, 218 commandArgs: []string{"foo", "bar", "echo", "watch", "event", "received"}, 219 interactive: false, 220 watchArgs: []string{"foo", "bar"}, 221 execArgs: []string{"echo", "watch", "event", "received"}, 222 err: nil, 223 }, 224 { 225 osArgs: []string{"./bin/etcdctl", "watch", "foo", "--rev", "1", "bar", "--", "echo", "Hello", "World"}, 226 commandArgs: []string{"foo", "bar", "echo", "Hello", "World"}, 227 interactive: false, 228 watchArgs: []string{"foo", "bar"}, 229 execArgs: []string{"echo", "Hello", "World"}, 230 err: nil, 231 }, 232 { 233 osArgs: []string{"./bin/etcdctl", "watch", "--rev", "1", "foo", "bar", "--", "echo", "Hello", "World"}, 234 commandArgs: []string{"foo", "bar", "echo", "Hello", "World"}, 235 interactive: false, 236 watchArgs: []string{"foo", "bar"}, 237 execArgs: []string{"echo", "Hello", "World"}, 238 err: nil, 239 }, 240 { 241 osArgs: []string{"./bin/etcdctl", "watch", "--rev", "1", "--", "echo", "Hello", "World"}, 242 commandArgs: []string{"echo", "Hello", "World"}, 243 envKey: "foo", 244 envRange: "", 245 interactive: false, 246 watchArgs: []string{"foo"}, 247 execArgs: []string{"echo", "Hello", "World"}, 248 err: nil, 249 }, 250 { 251 osArgs: []string{"./bin/etcdctl", "watch", "--rev", "1", "--", "echo", "Hello", "World"}, 252 commandArgs: []string{"echo", "Hello", "World"}, 253 envKey: "foo", 254 envRange: "bar", 255 interactive: false, 256 watchArgs: []string{"foo", "bar"}, 257 execArgs: []string{"echo", "Hello", "World"}, 258 err: nil, 259 }, 260 { 261 osArgs: []string{"./bin/etcdctl", "watch", "foo", "bar", "--rev", "1", "--", "echo", "Hello", "World"}, 262 commandArgs: []string{"foo", "bar", "echo", "Hello", "World"}, 263 envKey: "foo", 264 interactive: false, 265 watchArgs: nil, 266 execArgs: nil, 267 err: errBadArgsNumConflictEnv, 268 }, 269 { 270 osArgs: []string{"./bin/etcdctl", "watch", "-i"}, 271 commandArgs: []string{"foo", "bar", "--", "echo", "Hello", "World"}, 272 interactive: true, 273 interactiveWatchPrefix: false, 274 interactiveWatchRev: 0, 275 interactiveWatchPrevKey: false, 276 watchArgs: nil, 277 execArgs: nil, 278 err: errBadArgsInteractiveWatch, 279 }, 280 { 281 osArgs: []string{"./bin/etcdctl", "watch", "-i"}, 282 commandArgs: []string{"watch", "foo"}, 283 interactive: true, 284 interactiveWatchPrefix: false, 285 interactiveWatchRev: 0, 286 interactiveWatchPrevKey: false, 287 watchArgs: []string{"foo"}, 288 execArgs: nil, 289 err: nil, 290 }, 291 { 292 osArgs: []string{"./bin/etcdctl", "watch", "-i"}, 293 commandArgs: []string{"watch", "foo", "bar"}, 294 interactive: true, 295 interactiveWatchPrefix: false, 296 interactiveWatchRev: 0, 297 interactiveWatchPrevKey: false, 298 watchArgs: []string{"foo", "bar"}, 299 execArgs: nil, 300 err: nil, 301 }, 302 { 303 osArgs: []string{"./bin/etcdctl", "watch", "-i"}, 304 commandArgs: []string{"watch"}, 305 envKey: "foo", 306 envRange: "bar", 307 interactive: true, 308 interactiveWatchPrefix: false, 309 interactiveWatchRev: 0, 310 interactiveWatchPrevKey: false, 311 watchArgs: []string{"foo", "bar"}, 312 execArgs: nil, 313 err: nil, 314 }, 315 { 316 osArgs: []string{"./bin/etcdctl", "watch", "-i"}, 317 commandArgs: []string{"watch"}, 318 envKey: "hello world!", 319 envRange: "bar", 320 interactive: true, 321 interactiveWatchPrefix: false, 322 interactiveWatchRev: 0, 323 interactiveWatchPrevKey: false, 324 watchArgs: []string{"hello world!", "bar"}, 325 execArgs: nil, 326 err: nil, 327 }, 328 { 329 osArgs: []string{"./bin/etcdctl", "watch", "-i"}, 330 commandArgs: []string{"watch", "foo", "--rev", "1"}, 331 interactive: true, 332 interactiveWatchPrefix: false, 333 interactiveWatchRev: 1, 334 interactiveWatchPrevKey: false, 335 watchArgs: []string{"foo"}, 336 execArgs: nil, 337 err: nil, 338 }, 339 { 340 osArgs: []string{"./bin/etcdctl", "watch", "-i"}, 341 commandArgs: []string{"watch", "foo", "--rev", "1", "--", "echo", "Hello", "World"}, 342 interactive: true, 343 interactiveWatchPrefix: false, 344 interactiveWatchRev: 1, 345 interactiveWatchPrevKey: false, 346 watchArgs: []string{"foo"}, 347 execArgs: []string{"echo", "Hello", "World"}, 348 err: nil, 349 }, 350 { 351 osArgs: []string{"./bin/etcdctl", "watch", "-i"}, 352 commandArgs: []string{"watch", "--rev", "1", "foo", "--", "echo", "Hello", "World"}, 353 interactive: true, 354 interactiveWatchPrefix: false, 355 interactiveWatchRev: 1, 356 interactiveWatchPrevKey: false, 357 watchArgs: []string{"foo"}, 358 execArgs: []string{"echo", "Hello", "World"}, 359 err: nil, 360 }, 361 { 362 osArgs: []string{"./bin/etcdctl", "watch", "-i"}, 363 commandArgs: []string{"watch", "--rev", "5", "--prev-kv", "foo", "--", "echo", "Hello", "World"}, 364 interactive: true, 365 interactiveWatchPrefix: false, 366 interactiveWatchRev: 5, 367 interactiveWatchPrevKey: true, 368 watchArgs: []string{"foo"}, 369 execArgs: []string{"echo", "Hello", "World"}, 370 err: nil, 371 }, 372 { 373 osArgs: []string{"./bin/etcdctl", "watch", "-i"}, 374 commandArgs: []string{"watch", "--rev", "1"}, 375 envKey: "foo", 376 interactive: true, 377 interactiveWatchPrefix: false, 378 interactiveWatchRev: 1, 379 interactiveWatchPrevKey: false, 380 watchArgs: []string{"foo"}, 381 execArgs: nil, 382 err: nil, 383 }, 384 { 385 osArgs: []string{"./bin/etcdctl", "watch", "-i"}, 386 commandArgs: []string{"watch", "--rev", "1"}, 387 interactive: true, 388 interactiveWatchPrefix: false, 389 interactiveWatchRev: 0, 390 interactiveWatchPrevKey: false, 391 watchArgs: nil, 392 execArgs: nil, 393 err: errBadArgsNum, 394 }, 395 { 396 osArgs: []string{"./bin/etcdctl", "watch", "-i"}, 397 commandArgs: []string{"watch", "--rev", "1", "--prefix"}, 398 envKey: "foo", 399 interactive: true, 400 interactiveWatchPrefix: true, 401 interactiveWatchRev: 1, 402 interactiveWatchPrevKey: false, 403 watchArgs: []string{"foo"}, 404 execArgs: nil, 405 err: nil, 406 }, 407 { 408 osArgs: []string{"./bin/etcdctl", "watch", "-i"}, 409 commandArgs: []string{"watch", "--rev", "100", "--prefix", "--prev-kv"}, 410 envKey: "foo", 411 interactive: true, 412 interactiveWatchPrefix: true, 413 interactiveWatchRev: 100, 414 interactiveWatchPrevKey: true, 415 watchArgs: []string{"foo"}, 416 execArgs: nil, 417 err: nil, 418 }, 419 { 420 osArgs: []string{"./bin/etcdctl", "watch", "-i"}, 421 commandArgs: []string{"watch", "--rev", "1", "--prefix"}, 422 interactive: true, 423 interactiveWatchPrefix: false, 424 interactiveWatchRev: 0, 425 interactiveWatchPrevKey: false, 426 watchArgs: nil, 427 execArgs: nil, 428 err: errBadArgsNum, 429 }, 430 { 431 osArgs: []string{"./bin/etcdctl", "watch", "-i"}, 432 commandArgs: []string{"watch", "--", "echo", "Hello", "World"}, 433 envKey: "foo", 434 interactive: true, 435 interactiveWatchPrefix: false, 436 interactiveWatchRev: 0, 437 interactiveWatchPrevKey: false, 438 watchArgs: []string{"foo"}, 439 execArgs: []string{"echo", "Hello", "World"}, 440 err: nil, 441 }, 442 { 443 osArgs: []string{"./bin/etcdctl", "watch", "-i"}, 444 commandArgs: []string{"watch", "--", "echo", "Hello", "World"}, 445 envKey: "foo", 446 envRange: "bar", 447 interactive: true, 448 interactiveWatchPrefix: false, 449 interactiveWatchRev: 0, 450 interactiveWatchPrevKey: false, 451 watchArgs: []string{"foo", "bar"}, 452 execArgs: []string{"echo", "Hello", "World"}, 453 err: nil, 454 }, 455 { 456 osArgs: []string{"./bin/etcdctl", "watch", "-i"}, 457 commandArgs: []string{"watch", "foo", "bar", "--", "echo", "Hello", "World"}, 458 interactive: true, 459 interactiveWatchPrefix: false, 460 interactiveWatchRev: 0, 461 interactiveWatchPrevKey: false, 462 watchArgs: []string{"foo", "bar"}, 463 execArgs: []string{"echo", "Hello", "World"}, 464 err: nil, 465 }, 466 { 467 osArgs: []string{"./bin/etcdctl", "watch", "-i"}, 468 commandArgs: []string{"watch", "--rev", "1", "foo", "bar", "--", "echo", "Hello", "World"}, 469 interactive: true, 470 interactiveWatchPrefix: false, 471 interactiveWatchRev: 1, 472 interactiveWatchPrevKey: false, 473 watchArgs: []string{"foo", "bar"}, 474 execArgs: []string{"echo", "Hello", "World"}, 475 err: nil, 476 }, 477 { 478 osArgs: []string{"./bin/etcdctl", "watch", "-i"}, 479 commandArgs: []string{"watch", "--rev", "1", "--", "echo", "Hello", "World"}, 480 envKey: "foo", 481 envRange: "bar", 482 interactive: true, 483 interactiveWatchPrefix: false, 484 interactiveWatchRev: 1, 485 interactiveWatchPrevKey: false, 486 watchArgs: []string{"foo", "bar"}, 487 execArgs: []string{"echo", "Hello", "World"}, 488 err: nil, 489 }, 490 { 491 osArgs: []string{"./bin/etcdctl", "watch", "-i"}, 492 commandArgs: []string{"watch", "foo", "--rev", "1", "bar", "--", "echo", "Hello", "World"}, 493 interactive: true, 494 interactiveWatchPrefix: false, 495 interactiveWatchRev: 1, 496 interactiveWatchPrevKey: false, 497 watchArgs: []string{"foo", "bar"}, 498 execArgs: []string{"echo", "Hello", "World"}, 499 err: nil, 500 }, 501 { 502 osArgs: []string{"./bin/etcdctl", "watch", "-i"}, 503 commandArgs: []string{"watch", "foo", "bar", "--rev", "1", "--", "echo", "Hello", "World"}, 504 interactive: true, 505 interactiveWatchPrefix: false, 506 interactiveWatchRev: 1, 507 interactiveWatchPrevKey: false, 508 watchArgs: []string{"foo", "bar"}, 509 execArgs: []string{"echo", "Hello", "World"}, 510 err: nil, 511 }, 512 { 513 osArgs: []string{"./bin/etcdctl", "watch", "-i"}, 514 commandArgs: []string{"watch", "foo", "bar", "--rev", "7", "--prefix", "--", "echo", "Hello", "World"}, 515 interactive: true, 516 interactiveWatchPrefix: true, 517 interactiveWatchRev: 7, 518 interactiveWatchPrevKey: false, 519 watchArgs: []string{"foo", "bar"}, 520 execArgs: []string{"echo", "Hello", "World"}, 521 err: nil, 522 }, 523 { 524 osArgs: []string{"./bin/etcdctl", "watch", "-i"}, 525 commandArgs: []string{"watch", "foo", "bar", "--rev", "7", "--prefix", "--prev-kv", "--", "echo", "Hello", "World"}, 526 interactive: true, 527 interactiveWatchPrefix: true, 528 interactiveWatchRev: 7, 529 interactiveWatchPrevKey: true, 530 watchArgs: []string{"foo", "bar"}, 531 execArgs: []string{"echo", "Hello", "World"}, 532 err: nil, 533 }, 534 } 535 for i, ts := range tt { 536 watchArgs, execArgs, err := parseWatchArgs(ts.osArgs, ts.commandArgs, ts.envKey, ts.envRange, ts.interactive) 537 if err != ts.err { 538 t.Fatalf("#%d: error expected %v, got %v", i, ts.err, err) 539 } 540 if !reflect.DeepEqual(watchArgs, ts.watchArgs) { 541 t.Fatalf("#%d: watchArgs expected %q, got %v", i, ts.watchArgs, watchArgs) 542 } 543 if !reflect.DeepEqual(execArgs, ts.execArgs) { 544 t.Fatalf("#%d: execArgs expected %q, got %v", i, ts.execArgs, execArgs) 545 } 546 if ts.interactive { 547 if ts.interactiveWatchPrefix != watchPrefix { 548 t.Fatalf("#%d: interactive watchPrefix expected %v, got %v", i, ts.interactiveWatchPrefix, watchPrefix) 549 } 550 if ts.interactiveWatchRev != watchRev { 551 t.Fatalf("#%d: interactive watchRev expected %d, got %d", i, ts.interactiveWatchRev, watchRev) 552 } 553 if ts.interactiveWatchPrevKey != watchPrevKey { 554 t.Fatalf("#%d: interactive watchPrevKey expected %v, got %v", i, ts.interactiveWatchPrevKey, watchPrevKey) 555 } 556 } 557 } 558 }