cidr_set_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. /*
  2. Copyright 2016 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 cidrset
  14. import (
  15. "math/big"
  16. "net"
  17. "reflect"
  18. "testing"
  19. "k8s.io/klog"
  20. )
  21. func TestCIDRSetFullyAllocated(t *testing.T) {
  22. cases := []struct {
  23. clusterCIDRStr string
  24. subNetMaskSize int
  25. expectedCIDR string
  26. description string
  27. }{
  28. {
  29. clusterCIDRStr: "127.123.234.0/30",
  30. subNetMaskSize: 30,
  31. expectedCIDR: "127.123.234.0/30",
  32. description: "Fully allocated CIDR with IPv4",
  33. },
  34. {
  35. clusterCIDRStr: "beef:1234::/30",
  36. subNetMaskSize: 30,
  37. expectedCIDR: "beef:1234::/30",
  38. description: "Fully allocated CIDR with IPv6",
  39. },
  40. }
  41. for _, tc := range cases {
  42. _, clusterCIDR, _ := net.ParseCIDR(tc.clusterCIDRStr)
  43. a, err := NewCIDRSet(clusterCIDR, tc.subNetMaskSize)
  44. if err != nil {
  45. t.Fatalf("unexpected error: %v for %v", err, tc.description)
  46. }
  47. p, err := a.AllocateNext()
  48. if err != nil {
  49. t.Fatalf("unexpected error: %v for %v", err, tc.description)
  50. }
  51. if p.String() != tc.expectedCIDR {
  52. t.Fatalf("unexpected allocated cidr: %v, expecting %v for %v",
  53. p.String(), tc.expectedCIDR, tc.description)
  54. }
  55. _, err = a.AllocateNext()
  56. if err == nil {
  57. t.Fatalf("expected error because of fully-allocated range for %v", tc.description)
  58. }
  59. a.Release(p)
  60. p, err = a.AllocateNext()
  61. if err != nil {
  62. t.Fatalf("unexpected error: %v for %v", err, tc.description)
  63. }
  64. if p.String() != tc.expectedCIDR {
  65. t.Fatalf("unexpected allocated cidr: %v, expecting %v for %v",
  66. p.String(), tc.expectedCIDR, tc.description)
  67. }
  68. _, err = a.AllocateNext()
  69. if err == nil {
  70. t.Fatalf("expected error because of fully-allocated range for %v", tc.description)
  71. }
  72. }
  73. }
  74. func TestIndexToCIDRBlock(t *testing.T) {
  75. cases := []struct {
  76. clusterCIDRStr string
  77. subnetMaskSize int
  78. index int
  79. CIDRBlock string
  80. description string
  81. }{
  82. {
  83. clusterCIDRStr: "127.123.3.0/16",
  84. subnetMaskSize: 24,
  85. index: 0,
  86. CIDRBlock: "127.123.0.0/24",
  87. description: "1st IP address indexed with IPv4",
  88. },
  89. {
  90. clusterCIDRStr: "127.123.0.0/16",
  91. subnetMaskSize: 24,
  92. index: 15,
  93. CIDRBlock: "127.123.15.0/24",
  94. description: "16th IP address indexed with IPv4",
  95. },
  96. {
  97. clusterCIDRStr: "192.168.5.219/28",
  98. subnetMaskSize: 32,
  99. index: 5,
  100. CIDRBlock: "192.168.5.213/32",
  101. description: "5th IP address indexed with IPv4",
  102. },
  103. {
  104. clusterCIDRStr: "2001:0db8:1234:3::/48",
  105. subnetMaskSize: 64,
  106. index: 0,
  107. CIDRBlock: "2001:db8:1234::/64",
  108. description: "1st IP address indexed with IPv6 /64",
  109. },
  110. {
  111. clusterCIDRStr: "2001:0db8:1234::/48",
  112. subnetMaskSize: 64,
  113. index: 15,
  114. CIDRBlock: "2001:db8:1234:f::/64",
  115. description: "16th IP address indexed with IPv6 /64",
  116. },
  117. {
  118. clusterCIDRStr: "2001:0db8:85a3::8a2e:0370:7334/50",
  119. subnetMaskSize: 63,
  120. index: 6425,
  121. CIDRBlock: "2001:db8:85a3:3232::/63",
  122. description: "6426th IP address indexed with IPv6 /63",
  123. },
  124. {
  125. clusterCIDRStr: "2001:0db8::/32",
  126. subnetMaskSize: 48,
  127. index: 0,
  128. CIDRBlock: "2001:db8::/48",
  129. description: "1st IP address indexed with IPv6 /48",
  130. },
  131. {
  132. clusterCIDRStr: "2001:0db8::/32",
  133. subnetMaskSize: 48,
  134. index: 15,
  135. CIDRBlock: "2001:db8:f::/48",
  136. description: "16th IP address indexed with IPv6 /48",
  137. },
  138. {
  139. clusterCIDRStr: "2001:0db8:85a3::8a2e:0370:7334/32",
  140. subnetMaskSize: 48,
  141. index: 6425,
  142. CIDRBlock: "2001:db8:1919::/48",
  143. description: "6426th IP address indexed with IPv6 /48",
  144. },
  145. {
  146. clusterCIDRStr: "2001:0db8:1234:ff00::/56",
  147. subnetMaskSize: 72,
  148. index: 0,
  149. CIDRBlock: "2001:db8:1234:ff00::/72",
  150. description: "1st IP address indexed with IPv6 /72",
  151. },
  152. {
  153. clusterCIDRStr: "2001:0db8:1234:ff00::/56",
  154. subnetMaskSize: 72,
  155. index: 15,
  156. CIDRBlock: "2001:db8:1234:ff00:f00::/72",
  157. description: "16th IP address indexed with IPv6 /72",
  158. },
  159. {
  160. clusterCIDRStr: "2001:0db8:1234:ff00::0370:7334/56",
  161. subnetMaskSize: 72,
  162. index: 6425,
  163. CIDRBlock: "2001:db8:1234:ff19:1900::/72",
  164. description: "6426th IP address indexed with IPv6 /72",
  165. },
  166. {
  167. clusterCIDRStr: "2001:0db8:1234:0:1234::/80",
  168. subnetMaskSize: 96,
  169. index: 0,
  170. CIDRBlock: "2001:db8:1234:0:1234::/96",
  171. description: "1st IP address indexed with IPv6 /96",
  172. },
  173. {
  174. clusterCIDRStr: "2001:0db8:1234:0:1234::/80",
  175. subnetMaskSize: 96,
  176. index: 15,
  177. CIDRBlock: "2001:db8:1234:0:1234:f::/96",
  178. description: "16th IP address indexed with IPv6 /96",
  179. },
  180. {
  181. clusterCIDRStr: "2001:0db8:1234:ff00::0370:7334/80",
  182. subnetMaskSize: 96,
  183. index: 6425,
  184. CIDRBlock: "2001:db8:1234:ff00:0:1919::/96",
  185. description: "6426th IP address indexed with IPv6 /96",
  186. },
  187. }
  188. for _, tc := range cases {
  189. _, clusterCIDR, _ := net.ParseCIDR(tc.clusterCIDRStr)
  190. a, err := NewCIDRSet(clusterCIDR, tc.subnetMaskSize)
  191. if err != nil {
  192. t.Fatalf("error for %v ", tc.description)
  193. }
  194. cidr := a.indexToCIDRBlock(tc.index)
  195. if cidr.String() != tc.CIDRBlock {
  196. t.Fatalf("error for %v index %d %s", tc.description, tc.index, cidr.String())
  197. }
  198. }
  199. }
  200. func TestCIDRSet_RandomishAllocation(t *testing.T) {
  201. cases := []struct {
  202. clusterCIDRStr string
  203. description string
  204. }{
  205. {
  206. clusterCIDRStr: "127.123.234.0/16",
  207. description: "RandomishAllocation with IPv4",
  208. },
  209. {
  210. clusterCIDRStr: "beef:1234::/16",
  211. description: "RandomishAllocation with IPv6",
  212. },
  213. }
  214. for _, tc := range cases {
  215. _, clusterCIDR, _ := net.ParseCIDR(tc.clusterCIDRStr)
  216. a, err := NewCIDRSet(clusterCIDR, 24)
  217. if err != nil {
  218. t.Fatalf("Error allocating CIDRSet for %v", tc.description)
  219. }
  220. // allocate all the CIDRs
  221. var cidrs []*net.IPNet
  222. for i := 0; i < 256; i++ {
  223. if c, err := a.AllocateNext(); err == nil {
  224. cidrs = append(cidrs, c)
  225. } else {
  226. t.Fatalf("unexpected error: %v for %v", err, tc.description)
  227. }
  228. }
  229. //var err error
  230. _, err = a.AllocateNext()
  231. if err == nil {
  232. t.Fatalf("expected error because of fully-allocated range for %v", tc.description)
  233. }
  234. // release them all
  235. for i := 0; i < len(cidrs); i++ {
  236. a.Release(cidrs[i])
  237. }
  238. // allocate the CIDRs again
  239. var rcidrs []*net.IPNet
  240. for i := 0; i < 256; i++ {
  241. if c, err := a.AllocateNext(); err == nil {
  242. rcidrs = append(rcidrs, c)
  243. } else {
  244. t.Fatalf("unexpected error: %d, %v for %v", i, err, tc.description)
  245. }
  246. }
  247. _, err = a.AllocateNext()
  248. if err == nil {
  249. t.Fatalf("expected error because of fully-allocated range for %v", tc.description)
  250. }
  251. if !reflect.DeepEqual(cidrs, rcidrs) {
  252. t.Fatalf("expected re-allocated cidrs are the same collection for %v", tc.description)
  253. }
  254. }
  255. }
  256. func TestCIDRSet_AllocationOccupied(t *testing.T) {
  257. cases := []struct {
  258. clusterCIDRStr string
  259. description string
  260. }{
  261. {
  262. clusterCIDRStr: "127.123.234.0/16",
  263. description: "AllocationOccupied with IPv4",
  264. },
  265. {
  266. clusterCIDRStr: "beef:1234::/16",
  267. description: "AllocationOccupied with IPv6",
  268. },
  269. }
  270. for _, tc := range cases {
  271. _, clusterCIDR, _ := net.ParseCIDR(tc.clusterCIDRStr)
  272. a, err := NewCIDRSet(clusterCIDR, 24)
  273. if err != nil {
  274. t.Fatalf("Error allocating CIDRSet for %v", tc.description)
  275. }
  276. // allocate all the CIDRs
  277. var cidrs []*net.IPNet
  278. var numCIDRs = 256
  279. for i := 0; i < numCIDRs; i++ {
  280. if c, err := a.AllocateNext(); err == nil {
  281. cidrs = append(cidrs, c)
  282. } else {
  283. t.Fatalf("unexpected error: %v for %v", err, tc.description)
  284. }
  285. }
  286. //var err error
  287. _, err = a.AllocateNext()
  288. if err == nil {
  289. t.Fatalf("expected error because of fully-allocated range for %v", tc.description)
  290. }
  291. // release them all
  292. for i := 0; i < len(cidrs); i++ {
  293. a.Release(cidrs[i])
  294. }
  295. // occupy the last 128 CIDRs
  296. for i := numCIDRs / 2; i < numCIDRs; i++ {
  297. a.Occupy(cidrs[i])
  298. }
  299. // allocate the first 128 CIDRs again
  300. var rcidrs []*net.IPNet
  301. for i := 0; i < numCIDRs/2; i++ {
  302. if c, err := a.AllocateNext(); err == nil {
  303. rcidrs = append(rcidrs, c)
  304. } else {
  305. t.Fatalf("unexpected error: %d, %v for %v", i, err, tc.description)
  306. }
  307. }
  308. _, err = a.AllocateNext()
  309. if err == nil {
  310. t.Fatalf("expected error because of fully-allocated range for %v", tc.description)
  311. }
  312. // check Occupy() work properly
  313. for i := numCIDRs / 2; i < numCIDRs; i++ {
  314. rcidrs = append(rcidrs, cidrs[i])
  315. }
  316. if !reflect.DeepEqual(cidrs, rcidrs) {
  317. t.Fatalf("expected re-allocated cidrs are the same collection for %v", tc.description)
  318. }
  319. }
  320. }
  321. func TestGetBitforCIDR(t *testing.T) {
  322. cases := []struct {
  323. clusterCIDRStr string
  324. subNetMaskSize int
  325. subNetCIDRStr string
  326. expectedBit int
  327. expectErr bool
  328. description string
  329. }{
  330. {
  331. clusterCIDRStr: "127.0.0.0/8",
  332. subNetMaskSize: 16,
  333. subNetCIDRStr: "127.0.0.0/16",
  334. expectedBit: 0,
  335. expectErr: false,
  336. description: "Get 0 Bit with IPv4",
  337. },
  338. {
  339. clusterCIDRStr: "be00::/8",
  340. subNetMaskSize: 16,
  341. subNetCIDRStr: "be00::/16",
  342. expectedBit: 0,
  343. expectErr: false,
  344. description: "Get 0 Bit with IPv6",
  345. },
  346. {
  347. clusterCIDRStr: "127.0.0.0/8",
  348. subNetMaskSize: 16,
  349. subNetCIDRStr: "127.123.0.0/16",
  350. expectedBit: 123,
  351. expectErr: false,
  352. description: "Get 123rd Bit with IPv4",
  353. },
  354. {
  355. clusterCIDRStr: "be00::/8",
  356. subNetMaskSize: 16,
  357. subNetCIDRStr: "beef::/16",
  358. expectedBit: 0xef,
  359. expectErr: false,
  360. description: "Get xef Bit with IPv6",
  361. },
  362. {
  363. clusterCIDRStr: "127.0.0.0/8",
  364. subNetMaskSize: 16,
  365. subNetCIDRStr: "127.168.0.0/16",
  366. expectedBit: 168,
  367. expectErr: false,
  368. description: "Get 168th Bit with IPv4",
  369. },
  370. {
  371. clusterCIDRStr: "be00::/8",
  372. subNetMaskSize: 16,
  373. subNetCIDRStr: "be68::/16",
  374. expectedBit: 0x68,
  375. expectErr: false,
  376. description: "Get x68th Bit with IPv6",
  377. },
  378. {
  379. clusterCIDRStr: "127.0.0.0/8",
  380. subNetMaskSize: 16,
  381. subNetCIDRStr: "127.224.0.0/16",
  382. expectedBit: 224,
  383. expectErr: false,
  384. description: "Get 224th Bit with IPv4",
  385. },
  386. {
  387. clusterCIDRStr: "be00::/8",
  388. subNetMaskSize: 16,
  389. subNetCIDRStr: "be24::/16",
  390. expectedBit: 0x24,
  391. expectErr: false,
  392. description: "Get x24th Bit with IPv6",
  393. },
  394. {
  395. clusterCIDRStr: "192.168.0.0/16",
  396. subNetMaskSize: 24,
  397. subNetCIDRStr: "192.168.12.0/24",
  398. expectedBit: 12,
  399. expectErr: false,
  400. description: "Get 12th Bit with IPv4",
  401. },
  402. {
  403. clusterCIDRStr: "beef::/16",
  404. subNetMaskSize: 24,
  405. subNetCIDRStr: "beef:1200::/24",
  406. expectedBit: 0x12,
  407. expectErr: false,
  408. description: "Get x12th Bit with IPv6",
  409. },
  410. {
  411. clusterCIDRStr: "192.168.0.0/16",
  412. subNetMaskSize: 24,
  413. subNetCIDRStr: "192.168.151.0/24",
  414. expectedBit: 151,
  415. expectErr: false,
  416. description: "Get 151st Bit with IPv4",
  417. },
  418. {
  419. clusterCIDRStr: "beef::/16",
  420. subNetMaskSize: 24,
  421. subNetCIDRStr: "beef:9700::/24",
  422. expectedBit: 0x97,
  423. expectErr: false,
  424. description: "Get x97st Bit with IPv6",
  425. },
  426. {
  427. clusterCIDRStr: "192.168.0.0/16",
  428. subNetMaskSize: 24,
  429. subNetCIDRStr: "127.168.224.0/24",
  430. expectErr: true,
  431. description: "Get error with IPv4",
  432. },
  433. {
  434. clusterCIDRStr: "beef::/16",
  435. subNetMaskSize: 24,
  436. subNetCIDRStr: "2001:db00::/24",
  437. expectErr: true,
  438. description: "Get error with IPv6",
  439. },
  440. }
  441. for _, tc := range cases {
  442. _, clusterCIDR, err := net.ParseCIDR(tc.clusterCIDRStr)
  443. if err != nil {
  444. t.Fatalf("unexpected error: %v for %v", err, tc.description)
  445. }
  446. cs, err := NewCIDRSet(clusterCIDR, tc.subNetMaskSize)
  447. if err != nil {
  448. t.Fatalf("Error allocating CIDRSet for %v", tc.description)
  449. }
  450. _, subnetCIDR, err := net.ParseCIDR(tc.subNetCIDRStr)
  451. if err != nil {
  452. t.Fatalf("unexpected error: %v for %v", err, tc.description)
  453. }
  454. got, err := cs.getIndexForCIDR(subnetCIDR)
  455. if err == nil && tc.expectErr {
  456. klog.Errorf("expected error but got null for %v", tc.description)
  457. continue
  458. }
  459. if err != nil && !tc.expectErr {
  460. klog.Errorf("unexpected error: %v for %v", err, tc.description)
  461. continue
  462. }
  463. if got != tc.expectedBit {
  464. klog.Errorf("expected %v, but got %v for %v", tc.expectedBit, got, tc.description)
  465. }
  466. }
  467. }
  468. func TestOccupy(t *testing.T) {
  469. cases := []struct {
  470. clusterCIDRStr string
  471. subNetMaskSize int
  472. subNetCIDRStr string
  473. expectedUsedBegin int
  474. expectedUsedEnd int
  475. expectErr bool
  476. description string
  477. }{
  478. {
  479. clusterCIDRStr: "127.0.0.0/8",
  480. subNetMaskSize: 16,
  481. subNetCIDRStr: "127.0.0.0/8",
  482. expectedUsedBegin: 0,
  483. expectedUsedEnd: 255,
  484. expectErr: false,
  485. description: "Occupy all Bits with IPv4",
  486. },
  487. {
  488. clusterCIDRStr: "2001:beef:1200::/40",
  489. subNetMaskSize: 48,
  490. subNetCIDRStr: "2001:beef:1200::/40",
  491. expectedUsedBegin: 0,
  492. expectedUsedEnd: 255,
  493. expectErr: false,
  494. description: "Occupy all Bits with IPv6",
  495. },
  496. {
  497. clusterCIDRStr: "127.0.0.0/8",
  498. subNetMaskSize: 16,
  499. subNetCIDRStr: "127.0.0.0/2",
  500. expectedUsedBegin: 0,
  501. expectedUsedEnd: 255,
  502. expectErr: false,
  503. description: "Occupy every Bit with IPv4",
  504. },
  505. {
  506. clusterCIDRStr: "2001:beef:1200::/40",
  507. subNetMaskSize: 48,
  508. subNetCIDRStr: "2001:beef:1234::/34",
  509. expectedUsedBegin: 0,
  510. expectedUsedEnd: 255,
  511. expectErr: false,
  512. description: "Occupy every Bit with IPv6",
  513. },
  514. {
  515. clusterCIDRStr: "127.0.0.0/8",
  516. subNetMaskSize: 16,
  517. subNetCIDRStr: "127.0.0.0/16",
  518. expectedUsedBegin: 0,
  519. expectedUsedEnd: 0,
  520. expectErr: false,
  521. description: "Occupy 1st Bit with IPv4",
  522. },
  523. {
  524. clusterCIDRStr: "2001:beef:1200::/40",
  525. subNetMaskSize: 48,
  526. subNetCIDRStr: "2001:beef:1200::/48",
  527. expectedUsedBegin: 0,
  528. expectedUsedEnd: 0,
  529. expectErr: false,
  530. description: "Occupy 1st Bit with IPv6",
  531. },
  532. {
  533. clusterCIDRStr: "127.0.0.0/8",
  534. subNetMaskSize: 32,
  535. subNetCIDRStr: "127.0.0.0/16",
  536. expectedUsedBegin: 0,
  537. expectedUsedEnd: 65535,
  538. expectErr: false,
  539. description: "Occupy 65535 Bits with IPv4",
  540. },
  541. {
  542. clusterCIDRStr: "2001:beef:1200::/48",
  543. subNetMaskSize: 64,
  544. subNetCIDRStr: "2001:beef:1200::/48",
  545. expectedUsedBegin: 0,
  546. expectedUsedEnd: 65535,
  547. expectErr: false,
  548. description: "Occupy 65535 Bits with IPv6",
  549. },
  550. {
  551. clusterCIDRStr: "127.0.0.0/7",
  552. subNetMaskSize: 16,
  553. subNetCIDRStr: "127.0.0.0/15",
  554. expectedUsedBegin: 256,
  555. expectedUsedEnd: 257,
  556. expectErr: false,
  557. description: "Occupy 257th Bit with IPv4",
  558. },
  559. {
  560. clusterCIDRStr: "2001:beef:7f00::/39",
  561. subNetMaskSize: 48,
  562. subNetCIDRStr: "2001:beef:7f00::/47",
  563. expectedUsedBegin: 256,
  564. expectedUsedEnd: 257,
  565. expectErr: false,
  566. description: "Occupy 257th Bit with IPv6",
  567. },
  568. {
  569. clusterCIDRStr: "127.0.0.0/7",
  570. subNetMaskSize: 15,
  571. subNetCIDRStr: "127.0.0.0/15",
  572. expectedUsedBegin: 128,
  573. expectedUsedEnd: 128,
  574. expectErr: false,
  575. description: "Occupy 128th Bit with IPv4",
  576. },
  577. {
  578. clusterCIDRStr: "2001:beef:7f00::/39",
  579. subNetMaskSize: 47,
  580. subNetCIDRStr: "2001:beef:7f00::/47",
  581. expectedUsedBegin: 128,
  582. expectedUsedEnd: 128,
  583. expectErr: false,
  584. description: "Occupy 128th Bit with IPv6",
  585. },
  586. {
  587. clusterCIDRStr: "127.0.0.0/7",
  588. subNetMaskSize: 18,
  589. subNetCIDRStr: "127.0.0.0/15",
  590. expectedUsedBegin: 1024,
  591. expectedUsedEnd: 1031,
  592. expectErr: false,
  593. description: "Occupy 1031st Bit with IPv4",
  594. },
  595. {
  596. clusterCIDRStr: "2001:beef:7f00::/39",
  597. subNetMaskSize: 50,
  598. subNetCIDRStr: "2001:beef:7f00::/47",
  599. expectedUsedBegin: 1024,
  600. expectedUsedEnd: 1031,
  601. expectErr: false,
  602. description: "Occupy 1031st Bit with IPv6",
  603. },
  604. }
  605. for _, tc := range cases {
  606. _, clusterCIDR, err := net.ParseCIDR(tc.clusterCIDRStr)
  607. if err != nil {
  608. t.Fatalf("unexpected error: %v for %v", err, tc.description)
  609. }
  610. cs, err := NewCIDRSet(clusterCIDR, tc.subNetMaskSize)
  611. if err != nil {
  612. t.Fatalf("Error allocating CIDRSet for %v", tc.description)
  613. }
  614. _, subnetCIDR, err := net.ParseCIDR(tc.subNetCIDRStr)
  615. if err != nil {
  616. t.Fatalf("unexpected error: %v for %v", err, tc.description)
  617. }
  618. err = cs.Occupy(subnetCIDR)
  619. if err == nil && tc.expectErr {
  620. t.Errorf("expected error but got none for %v", tc.description)
  621. continue
  622. }
  623. if err != nil && !tc.expectErr {
  624. t.Errorf("unexpected error: %v for %v", err, tc.description)
  625. continue
  626. }
  627. expectedUsed := big.Int{}
  628. for i := tc.expectedUsedBegin; i <= tc.expectedUsedEnd; i++ {
  629. expectedUsed.SetBit(&expectedUsed, i, 1)
  630. }
  631. if expectedUsed.Cmp(&cs.used) != 0 {
  632. t.Errorf("error for %v", tc.description)
  633. }
  634. }
  635. }
  636. func TestCIDRSetv6(t *testing.T) {
  637. cases := []struct {
  638. clusterCIDRStr string
  639. subNetMaskSize int
  640. expectedCIDR string
  641. expectedCIDR2 string
  642. expectErr bool
  643. description string
  644. }{
  645. {
  646. clusterCIDRStr: "127.0.0.0/8",
  647. subNetMaskSize: 32,
  648. expectErr: false,
  649. expectedCIDR: "127.0.0.0/32",
  650. expectedCIDR2: "127.0.0.1/32",
  651. description: "Max cluster subnet size with IPv4",
  652. },
  653. {
  654. clusterCIDRStr: "beef:1234::/32",
  655. subNetMaskSize: 49,
  656. expectErr: true,
  657. description: "Max cluster subnet size with IPv6",
  658. },
  659. {
  660. clusterCIDRStr: "2001:beef:1234:369b::/60",
  661. subNetMaskSize: 64,
  662. expectedCIDR: "2001:beef:1234:3690::/64",
  663. expectedCIDR2: "2001:beef:1234:3691::/64",
  664. expectErr: false,
  665. description: "Allocate a few IPv6",
  666. },
  667. }
  668. for _, tc := range cases {
  669. t.Run(tc.description, func(t *testing.T) {
  670. _, clusterCIDR, _ := net.ParseCIDR(tc.clusterCIDRStr)
  671. a, err := NewCIDRSet(clusterCIDR, tc.subNetMaskSize)
  672. if gotErr := err != nil; gotErr != tc.expectErr {
  673. t.Fatalf("NewCIDRSet(%v, %v) = %v, %v; gotErr = %t, want %t", clusterCIDR, tc.subNetMaskSize, a, err, gotErr, tc.expectErr)
  674. }
  675. if a == nil {
  676. return
  677. }
  678. p, err := a.AllocateNext()
  679. if err == nil && tc.expectErr {
  680. t.Errorf("a.AllocateNext() = nil, want error")
  681. }
  682. if err != nil && !tc.expectErr {
  683. t.Errorf("a.AllocateNext() = %+v, want no error", err)
  684. }
  685. if !tc.expectErr {
  686. if p != nil && p.String() != tc.expectedCIDR {
  687. t.Fatalf("a.AllocateNext() got %+v, want %+v", p.String(), tc.expectedCIDR)
  688. }
  689. }
  690. p2, err := a.AllocateNext()
  691. if err == nil && tc.expectErr {
  692. t.Errorf("a.AllocateNext() = nil, want error")
  693. }
  694. if err != nil && !tc.expectErr {
  695. t.Errorf("a.AllocateNext() = %+v, want no error", err)
  696. }
  697. if !tc.expectErr {
  698. if p2 != nil && p2.String() != tc.expectedCIDR2 {
  699. t.Fatalf("a.AllocateNext() got %+v, want %+v", p2.String(), tc.expectedCIDR)
  700. }
  701. }
  702. })
  703. }
  704. }