ipvs_linux_test.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. // +build linux
  2. /*
  3. Copyright 2017 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package ipvs
  15. import (
  16. "fmt"
  17. "net"
  18. "reflect"
  19. "syscall"
  20. "testing"
  21. libipvs "github.com/docker/libnetwork/ipvs"
  22. )
  23. func Test_toVirtualServer(t *testing.T) {
  24. Tests := []struct {
  25. ipvsService libipvs.Service
  26. virtualServer VirtualServer
  27. expectError bool
  28. reason string
  29. }{
  30. {
  31. libipvs.Service{
  32. Flags: 0x0,
  33. },
  34. VirtualServer{},
  35. true,
  36. fmt.Sprintf("IPVS Service Flags should be >= %d, got 0x0", FlagHashed),
  37. },
  38. {
  39. libipvs.Service{
  40. Flags: 0x1,
  41. },
  42. VirtualServer{},
  43. true,
  44. fmt.Sprintf("IPVS Service Flags should be >= %d, got 0x1", FlagHashed),
  45. },
  46. {
  47. libipvs.Service{
  48. Protocol: syscall.IPPROTO_TCP,
  49. Port: 80,
  50. FWMark: 0,
  51. SchedName: "",
  52. Flags: uint32(FlagPersistent + FlagHashed),
  53. Timeout: 0,
  54. Netmask: 0xffffffff,
  55. AddressFamily: syscall.AF_INET,
  56. Address: nil,
  57. PEName: "",
  58. },
  59. VirtualServer{
  60. Address: net.ParseIP("0.0.0.0"),
  61. Protocol: "TCP",
  62. Port: 80,
  63. Scheduler: "",
  64. Flags: ServiceFlags(FlagPersistent),
  65. Timeout: 0,
  66. },
  67. false,
  68. "",
  69. },
  70. {
  71. libipvs.Service{
  72. Protocol: syscall.IPPROTO_UDP,
  73. Port: 33434,
  74. FWMark: 0,
  75. SchedName: "wlc",
  76. Flags: uint32(0 + FlagHashed),
  77. Timeout: 100,
  78. Netmask: 128,
  79. AddressFamily: syscall.AF_INET6,
  80. Address: net.ParseIP("2012::beef"),
  81. PEName: "",
  82. },
  83. VirtualServer{
  84. Address: net.ParseIP("2012::beef"),
  85. Protocol: "UDP",
  86. Port: 33434,
  87. Scheduler: "wlc",
  88. Flags: ServiceFlags(0),
  89. Timeout: 100,
  90. },
  91. false,
  92. "",
  93. },
  94. {
  95. libipvs.Service{
  96. Protocol: 0,
  97. Port: 0,
  98. FWMark: 0,
  99. SchedName: "lc",
  100. Flags: uint32(0 + FlagHashed),
  101. Timeout: 0,
  102. Netmask: 0xffffffff,
  103. AddressFamily: syscall.AF_INET,
  104. Address: net.ParseIP("1.2.3.4"),
  105. PEName: "",
  106. },
  107. VirtualServer{
  108. Address: net.ParseIP("1.2.3.4"),
  109. Protocol: "",
  110. Port: 0,
  111. Scheduler: "lc",
  112. Flags: ServiceFlags(0),
  113. Timeout: 0,
  114. },
  115. false,
  116. "",
  117. },
  118. {
  119. libipvs.Service{
  120. Protocol: 0,
  121. Port: 0,
  122. FWMark: 0,
  123. SchedName: "wrr",
  124. Flags: uint32(FlagPersistent + FlagHashed),
  125. Timeout: 0,
  126. Netmask: 128,
  127. AddressFamily: syscall.AF_INET6,
  128. Address: nil,
  129. PEName: "",
  130. },
  131. VirtualServer{
  132. Address: net.ParseIP("::0"),
  133. Protocol: "",
  134. Port: 0,
  135. Scheduler: "wrr",
  136. Flags: ServiceFlags(FlagPersistent),
  137. Timeout: 0,
  138. },
  139. false,
  140. "",
  141. },
  142. {
  143. libipvs.Service{
  144. Protocol: syscall.IPPROTO_SCTP,
  145. Port: 80,
  146. FWMark: 0,
  147. SchedName: "",
  148. Flags: uint32(FlagPersistent + FlagHashed),
  149. Timeout: 0,
  150. Netmask: 0xffffffff,
  151. AddressFamily: syscall.AF_INET,
  152. Address: nil,
  153. PEName: "",
  154. },
  155. VirtualServer{
  156. Address: net.ParseIP("0.0.0.0"),
  157. Protocol: "SCTP",
  158. Port: 80,
  159. Scheduler: "",
  160. Flags: ServiceFlags(FlagPersistent),
  161. Timeout: 0,
  162. },
  163. false,
  164. "",
  165. },
  166. }
  167. for i := range Tests {
  168. got, err := toVirtualServer(&Tests[i].ipvsService)
  169. if Tests[i].expectError && err == nil {
  170. t.Errorf("case: %d, expected error: %s, got nil", i, Tests[i].reason)
  171. }
  172. if !Tests[i].expectError && err != nil {
  173. t.Errorf("case: %d, unexpected error: %v", i, err)
  174. }
  175. if got != nil && &Tests[i].virtualServer != nil {
  176. if !reflect.DeepEqual(*got, Tests[i].virtualServer) {
  177. t.Errorf("case: %d, got %#v, want %#v", i, *got, Tests[i].virtualServer)
  178. }
  179. }
  180. }
  181. }
  182. func Test_toIPVSService(t *testing.T) {
  183. Tests := []struct {
  184. ipvsService libipvs.Service
  185. virtualServer VirtualServer
  186. }{
  187. {
  188. libipvs.Service{
  189. Protocol: syscall.IPPROTO_TCP,
  190. Port: 80,
  191. FWMark: 0,
  192. SchedName: "",
  193. Flags: 0,
  194. Timeout: 0,
  195. Netmask: 0xffffffff,
  196. AddressFamily: syscall.AF_INET,
  197. Address: net.ParseIP("0.0.0.0"),
  198. PEName: "",
  199. },
  200. VirtualServer{
  201. Address: net.ParseIP("0.0.0.0"),
  202. Protocol: "TCP",
  203. Port: 80,
  204. Scheduler: "",
  205. Flags: 0,
  206. Timeout: 0,
  207. },
  208. },
  209. {
  210. libipvs.Service{
  211. Protocol: syscall.IPPROTO_UDP,
  212. Port: 33434,
  213. FWMark: 0,
  214. SchedName: "wlc",
  215. Flags: 1234,
  216. Timeout: 100,
  217. Netmask: 128,
  218. AddressFamily: syscall.AF_INET6,
  219. Address: net.ParseIP("2012::beef"),
  220. PEName: "",
  221. },
  222. VirtualServer{
  223. Address: net.ParseIP("2012::beef"),
  224. Protocol: "UDP",
  225. Port: 33434,
  226. Scheduler: "wlc",
  227. Flags: 1234,
  228. Timeout: 100,
  229. },
  230. },
  231. {
  232. libipvs.Service{
  233. Protocol: 0,
  234. Port: 0,
  235. FWMark: 0,
  236. SchedName: "lc",
  237. Flags: 0,
  238. Timeout: 0,
  239. Netmask: 0xffffffff,
  240. AddressFamily: syscall.AF_INET,
  241. Address: net.ParseIP("1.2.3.4"),
  242. PEName: "",
  243. },
  244. VirtualServer{
  245. Address: net.ParseIP("1.2.3.4"),
  246. Protocol: "",
  247. Port: 0,
  248. Scheduler: "lc",
  249. Flags: 0,
  250. Timeout: 0,
  251. },
  252. },
  253. {
  254. libipvs.Service{
  255. Protocol: 0,
  256. Port: 0,
  257. FWMark: 0,
  258. SchedName: "wrr",
  259. Flags: 0,
  260. Timeout: 0,
  261. Netmask: 128,
  262. AddressFamily: syscall.AF_INET6,
  263. Address: net.ParseIP("::0"),
  264. PEName: "",
  265. },
  266. VirtualServer{
  267. Address: net.ParseIP("::0"),
  268. Protocol: "",
  269. Port: 0,
  270. Scheduler: "wrr",
  271. Flags: 0,
  272. Timeout: 0,
  273. },
  274. },
  275. }
  276. for i := range Tests {
  277. got, err := toIPVSService(&Tests[i].virtualServer)
  278. if err != nil {
  279. t.Errorf("case: %d, unexpected error: %v", i, err)
  280. }
  281. if !reflect.DeepEqual(*got, Tests[i].ipvsService) {
  282. t.Errorf("case: %d - got %#v, want %#v", i, *got, Tests[i].ipvsService)
  283. }
  284. }
  285. }
  286. func Test_toRealServer(t *testing.T) {
  287. Tests := []struct {
  288. ipvsDestination libipvs.Destination
  289. realServer RealServer
  290. }{
  291. {
  292. libipvs.Destination{
  293. Port: 54321,
  294. ConnectionFlags: 0,
  295. Weight: 1,
  296. Address: net.ParseIP("1.2.3.4"),
  297. },
  298. RealServer{
  299. Address: net.ParseIP("1.2.3.4"),
  300. Port: 54321,
  301. Weight: 1,
  302. },
  303. },
  304. {
  305. libipvs.Destination{
  306. Port: 53,
  307. ConnectionFlags: 0,
  308. Weight: 1,
  309. Address: net.ParseIP("2002::cafe"),
  310. },
  311. RealServer{
  312. Address: net.ParseIP("2002::cafe"),
  313. Port: 53,
  314. Weight: 1,
  315. },
  316. },
  317. }
  318. for i := range Tests {
  319. got, err := toRealServer(&Tests[i].ipvsDestination)
  320. if err != nil {
  321. t.Errorf("case %d unexpected error: %v", i, err)
  322. }
  323. if !reflect.DeepEqual(*got, Tests[i].realServer) {
  324. t.Errorf("case %d Failed to translate Destination - got %#v, want %#v", i, *got, Tests[i].realServer)
  325. }
  326. }
  327. }
  328. func Test_toIPVSDestination(t *testing.T) {
  329. Tests := []struct {
  330. realServer RealServer
  331. ipvsDestination libipvs.Destination
  332. }{
  333. {
  334. RealServer{
  335. Address: net.ParseIP("1.2.3.4"),
  336. Port: 54321,
  337. Weight: 1,
  338. },
  339. libipvs.Destination{
  340. Port: 54321,
  341. ConnectionFlags: 0,
  342. Weight: 1,
  343. Address: net.ParseIP("1.2.3.4"),
  344. },
  345. },
  346. {
  347. RealServer{
  348. Address: net.ParseIP("2002::cafe"),
  349. Port: 53,
  350. Weight: 1,
  351. },
  352. libipvs.Destination{
  353. Port: 53,
  354. ConnectionFlags: 0,
  355. Weight: 1,
  356. Address: net.ParseIP("2002::cafe"),
  357. },
  358. },
  359. }
  360. for i := range Tests {
  361. got, err := toIPVSDestination(&Tests[i].realServer)
  362. if err != nil {
  363. t.Errorf("case %d unexpected error: %v", i, err)
  364. }
  365. if !reflect.DeepEqual(*got, Tests[i].ipvsDestination) {
  366. t.Errorf("case %d failed to translate Destination - got %#v, want %#v", i, *got, Tests[i].ipvsDestination)
  367. }
  368. }
  369. }
  370. func Test_stringToProtocol(t *testing.T) {
  371. tests := []string{
  372. "TCP", "UDP", "ICMP", "SCTP",
  373. }
  374. expected := []uint16{
  375. uint16(syscall.IPPROTO_TCP), uint16(syscall.IPPROTO_UDP), uint16(0), uint16(syscall.IPPROTO_SCTP),
  376. }
  377. for i := range tests {
  378. got := stringToProtocol(tests[i])
  379. if got != expected[i] {
  380. t.Errorf("stringToProtocol() failed - got %#v, want %#v",
  381. got, expected[i])
  382. }
  383. }
  384. }
  385. func Test_protocolToString(t *testing.T) {
  386. tests := []Protocol{
  387. syscall.IPPROTO_TCP, syscall.IPPROTO_UDP, Protocol(0), syscall.IPPROTO_SCTP,
  388. }
  389. expected := []string{
  390. "TCP", "UDP", "", "SCTP",
  391. }
  392. for i := range tests {
  393. got := protocolToString(tests[i])
  394. if got != expected[i] {
  395. t.Errorf("protocolToString() failed - got %#v, want %#v",
  396. got, expected[i])
  397. }
  398. }
  399. }