watch_command_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. interactive bool
  24. watchArgs []string
  25. execArgs []string
  26. err error
  27. }{
  28. {
  29. osArgs: []string{"./bin/etcdctl", "watch", "foo", "bar"},
  30. commandArgs: []string{"foo", "bar"},
  31. interactive: false,
  32. watchArgs: []string{"foo", "bar"},
  33. execArgs: nil,
  34. err: nil,
  35. },
  36. {
  37. osArgs: []string{"./bin/etcdctl", "watch", "foo", "bar", "--"},
  38. commandArgs: []string{"foo", "bar"},
  39. interactive: false,
  40. watchArgs: nil,
  41. execArgs: nil,
  42. err: errBadArgsNumSeparator,
  43. },
  44. {
  45. osArgs: []string{"./bin/etcdctl", "watch", "foo"},
  46. commandArgs: []string{"foo"},
  47. interactive: false,
  48. watchArgs: []string{"foo"},
  49. execArgs: nil,
  50. err: nil,
  51. },
  52. {
  53. osArgs: []string{"./bin/etcdctl", "watch", "--rev", "1", "foo"},
  54. commandArgs: []string{"foo"},
  55. interactive: false,
  56. watchArgs: []string{"foo"},
  57. execArgs: nil,
  58. err: nil,
  59. },
  60. {
  61. osArgs: []string{"./bin/etcdctl", "watch", "foo", "--rev", "1"},
  62. commandArgs: []string{"foo"},
  63. interactive: false,
  64. watchArgs: []string{"foo"},
  65. execArgs: nil,
  66. err: nil,
  67. },
  68. {
  69. osArgs: []string{"./bin/etcdctl", "watch", "foo", "--", "echo", "Hello", "World"},
  70. commandArgs: []string{"foo", "echo", "Hello", "World"},
  71. interactive: false,
  72. watchArgs: []string{"foo"},
  73. execArgs: []string{"echo", "Hello", "World"},
  74. err: nil,
  75. },
  76. {
  77. osArgs: []string{"./bin/etcdctl", "watch", "foo", "--rev", "1", "--", "echo", "Hello", "World"},
  78. commandArgs: []string{"foo", "echo", "Hello", "World"},
  79. interactive: false,
  80. watchArgs: []string{"foo"},
  81. execArgs: []string{"echo", "Hello", "World"},
  82. err: nil,
  83. },
  84. {
  85. osArgs: []string{"./bin/etcdctl", "watch", "foo", "bar", "--", "echo", "Hello", "World"},
  86. commandArgs: []string{"foo", "bar", "echo", "Hello", "World"},
  87. interactive: false,
  88. watchArgs: []string{"foo", "bar"},
  89. execArgs: []string{"echo", "Hello", "World"},
  90. err: nil,
  91. },
  92. {
  93. osArgs: []string{"./bin/etcdctl", "watch", "--rev", "1", "foo", "bar", "--", "echo", "Hello", "World"},
  94. commandArgs: []string{"foo", "bar", "echo", "Hello", "World"},
  95. interactive: false,
  96. watchArgs: []string{"foo", "bar"},
  97. execArgs: []string{"echo", "Hello", "World"},
  98. err: nil,
  99. },
  100. {
  101. osArgs: []string{"./bin/etcdctl", "watch", "foo", "--rev", "1", "bar", "--", "echo", "Hello", "World"},
  102. commandArgs: []string{"foo", "bar", "echo", "Hello", "World"},
  103. interactive: false,
  104. watchArgs: []string{"foo", "bar"},
  105. execArgs: []string{"echo", "Hello", "World"},
  106. err: nil,
  107. },
  108. {
  109. osArgs: []string{"./bin/etcdctl", "watch", "foo", "bar", "--rev", "1", "--", "echo", "Hello", "World"},
  110. commandArgs: []string{"foo", "bar", "echo", "Hello", "World"},
  111. interactive: false,
  112. watchArgs: []string{"foo", "bar"},
  113. execArgs: []string{"echo", "Hello", "World"},
  114. err: nil,
  115. },
  116. {
  117. osArgs: []string{"./bin/etcdctl", "watch", "-i"},
  118. commandArgs: []string{"foo", "bar", "--", "echo", "Hello", "World"},
  119. interactive: true,
  120. watchArgs: nil,
  121. execArgs: nil,
  122. err: errBadArgsInteractiveWatch,
  123. },
  124. {
  125. osArgs: []string{"./bin/etcdctl", "watch", "-i"},
  126. commandArgs: []string{"watch", "foo"},
  127. interactive: true,
  128. watchArgs: []string{"foo"},
  129. execArgs: nil,
  130. err: nil,
  131. },
  132. {
  133. osArgs: []string{"./bin/etcdctl", "watch", "-i"},
  134. commandArgs: []string{"watch", "foo", "bar"},
  135. interactive: true,
  136. watchArgs: []string{"foo", "bar"},
  137. execArgs: nil,
  138. err: nil,
  139. },
  140. {
  141. osArgs: []string{"./bin/etcdctl", "watch", "-i"},
  142. commandArgs: []string{"watch", "foo", "--rev", "1"},
  143. interactive: true,
  144. watchArgs: []string{"foo"},
  145. execArgs: nil,
  146. err: nil,
  147. },
  148. {
  149. osArgs: []string{"./bin/etcdctl", "watch", "-i"},
  150. commandArgs: []string{"watch", "foo", "--rev", "1", "--", "echo", "Hello", "World"},
  151. interactive: true,
  152. watchArgs: []string{"foo"},
  153. execArgs: []string{"echo", "Hello", "World"},
  154. err: nil,
  155. },
  156. {
  157. osArgs: []string{"./bin/etcdctl", "watch", "-i"},
  158. commandArgs: []string{"watch", "--rev", "1", "foo", "--", "echo", "Hello", "World"},
  159. interactive: true,
  160. watchArgs: []string{"foo"},
  161. execArgs: []string{"echo", "Hello", "World"},
  162. err: nil,
  163. },
  164. {
  165. osArgs: []string{"./bin/etcdctl", "watch", "-i"},
  166. commandArgs: []string{"watch", "foo", "bar", "--", "echo", "Hello", "World"},
  167. interactive: true,
  168. watchArgs: []string{"foo", "bar"},
  169. execArgs: []string{"echo", "Hello", "World"},
  170. err: nil,
  171. },
  172. {
  173. osArgs: []string{"./bin/etcdctl", "watch", "-i"},
  174. commandArgs: []string{"watch", "--rev", "1", "foo", "bar", "--", "echo", "Hello", "World"},
  175. interactive: true,
  176. watchArgs: []string{"foo", "bar"},
  177. execArgs: []string{"echo", "Hello", "World"},
  178. err: nil,
  179. },
  180. {
  181. osArgs: []string{"./bin/etcdctl", "watch", "-i"},
  182. commandArgs: []string{"watch", "foo", "--rev", "1", "bar", "--", "echo", "Hello", "World"},
  183. interactive: true,
  184. watchArgs: []string{"foo", "bar"},
  185. execArgs: []string{"echo", "Hello", "World"},
  186. err: nil,
  187. },
  188. {
  189. osArgs: []string{"./bin/etcdctl", "watch", "-i"},
  190. commandArgs: []string{"watch", "foo", "bar", "--rev", "1", "--", "echo", "Hello", "World"},
  191. interactive: true,
  192. watchArgs: []string{"foo", "bar"},
  193. execArgs: []string{"echo", "Hello", "World"},
  194. err: nil,
  195. },
  196. }
  197. for i, ts := range tt {
  198. watchArgs, execArgs, err := parseWatchArgs(ts.osArgs, ts.commandArgs, ts.interactive)
  199. if err != ts.err {
  200. t.Fatalf("#%d: error expected %v, got %v", i, ts.err, err)
  201. }
  202. if !reflect.DeepEqual(watchArgs, ts.watchArgs) {
  203. t.Fatalf("#%d: watchArgs expected %q, got %v", i, ts.watchArgs, watchArgs)
  204. }
  205. if !reflect.DeepEqual(execArgs, ts.execArgs) {
  206. t.Fatalf("#%d: execArgs expected %q, got %v", i, ts.execArgs, execArgs)
  207. }
  208. }
  209. }