metrics_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package metrics
  14. import (
  15. "testing"
  16. "gotest.tools/v3/assert"
  17. )
  18. func TestGetCommand(t *testing.T) {
  19. testCases := []struct {
  20. name string
  21. args []string
  22. expected string
  23. }{
  24. {
  25. name: "with long flags",
  26. args: []string{"--debug", "run"},
  27. expected: "run",
  28. },
  29. {
  30. name: "with short flags",
  31. args: []string{"-D", "run"},
  32. expected: "run",
  33. },
  34. {
  35. name: "with flags with value",
  36. args: []string{"--debug", "--str", "str-value", "run"},
  37. expected: "run",
  38. },
  39. {
  40. name: "with --",
  41. args: []string{"--debug", "--str", "str-value", "--", "run"},
  42. expected: "",
  43. },
  44. {
  45. name: "without a command",
  46. args: []string{"--debug", "--str", "str-value"},
  47. expected: "",
  48. },
  49. {
  50. name: "management command",
  51. args: []string{"image", "ls"},
  52. expected: "image ls",
  53. },
  54. {
  55. name: "management command with flag",
  56. args: []string{"image", "--test", "ls"},
  57. expected: "image ls",
  58. },
  59. {
  60. name: "management subcommand with flag",
  61. args: []string{"image", "ls", "-q"},
  62. expected: "image ls",
  63. },
  64. {
  65. name: "azure login",
  66. args: []string{"login", "azure"},
  67. expected: "login azure",
  68. },
  69. {
  70. name: "azure logout",
  71. args: []string{"logout", "azure"},
  72. expected: "logout azure",
  73. },
  74. {
  75. name: "azure login with flags",
  76. args: []string{"login", "-u", "test", "azure"},
  77. expected: "login azure",
  78. },
  79. {
  80. name: "login to a registry",
  81. args: []string{"login", "myregistry"},
  82. expected: "login",
  83. },
  84. {
  85. name: "logout from a registry",
  86. args: []string{"logout", "myregistry"},
  87. expected: "logout",
  88. },
  89. {
  90. name: "context create aci",
  91. args: []string{"context", "create", "aci"},
  92. expected: "context create aci",
  93. },
  94. {
  95. name: "context create ecs",
  96. args: []string{"context", "create", "ecs"},
  97. expected: "context create ecs",
  98. },
  99. {
  100. name: "create a context from another context",
  101. args: []string{"context", "create", "test-context", "--from=default"},
  102. expected: "context create",
  103. },
  104. {
  105. name: "create a container",
  106. args: []string{"create"},
  107. expected: "create",
  108. },
  109. {
  110. name: "start a container named aci",
  111. args: []string{"start", "aci"},
  112. expected: "start",
  113. },
  114. {
  115. name: "create a container named test-container",
  116. args: []string{"create", "test-container"},
  117. expected: "create",
  118. },
  119. {
  120. name: "create with flags",
  121. args: []string{"create", "--rm", "test"},
  122. expected: "create",
  123. },
  124. {
  125. name: "compose up -f xxx",
  126. args: []string{"compose", "up", "-f", "titi.yaml"},
  127. expected: "compose up",
  128. },
  129. {
  130. name: "compose -f xxx up",
  131. args: []string{"compose", "-f", "titi.yaml", "up"},
  132. expected: "compose up",
  133. },
  134. {
  135. name: "-D compose -f xxx up",
  136. args: []string{"--debug", "compose", "-f", "titi.yaml", "up"},
  137. expected: "compose up",
  138. },
  139. }
  140. for _, testCase := range testCases {
  141. t.Run(testCase.name, func(t *testing.T) {
  142. result := GetCommand(testCase.args)
  143. assert.Equal(t, testCase.expected, result)
  144. })
  145. }
  146. }
  147. func TestIgnoreHelpCommands(t *testing.T) {
  148. testCases := []struct {
  149. name string
  150. args []string
  151. expected string
  152. }{
  153. {
  154. name: "help",
  155. args: []string{"--help"},
  156. expected: "",
  157. },
  158. {
  159. name: "help on run",
  160. args: []string{"run", "--help"},
  161. expected: "",
  162. },
  163. {
  164. name: "help on compose up",
  165. args: []string{"compose", "up", "--help"},
  166. expected: "",
  167. },
  168. }
  169. for _, testCase := range testCases {
  170. t.Run(testCase.name, func(t *testing.T) {
  171. result := GetCommand(testCase.args)
  172. assert.Equal(t, testCase.expected, result)
  173. })
  174. }
  175. }
  176. func TestEcs(t *testing.T) {
  177. testCases := []struct {
  178. name string
  179. args []string
  180. expected string
  181. }{
  182. {
  183. name: "compose up",
  184. args: []string{"ecs", "compose", "-f", "test", "up"},
  185. expected: "ecs compose up",
  186. },
  187. {
  188. name: "compose up",
  189. args: []string{"ecs", "compose", "--file", "test", "up"},
  190. expected: "ecs compose up",
  191. },
  192. {
  193. name: "compose up",
  194. args: []string{"ecs", "compose", "--file", "test", "-n", "test", "up"},
  195. expected: "ecs compose up",
  196. },
  197. {
  198. name: "compose up",
  199. args: []string{"ecs", "compose", "--file", "test", "--project-name", "test", "up"},
  200. expected: "ecs compose up",
  201. },
  202. {
  203. name: "compose up",
  204. args: []string{"ecs", "compose", "up"},
  205. expected: "ecs compose up",
  206. },
  207. {
  208. name: "compose down",
  209. args: []string{"ecs", "compose", "-f", "test", "down"},
  210. expected: "ecs compose down",
  211. },
  212. {
  213. name: "compose down",
  214. args: []string{"ecs", "compose", "down"},
  215. expected: "ecs compose down",
  216. },
  217. {
  218. name: "compose ps",
  219. args: []string{"ecs", "compose", "-f", "test", "ps"},
  220. expected: "ecs compose ps",
  221. },
  222. {
  223. name: "compose ps",
  224. args: []string{"ecs", "compose", "ps"},
  225. expected: "ecs compose ps",
  226. },
  227. {
  228. name: "compose logs",
  229. args: []string{"ecs", "compose", "-f", "test", "logs"},
  230. expected: "ecs compose logs",
  231. },
  232. {
  233. name: "ecs",
  234. args: []string{"ecs", "anything"},
  235. expected: "ecs",
  236. },
  237. }
  238. for _, testCase := range testCases {
  239. t.Run(testCase.name, func(t *testing.T) {
  240. result := GetCommand(testCase.args)
  241. assert.Equal(t, testCase.expected, result)
  242. })
  243. }
  244. }
  245. func TestScan(t *testing.T) {
  246. testCases := []struct {
  247. name string
  248. args []string
  249. expected string
  250. }{
  251. {
  252. name: "scan",
  253. args: []string{"scan"},
  254. expected: "scan",
  255. },
  256. {
  257. name: "scan image with long flags",
  258. args: []string{"scan", "--file", "file", "myimage"},
  259. expected: "scan",
  260. },
  261. {
  262. name: "scan image with short flags",
  263. args: []string{"scan", "-f", "file", "myimage"},
  264. expected: "scan",
  265. },
  266. {
  267. name: "scan with long flag",
  268. args: []string{"scan", "--dependency-tree", "myimage"},
  269. expected: "scan",
  270. },
  271. {
  272. name: "auth",
  273. args: []string{"scan", "--login"},
  274. expected: "scan --login",
  275. },
  276. {
  277. name: "version",
  278. args: []string{"scan", "--version"},
  279. expected: "scan --version",
  280. },
  281. }
  282. for _, testCase := range testCases {
  283. t.Run(testCase.name, func(t *testing.T) {
  284. result := GetCommand(testCase.args)
  285. assert.Equal(t, testCase.expected, result)
  286. })
  287. }
  288. }