opts_test.go 3.7 KB

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