ipvs_test.go 8.6 KB

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