opts_test.go 3.6 KB

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