ports_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  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 convert
  14. import (
  15. "context"
  16. "testing"
  17. "github.com/Azure/azure-sdk-for-go/services/containerinstance/mgmt/2019-12-01/containerinstance"
  18. "github.com/Azure/go-autorest/autorest/to"
  19. "github.com/compose-spec/compose-go/types"
  20. "gotest.tools/v3/assert"
  21. is "gotest.tools/v3/assert/cmp"
  22. "github.com/docker/compose-cli/api/containers"
  23. )
  24. func TestComposeContainerGroupToContainerMultiplePorts(t *testing.T) {
  25. project := types.Project{
  26. Services: []types.ServiceConfig{
  27. {
  28. Name: "service1",
  29. Image: "image1",
  30. Ports: []types.ServicePortConfig{
  31. {
  32. Published: 80,
  33. Target: 80,
  34. },
  35. },
  36. },
  37. {
  38. Name: "service2",
  39. Image: "image2",
  40. Ports: []types.ServicePortConfig{
  41. {
  42. Published: 8080,
  43. Target: 8080,
  44. },
  45. },
  46. },
  47. },
  48. }
  49. group, err := ToContainerGroup(context.TODO(), convertCtx, project, mockStorageHelper)
  50. assert.NilError(t, err)
  51. assert.Assert(t, is.Len(*group.Containers, 3))
  52. container1 := (*group.Containers)[0]
  53. assert.Equal(t, *container1.Name, "service1")
  54. assert.Equal(t, *container1.Image, "image1")
  55. assert.Equal(t, *(*container1.Ports)[0].Port, int32(80))
  56. container2 := (*group.Containers)[1]
  57. assert.Equal(t, *container2.Name, "service2")
  58. assert.Equal(t, *container2.Image, "image2")
  59. assert.Equal(t, *(*container2.Ports)[0].Port, int32(8080))
  60. groupPorts := *group.IPAddress.Ports
  61. assert.Assert(t, is.Len(groupPorts, 2))
  62. assert.Equal(t, *groupPorts[0].Port, int32(80))
  63. assert.Equal(t, *groupPorts[1].Port, int32(8080))
  64. assert.Assert(t, group.IPAddress.DNSNameLabel == nil)
  65. }
  66. func TestPortConvert(t *testing.T) {
  67. expectedPorts := []containers.Port{
  68. {
  69. HostPort: 80,
  70. ContainerPort: 80,
  71. HostIP: "10.0.0.1",
  72. Protocol: "tcp",
  73. },
  74. }
  75. testCases := []struct {
  76. name string
  77. ip *containerinstance.IPAddress
  78. ports []containerinstance.ContainerPort
  79. expected []containers.Port
  80. }{
  81. {
  82. name: "convert port",
  83. ip: &containerinstance.IPAddress{
  84. IP: to.StringPtr("10.0.0.1"),
  85. },
  86. ports: []containerinstance.ContainerPort{
  87. {
  88. Protocol: "tcp",
  89. Port: to.Int32Ptr(80),
  90. },
  91. },
  92. expected: expectedPorts,
  93. },
  94. {
  95. name: "with nil ip",
  96. ip: nil,
  97. ports: []containerinstance.ContainerPort{
  98. {
  99. Protocol: "tcp",
  100. Port: to.Int32Ptr(80),
  101. },
  102. },
  103. expected: []containers.Port{
  104. {
  105. HostPort: 80,
  106. ContainerPort: 80,
  107. HostIP: "",
  108. Protocol: "tcp",
  109. },
  110. },
  111. },
  112. {
  113. name: "with nil ip value",
  114. ip: &containerinstance.IPAddress{
  115. IP: nil,
  116. },
  117. ports: []containerinstance.ContainerPort{
  118. {
  119. Protocol: "tcp",
  120. Port: to.Int32Ptr(80),
  121. },
  122. },
  123. expected: []containers.Port{
  124. {
  125. HostPort: 80,
  126. ContainerPort: 80,
  127. HostIP: "",
  128. Protocol: "tcp",
  129. },
  130. },
  131. },
  132. {
  133. name: "skip nil ports",
  134. ip: nil,
  135. ports: []containerinstance.ContainerPort{
  136. {
  137. Protocol: "tcp",
  138. Port: to.Int32Ptr(80),
  139. },
  140. {
  141. Protocol: "tcp",
  142. Port: nil,
  143. },
  144. },
  145. expected: []containers.Port{
  146. {
  147. HostPort: 80,
  148. ContainerPort: 80,
  149. HostIP: "",
  150. Protocol: "tcp",
  151. },
  152. },
  153. },
  154. }
  155. for _, testCase := range testCases {
  156. t.Run(testCase.name, func(t *testing.T) {
  157. ports := ToPorts(testCase.ip, testCase.ports)
  158. assert.DeepEqual(t, testCase.expected, ports)
  159. })
  160. }
  161. }
  162. func TestConvertTCPPortsToAci(t *testing.T) {
  163. service := types.ServiceConfig{
  164. Name: "myService",
  165. Ports: []types.ServicePortConfig{
  166. {
  167. Protocol: "",
  168. Target: 80,
  169. Published: 80,
  170. },
  171. {
  172. Protocol: "tcp",
  173. Target: 90,
  174. Published: 90,
  175. },
  176. },
  177. }
  178. containerPorts, groupPports, _, err := convertPortsToAci(serviceConfigAciHelper(service))
  179. assert.NilError(t, err)
  180. assert.DeepEqual(t, containerPorts, []containerinstance.ContainerPort{
  181. {
  182. Port: to.Int32Ptr(80),
  183. Protocol: containerinstance.ContainerNetworkProtocolTCP,
  184. },
  185. {
  186. Port: to.Int32Ptr(90),
  187. Protocol: containerinstance.ContainerNetworkProtocolTCP,
  188. },
  189. })
  190. assert.DeepEqual(t, groupPports, []containerinstance.Port{
  191. {
  192. Port: to.Int32Ptr(80),
  193. Protocol: containerinstance.TCP,
  194. },
  195. {
  196. Port: to.Int32Ptr(90),
  197. Protocol: containerinstance.TCP,
  198. },
  199. })
  200. }
  201. func TestConvertUDPPortsToAci(t *testing.T) {
  202. service := types.ServiceConfig{
  203. Name: "myService",
  204. Ports: []types.ServicePortConfig{
  205. {
  206. Protocol: "udp",
  207. Target: 80,
  208. Published: 80,
  209. },
  210. },
  211. }
  212. containerPorts, groupPports, _, err := convertPortsToAci(serviceConfigAciHelper(service))
  213. assert.NilError(t, err)
  214. assert.DeepEqual(t, containerPorts, []containerinstance.ContainerPort{
  215. {
  216. Port: to.Int32Ptr(80),
  217. Protocol: containerinstance.ContainerNetworkProtocolUDP,
  218. },
  219. })
  220. assert.DeepEqual(t, groupPports, []containerinstance.Port{
  221. {
  222. Port: to.Int32Ptr(80),
  223. Protocol: containerinstance.UDP,
  224. },
  225. })
  226. }
  227. func TestConvertErrorOnMappingPorts(t *testing.T) {
  228. service := types.ServiceConfig{
  229. Name: "myService",
  230. Ports: []types.ServicePortConfig{
  231. {
  232. Protocol: "",
  233. Target: 80,
  234. Published: 90,
  235. },
  236. },
  237. }
  238. _, _, _, err := convertPortsToAci(serviceConfigAciHelper(service))
  239. assert.Error(t, err, "Port mapping is not supported with ACI, cannot map port 90 to 80 for container myService")
  240. }