host_ports_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 nodeinfo
  14. import (
  15. "testing"
  16. )
  17. type hostPortInfoParam struct {
  18. protocol, ip string
  19. port int32
  20. }
  21. func TestHostPortInfo_AddRemove(t *testing.T) {
  22. tests := []struct {
  23. desc string
  24. added []hostPortInfoParam
  25. removed []hostPortInfoParam
  26. length int
  27. }{
  28. {
  29. desc: "normal add case",
  30. added: []hostPortInfoParam{
  31. {"TCP", "127.0.0.1", 79},
  32. {"UDP", "127.0.0.1", 80},
  33. {"TCP", "127.0.0.1", 81},
  34. {"TCP", "127.0.0.1", 82},
  35. // this might not make sense in real case, but the struct doesn't forbid it.
  36. {"TCP", "0.0.0.0", 79},
  37. {"UDP", "0.0.0.0", 80},
  38. {"TCP", "0.0.0.0", 81},
  39. {"TCP", "0.0.0.0", 82},
  40. {"TCP", "0.0.0.0", 0},
  41. {"TCP", "0.0.0.0", -1},
  42. },
  43. length: 8,
  44. },
  45. {
  46. desc: "empty ip and protocol add should work",
  47. added: []hostPortInfoParam{
  48. {"", "127.0.0.1", 79},
  49. {"UDP", "127.0.0.1", 80},
  50. {"", "127.0.0.1", 81},
  51. {"", "127.0.0.1", 82},
  52. {"", "", 79},
  53. {"UDP", "", 80},
  54. {"", "", 81},
  55. {"", "", 82},
  56. {"", "", 0},
  57. {"", "", -1},
  58. },
  59. length: 8,
  60. },
  61. {
  62. desc: "normal remove case",
  63. added: []hostPortInfoParam{
  64. {"TCP", "127.0.0.1", 79},
  65. {"UDP", "127.0.0.1", 80},
  66. {"TCP", "127.0.0.1", 81},
  67. {"TCP", "127.0.0.1", 82},
  68. {"TCP", "0.0.0.0", 79},
  69. {"UDP", "0.0.0.0", 80},
  70. {"TCP", "0.0.0.0", 81},
  71. {"TCP", "0.0.0.0", 82},
  72. },
  73. removed: []hostPortInfoParam{
  74. {"TCP", "127.0.0.1", 79},
  75. {"UDP", "127.0.0.1", 80},
  76. {"TCP", "127.0.0.1", 81},
  77. {"TCP", "127.0.0.1", 82},
  78. {"TCP", "0.0.0.0", 79},
  79. {"UDP", "0.0.0.0", 80},
  80. {"TCP", "0.0.0.0", 81},
  81. {"TCP", "0.0.0.0", 82},
  82. },
  83. length: 0,
  84. },
  85. {
  86. desc: "empty ip and protocol remove should work",
  87. added: []hostPortInfoParam{
  88. {"TCP", "127.0.0.1", 79},
  89. {"UDP", "127.0.0.1", 80},
  90. {"TCP", "127.0.0.1", 81},
  91. {"TCP", "127.0.0.1", 82},
  92. {"TCP", "0.0.0.0", 79},
  93. {"UDP", "0.0.0.0", 80},
  94. {"TCP", "0.0.0.0", 81},
  95. {"TCP", "0.0.0.0", 82},
  96. },
  97. removed: []hostPortInfoParam{
  98. {"", "127.0.0.1", 79},
  99. {"", "127.0.0.1", 81},
  100. {"", "127.0.0.1", 82},
  101. {"UDP", "127.0.0.1", 80},
  102. {"", "", 79},
  103. {"", "", 81},
  104. {"", "", 82},
  105. {"UDP", "", 80},
  106. },
  107. length: 0,
  108. },
  109. }
  110. for _, test := range tests {
  111. hp := make(HostPortInfo)
  112. for _, param := range test.added {
  113. hp.Add(param.ip, param.protocol, param.port)
  114. }
  115. for _, param := range test.removed {
  116. hp.Remove(param.ip, param.protocol, param.port)
  117. }
  118. if hp.Len() != test.length {
  119. t.Errorf("%v failed: expect length %d; got %d", test.desc, test.length, hp.Len())
  120. t.Error(hp)
  121. }
  122. }
  123. }
  124. func TestHostPortInfo_Check(t *testing.T) {
  125. tests := []struct {
  126. desc string
  127. added []hostPortInfoParam
  128. check hostPortInfoParam
  129. expect bool
  130. }{
  131. {
  132. desc: "empty check should check 0.0.0.0 and TCP",
  133. added: []hostPortInfoParam{
  134. {"TCP", "127.0.0.1", 80},
  135. },
  136. check: hostPortInfoParam{"", "", 81},
  137. expect: false,
  138. },
  139. {
  140. desc: "empty check should check 0.0.0.0 and TCP (conflicted)",
  141. added: []hostPortInfoParam{
  142. {"TCP", "127.0.0.1", 80},
  143. },
  144. check: hostPortInfoParam{"", "", 80},
  145. expect: true,
  146. },
  147. {
  148. desc: "empty port check should pass",
  149. added: []hostPortInfoParam{
  150. {"TCP", "127.0.0.1", 80},
  151. },
  152. check: hostPortInfoParam{"", "", 0},
  153. expect: false,
  154. },
  155. {
  156. desc: "0.0.0.0 should check all registered IPs",
  157. added: []hostPortInfoParam{
  158. {"TCP", "127.0.0.1", 80},
  159. },
  160. check: hostPortInfoParam{"TCP", "0.0.0.0", 80},
  161. expect: true,
  162. },
  163. {
  164. desc: "0.0.0.0 with different protocol should be allowed",
  165. added: []hostPortInfoParam{
  166. {"UDP", "127.0.0.1", 80},
  167. },
  168. check: hostPortInfoParam{"TCP", "0.0.0.0", 80},
  169. expect: false,
  170. },
  171. {
  172. desc: "0.0.0.0 with different port should be allowed",
  173. added: []hostPortInfoParam{
  174. {"TCP", "127.0.0.1", 79},
  175. {"TCP", "127.0.0.1", 81},
  176. {"TCP", "127.0.0.1", 82},
  177. },
  178. check: hostPortInfoParam{"TCP", "0.0.0.0", 80},
  179. expect: false,
  180. },
  181. {
  182. desc: "normal ip should check all registered 0.0.0.0",
  183. added: []hostPortInfoParam{
  184. {"TCP", "0.0.0.0", 80},
  185. },
  186. check: hostPortInfoParam{"TCP", "127.0.0.1", 80},
  187. expect: true,
  188. },
  189. {
  190. desc: "normal ip with different port/protocol should be allowed (0.0.0.0)",
  191. added: []hostPortInfoParam{
  192. {"TCP", "0.0.0.0", 79},
  193. {"UDP", "0.0.0.0", 80},
  194. {"TCP", "0.0.0.0", 81},
  195. {"TCP", "0.0.0.0", 82},
  196. },
  197. check: hostPortInfoParam{"TCP", "127.0.0.1", 80},
  198. expect: false,
  199. },
  200. {
  201. desc: "normal ip with different port/protocol should be allowed",
  202. added: []hostPortInfoParam{
  203. {"TCP", "127.0.0.1", 79},
  204. {"UDP", "127.0.0.1", 80},
  205. {"TCP", "127.0.0.1", 81},
  206. {"TCP", "127.0.0.1", 82},
  207. },
  208. check: hostPortInfoParam{"TCP", "127.0.0.1", 80},
  209. expect: false,
  210. },
  211. }
  212. for _, test := range tests {
  213. hp := make(HostPortInfo)
  214. for _, param := range test.added {
  215. hp.Add(param.ip, param.protocol, param.port)
  216. }
  217. if hp.CheckConflict(test.check.ip, test.check.protocol, test.check.port) != test.expect {
  218. t.Errorf("%v failed, expected %t; got %t", test.desc, test.expect, !test.expect)
  219. }
  220. }
  221. }