opts_test.go 3.1 KB

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