github.com/erriapo/docker@v1.6.0-rc2/daemon/networkdriver/ipallocator/allocator_test.go (about) 1 package ipallocator 2 3 import ( 4 "fmt" 5 "math/big" 6 "net" 7 "testing" 8 ) 9 10 func reset() { 11 allocatedIPs = networkSet{} 12 } 13 14 func TestConversion(t *testing.T) { 15 ip := net.ParseIP("127.0.0.1") 16 i := ipToBigInt(ip) 17 if i.Cmp(big.NewInt(0x7f000001)) != 0 { 18 t.Fatal("incorrect conversion") 19 } 20 conv := bigIntToIP(i) 21 if !ip.Equal(conv) { 22 t.Error(conv.String()) 23 } 24 } 25 26 func TestConversionIPv6(t *testing.T) { 27 ip := net.ParseIP("2a00:1450::1") 28 ip2 := net.ParseIP("2a00:1450::2") 29 ip3 := net.ParseIP("2a00:1450::1:1") 30 i := ipToBigInt(ip) 31 val, success := big.NewInt(0).SetString("2a001450000000000000000000000001", 16) 32 if !success { 33 t.Fatal("Hex-String to BigInt conversion failed.") 34 } 35 if i.Cmp(val) != 0 { 36 t.Fatal("incorrent conversion") 37 } 38 39 conv := bigIntToIP(i) 40 conv2 := bigIntToIP(big.NewInt(0).Add(i, big.NewInt(1))) 41 conv3 := bigIntToIP(big.NewInt(0).Add(i, big.NewInt(0x10000))) 42 43 if !ip.Equal(conv) { 44 t.Error("2a00:1450::1 should be equal to " + conv.String()) 45 } 46 if !ip2.Equal(conv2) { 47 t.Error("2a00:1450::2 should be equal to " + conv2.String()) 48 } 49 if !ip3.Equal(conv3) { 50 t.Error("2a00:1450::1:1 should be equal to " + conv3.String()) 51 } 52 } 53 54 func TestRequestNewIps(t *testing.T) { 55 defer reset() 56 network := &net.IPNet{ 57 IP: []byte{192, 168, 0, 1}, 58 Mask: []byte{255, 255, 255, 0}, 59 } 60 61 var ip net.IP 62 var err error 63 64 for i := 1; i < 10; i++ { 65 ip, err = RequestIP(network, nil) 66 if err != nil { 67 t.Fatal(err) 68 } 69 70 if expected := fmt.Sprintf("192.168.0.%d", i); ip.String() != expected { 71 t.Fatalf("Expected ip %s got %s", expected, ip.String()) 72 } 73 } 74 value := bigIntToIP(big.NewInt(0).Add(ipToBigInt(ip), big.NewInt(1))).String() 75 if err := ReleaseIP(network, ip); err != nil { 76 t.Fatal(err) 77 } 78 ip, err = RequestIP(network, nil) 79 if err != nil { 80 t.Fatal(err) 81 } 82 if ip.String() != value { 83 t.Fatalf("Expected to receive the next ip %s got %s", value, ip.String()) 84 } 85 } 86 87 func TestRequestNewIpV6(t *testing.T) { 88 defer reset() 89 network := &net.IPNet{ 90 IP: []byte{0x2a, 0x00, 0x14, 0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 91 Mask: []byte{255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0}, // /64 netmask 92 } 93 94 var ip net.IP 95 var err error 96 for i := 1; i < 10; i++ { 97 ip, err = RequestIP(network, nil) 98 if err != nil { 99 t.Fatal(err) 100 } 101 102 if expected := fmt.Sprintf("2a00:1450::%d", i); ip.String() != expected { 103 t.Fatalf("Expected ip %s got %s", expected, ip.String()) 104 } 105 } 106 value := bigIntToIP(big.NewInt(0).Add(ipToBigInt(ip), big.NewInt(1))).String() 107 if err := ReleaseIP(network, ip); err != nil { 108 t.Fatal(err) 109 } 110 ip, err = RequestIP(network, nil) 111 if err != nil { 112 t.Fatal(err) 113 } 114 if ip.String() != value { 115 t.Fatalf("Expected to receive the next ip %s got %s", value, ip.String()) 116 } 117 } 118 119 func TestReleaseIp(t *testing.T) { 120 defer reset() 121 network := &net.IPNet{ 122 IP: []byte{192, 168, 0, 1}, 123 Mask: []byte{255, 255, 255, 0}, 124 } 125 126 ip, err := RequestIP(network, nil) 127 if err != nil { 128 t.Fatal(err) 129 } 130 131 if err := ReleaseIP(network, ip); err != nil { 132 t.Fatal(err) 133 } 134 } 135 136 func TestReleaseIpV6(t *testing.T) { 137 defer reset() 138 network := &net.IPNet{ 139 IP: []byte{0x2a, 0x00, 0x14, 0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 140 Mask: []byte{255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0}, // /64 netmask 141 } 142 143 ip, err := RequestIP(network, nil) 144 if err != nil { 145 t.Fatal(err) 146 } 147 148 if err := ReleaseIP(network, ip); err != nil { 149 t.Fatal(err) 150 } 151 } 152 153 func TestGetReleasedIp(t *testing.T) { 154 defer reset() 155 network := &net.IPNet{ 156 IP: []byte{192, 168, 0, 1}, 157 Mask: []byte{255, 255, 255, 0}, 158 } 159 160 ip, err := RequestIP(network, nil) 161 if err != nil { 162 t.Fatal(err) 163 } 164 165 value := ip.String() 166 if err := ReleaseIP(network, ip); err != nil { 167 t.Fatal(err) 168 } 169 170 for i := 0; i < 253; i++ { 171 _, err = RequestIP(network, nil) 172 if err != nil { 173 t.Fatal(err) 174 } 175 err = ReleaseIP(network, ip) 176 if err != nil { 177 t.Fatal(err) 178 } 179 } 180 181 ip, err = RequestIP(network, nil) 182 if err != nil { 183 t.Fatal(err) 184 } 185 186 if ip.String() != value { 187 t.Fatalf("Expected to receive same ip %s got %s", value, ip.String()) 188 } 189 } 190 191 func TestGetReleasedIpV6(t *testing.T) { 192 defer reset() 193 network := &net.IPNet{ 194 IP: []byte{0x2a, 0x00, 0x14, 0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 195 Mask: []byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0}, 196 } 197 198 ip, err := RequestIP(network, nil) 199 if err != nil { 200 t.Fatal(err) 201 } 202 203 value := ip.String() 204 if err := ReleaseIP(network, ip); err != nil { 205 t.Fatal(err) 206 } 207 208 for i := 0; i < 253; i++ { 209 _, err = RequestIP(network, nil) 210 if err != nil { 211 t.Fatal(err) 212 } 213 err = ReleaseIP(network, ip) 214 if err != nil { 215 t.Fatal(err) 216 } 217 } 218 219 ip, err = RequestIP(network, nil) 220 if err != nil { 221 t.Fatal(err) 222 } 223 224 if ip.String() != value { 225 t.Fatalf("Expected to receive same ip %s got %s", value, ip.String()) 226 } 227 } 228 229 func TestRequestSpecificIp(t *testing.T) { 230 defer reset() 231 network := &net.IPNet{ 232 IP: []byte{192, 168, 0, 1}, 233 Mask: []byte{255, 255, 255, 224}, 234 } 235 236 ip := net.ParseIP("192.168.0.5") 237 238 // Request a "good" IP. 239 if _, err := RequestIP(network, ip); err != nil { 240 t.Fatal(err) 241 } 242 243 // Request the same IP again. 244 if _, err := RequestIP(network, ip); err != ErrIPAlreadyAllocated { 245 t.Fatalf("Got the same IP twice: %#v", err) 246 } 247 248 // Request an out of range IP. 249 if _, err := RequestIP(network, net.ParseIP("192.168.0.42")); err != ErrIPOutOfRange { 250 t.Fatalf("Got an out of range IP: %#v", err) 251 } 252 } 253 254 func TestRequestSpecificIpV6(t *testing.T) { 255 defer reset() 256 network := &net.IPNet{ 257 IP: []byte{0x2a, 0x00, 0x14, 0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 258 Mask: []byte{255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0}, // /64 netmask 259 } 260 261 ip := net.ParseIP("2a00:1450::5") 262 263 // Request a "good" IP. 264 if _, err := RequestIP(network, ip); err != nil { 265 t.Fatal(err) 266 } 267 268 // Request the same IP again. 269 if _, err := RequestIP(network, ip); err != ErrIPAlreadyAllocated { 270 t.Fatalf("Got the same IP twice: %#v", err) 271 } 272 273 // Request an out of range IP. 274 if _, err := RequestIP(network, net.ParseIP("2a00:1500::1")); err != ErrIPOutOfRange { 275 t.Fatalf("Got an out of range IP: %#v", err) 276 } 277 } 278 279 func TestIPAllocator(t *testing.T) { 280 expectedIPs := []net.IP{ 281 0: net.IPv4(127, 0, 0, 1), 282 1: net.IPv4(127, 0, 0, 2), 283 2: net.IPv4(127, 0, 0, 3), 284 3: net.IPv4(127, 0, 0, 4), 285 4: net.IPv4(127, 0, 0, 5), 286 5: net.IPv4(127, 0, 0, 6), 287 } 288 289 gwIP, n, _ := net.ParseCIDR("127.0.0.1/29") 290 291 network := &net.IPNet{IP: gwIP, Mask: n.Mask} 292 // Pool after initialisation (f = free, u = used) 293 // 1(f) - 2(f) - 3(f) - 4(f) - 5(f) - 6(f) 294 // ↑ 295 296 // Check that we get 6 IPs, from 127.0.0.1–127.0.0.6, in that 297 // order. 298 for i := 0; i < 6; i++ { 299 ip, err := RequestIP(network, nil) 300 if err != nil { 301 t.Fatal(err) 302 } 303 304 assertIPEquals(t, expectedIPs[i], ip) 305 } 306 // Before loop begin 307 // 1(f) - 2(f) - 3(f) - 4(f) - 5(f) - 6(f) 308 // ↑ 309 310 // After i = 0 311 // 1(u) - 2(f) - 3(f) - 4(f) - 5(f) - 6(f) 312 // ↑ 313 314 // After i = 1 315 // 1(u) - 2(u) - 3(f) - 4(f) - 5(f) - 6(f) 316 // ↑ 317 318 // After i = 2 319 // 1(u) - 2(u) - 3(u) - 4(f) - 5(f) - 6(f) 320 // ↑ 321 322 // After i = 3 323 // 1(u) - 2(u) - 3(u) - 4(u) - 5(f) - 6(f) 324 // ↑ 325 326 // After i = 4 327 // 1(u) - 2(u) - 3(u) - 4(u) - 5(u) - 6(f) 328 // ↑ 329 330 // After i = 5 331 // 1(u) - 2(u) - 3(u) - 4(u) - 5(u) - 6(u) 332 // ↑ 333 334 // Check that there are no more IPs 335 ip, err := RequestIP(network, nil) 336 if err == nil { 337 t.Fatalf("There shouldn't be any IP addresses at this point, got %s\n", ip) 338 } 339 340 // Release some IPs in non-sequential order 341 if err := ReleaseIP(network, expectedIPs[3]); err != nil { 342 t.Fatal(err) 343 } 344 // 1(u) - 2(u) - 3(u) - 4(f) - 5(u) - 6(u) 345 // ↑ 346 347 if err := ReleaseIP(network, expectedIPs[2]); err != nil { 348 t.Fatal(err) 349 } 350 // 1(u) - 2(u) - 3(f) - 4(f) - 5(u) - 6(u) 351 // ↑ 352 353 if err := ReleaseIP(network, expectedIPs[4]); err != nil { 354 t.Fatal(err) 355 } 356 // 1(u) - 2(u) - 3(f) - 4(f) - 5(f) - 6(u) 357 // ↑ 358 359 // Make sure that IPs are reused in sequential order, starting 360 // with the first released IP 361 newIPs := make([]net.IP, 3) 362 for i := 0; i < 3; i++ { 363 ip, err := RequestIP(network, nil) 364 if err != nil { 365 t.Fatal(err) 366 } 367 368 newIPs[i] = ip 369 } 370 assertIPEquals(t, expectedIPs[2], newIPs[0]) 371 assertIPEquals(t, expectedIPs[3], newIPs[1]) 372 assertIPEquals(t, expectedIPs[4], newIPs[2]) 373 374 _, err = RequestIP(network, nil) 375 if err == nil { 376 t.Fatal("There shouldn't be any IP addresses at this point") 377 } 378 } 379 380 func TestAllocateFirstIP(t *testing.T) { 381 defer reset() 382 network := &net.IPNet{ 383 IP: []byte{192, 168, 0, 0}, 384 Mask: []byte{255, 255, 255, 0}, 385 } 386 387 firstIP := network.IP.To4().Mask(network.Mask) 388 first := big.NewInt(0).Add(ipToBigInt(firstIP), big.NewInt(1)) 389 390 ip, err := RequestIP(network, nil) 391 if err != nil { 392 t.Fatal(err) 393 } 394 allocated := ipToBigInt(ip) 395 396 if allocated == first { 397 t.Fatalf("allocated ip should not equal first ip: %d == %d", first, allocated) 398 } 399 } 400 401 func TestAllocateAllIps(t *testing.T) { 402 defer reset() 403 network := &net.IPNet{ 404 IP: []byte{192, 168, 0, 1}, 405 Mask: []byte{255, 255, 255, 0}, 406 } 407 408 var ( 409 current, first net.IP 410 err error 411 isFirst = true 412 ) 413 414 for err == nil { 415 current, err = RequestIP(network, nil) 416 if isFirst { 417 first = current 418 isFirst = false 419 } 420 } 421 422 if err != ErrNoAvailableIPs { 423 t.Fatal(err) 424 } 425 426 if _, err := RequestIP(network, nil); err != ErrNoAvailableIPs { 427 t.Fatal(err) 428 } 429 430 if err := ReleaseIP(network, first); err != nil { 431 t.Fatal(err) 432 } 433 434 again, err := RequestIP(network, nil) 435 if err != nil { 436 t.Fatal(err) 437 } 438 439 assertIPEquals(t, first, again) 440 441 // ensure that alloc.last == alloc.begin won't result in dead loop 442 if _, err := RequestIP(network, nil); err != ErrNoAvailableIPs { 443 t.Fatal(err) 444 } 445 446 // Test by making alloc.last the only free ip and ensure we get it back 447 // #1. first of the range, (alloc.last == ipToInt(first) already) 448 if err := ReleaseIP(network, first); err != nil { 449 t.Fatal(err) 450 } 451 452 ret, err := RequestIP(network, nil) 453 if err != nil { 454 t.Fatal(err) 455 } 456 457 assertIPEquals(t, first, ret) 458 459 // #2. last of the range, note that current is the last one 460 last := net.IPv4(192, 168, 0, 254) 461 setLastTo(t, network, last) 462 463 ret, err = RequestIP(network, nil) 464 if err != nil { 465 t.Fatal(err) 466 } 467 468 assertIPEquals(t, last, ret) 469 470 // #3. middle of the range 471 mid := net.IPv4(192, 168, 0, 7) 472 setLastTo(t, network, mid) 473 474 ret, err = RequestIP(network, nil) 475 if err != nil { 476 t.Fatal(err) 477 } 478 479 assertIPEquals(t, mid, ret) 480 } 481 482 // make sure the pool is full when calling setLastTo. 483 // we don't cheat here 484 func setLastTo(t *testing.T, network *net.IPNet, ip net.IP) { 485 if err := ReleaseIP(network, ip); err != nil { 486 t.Fatal(err) 487 } 488 489 ret, err := RequestIP(network, nil) 490 if err != nil { 491 t.Fatal(err) 492 } 493 494 assertIPEquals(t, ip, ret) 495 496 if err := ReleaseIP(network, ip); err != nil { 497 t.Fatal(err) 498 } 499 } 500 501 func TestAllocateDifferentSubnets(t *testing.T) { 502 defer reset() 503 network1 := &net.IPNet{ 504 IP: []byte{192, 168, 0, 1}, 505 Mask: []byte{255, 255, 255, 0}, 506 } 507 network2 := &net.IPNet{ 508 IP: []byte{127, 0, 0, 1}, 509 Mask: []byte{255, 255, 255, 0}, 510 } 511 network3 := &net.IPNet{ 512 IP: []byte{0x2a, 0x00, 0x14, 0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 513 Mask: []byte{255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0}, // /64 netmask 514 } 515 network4 := &net.IPNet{ 516 IP: []byte{0x2a, 0x00, 0x16, 0x32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 517 Mask: []byte{255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0}, // /64 netmask 518 } 519 expectedIPs := []net.IP{ 520 0: net.IPv4(192, 168, 0, 1), 521 1: net.IPv4(192, 168, 0, 2), 522 2: net.IPv4(127, 0, 0, 1), 523 3: net.IPv4(127, 0, 0, 2), 524 4: net.ParseIP("2a00:1450::1"), 525 5: net.ParseIP("2a00:1450::2"), 526 6: net.ParseIP("2a00:1450::3"), 527 7: net.ParseIP("2a00:1632::1"), 528 8: net.ParseIP("2a00:1632::2"), 529 } 530 531 ip11, err := RequestIP(network1, nil) 532 if err != nil { 533 t.Fatal(err) 534 } 535 ip12, err := RequestIP(network1, nil) 536 if err != nil { 537 t.Fatal(err) 538 } 539 ip21, err := RequestIP(network2, nil) 540 if err != nil { 541 t.Fatal(err) 542 } 543 ip22, err := RequestIP(network2, nil) 544 if err != nil { 545 t.Fatal(err) 546 } 547 ip31, err := RequestIP(network3, nil) 548 if err != nil { 549 t.Fatal(err) 550 } 551 ip32, err := RequestIP(network3, nil) 552 if err != nil { 553 t.Fatal(err) 554 } 555 ip33, err := RequestIP(network3, nil) 556 if err != nil { 557 t.Fatal(err) 558 } 559 ip41, err := RequestIP(network4, nil) 560 if err != nil { 561 t.Fatal(err) 562 } 563 ip42, err := RequestIP(network4, nil) 564 if err != nil { 565 t.Fatal(err) 566 } 567 assertIPEquals(t, expectedIPs[0], ip11) 568 assertIPEquals(t, expectedIPs[1], ip12) 569 assertIPEquals(t, expectedIPs[2], ip21) 570 assertIPEquals(t, expectedIPs[3], ip22) 571 assertIPEquals(t, expectedIPs[4], ip31) 572 assertIPEquals(t, expectedIPs[5], ip32) 573 assertIPEquals(t, expectedIPs[6], ip33) 574 assertIPEquals(t, expectedIPs[7], ip41) 575 assertIPEquals(t, expectedIPs[8], ip42) 576 } 577 578 func TestRegisterBadTwice(t *testing.T) { 579 defer reset() 580 network := &net.IPNet{ 581 IP: []byte{192, 168, 1, 1}, 582 Mask: []byte{255, 255, 255, 0}, 583 } 584 subnet := &net.IPNet{ 585 IP: []byte{192, 168, 1, 8}, 586 Mask: []byte{255, 255, 255, 248}, 587 } 588 589 if err := RegisterSubnet(network, subnet); err != nil { 590 t.Fatal(err) 591 } 592 subnet = &net.IPNet{ 593 IP: []byte{192, 168, 1, 16}, 594 Mask: []byte{255, 255, 255, 248}, 595 } 596 if err := RegisterSubnet(network, subnet); err != ErrNetworkAlreadyRegistered { 597 t.Fatalf("Expecteded ErrNetworkAlreadyRegistered error, got %v", err) 598 } 599 } 600 601 func TestRegisterBadRange(t *testing.T) { 602 defer reset() 603 network := &net.IPNet{ 604 IP: []byte{192, 168, 1, 1}, 605 Mask: []byte{255, 255, 255, 0}, 606 } 607 subnet := &net.IPNet{ 608 IP: []byte{192, 168, 1, 1}, 609 Mask: []byte{255, 255, 0, 0}, 610 } 611 if err := RegisterSubnet(network, subnet); err != ErrBadSubnet { 612 t.Fatalf("Expected ErrBadSubnet error, got %v", err) 613 } 614 } 615 616 func TestAllocateFromRange(t *testing.T) { 617 defer reset() 618 network := &net.IPNet{ 619 IP: []byte{192, 168, 0, 1}, 620 Mask: []byte{255, 255, 255, 0}, 621 } 622 // 192.168.1.9 - 192.168.1.14 623 subnet := &net.IPNet{ 624 IP: []byte{192, 168, 0, 8}, 625 Mask: []byte{255, 255, 255, 248}, 626 } 627 628 if err := RegisterSubnet(network, subnet); err != nil { 629 t.Fatal(err) 630 } 631 expectedIPs := []net.IP{ 632 0: net.IPv4(192, 168, 0, 9), 633 1: net.IPv4(192, 168, 0, 10), 634 2: net.IPv4(192, 168, 0, 11), 635 3: net.IPv4(192, 168, 0, 12), 636 4: net.IPv4(192, 168, 0, 13), 637 5: net.IPv4(192, 168, 0, 14), 638 } 639 for _, ip := range expectedIPs { 640 rip, err := RequestIP(network, nil) 641 if err != nil { 642 t.Fatal(err) 643 } 644 assertIPEquals(t, ip, rip) 645 } 646 647 if _, err := RequestIP(network, nil); err != ErrNoAvailableIPs { 648 t.Fatalf("Expected ErrNoAvailableIPs error, got %v", err) 649 } 650 for _, ip := range expectedIPs { 651 ReleaseIP(network, ip) 652 rip, err := RequestIP(network, nil) 653 if err != nil { 654 t.Fatal(err) 655 } 656 assertIPEquals(t, ip, rip) 657 } 658 } 659 660 func assertIPEquals(t *testing.T, ip1, ip2 net.IP) { 661 if !ip1.Equal(ip2) { 662 t.Fatalf("Expected IP %s, got %s", ip1, ip2) 663 } 664 } 665 666 func BenchmarkRequestIP(b *testing.B) { 667 network := &net.IPNet{ 668 IP: []byte{192, 168, 0, 1}, 669 Mask: []byte{255, 255, 255, 0}, 670 } 671 b.ResetTimer() 672 for i := 0; i < b.N; i++ { 673 for j := 0; j < 253; j++ { 674 _, err := RequestIP(network, nil) 675 if err != nil { 676 b.Fatal(err) 677 } 678 } 679 reset() 680 } 681 }