metics_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. Copyright 2020 Docker, Inc.
  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. "github.com/spf13/cobra"
  17. "github.com/stretchr/testify/assert"
  18. )
  19. func TestFlag(t *testing.T) {
  20. root := &cobra.Command{}
  21. root.PersistentFlags().BoolP("debug", "d", false, "debug")
  22. root.PersistentFlags().String("str", "str", "str")
  23. testCases := []struct {
  24. name string
  25. args []string
  26. expected string
  27. }{
  28. {
  29. name: "with long flags",
  30. args: []string{"--debug", "run"},
  31. expected: "run",
  32. },
  33. {
  34. name: "with short flags",
  35. args: []string{"-d", "run"},
  36. expected: "run",
  37. },
  38. {
  39. name: "with flags with value",
  40. args: []string{"--debug", "--str", "str-value", "run"},
  41. expected: "run",
  42. },
  43. {
  44. name: "with --",
  45. args: []string{"--debug", "--str", "str-value", "--", "run"},
  46. expected: "",
  47. },
  48. {
  49. name: "without a command",
  50. args: []string{"--debug", "--str", "str-value"},
  51. expected: "",
  52. },
  53. {
  54. name: "with unknown short flag",
  55. args: []string{"-f", "run"},
  56. expected: "",
  57. },
  58. {
  59. name: "with unknown long flag",
  60. args: []string{"--unknown-flag", "run"},
  61. expected: "",
  62. },
  63. {
  64. name: "management command",
  65. args: []string{"image", "ls"},
  66. expected: "image ls",
  67. },
  68. {
  69. name: "management command with flag",
  70. args: []string{"image", "--test", "ls"},
  71. expected: "image",
  72. },
  73. {
  74. name: "management subcommand with flag",
  75. args: []string{"image", "ls", "-q"},
  76. expected: "image ls",
  77. },
  78. }
  79. for _, testCase := range testCases {
  80. t.Run(testCase.name, func(t *testing.T) {
  81. result := getCommand(testCase.args, root.PersistentFlags())
  82. assert.Equal(t, testCase.expected, result)
  83. })
  84. }
  85. }
  86. func TestEcs(t *testing.T) {
  87. root := &cobra.Command{}
  88. testCases := []struct {
  89. name string
  90. args []string
  91. expected string
  92. }{
  93. {
  94. name: "compose up",
  95. args: []string{"ecs", "compose", "-f", "test", "up"},
  96. expected: "ecs compose up",
  97. },
  98. {
  99. name: "compose up",
  100. args: []string{"ecs", "compose", "--file", "test", "up"},
  101. expected: "ecs compose up",
  102. },
  103. {
  104. name: "compose up",
  105. args: []string{"ecs", "compose", "--file", "test", "-n", "test", "up"},
  106. expected: "ecs compose up",
  107. },
  108. {
  109. name: "compose up",
  110. args: []string{"ecs", "compose", "--file", "test", "--project-name", "test", "up"},
  111. expected: "ecs compose up",
  112. },
  113. {
  114. name: "compose up",
  115. args: []string{"ecs", "compose", "up"},
  116. expected: "ecs compose up",
  117. },
  118. {
  119. name: "compose down",
  120. args: []string{"ecs", "compose", "-f", "test", "down"},
  121. expected: "ecs compose down",
  122. },
  123. {
  124. name: "compose down",
  125. args: []string{"ecs", "compose", "down"},
  126. expected: "ecs compose down",
  127. },
  128. {
  129. name: "compose ps",
  130. args: []string{"ecs", "compose", "-f", "test", "ps"},
  131. expected: "ecs compose ps",
  132. },
  133. {
  134. name: "compose ps",
  135. args: []string{"ecs", "compose", "ps"},
  136. expected: "ecs compose ps",
  137. },
  138. {
  139. name: "compose logs",
  140. args: []string{"ecs", "compose", "-f", "test", "logs"},
  141. expected: "ecs compose logs",
  142. },
  143. {
  144. name: "setup",
  145. args: []string{"ecs", "setup"},
  146. expected: "ecs setup",
  147. },
  148. }
  149. for _, testCase := range testCases {
  150. t.Run(testCase.name, func(t *testing.T) {
  151. result := getCommand(testCase.args, root.PersistentFlags())
  152. assert.Equal(t, testCase.expected, result)
  153. })
  154. }
  155. }
  156. func TestScan(t *testing.T) {
  157. root := &cobra.Command{}
  158. testCases := []struct {
  159. name string
  160. args []string
  161. expected string
  162. }{
  163. {
  164. name: "scan",
  165. args: []string{"scan"},
  166. expected: "scan",
  167. },
  168. {
  169. name: "scan image with long flags",
  170. args: []string{"scan", "--file", "file", "image"},
  171. expected: "scan",
  172. },
  173. {
  174. name: "scan image with short flags",
  175. args: []string{"scan", "-f", "file", "image"},
  176. expected: "scan",
  177. },
  178. {
  179. name: "scan with long flag",
  180. args: []string{"scan", "--dependency-tree", "image"},
  181. expected: "scan",
  182. },
  183. {
  184. name: "auth",
  185. args: []string{"scan", "--auth"},
  186. expected: "scan auth",
  187. },
  188. {
  189. name: "version",
  190. args: []string{"scan", "--version"},
  191. expected: "scan version",
  192. },
  193. }
  194. for _, testCase := range testCases {
  195. t.Run(testCase.name, func(t *testing.T) {
  196. result := getCommand(testCase.args, root.PersistentFlags())
  197. assert.Equal(t, testCase.expected, result)
  198. })
  199. }
  200. }