watch_command_test.go 20 KB

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