util_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. Copyright 2015 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 pod
  14. import (
  15. "testing"
  16. "k8s.io/kubernetes/pkg/api"
  17. "k8s.io/kubernetes/pkg/util/intstr"
  18. )
  19. func TestFindPort(t *testing.T) {
  20. testCases := []struct {
  21. name string
  22. containers []api.Container
  23. port intstr.IntOrString
  24. expected int
  25. pass bool
  26. }{{
  27. name: "valid int, no ports",
  28. containers: []api.Container{{}},
  29. port: intstr.FromInt(93),
  30. expected: 93,
  31. pass: true,
  32. }, {
  33. name: "valid int, with ports",
  34. containers: []api.Container{{Ports: []api.ContainerPort{{
  35. Name: "",
  36. ContainerPort: 11,
  37. Protocol: "TCP",
  38. }, {
  39. Name: "p",
  40. ContainerPort: 22,
  41. Protocol: "TCP",
  42. }}}},
  43. port: intstr.FromInt(93),
  44. expected: 93,
  45. pass: true,
  46. }, {
  47. name: "valid str, no ports",
  48. containers: []api.Container{{}},
  49. port: intstr.FromString("p"),
  50. expected: 0,
  51. pass: false,
  52. }, {
  53. name: "valid str, one ctr with ports",
  54. containers: []api.Container{{Ports: []api.ContainerPort{{
  55. Name: "",
  56. ContainerPort: 11,
  57. Protocol: "UDP",
  58. }, {
  59. Name: "p",
  60. ContainerPort: 22,
  61. Protocol: "TCP",
  62. }, {
  63. Name: "q",
  64. ContainerPort: 33,
  65. Protocol: "TCP",
  66. }}}},
  67. port: intstr.FromString("q"),
  68. expected: 33,
  69. pass: true,
  70. }, {
  71. name: "valid str, two ctr with ports",
  72. containers: []api.Container{{}, {Ports: []api.ContainerPort{{
  73. Name: "",
  74. ContainerPort: 11,
  75. Protocol: "UDP",
  76. }, {
  77. Name: "p",
  78. ContainerPort: 22,
  79. Protocol: "TCP",
  80. }, {
  81. Name: "q",
  82. ContainerPort: 33,
  83. Protocol: "TCP",
  84. }}}},
  85. port: intstr.FromString("q"),
  86. expected: 33,
  87. pass: true,
  88. }, {
  89. name: "valid str, two ctr with same port",
  90. containers: []api.Container{{}, {Ports: []api.ContainerPort{{
  91. Name: "",
  92. ContainerPort: 11,
  93. Protocol: "UDP",
  94. }, {
  95. Name: "p",
  96. ContainerPort: 22,
  97. Protocol: "TCP",
  98. }, {
  99. Name: "q",
  100. ContainerPort: 22,
  101. Protocol: "TCP",
  102. }}}},
  103. port: intstr.FromString("q"),
  104. expected: 22,
  105. pass: true,
  106. }, {
  107. name: "valid str, invalid protocol",
  108. containers: []api.Container{{}, {Ports: []api.ContainerPort{{
  109. Name: "a",
  110. ContainerPort: 11,
  111. Protocol: "snmp",
  112. },
  113. }}},
  114. port: intstr.FromString("a"),
  115. expected: 0,
  116. pass: false,
  117. }, {
  118. name: "valid hostPort",
  119. containers: []api.Container{{}, {Ports: []api.ContainerPort{{
  120. Name: "a",
  121. ContainerPort: 11,
  122. HostPort: 81,
  123. Protocol: "TCP",
  124. },
  125. }}},
  126. port: intstr.FromString("a"),
  127. expected: 11,
  128. pass: true,
  129. },
  130. {
  131. name: "invalid hostPort",
  132. containers: []api.Container{{}, {Ports: []api.ContainerPort{{
  133. Name: "a",
  134. ContainerPort: 11,
  135. HostPort: -1,
  136. Protocol: "TCP",
  137. },
  138. }}},
  139. port: intstr.FromString("a"),
  140. expected: 11,
  141. pass: true,
  142. //this should fail but passes.
  143. },
  144. {
  145. name: "invalid ContainerPort",
  146. containers: []api.Container{{}, {Ports: []api.ContainerPort{{
  147. Name: "a",
  148. ContainerPort: -1,
  149. Protocol: "TCP",
  150. },
  151. }}},
  152. port: intstr.FromString("a"),
  153. expected: -1,
  154. pass: true,
  155. //this should fail but passes
  156. },
  157. {
  158. name: "HostIP Address",
  159. containers: []api.Container{{}, {Ports: []api.ContainerPort{{
  160. Name: "a",
  161. ContainerPort: 11,
  162. HostIP: "192.168.1.1",
  163. Protocol: "TCP",
  164. },
  165. }}},
  166. port: intstr.FromString("a"),
  167. expected: 11,
  168. pass: true,
  169. },
  170. }
  171. for _, tc := range testCases {
  172. port, err := FindPort(&api.Pod{Spec: api.PodSpec{Containers: tc.containers}},
  173. &api.ServicePort{Protocol: "TCP", TargetPort: tc.port})
  174. if err != nil && tc.pass {
  175. t.Errorf("unexpected error for %s: %v", tc.name, err)
  176. }
  177. if err == nil && !tc.pass {
  178. t.Errorf("unexpected non-error for %s: %d", tc.name, port)
  179. }
  180. if port != tc.expected {
  181. t.Errorf("wrong result for %s: expected %d, got %d", tc.name, tc.expected, port)
  182. }
  183. }
  184. }