opts_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 run
  14. import (
  15. "errors"
  16. "regexp"
  17. "testing"
  18. "github.com/google/go-cmp/cmp/cmpopts"
  19. "gotest.tools/v3/assert"
  20. "gotest.tools/v3/assert/cmp"
  21. "github.com/docker/api/containers"
  22. )
  23. var (
  24. // AzureNameRegex is used to validate container names
  25. // Regex was taken from server side error:
  26. // 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').
  27. AzureNameRegex = regexp.MustCompile("[a-z0-9]([-a-z0-9]*[a-z0-9])")
  28. )
  29. // TestAzureRandomName ensures compliance with Azure naming requirements
  30. func TestAzureRandomName(t *testing.T) {
  31. n := getRandomName()
  32. assert.Assert(t, len(n) < 64)
  33. assert.Assert(t, len(n) > 1)
  34. assert.Assert(t, cmp.Regexp(AzureNameRegex, n))
  35. }
  36. func TestPortParse(t *testing.T) {
  37. testCases := []struct {
  38. in string
  39. expected []containers.Port
  40. }{
  41. {
  42. in: "80",
  43. expected: []containers.Port{
  44. {
  45. HostPort: 80,
  46. ContainerPort: 80,
  47. Protocol: "tcp",
  48. },
  49. },
  50. },
  51. {
  52. in: "80:80",
  53. expected: []containers.Port{
  54. {
  55. HostPort: 80,
  56. ContainerPort: 80,
  57. Protocol: "tcp",
  58. },
  59. },
  60. },
  61. {
  62. in: "80:80/udp",
  63. expected: []containers.Port{
  64. {
  65. ContainerPort: 80,
  66. HostPort: 80,
  67. Protocol: "udp",
  68. },
  69. },
  70. },
  71. {
  72. in: "8080:80",
  73. expected: []containers.Port{
  74. {
  75. HostPort: 8080,
  76. ContainerPort: 80,
  77. Protocol: "tcp",
  78. },
  79. },
  80. },
  81. {
  82. in: "192.168.0.2:8080:80",
  83. expected: []containers.Port{
  84. {
  85. HostPort: 8080,
  86. ContainerPort: 80,
  87. Protocol: "tcp",
  88. HostIP: "192.168.0.2",
  89. },
  90. },
  91. },
  92. {
  93. in: "80-81:80-81",
  94. expected: []containers.Port{
  95. {
  96. HostPort: 80,
  97. ContainerPort: 80,
  98. Protocol: "tcp",
  99. },
  100. {
  101. HostPort: 81,
  102. ContainerPort: 81,
  103. Protocol: "tcp",
  104. },
  105. },
  106. },
  107. }
  108. for _, testCase := range testCases {
  109. opts := Opts{
  110. Publish: []string{testCase.in},
  111. }
  112. result, err := opts.toPorts()
  113. assert.NilError(t, err)
  114. assert.DeepEqual(t, result, testCase.expected, cmpopts.SortSlices(func(x, y containers.Port) bool {
  115. return x.ContainerPort < y.ContainerPort
  116. }))
  117. }
  118. }
  119. func TestLabels(t *testing.T) {
  120. testCases := []struct {
  121. in []string
  122. expected map[string]string
  123. expectedError error
  124. }{
  125. {
  126. in: []string{"label=value"},
  127. expected: map[string]string{
  128. "label": "value",
  129. },
  130. expectedError: nil,
  131. },
  132. {
  133. in: []string{"label=value", "label=value2"},
  134. expected: map[string]string{
  135. "label": "value2",
  136. },
  137. expectedError: nil,
  138. },
  139. {
  140. in: []string{"label=value", "label2=value2"},
  141. expected: map[string]string{
  142. "label": "value",
  143. "label2": "value2",
  144. },
  145. expectedError: nil,
  146. },
  147. {
  148. in: []string{"label"},
  149. expected: nil,
  150. expectedError: errors.New(`wrong label format "label"`),
  151. },
  152. }
  153. for _, testCase := range testCases {
  154. result, err := toLabels(testCase.in)
  155. if testCase.expectedError == nil {
  156. assert.NilError(t, err)
  157. } else {
  158. assert.Error(t, err, testCase.expectedError.Error())
  159. }
  160. assert.DeepEqual(t, result, testCase.expected)
  161. }
  162. }