service_port_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. Copyright 2018 The Kubernetes 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 util
  14. import (
  15. "testing"
  16. "k8s.io/api/core/v1"
  17. "k8s.io/apimachinery/pkg/util/intstr"
  18. )
  19. func TestLookupContainerPortNumberByServicePort(t *testing.T) {
  20. tests := []struct {
  21. name string
  22. svc v1.Service
  23. pod v1.Pod
  24. port int32
  25. containerPort int32
  26. err bool
  27. }{
  28. {
  29. name: "test success 1 (int port)",
  30. svc: v1.Service{
  31. Spec: v1.ServiceSpec{
  32. Ports: []v1.ServicePort{
  33. {
  34. Port: 80,
  35. TargetPort: intstr.FromInt(8080),
  36. },
  37. },
  38. },
  39. },
  40. pod: v1.Pod{
  41. Spec: v1.PodSpec{
  42. Containers: []v1.Container{
  43. {
  44. Ports: []v1.ContainerPort{
  45. {
  46. Name: "http",
  47. ContainerPort: int32(8080)},
  48. },
  49. },
  50. },
  51. },
  52. },
  53. port: 80,
  54. containerPort: 8080,
  55. err: false,
  56. },
  57. {
  58. name: "test success 2 (clusterIP: None)",
  59. svc: v1.Service{
  60. Spec: v1.ServiceSpec{
  61. ClusterIP: v1.ClusterIPNone,
  62. Ports: []v1.ServicePort{
  63. {
  64. Port: 80,
  65. TargetPort: intstr.FromInt(8080),
  66. },
  67. },
  68. },
  69. },
  70. pod: v1.Pod{
  71. Spec: v1.PodSpec{
  72. Containers: []v1.Container{
  73. {
  74. Ports: []v1.ContainerPort{
  75. {
  76. Name: "http",
  77. ContainerPort: int32(8080)},
  78. },
  79. },
  80. },
  81. },
  82. },
  83. port: 80,
  84. containerPort: 80,
  85. err: false,
  86. },
  87. {
  88. name: "test success 3 (named port)",
  89. svc: v1.Service{
  90. Spec: v1.ServiceSpec{
  91. Ports: []v1.ServicePort{
  92. {
  93. Port: 80,
  94. TargetPort: intstr.FromString("http"),
  95. },
  96. },
  97. },
  98. },
  99. pod: v1.Pod{
  100. Spec: v1.PodSpec{
  101. Containers: []v1.Container{
  102. {
  103. Ports: []v1.ContainerPort{
  104. {
  105. Name: "http",
  106. ContainerPort: int32(8080)},
  107. },
  108. },
  109. },
  110. },
  111. },
  112. port: 80,
  113. containerPort: 8080,
  114. err: false,
  115. },
  116. {
  117. name: "test success (targetPort omitted)",
  118. svc: v1.Service{
  119. Spec: v1.ServiceSpec{
  120. Ports: []v1.ServicePort{
  121. {
  122. Port: 80,
  123. },
  124. },
  125. },
  126. },
  127. pod: v1.Pod{
  128. Spec: v1.PodSpec{
  129. Containers: []v1.Container{
  130. {
  131. Ports: []v1.ContainerPort{
  132. {
  133. Name: "http",
  134. ContainerPort: int32(80)},
  135. },
  136. },
  137. },
  138. },
  139. },
  140. port: 80,
  141. containerPort: 80,
  142. err: false,
  143. },
  144. {
  145. name: "test failure 1 (cannot find a matching named port)",
  146. svc: v1.Service{
  147. Spec: v1.ServiceSpec{
  148. Ports: []v1.ServicePort{
  149. {
  150. Port: 80,
  151. TargetPort: intstr.FromString("http"),
  152. },
  153. },
  154. },
  155. },
  156. pod: v1.Pod{
  157. Spec: v1.PodSpec{
  158. Containers: []v1.Container{
  159. {
  160. Ports: []v1.ContainerPort{
  161. {
  162. Name: "https",
  163. ContainerPort: int32(443)},
  164. },
  165. },
  166. },
  167. },
  168. },
  169. port: 80,
  170. containerPort: -1,
  171. err: true,
  172. },
  173. {
  174. name: "test failure 2 (cannot find a matching service port)",
  175. svc: v1.Service{
  176. Spec: v1.ServiceSpec{
  177. Ports: []v1.ServicePort{
  178. {
  179. Port: 80,
  180. TargetPort: intstr.FromString("http"),
  181. },
  182. },
  183. },
  184. },
  185. pod: v1.Pod{
  186. Spec: v1.PodSpec{
  187. Containers: []v1.Container{
  188. {
  189. Ports: []v1.ContainerPort{
  190. {
  191. Name: "https",
  192. ContainerPort: int32(443)},
  193. },
  194. },
  195. },
  196. },
  197. },
  198. port: 443,
  199. containerPort: 443,
  200. err: true,
  201. },
  202. {
  203. name: "test failure 2 (cannot find a matching service port, but ClusterIP: None)",
  204. svc: v1.Service{
  205. Spec: v1.ServiceSpec{
  206. ClusterIP: v1.ClusterIPNone,
  207. Ports: []v1.ServicePort{
  208. {
  209. Port: 80,
  210. TargetPort: intstr.FromString("http"),
  211. },
  212. },
  213. },
  214. },
  215. pod: v1.Pod{
  216. Spec: v1.PodSpec{
  217. Containers: []v1.Container{
  218. {
  219. Ports: []v1.ContainerPort{
  220. {
  221. Name: "http",
  222. ContainerPort: int32(80)},
  223. },
  224. },
  225. },
  226. },
  227. },
  228. port: 443,
  229. containerPort: 443,
  230. err: true,
  231. },
  232. }
  233. for _, tt := range tests {
  234. t.Run(tt.name, func(t *testing.T) {
  235. containerPort, err := LookupContainerPortNumberByServicePort(tt.svc, tt.pod, tt.port)
  236. if err != nil {
  237. if tt.err {
  238. if containerPort != tt.containerPort {
  239. t.Errorf("%v: expected port %v; got %v", tt.name, tt.containerPort, containerPort)
  240. }
  241. return
  242. }
  243. t.Errorf("%v: unexpected error: %v", tt.name, err)
  244. return
  245. }
  246. if tt.err {
  247. t.Errorf("%v: unexpected success", tt.name)
  248. return
  249. }
  250. if containerPort != tt.containerPort {
  251. t.Errorf("%v: expected port %v; got %v", tt.name, tt.containerPort, containerPort)
  252. }
  253. })
  254. }
  255. }