opts_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 run
  14. import (
  15. "errors"
  16. "regexp"
  17. "testing"
  18. "time"
  19. "github.com/compose-spec/compose-go/types"
  20. "github.com/google/go-cmp/cmp/cmpopts"
  21. "gotest.tools/v3/assert"
  22. "gotest.tools/v3/assert/cmp"
  23. "github.com/docker/compose-cli/api/containers"
  24. )
  25. var (
  26. // AzureNameRegex is used to validate container names
  27. // Regex was taken from server side error:
  28. // The container name must contain no more than 63 characters and must match the regex '[a-z0-9]([-a-z0-9]*[a-z0-9])?' (e.g. 'my-name').
  29. AzureNameRegex = regexp.MustCompile("[a-z0-9]([-a-z0-9]*[a-z0-9])")
  30. )
  31. // TestAzureRandomName ensures compliance with Azure naming requirements
  32. func TestAzureRandomName(t *testing.T) {
  33. n := getRandomName()
  34. assert.Assert(t, len(n) < 64)
  35. assert.Assert(t, len(n) > 1)
  36. assert.Assert(t, cmp.Regexp(AzureNameRegex, n))
  37. }
  38. func TestPortParse(t *testing.T) {
  39. testCases := []struct {
  40. in string
  41. expected []containers.Port
  42. }{
  43. {
  44. in: "80",
  45. expected: []containers.Port{
  46. {
  47. HostPort: 80,
  48. ContainerPort: 80,
  49. Protocol: "tcp",
  50. },
  51. },
  52. },
  53. {
  54. in: "80:80",
  55. expected: []containers.Port{
  56. {
  57. HostPort: 80,
  58. ContainerPort: 80,
  59. Protocol: "tcp",
  60. },
  61. },
  62. },
  63. {
  64. in: "80:80/udp",
  65. expected: []containers.Port{
  66. {
  67. ContainerPort: 80,
  68. HostPort: 80,
  69. Protocol: "udp",
  70. },
  71. },
  72. },
  73. {
  74. in: "8080:80",
  75. expected: []containers.Port{
  76. {
  77. HostPort: 8080,
  78. ContainerPort: 80,
  79. Protocol: "tcp",
  80. },
  81. },
  82. },
  83. {
  84. in: "192.168.0.2:8080:80",
  85. expected: []containers.Port{
  86. {
  87. HostPort: 8080,
  88. ContainerPort: 80,
  89. Protocol: "tcp",
  90. HostIP: "192.168.0.2",
  91. },
  92. },
  93. },
  94. {
  95. in: "80-81:80-81",
  96. expected: []containers.Port{
  97. {
  98. HostPort: 80,
  99. ContainerPort: 80,
  100. Protocol: "tcp",
  101. },
  102. {
  103. HostPort: 81,
  104. ContainerPort: 81,
  105. Protocol: "tcp",
  106. },
  107. },
  108. },
  109. }
  110. for _, testCase := range testCases {
  111. opts := Opts{
  112. Publish: []string{testCase.in},
  113. }
  114. result, err := opts.toPorts()
  115. assert.NilError(t, err)
  116. assert.DeepEqual(t, result, testCase.expected, cmpopts.SortSlices(func(x, y containers.Port) bool {
  117. return x.ContainerPort < y.ContainerPort
  118. }))
  119. }
  120. }
  121. func TestLabels(t *testing.T) {
  122. testCases := []struct {
  123. in []string
  124. expected map[string]string
  125. expectedError error
  126. }{
  127. {
  128. in: []string{"label=value"},
  129. expected: map[string]string{
  130. "label": "value",
  131. },
  132. expectedError: nil,
  133. },
  134. {
  135. in: []string{"label=value", "label=value2"},
  136. expected: map[string]string{
  137. "label": "value2",
  138. },
  139. expectedError: nil,
  140. },
  141. {
  142. in: []string{"label=value", "label2=value2"},
  143. expected: map[string]string{
  144. "label": "value",
  145. "label2": "value2",
  146. },
  147. expectedError: nil,
  148. },
  149. {
  150. in: []string{"label"},
  151. expected: nil,
  152. expectedError: errors.New(`wrong label format "label"`),
  153. },
  154. }
  155. for _, testCase := range testCases {
  156. result, err := toLabels(testCase.in)
  157. if testCase.expectedError == nil {
  158. assert.NilError(t, err)
  159. } else {
  160. assert.Error(t, err, testCase.expectedError.Error())
  161. }
  162. assert.DeepEqual(t, result, testCase.expected)
  163. }
  164. }
  165. func TestValidateRestartPolicy(t *testing.T) {
  166. testCases := []struct {
  167. in string
  168. expected string
  169. expectedError error
  170. }{
  171. {
  172. in: "none",
  173. expected: "none",
  174. expectedError: nil,
  175. },
  176. {
  177. in: "any",
  178. expected: "any",
  179. expectedError: nil,
  180. },
  181. {
  182. in: "on-failure",
  183. expected: "on-failure",
  184. expectedError: nil,
  185. },
  186. {
  187. in: "",
  188. expected: "none",
  189. expectedError: nil,
  190. },
  191. {
  192. in: "no",
  193. expected: "none",
  194. expectedError: nil,
  195. },
  196. {
  197. in: "always",
  198. expected: "any",
  199. expectedError: nil,
  200. },
  201. {
  202. in: "toto",
  203. expected: "",
  204. expectedError: errors.New("invalid restart value, must be one of no, always, on-failure"),
  205. },
  206. }
  207. for _, testCase := range testCases {
  208. result, err := toRestartPolicy(testCase.in)
  209. if testCase.expectedError == nil {
  210. assert.NilError(t, err)
  211. } else {
  212. assert.Error(t, err, testCase.expectedError.Error())
  213. }
  214. assert.Equal(t, testCase.expected, result)
  215. }
  216. }
  217. func TestToHealthcheck(t *testing.T) {
  218. testOpt := Opts{
  219. HealthCmd: "curl",
  220. }
  221. assert.DeepEqual(t, testOpt.toHealthcheck(), containers.Healthcheck{
  222. Disable: false,
  223. Test: []string{"curl"},
  224. })
  225. testOpt = Opts{
  226. HealthCmd: "curl",
  227. HealthRetries: 3,
  228. HealthInterval: 5 * time.Second,
  229. HealthTimeout: 2 * time.Second,
  230. HealthStartPeriod: 10 * time.Second,
  231. }
  232. assert.DeepEqual(t, testOpt.toHealthcheck(), containers.Healthcheck{
  233. Disable: false,
  234. Test: []string{"curl"},
  235. Retries: 3,
  236. Interval: types.Duration(5 * time.Second),
  237. StartPeriod: types.Duration(10 * time.Second),
  238. Timeout: types.Duration(2 * time.Second),
  239. })
  240. }