watch_command_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. watchArgs []string
  26. execArgs []string
  27. err error
  28. }{
  29. {
  30. osArgs: []string{"./bin/etcdctl", "watch", "foo", "bar"},
  31. commandArgs: []string{"foo", "bar"},
  32. interactive: false,
  33. watchArgs: []string{"foo", "bar"},
  34. execArgs: nil,
  35. err: nil,
  36. },
  37. {
  38. osArgs: []string{"./bin/etcdctl", "watch", "foo", "bar", "--"},
  39. commandArgs: []string{"foo", "bar"},
  40. interactive: false,
  41. watchArgs: nil,
  42. execArgs: nil,
  43. err: errBadArgsNumSeparator,
  44. },
  45. {
  46. osArgs: []string{"./bin/etcdctl", "watch"},
  47. commandArgs: nil,
  48. envKey: "foo",
  49. envRange: "bar",
  50. interactive: false,
  51. watchArgs: []string{"foo", "bar"},
  52. execArgs: nil,
  53. err: nil,
  54. },
  55. {
  56. osArgs: []string{"./bin/etcdctl", "watch", "foo"},
  57. commandArgs: []string{"foo"},
  58. envKey: "foo",
  59. envRange: "",
  60. interactive: false,
  61. watchArgs: nil,
  62. execArgs: nil,
  63. err: errBadArgsNumConflictEnv,
  64. },
  65. {
  66. osArgs: []string{"./bin/etcdctl", "watch", "foo", "bar"},
  67. commandArgs: []string{"foo", "bar"},
  68. envKey: "foo",
  69. envRange: "",
  70. interactive: false,
  71. watchArgs: nil,
  72. execArgs: nil,
  73. err: errBadArgsNumConflictEnv,
  74. },
  75. {
  76. osArgs: []string{"./bin/etcdctl", "watch", "foo", "bar"},
  77. commandArgs: []string{"foo", "bar"},
  78. envKey: "foo",
  79. envRange: "bar",
  80. interactive: false,
  81. watchArgs: nil,
  82. execArgs: nil,
  83. err: errBadArgsNumConflictEnv,
  84. },
  85. {
  86. osArgs: []string{"./bin/etcdctl", "watch", "foo"},
  87. commandArgs: []string{"foo"},
  88. interactive: false,
  89. watchArgs: []string{"foo"},
  90. execArgs: nil,
  91. err: nil,
  92. },
  93. {
  94. osArgs: []string{"./bin/etcdctl", "watch"},
  95. commandArgs: nil,
  96. envKey: "foo",
  97. interactive: false,
  98. watchArgs: []string{"foo"},
  99. execArgs: nil,
  100. err: nil,
  101. },
  102. {
  103. osArgs: []string{"./bin/etcdctl", "watch", "--rev", "1", "foo"},
  104. commandArgs: []string{"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. envKey: "foo",
  114. interactive: false,
  115. watchArgs: nil,
  116. execArgs: nil,
  117. err: errBadArgsNumConflictEnv,
  118. },
  119. {
  120. osArgs: []string{"./bin/etcdctl", "watch", "--rev", "1"},
  121. commandArgs: nil,
  122. envKey: "foo",
  123. interactive: false,
  124. watchArgs: []string{"foo"},
  125. execArgs: nil,
  126. err: nil,
  127. },
  128. {
  129. osArgs: []string{"./bin/etcdctl", "watch", "foo", "--rev", "1"},
  130. commandArgs: []string{"foo"},
  131. interactive: false,
  132. watchArgs: []string{"foo"},
  133. execArgs: nil,
  134. err: nil,
  135. },
  136. {
  137. osArgs: []string{"./bin/etcdctl", "watch", "foo", "--", "echo", "Hello", "World"},
  138. commandArgs: []string{"foo", "echo", "Hello", "World"},
  139. interactive: false,
  140. watchArgs: []string{"foo"},
  141. execArgs: []string{"echo", "Hello", "World"},
  142. err: nil,
  143. },
  144. {
  145. osArgs: []string{"./bin/etcdctl", "watch", "foo", "--rev", "1", "--", "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", "bar", "--", "echo", "Hello", "World"},
  154. commandArgs: []string{"foo", "bar", "echo", "Hello", "World"},
  155. interactive: false,
  156. watchArgs: []string{"foo", "bar"},
  157. execArgs: []string{"echo", "Hello", "World"},
  158. err: nil,
  159. },
  160. {
  161. osArgs: []string{"./bin/etcdctl", "watch", "--rev", "1", "foo", "bar", "--", "echo", "Hello", "World"},
  162. commandArgs: []string{"foo", "bar", "echo", "Hello", "World"},
  163. interactive: false,
  164. watchArgs: []string{"foo", "bar"},
  165. execArgs: []string{"echo", "Hello", "World"},
  166. err: nil,
  167. },
  168. {
  169. osArgs: []string{"./bin/etcdctl", "watch", "foo", "--rev", "1", "bar", "--", "echo", "Hello", "World"},
  170. commandArgs: []string{"foo", "bar", "echo", "Hello", "World"},
  171. interactive: false,
  172. watchArgs: []string{"foo", "bar"},
  173. execArgs: []string{"echo", "Hello", "World"},
  174. err: nil,
  175. },
  176. {
  177. osArgs: []string{"./bin/etcdctl", "watch", "foo", "bar", "--rev", "1", "--", "echo", "Hello", "World"},
  178. commandArgs: []string{"foo", "bar", "echo", "Hello", "World"},
  179. interactive: false,
  180. watchArgs: []string{"foo", "bar"},
  181. execArgs: []string{"echo", "Hello", "World"},
  182. err: nil,
  183. },
  184. {
  185. osArgs: []string{"./bin/etcdctl", "watch", "--rev", "1", "--", "echo", "Hello", "World"},
  186. commandArgs: []string{"echo", "Hello", "World"},
  187. envKey: "foo",
  188. envRange: "",
  189. interactive: false,
  190. watchArgs: []string{"foo"},
  191. execArgs: []string{"echo", "Hello", "World"},
  192. err: nil,
  193. },
  194. {
  195. osArgs: []string{"./bin/etcdctl", "watch", "--rev", "1", "--", "echo", "Hello", "World"},
  196. commandArgs: []string{"echo", "Hello", "World"},
  197. envKey: "foo",
  198. envRange: "bar",
  199. interactive: false,
  200. watchArgs: []string{"foo", "bar"},
  201. execArgs: []string{"echo", "Hello", "World"},
  202. err: nil,
  203. },
  204. {
  205. osArgs: []string{"./bin/etcdctl", "watch", "foo", "bar", "--rev", "1", "--", "echo", "Hello", "World"},
  206. commandArgs: []string{"foo", "bar", "echo", "Hello", "World"},
  207. envKey: "foo",
  208. interactive: false,
  209. watchArgs: nil,
  210. execArgs: nil,
  211. err: errBadArgsNumConflictEnv,
  212. },
  213. {
  214. osArgs: []string{"./bin/etcdctl", "watch", "-i"},
  215. commandArgs: []string{"foo", "bar", "--", "echo", "Hello", "World"},
  216. interactive: true,
  217. watchArgs: nil,
  218. execArgs: nil,
  219. err: errBadArgsInteractiveWatch,
  220. },
  221. {
  222. osArgs: []string{"./bin/etcdctl", "watch", "-i"},
  223. commandArgs: []string{"watch", "foo"},
  224. interactive: true,
  225. watchArgs: []string{"foo"},
  226. execArgs: nil,
  227. err: nil,
  228. },
  229. {
  230. osArgs: []string{"./bin/etcdctl", "watch", "-i"},
  231. commandArgs: []string{"watch", "foo", "bar"},
  232. interactive: true,
  233. watchArgs: []string{"foo", "bar"},
  234. execArgs: nil,
  235. err: nil,
  236. },
  237. {
  238. osArgs: []string{"./bin/etcdctl", "watch", "-i"},
  239. commandArgs: []string{"watch"},
  240. envKey: "foo",
  241. envRange: "bar",
  242. interactive: true,
  243. watchArgs: []string{"foo", "bar"},
  244. execArgs: nil,
  245. err: nil,
  246. },
  247. {
  248. osArgs: []string{"./bin/etcdctl", "watch", "-i"},
  249. commandArgs: []string{"watch"},
  250. envKey: "hello world!",
  251. envRange: "bar",
  252. interactive: true,
  253. watchArgs: []string{"hello world!", "bar"},
  254. execArgs: nil,
  255. err: nil,
  256. },
  257. {
  258. osArgs: []string{"./bin/etcdctl", "watch", "-i"},
  259. commandArgs: []string{"watch", "foo", "--rev", "1"},
  260. interactive: true,
  261. watchArgs: []string{"foo"},
  262. execArgs: nil,
  263. err: nil,
  264. },
  265. {
  266. osArgs: []string{"./bin/etcdctl", "watch", "-i"},
  267. commandArgs: []string{"watch", "foo", "--rev", "1", "--", "echo", "Hello", "World"},
  268. interactive: true,
  269. watchArgs: []string{"foo"},
  270. execArgs: []string{"echo", "Hello", "World"},
  271. err: nil,
  272. },
  273. {
  274. osArgs: []string{"./bin/etcdctl", "watch", "-i"},
  275. commandArgs: []string{"watch", "--rev", "1", "foo", "--", "echo", "Hello", "World"},
  276. interactive: true,
  277. watchArgs: []string{"foo"},
  278. execArgs: []string{"echo", "Hello", "World"},
  279. err: nil,
  280. },
  281. {
  282. osArgs: []string{"./bin/etcdctl", "watch", "-i"},
  283. commandArgs: []string{"watch", "--", "echo", "Hello", "World"},
  284. envKey: "foo",
  285. interactive: true,
  286. watchArgs: []string{"foo"},
  287. execArgs: []string{"echo", "Hello", "World"},
  288. err: nil,
  289. },
  290. {
  291. osArgs: []string{"./bin/etcdctl", "watch", "-i"},
  292. commandArgs: []string{"watch", "--", "echo", "Hello", "World"},
  293. envKey: "foo",
  294. envRange: "bar",
  295. interactive: true,
  296. watchArgs: []string{"foo", "bar"},
  297. execArgs: []string{"echo", "Hello", "World"},
  298. err: nil,
  299. },
  300. {
  301. osArgs: []string{"./bin/etcdctl", "watch", "-i"},
  302. commandArgs: []string{"watch", "foo", "bar", "--", "echo", "Hello", "World"},
  303. interactive: true,
  304. watchArgs: []string{"foo", "bar"},
  305. execArgs: []string{"echo", "Hello", "World"},
  306. err: nil,
  307. },
  308. {
  309. osArgs: []string{"./bin/etcdctl", "watch", "-i"},
  310. commandArgs: []string{"watch", "--rev", "1", "foo", "bar", "--", "echo", "Hello", "World"},
  311. interactive: true,
  312. watchArgs: []string{"foo", "bar"},
  313. execArgs: []string{"echo", "Hello", "World"},
  314. err: nil,
  315. },
  316. {
  317. osArgs: []string{"./bin/etcdctl", "watch", "-i"},
  318. commandArgs: []string{"watch", "--rev", "1", "--", "echo", "Hello", "World"},
  319. envKey: "foo",
  320. envRange: "bar",
  321. interactive: true,
  322. watchArgs: []string{"foo", "bar"},
  323. execArgs: []string{"echo", "Hello", "World"},
  324. err: nil,
  325. },
  326. {
  327. osArgs: []string{"./bin/etcdctl", "watch", "-i"},
  328. commandArgs: []string{"watch", "foo", "--rev", "1", "bar", "--", "echo", "Hello", "World"},
  329. interactive: true,
  330. watchArgs: []string{"foo", "bar"},
  331. execArgs: []string{"echo", "Hello", "World"},
  332. err: nil,
  333. },
  334. {
  335. osArgs: []string{"./bin/etcdctl", "watch", "-i"},
  336. commandArgs: []string{"watch", "foo", "bar", "--rev", "1", "--", "echo", "Hello", "World"},
  337. interactive: true,
  338. watchArgs: []string{"foo", "bar"},
  339. execArgs: []string{"echo", "Hello", "World"},
  340. err: nil,
  341. },
  342. }
  343. for i, ts := range tt {
  344. watchArgs, execArgs, err := parseWatchArgs(ts.osArgs, ts.commandArgs, ts.envKey, ts.envRange, ts.interactive)
  345. if err != ts.err {
  346. t.Fatalf("#%d: error expected %v, got %v", i, ts.err, err)
  347. }
  348. if !reflect.DeepEqual(watchArgs, ts.watchArgs) {
  349. t.Fatalf("#%d: watchArgs expected %q, got %v", i, ts.watchArgs, watchArgs)
  350. }
  351. if !reflect.DeepEqual(execArgs, ts.execArgs) {
  352. t.Fatalf("#%d: execArgs expected %q, got %v", i, ts.execArgs, execArgs)
  353. }
  354. }
  355. }