ipvs_test.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. Copyright 2017 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 ipvs
  14. import (
  15. "net"
  16. "testing"
  17. )
  18. func TestVirtualServerEqual(t *testing.T) {
  19. Tests := []struct {
  20. svcA *VirtualServer
  21. svcB *VirtualServer
  22. equal bool
  23. reason string
  24. }{
  25. {
  26. svcA: &VirtualServer{
  27. Address: net.ParseIP("10.20.30.40"),
  28. Protocol: "",
  29. Port: 0,
  30. Scheduler: "wrr",
  31. Flags: 0,
  32. Timeout: 0,
  33. },
  34. svcB: &VirtualServer{
  35. Address: net.ParseIP("10.20.30.41"),
  36. Protocol: "",
  37. Port: 0,
  38. Scheduler: "wrr",
  39. Flags: 0,
  40. Timeout: 0,
  41. },
  42. equal: false,
  43. reason: "IPv4 address not equal",
  44. },
  45. {
  46. svcA: &VirtualServer{
  47. Address: net.ParseIP("2012::beef"),
  48. Protocol: "",
  49. Port: 0,
  50. Scheduler: "wrr",
  51. Flags: 0,
  52. Timeout: 0,
  53. },
  54. svcB: &VirtualServer{
  55. Address: net.ParseIP("2017::beef"),
  56. Protocol: "",
  57. Port: 0,
  58. Scheduler: "wrr",
  59. Flags: 0,
  60. Timeout: 0,
  61. },
  62. equal: false,
  63. reason: "IPv6 address not equal",
  64. },
  65. {
  66. svcA: &VirtualServer{
  67. Address: net.ParseIP("2012::beef"),
  68. Protocol: "TCP",
  69. Port: 0,
  70. Scheduler: "wrr",
  71. Flags: 0,
  72. Timeout: 0,
  73. },
  74. svcB: &VirtualServer{
  75. Address: net.ParseIP("2012::beeef"),
  76. Protocol: "UDP",
  77. Port: 0,
  78. Scheduler: "wrr",
  79. Flags: 0,
  80. Timeout: 0,
  81. },
  82. equal: false,
  83. reason: "Protocol not equal",
  84. },
  85. {
  86. svcA: &VirtualServer{
  87. Address: net.ParseIP("2012::beef"),
  88. Protocol: "TCP",
  89. Port: 80,
  90. Scheduler: "wrr",
  91. Flags: 0,
  92. Timeout: 0,
  93. },
  94. svcB: &VirtualServer{
  95. Address: net.ParseIP("2012::beef"),
  96. Protocol: "TCP",
  97. Port: 8080,
  98. Scheduler: "wrr",
  99. Flags: 0,
  100. Timeout: 0,
  101. },
  102. equal: false,
  103. reason: "Port not equal",
  104. },
  105. {
  106. svcA: &VirtualServer{
  107. Address: net.ParseIP("1.2.3.4"),
  108. Protocol: "TCP",
  109. Port: 80,
  110. Scheduler: "rr",
  111. Flags: 0,
  112. Timeout: 0,
  113. },
  114. svcB: &VirtualServer{
  115. Address: net.ParseIP("1.2.3.4"),
  116. Protocol: "TCP",
  117. Port: 80,
  118. Scheduler: "wlc",
  119. Flags: 0,
  120. Timeout: 0,
  121. },
  122. equal: false,
  123. reason: "Scheduler not equal",
  124. },
  125. {
  126. svcA: &VirtualServer{
  127. Address: net.ParseIP("1.2.3.4"),
  128. Protocol: "TCP",
  129. Port: 80,
  130. Scheduler: "rr",
  131. Flags: 2,
  132. Timeout: 0,
  133. },
  134. svcB: &VirtualServer{
  135. Address: net.ParseIP("1.2.3.4"),
  136. Protocol: "TCP",
  137. Port: 80,
  138. Scheduler: "rr",
  139. Flags: 3,
  140. Timeout: 0,
  141. },
  142. equal: false,
  143. reason: "Flags not equal",
  144. },
  145. {
  146. svcA: &VirtualServer{
  147. Address: net.ParseIP("2012::beef"),
  148. Protocol: "",
  149. Port: 0,
  150. Scheduler: "wrr",
  151. Flags: 0,
  152. Timeout: 0,
  153. },
  154. svcB: &VirtualServer{
  155. Address: net.ParseIP("2012::beef"),
  156. Protocol: "",
  157. Port: 0,
  158. Scheduler: "wrr",
  159. Flags: 0,
  160. Timeout: 10800,
  161. },
  162. equal: false,
  163. reason: "Timeout not equal",
  164. },
  165. {
  166. svcA: &VirtualServer{
  167. Address: net.ParseIP("1.2.3.4"),
  168. Protocol: "TCP",
  169. Port: 80,
  170. Scheduler: "rr",
  171. Flags: 0x1,
  172. Timeout: 10800,
  173. },
  174. svcB: &VirtualServer{
  175. Address: net.ParseIP("1.2.3.4"),
  176. Protocol: "TCP",
  177. Port: 80,
  178. Scheduler: "rr",
  179. Flags: 0x1,
  180. Timeout: 10800,
  181. },
  182. equal: true,
  183. reason: "All fields equal",
  184. },
  185. {
  186. svcA: &VirtualServer{
  187. Address: net.ParseIP("2012::beef"),
  188. Protocol: "TCP",
  189. Port: 0,
  190. Scheduler: "wrr",
  191. Flags: 0,
  192. Timeout: 0,
  193. },
  194. svcB: &VirtualServer{
  195. Address: net.ParseIP("2012::beeef"),
  196. Protocol: "SCTP",
  197. Port: 0,
  198. Scheduler: "wrr",
  199. Flags: 0,
  200. Timeout: 0,
  201. },
  202. equal: false,
  203. reason: "Protocol not equal",
  204. },
  205. {
  206. svcA: &VirtualServer{
  207. Address: net.ParseIP("1.2.3.4"),
  208. Protocol: "SCTP",
  209. Port: 80,
  210. Scheduler: "rr",
  211. Flags: 0x1,
  212. Timeout: 10800,
  213. },
  214. svcB: &VirtualServer{
  215. Address: net.ParseIP("1.2.3.4"),
  216. Protocol: "SCTP",
  217. Port: 80,
  218. Scheduler: "rr",
  219. Flags: 0x1,
  220. Timeout: 10800,
  221. },
  222. equal: true,
  223. reason: "All fields equal",
  224. },
  225. }
  226. for i := range Tests {
  227. equal := Tests[i].svcA.Equal(Tests[i].svcB)
  228. if equal != Tests[i].equal {
  229. t.Errorf("case: %d got %v, expected %v, reason: %s", i, equal, Tests[i].equal, Tests[i].reason)
  230. }
  231. }
  232. }
  233. func TestRealServerEqual(t *testing.T) {
  234. Tests := []struct {
  235. rsA *RealServer
  236. rsB *RealServer
  237. equal bool
  238. reason string
  239. }{
  240. {
  241. rsA: &RealServer{
  242. Address: net.ParseIP("10.20.30.40"),
  243. Port: 80,
  244. },
  245. rsB: &RealServer{
  246. Address: net.ParseIP("10.20.30.41"),
  247. Port: 80,
  248. },
  249. equal: false,
  250. reason: "IPv4 address not equal",
  251. },
  252. {
  253. rsA: &RealServer{
  254. Address: net.ParseIP("2012::beef"),
  255. Port: 80,
  256. },
  257. rsB: &RealServer{
  258. Address: net.ParseIP("2017::beef"),
  259. Port: 80,
  260. },
  261. equal: false,
  262. reason: "IPv6 address not equal",
  263. },
  264. {
  265. rsA: &RealServer{
  266. Address: net.ParseIP("2012::beef"),
  267. Port: 80,
  268. },
  269. rsB: &RealServer{
  270. Address: net.ParseIP("2012::beef"),
  271. Port: 8080,
  272. },
  273. equal: false,
  274. reason: "Port not equal",
  275. },
  276. {
  277. rsA: &RealServer{
  278. Address: net.ParseIP("1.2.3.4"),
  279. Port: 3080,
  280. },
  281. rsB: &RealServer{
  282. Address: net.ParseIP("1.2.3.4"),
  283. Port: 3080,
  284. },
  285. equal: true,
  286. reason: "All fields equal",
  287. },
  288. {
  289. rsA: &RealServer{
  290. Address: net.ParseIP("2012::beef"),
  291. Port: 3080,
  292. },
  293. rsB: &RealServer{
  294. Address: net.ParseIP("2012::beef"),
  295. Port: 3080,
  296. },
  297. equal: true,
  298. reason: "All fields equal",
  299. },
  300. }
  301. for i := range Tests {
  302. equal := Tests[i].rsA.Equal(Tests[i].rsB)
  303. if equal != Tests[i].equal {
  304. t.Errorf("case: %d got %v, expected %v, reason: %s", i, equal, Tests[i].equal, Tests[i].reason)
  305. }
  306. }
  307. }
  308. func TestFrontendServiceString(t *testing.T) {
  309. Tests := []struct {
  310. svc *VirtualServer
  311. expected string
  312. }{
  313. {
  314. svc: &VirtualServer{
  315. Address: net.ParseIP("10.20.30.40"),
  316. Protocol: "TCP",
  317. Port: 80,
  318. },
  319. expected: "10.20.30.40:80/TCP",
  320. },
  321. {
  322. svc: &VirtualServer{
  323. Address: net.ParseIP("2012::beef"),
  324. Protocol: "UDP",
  325. Port: 8080,
  326. },
  327. expected: "[2012::beef]:8080/UDP",
  328. },
  329. {
  330. svc: &VirtualServer{
  331. Address: net.ParseIP("10.20.30.41"),
  332. Protocol: "ESP",
  333. Port: 1234,
  334. },
  335. expected: "10.20.30.41:1234/ESP",
  336. },
  337. }
  338. for i := range Tests {
  339. if Tests[i].expected != Tests[i].svc.String() {
  340. t.Errorf("case: %d got %v, expected %v", i, Tests[i].svc.String(), Tests[i].expected)
  341. }
  342. }
  343. }
  344. func TestFrontendDestinationString(t *testing.T) {
  345. Tests := []struct {
  346. svc *RealServer
  347. expected string
  348. }{
  349. {
  350. svc: &RealServer{
  351. Address: net.ParseIP("10.20.30.40"),
  352. Port: 80,
  353. },
  354. expected: "10.20.30.40:80",
  355. },
  356. {
  357. svc: &RealServer{
  358. Address: net.ParseIP("2012::beef"),
  359. Port: 8080,
  360. },
  361. expected: "[2012::beef]:8080",
  362. },
  363. }
  364. for i := range Tests {
  365. if Tests[i].expected != Tests[i].svc.String() {
  366. t.Errorf("case: %d got %v, expected %v", i, Tests[i].svc.String(), Tests[i].expected)
  367. }
  368. }
  369. }