port_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. package opts
  2. import (
  3. "testing"
  4. "github.com/docker/docker/api/types/swarm"
  5. "github.com/docker/docker/pkg/testutil/assert"
  6. )
  7. func TestPortOptValidSimpleSyntax(t *testing.T) {
  8. testCases := []struct {
  9. value string
  10. expected []swarm.PortConfig
  11. }{
  12. {
  13. value: "80",
  14. expected: []swarm.PortConfig{
  15. {
  16. Protocol: "tcp",
  17. TargetPort: 80,
  18. PublishMode: swarm.PortConfigPublishModeIngress,
  19. },
  20. },
  21. },
  22. {
  23. value: "80:8080",
  24. expected: []swarm.PortConfig{
  25. {
  26. Protocol: "tcp",
  27. TargetPort: 8080,
  28. PublishedPort: 80,
  29. PublishMode: swarm.PortConfigPublishModeIngress,
  30. },
  31. },
  32. },
  33. {
  34. value: "8080:80/tcp",
  35. expected: []swarm.PortConfig{
  36. {
  37. Protocol: "tcp",
  38. TargetPort: 80,
  39. PublishedPort: 8080,
  40. PublishMode: swarm.PortConfigPublishModeIngress,
  41. },
  42. },
  43. },
  44. {
  45. value: "80:8080/udp",
  46. expected: []swarm.PortConfig{
  47. {
  48. Protocol: "udp",
  49. TargetPort: 8080,
  50. PublishedPort: 80,
  51. PublishMode: swarm.PortConfigPublishModeIngress,
  52. },
  53. },
  54. },
  55. {
  56. value: "80-81:8080-8081/tcp",
  57. expected: []swarm.PortConfig{
  58. {
  59. Protocol: "tcp",
  60. TargetPort: 8080,
  61. PublishedPort: 80,
  62. PublishMode: swarm.PortConfigPublishModeIngress,
  63. },
  64. {
  65. Protocol: "tcp",
  66. TargetPort: 8081,
  67. PublishedPort: 81,
  68. PublishMode: swarm.PortConfigPublishModeIngress,
  69. },
  70. },
  71. },
  72. {
  73. value: "80-82:8080-8082/udp",
  74. expected: []swarm.PortConfig{
  75. {
  76. Protocol: "udp",
  77. TargetPort: 8080,
  78. PublishedPort: 80,
  79. PublishMode: swarm.PortConfigPublishModeIngress,
  80. },
  81. {
  82. Protocol: "udp",
  83. TargetPort: 8081,
  84. PublishedPort: 81,
  85. PublishMode: swarm.PortConfigPublishModeIngress,
  86. },
  87. {
  88. Protocol: "udp",
  89. TargetPort: 8082,
  90. PublishedPort: 82,
  91. PublishMode: swarm.PortConfigPublishModeIngress,
  92. },
  93. },
  94. },
  95. }
  96. for _, tc := range testCases {
  97. var port PortOpt
  98. assert.NilError(t, port.Set(tc.value))
  99. assert.Equal(t, len(port.Value()), len(tc.expected))
  100. for _, expectedPortConfig := range tc.expected {
  101. assertContains(t, port.Value(), expectedPortConfig)
  102. }
  103. }
  104. }
  105. func TestPortOptValidComplexSyntax(t *testing.T) {
  106. testCases := []struct {
  107. value string
  108. expected []swarm.PortConfig
  109. }{
  110. {
  111. value: "target=80",
  112. expected: []swarm.PortConfig{
  113. {
  114. TargetPort: 80,
  115. Protocol: "tcp",
  116. PublishMode: swarm.PortConfigPublishModeIngress,
  117. },
  118. },
  119. },
  120. {
  121. value: "target=80,protocol=tcp",
  122. expected: []swarm.PortConfig{
  123. {
  124. Protocol: "tcp",
  125. TargetPort: 80,
  126. PublishMode: swarm.PortConfigPublishModeIngress,
  127. },
  128. },
  129. },
  130. {
  131. value: "target=80,published=8080,protocol=tcp",
  132. expected: []swarm.PortConfig{
  133. {
  134. Protocol: "tcp",
  135. TargetPort: 80,
  136. PublishedPort: 8080,
  137. PublishMode: swarm.PortConfigPublishModeIngress,
  138. },
  139. },
  140. },
  141. {
  142. value: "published=80,target=8080,protocol=tcp",
  143. expected: []swarm.PortConfig{
  144. {
  145. Protocol: "tcp",
  146. TargetPort: 8080,
  147. PublishedPort: 80,
  148. PublishMode: swarm.PortConfigPublishModeIngress,
  149. },
  150. },
  151. },
  152. {
  153. value: "target=80,published=8080,protocol=tcp,mode=host",
  154. expected: []swarm.PortConfig{
  155. {
  156. Protocol: "tcp",
  157. TargetPort: 80,
  158. PublishedPort: 8080,
  159. PublishMode: "host",
  160. },
  161. },
  162. },
  163. {
  164. value: "target=80,published=8080,mode=host",
  165. expected: []swarm.PortConfig{
  166. {
  167. TargetPort: 80,
  168. PublishedPort: 8080,
  169. PublishMode: "host",
  170. Protocol: "tcp",
  171. },
  172. },
  173. },
  174. {
  175. value: "target=80,published=8080,mode=ingress",
  176. expected: []swarm.PortConfig{
  177. {
  178. TargetPort: 80,
  179. PublishedPort: 8080,
  180. PublishMode: "ingress",
  181. Protocol: "tcp",
  182. },
  183. },
  184. },
  185. }
  186. for _, tc := range testCases {
  187. var port PortOpt
  188. assert.NilError(t, port.Set(tc.value))
  189. assert.Equal(t, len(port.Value()), len(tc.expected))
  190. for _, expectedPortConfig := range tc.expected {
  191. assertContains(t, port.Value(), expectedPortConfig)
  192. }
  193. }
  194. }
  195. func TestPortOptInvalidComplexSyntax(t *testing.T) {
  196. testCases := []struct {
  197. value string
  198. expectedError string
  199. }{
  200. {
  201. value: "invalid,target=80",
  202. expectedError: "invalid field",
  203. },
  204. {
  205. value: "invalid=field",
  206. expectedError: "invalid field",
  207. },
  208. {
  209. value: "protocol=invalid",
  210. expectedError: "invalid protocol value",
  211. },
  212. {
  213. value: "target=invalid",
  214. expectedError: "invalid syntax",
  215. },
  216. {
  217. value: "published=invalid",
  218. expectedError: "invalid syntax",
  219. },
  220. {
  221. value: "mode=invalid",
  222. expectedError: "invalid publish mode value",
  223. },
  224. {
  225. value: "published=8080,protocol=tcp,mode=ingress",
  226. expectedError: "missing mandatory field",
  227. },
  228. {
  229. value: `target=80,protocol="tcp,mode=ingress"`,
  230. expectedError: "non-quoted-field",
  231. },
  232. {
  233. value: `target=80,"protocol=tcp,mode=ingress"`,
  234. expectedError: "invalid protocol value",
  235. },
  236. }
  237. for _, tc := range testCases {
  238. var port PortOpt
  239. assert.Error(t, port.Set(tc.value), tc.expectedError)
  240. }
  241. }
  242. func assertContains(t *testing.T, portConfigs []swarm.PortConfig, expected swarm.PortConfig) {
  243. var contains = false
  244. for _, portConfig := range portConfigs {
  245. if portConfig == expected {
  246. contains = true
  247. break
  248. }
  249. }
  250. if !contains {
  251. t.Errorf("expected %v to contain %v, did not", portConfigs, expected)
  252. }
  253. }