1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
|
package defaultipam
import (
"context"
"errors"
"flag"
"fmt"
"math/rand"
"net"
"net/netip"
"runtime"
"strconv"
"sync"
"testing"
"time"
"github.com/docker/docker/libnetwork/internal/addrset"
"github.com/docker/docker/libnetwork/internal/netiputil"
"github.com/docker/docker/libnetwork/ipamapi"
"github.com/docker/docker/libnetwork/ipamutils"
"github.com/docker/docker/libnetwork/types"
"golang.org/x/sync/errgroup"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
func TestKeyString(t *testing.T) {
k := &PoolID{AddressSpace: "default", SubnetKey: SubnetKey{Subnet: netip.MustParsePrefix("172.27.0.0/16")}}
expected := "default/172.27.0.0/16"
if expected != k.String() {
t.Fatalf("Unexpected key string: %s", k.String())
}
k2, err := PoolIDFromString(expected)
if err != nil {
t.Fatal(err)
}
if k2.AddressSpace != k.AddressSpace || k2.Subnet != k.Subnet {
t.Fatalf("SubnetKey.FromString() failed. Expected %v. Got %v", k, k2)
}
expected = fmt.Sprintf("%s/%s", expected, "172.27.3.0/24")
k.ChildSubnet = netip.MustParsePrefix("172.27.3.0/24")
if expected != k.String() {
t.Fatalf("Unexpected key string: %s", k.String())
}
k2, err = PoolIDFromString(expected)
if err != nil {
t.Fatal(err)
}
if k2.AddressSpace != k.AddressSpace || k2.Subnet != k.Subnet || k2.ChildSubnet != k.ChildSubnet {
t.Fatalf("SubnetKey.FromString() failed. Expected %v. Got %v", k, k2)
}
}
func TestAddSubnets(t *testing.T) {
a, err := NewAllocator(ipamutils.GetLocalScopeDefaultNetworks(), ipamutils.GetGlobalScopeDefaultNetworks())
if err != nil {
t.Fatal(err)
}
alloc1, err := a.RequestPool(ipamapi.PoolRequest{AddressSpace: localAddressSpace, Pool: "10.0.0.0/8"})
if err != nil {
t.Fatal("Unexpected failure in adding subnet")
}
alloc2, err := a.RequestPool(ipamapi.PoolRequest{AddressSpace: globalAddressSpace, Pool: "10.0.0.0/8"})
if err != nil {
t.Fatalf("Unexpected failure in adding overlapping subnets to different address spaces: %v", err)
}
if alloc1.PoolID == alloc2.PoolID {
t.Fatal("returned same pool id for same subnets in different namespaces")
}
_, err = a.RequestPool(ipamapi.PoolRequest{AddressSpace: globalAddressSpace, Pool: "10.0.0.0/8"})
if err == nil {
t.Fatalf("Expected failure requesting existing subnet")
}
_, err = a.RequestPool(ipamapi.PoolRequest{AddressSpace: globalAddressSpace, Pool: "10.128.0.0/9"})
if err == nil {
t.Fatal("Expected failure on adding overlapping base subnet")
}
_, err = a.RequestPool(ipamapi.PoolRequest{AddressSpace: globalAddressSpace, Pool: "10.0.0.0/8", SubPool: "10.128.0.0/9"})
if err != nil {
t.Fatalf("Unexpected failure on adding sub pool: %v", err)
}
_, err = a.RequestPool(ipamapi.PoolRequest{AddressSpace: globalAddressSpace, Pool: "10.0.0.0/8", SubPool: "10.128.0.0/9"})
if err == nil {
t.Fatalf("Expected failure on adding overlapping sub pool")
}
_, err = a.RequestPool(ipamapi.PoolRequest{AddressSpace: localAddressSpace, Pool: "10.20.2.0/24"})
if err == nil {
t.Fatal("Failed to detect overlapping subnets")
}
_, err = a.RequestPool(ipamapi.PoolRequest{AddressSpace: localAddressSpace, Pool: "10.128.0.0/9"})
if err == nil {
t.Fatal("Failed to detect overlapping subnets")
}
_, err = a.RequestPool(ipamapi.PoolRequest{AddressSpace: localAddressSpace, Pool: "1003:1:2:3:4:5:6::/112"})
if err != nil {
t.Fatalf("Failed to add v6 subnet: %s", err.Error())
}
_, err = a.RequestPool(ipamapi.PoolRequest{AddressSpace: localAddressSpace, Pool: "1003:1:2:3::/64"})
if err == nil {
t.Fatal("Failed to detect overlapping v6 subnet")
}
}
// TestDoublePoolRelease tests that releasing a pool which has already
// been released raises an error.
func TestDoublePoolRelease(t *testing.T) {
a, err := NewAllocator(ipamutils.GetLocalScopeDefaultNetworks(), ipamutils.GetGlobalScopeDefaultNetworks())
assert.NilError(t, err)
alloc1, err := a.RequestPool(ipamapi.PoolRequest{AddressSpace: localAddressSpace, Pool: "10.0.0.0/8"})
assert.NilError(t, err)
err = a.ReleasePool(alloc1.PoolID)
assert.NilError(t, err)
err = a.ReleasePool(alloc1.PoolID)
assert.Check(t, is.ErrorContains(err, ""))
}
func TestAddReleasePoolID(t *testing.T) {
a, err := NewAllocator(ipamutils.GetLocalScopeDefaultNetworks(), ipamutils.GetGlobalScopeDefaultNetworks())
assert.NilError(t, err)
_, err = a.getAddrSpace(localAddressSpace, false)
if err != nil {
t.Fatal(err)
}
alloc1, err := a.RequestPool(ipamapi.PoolRequest{AddressSpace: localAddressSpace, Pool: "10.0.0.0/8"})
if err != nil {
t.Fatalf("Unexpected failure in adding pool: %v", err)
}
k0, err := PoolIDFromString(alloc1.PoolID)
if err != nil {
t.Fatal(err)
}
aSpace, err := a.getAddrSpace(localAddressSpace, false)
if err != nil {
t.Fatal(err)
}
if got := aSpace.subnets[k0.Subnet].autoRelease; got != false {
t.Errorf("Unexpected autoRelease value for %s: %v", k0, got)
}
alloc2, err := a.RequestPool(ipamapi.PoolRequest{AddressSpace: localAddressSpace, Pool: "10.0.0.0/8", SubPool: "10.0.0.0/16"})
if err != nil {
t.Fatalf("Unexpected failure in adding sub pool: %v", err)
}
k1, err := PoolIDFromString(alloc2.PoolID)
if err != nil {
t.Fatal(err)
}
if alloc1.PoolID == alloc2.PoolID {
t.Fatalf("Incorrect poolIDs returned %s, %s", alloc1.PoolID, alloc2.PoolID)
}
aSpace, err = a.getAddrSpace(localAddressSpace, false)
if err != nil {
t.Fatal(err)
}
if got := aSpace.subnets[k1.Subnet].autoRelease; got != false {
t.Errorf("Unexpected autoRelease value for %s: %v", k1, got)
}
_, err = a.RequestPool(ipamapi.PoolRequest{AddressSpace: localAddressSpace, Pool: "10.0.0.0/8", SubPool: "10.0.0.0/16"})
if err == nil {
t.Fatalf("Expected failure in adding sub pool: %v", err)
}
aSpace, err = a.getAddrSpace(localAddressSpace, false)
if err != nil {
t.Fatal(err)
}
if got := aSpace.subnets[k0.Subnet].autoRelease; got != false {
t.Errorf("Unexpected autoRelease value for %s: %v", k0, got)
}
if err := a.ReleasePool(alloc2.PoolID); err != nil {
t.Fatal(err)
}
aSpace, err = a.getAddrSpace(localAddressSpace, false)
if err != nil {
t.Fatal(err)
}
if got := aSpace.subnets[k0.Subnet].autoRelease; got != false {
t.Errorf("Unexpected autoRelease value for %s: %v", k0, got)
}
if err := a.ReleasePool(alloc1.PoolID); err != nil {
t.Error(err)
}
if _, ok := aSpace.subnets[k0.Subnet]; ok {
t.Error("Pool should have been deleted when released")
}
alloc10, err := a.RequestPool(ipamapi.PoolRequest{AddressSpace: localAddressSpace, Pool: "10.0.0.0/8"})
if err != nil {
t.Errorf("Unexpected failure in adding pool: %v", err)
}
if alloc10.PoolID != alloc1.PoolID {
t.Errorf("main pool should still exist. Got poolID %q, want %q", alloc10.PoolID, alloc1.PoolID)
}
aSpace, err = a.getAddrSpace(localAddressSpace, false)
if err != nil {
t.Fatal(err)
}
if got := aSpace.subnets[k0.Subnet].autoRelease; got != false {
t.Errorf("Unexpected autoRelease value for %s: %v", k0, got)
}
if err := a.ReleasePool(alloc10.PoolID); err != nil {
t.Error(err)
}
aSpace, err = a.getAddrSpace(localAddressSpace, false)
if err != nil {
t.Fatal(err)
}
if bp, ok := aSpace.subnets[k0.Subnet]; ok {
t.Errorf("Base pool %s is still present: %v", k0, bp)
}
_, err = a.RequestPool(ipamapi.PoolRequest{AddressSpace: localAddressSpace, Pool: "10.0.0.0/8"})
if err != nil {
t.Errorf("Unexpected failure in adding pool: %v", err)
}
aSpace, err = a.getAddrSpace(localAddressSpace, false)
if err != nil {
t.Fatal(err)
}
if got := aSpace.subnets[k0.Subnet].autoRelease; got != false {
t.Errorf("Unexpected autoRelease value for %s: %v", k0, got)
}
}
func TestPredefinedPool(t *testing.T) {
a, err := NewAllocator(ipamutils.GetLocalScopeDefaultNetworks(), ipamutils.GetGlobalScopeDefaultNetworks())
assert.NilError(t, err)
alloc1, err := a.RequestPool(ipamapi.PoolRequest{AddressSpace: localAddressSpace})
if err != nil {
t.Fatal(err)
}
alloc2, err := a.RequestPool(ipamapi.PoolRequest{AddressSpace: localAddressSpace})
if err != nil {
t.Fatal(err)
}
if alloc1.Pool == alloc2.Pool {
t.Fatalf("Unexpected default network returned: %s = %s", alloc2.Pool, alloc1.Pool)
}
if err := a.ReleasePool(alloc1.PoolID); err != nil {
t.Fatal(err)
}
if err := a.ReleasePool(alloc2.PoolID); err != nil {
t.Fatal(err)
}
}
func TestRemoveSubnet(t *testing.T) {
a, err := NewAllocator(ipamutils.GetLocalScopeDefaultNetworks(), ipamutils.GetGlobalScopeDefaultNetworks())
assert.NilError(t, err)
reqs := []ipamapi.PoolRequest{
{AddressSpace: localAddressSpace, Pool: "192.168.0.0/16"},
{AddressSpace: localAddressSpace, Pool: "172.17.0.0/16"},
{AddressSpace: localAddressSpace, Pool: "10.0.0.0/8"},
{AddressSpace: localAddressSpace, Pool: "2001:db8:1:2:3:4:ffff::/112", V6: true},
{AddressSpace: globalAddressSpace, Pool: "172.17.0.0/16"},
{AddressSpace: globalAddressSpace, Pool: "10.0.0.0/8"},
{AddressSpace: globalAddressSpace, Pool: "2001:db8:1:2:3:4:5::/112", V6: true},
{AddressSpace: globalAddressSpace, Pool: "2001:db8:1:2:3:4:ffff::/112", V6: true},
}
allocs := make([]ipamapi.AllocatedPool, 0, len(reqs))
for _, req := range reqs {
alloc, err := a.RequestPool(req)
if err != nil {
t.Fatalf("Failed to apply input. Can't proceed: %s", err.Error())
}
allocs = append(allocs, alloc)
}
for idx, alloc := range allocs {
if err := a.ReleasePool(alloc.PoolID); err != nil {
t.Fatalf("Failed to release poolID %s (%d)", alloc.PoolID, idx)
}
}
}
func TestGetSameAddress(t *testing.T) {
a, err := NewAllocator(ipamutils.GetLocalScopeDefaultNetworks(), ipamutils.GetGlobalScopeDefaultNetworks())
assert.NilError(t, err)
alloc, err := a.RequestPool(ipamapi.PoolRequest{AddressSpace: localAddressSpace, Pool: "192.168.100.0/24"})
if err != nil {
t.Fatal(err)
}
ip := net.ParseIP("192.168.100.250")
_, _, err = a.RequestAddress(alloc.PoolID, ip, nil)
if err != nil {
t.Fatal(err)
}
_, _, err = a.RequestAddress(alloc.PoolID, ip, nil)
if err == nil {
t.Fatal(err)
}
}
// TestRequestFromSamePool verify the allocator implements the validation
// inconsistencies described in https://github.com/moby/moby/issues/46756.
func TestRequestFromSamePool(t *testing.T) {
a, err := NewAllocator(ipamutils.GetLocalScopeDefaultNetworks(), ipamutils.GetGlobalScopeDefaultNetworks())
assert.NilError(t, err)
_, err = a.RequestPool(ipamapi.PoolRequest{
AddressSpace: localAddressSpace,
Pool: "10.0.0.0/8",
SubPool: "10.10.0.0/16",
})
assert.NilError(t, err)
_, err = a.RequestPool(ipamapi.PoolRequest{
AddressSpace: localAddressSpace,
Pool: "10.0.0.0/8",
SubPool: "10.10.0.0/16",
})
assert.ErrorContains(t, err, "invalid pool request")
_, err = a.RequestPool(ipamapi.PoolRequest{
AddressSpace: localAddressSpace,
Pool: "10.0.0.0/8",
SubPool: "10.10.0.0/17",
})
assert.NilError(t, err)
_, err = a.RequestPool(ipamapi.PoolRequest{
AddressSpace: localAddressSpace,
Pool: "10.0.0.0/8",
SubPool: "10.11.0.0/16",
})
assert.NilError(t, err)
}
func TestGetAddressSubPoolEqualPool(t *testing.T) {
a, err := NewAllocator(ipamutils.GetLocalScopeDefaultNetworks(), ipamutils.GetGlobalScopeDefaultNetworks())
assert.NilError(t, err)
// Requesting a subpool of same size of the master pool should not cause any problem on ip allocation
alloc, err := a.RequestPool(ipamapi.PoolRequest{AddressSpace: localAddressSpace, Pool: "172.18.0.0/16", SubPool: "172.18.0.0/16"})
if err != nil {
t.Fatal(err)
}
_, _, err = a.RequestAddress(alloc.PoolID, nil, nil)
if err != nil {
t.Fatal(err)
}
}
func TestRequestReleaseAddressFromSubPool(t *testing.T) {
a, err := NewAllocator(ipamutils.GetLocalScopeDefaultNetworks(), ipamutils.GetGlobalScopeDefaultNetworks())
assert.NilError(t, err)
alloc, err := a.RequestPool(ipamapi.PoolRequest{AddressSpace: localAddressSpace, Pool: "172.28.0.0/16", SubPool: "172.28.30.0/24"})
if err != nil {
t.Fatal(err)
}
var ip *net.IPNet
expected := &net.IPNet{IP: net.IP{172, 28, 30, 255}, Mask: net.IPMask{255, 255, 0, 0}}
for err == nil {
var c *net.IPNet
if c, _, err = a.RequestAddress(alloc.PoolID, nil, nil); err == nil {
ip = c
}
}
if !errors.Is(err, ipamapi.ErrNoAvailableIPs) {
t.Fatal(err)
}
if !types.CompareIPNet(expected, ip) {
t.Fatalf("Unexpected last IP from subpool. Expected: %s. Got: %v.", expected, ip)
}
rp := &net.IPNet{IP: net.IP{172, 28, 30, 97}, Mask: net.IPMask{255, 255, 0, 0}}
if err = a.ReleaseAddress(alloc.PoolID, rp.IP); err != nil {
t.Fatal(err)
}
if ip, _, err = a.RequestAddress(alloc.PoolID, nil, nil); err != nil {
t.Fatal(err)
}
if !types.CompareIPNet(rp, ip) {
t.Fatalf("Unexpected IP from subpool. Expected: %s. Got: %v.", rp, ip)
}
_, err = a.RequestPool(ipamapi.PoolRequest{AddressSpace: localAddressSpace, Pool: "10.0.0.0/8", SubPool: "10.0.0.0/16"})
if err != nil {
t.Fatal(err)
}
alloc, err = a.RequestPool(ipamapi.PoolRequest{AddressSpace: localAddressSpace, Pool: "10.0.0.0/16", SubPool: "10.0.0.0/24"})
if err != nil {
t.Fatal(err)
}
expected = &net.IPNet{IP: net.IP{10, 0, 0, 255}, Mask: net.IPMask{255, 255, 0, 0}}
for err == nil {
var c *net.IPNet
if c, _, err = a.RequestAddress(alloc.PoolID, nil, nil); err == nil {
ip = c
}
}
if !errors.Is(err, ipamapi.ErrNoAvailableIPs) {
t.Fatal(err)
}
if !types.CompareIPNet(expected, ip) {
t.Fatalf("Unexpected last IP from subpool. Expected: %s. Got: %v.", expected, ip)
}
rp = &net.IPNet{IP: net.IP{10, 0, 0, 79}, Mask: net.IPMask{255, 255, 0, 0}}
if err = a.ReleaseAddress(alloc.PoolID, rp.IP); err != nil {
t.Fatal(err)
}
if ip, _, err = a.RequestAddress(alloc.PoolID, nil, nil); err != nil {
t.Fatal(err)
}
if !types.CompareIPNet(rp, ip) {
t.Fatalf("Unexpected IP from subpool. Expected: %s. Got: %v.", rp, ip)
}
// Request any addresses from subpool after explicit address request
unoExp, _ := types.ParseCIDR("10.2.2.0/16")
dueExp, _ := types.ParseCIDR("10.2.2.2/16")
treExp, _ := types.ParseCIDR("10.2.2.1/16")
if alloc, err = a.RequestPool(ipamapi.PoolRequest{AddressSpace: localAddressSpace, Pool: "10.2.0.0/16", SubPool: "10.2.2.0/24"}); err != nil {
t.Fatal(err)
}
tre, _, err := a.RequestAddress(alloc.PoolID, treExp.IP, nil)
if err != nil {
t.Fatal(err)
}
if !types.CompareIPNet(tre, treExp) {
t.Fatalf("Unexpected address: want %v, got %v", treExp, tre)
}
uno, _, err := a.RequestAddress(alloc.PoolID, nil, nil)
if err != nil {
t.Fatal(err)
}
if !types.CompareIPNet(uno, unoExp) {
t.Fatalf("Unexpected address: %v", uno)
}
due, _, err := a.RequestAddress(alloc.PoolID, nil, nil)
if err != nil {
t.Fatal(err)
}
if !types.CompareIPNet(due, dueExp) {
t.Fatalf("Unexpected address: %v", due)
}
if err = a.ReleaseAddress(alloc.PoolID, uno.IP); err != nil {
t.Fatal(err)
}
uno, _, err = a.RequestAddress(alloc.PoolID, nil, nil)
if err != nil {
t.Fatal(err)
}
if !types.CompareIPNet(uno, unoExp) {
t.Fatalf("Unexpected address: %v", uno)
}
if err = a.ReleaseAddress(alloc.PoolID, tre.IP); err != nil {
t.Fatal(err)
}
tre, _, err = a.RequestAddress(alloc.PoolID, nil, nil)
if err != nil {
t.Fatal(err)
}
if !types.CompareIPNet(tre, treExp) {
t.Fatalf("Unexpected address: %v", tre)
}
}
func TestSerializeRequestReleaseAddressFromSubPool(t *testing.T) {
opts := map[string]string{
ipamapi.AllocSerialPrefix: "true",
}
a, err := NewAllocator(ipamutils.GetLocalScopeDefaultNetworks(), ipamutils.GetGlobalScopeDefaultNetworks())
assert.NilError(t, err)
alloc, err := a.RequestPool(ipamapi.PoolRequest{AddressSpace: localAddressSpace, Pool: "172.28.0.0/16", SubPool: "172.28.30.0/24"})
if err != nil {
t.Fatal(err)
}
var ip *net.IPNet
expected := &net.IPNet{IP: net.IP{172, 28, 30, 255}, Mask: net.IPMask{255, 255, 0, 0}}
for err == nil {
var c *net.IPNet
if c, _, err = a.RequestAddress(alloc.PoolID, nil, opts); err == nil {
ip = c
}
}
if !errors.Is(err, ipamapi.ErrNoAvailableIPs) {
t.Fatal(err)
}
if !types.CompareIPNet(expected, ip) {
t.Fatalf("Unexpected last IP from subpool. Expected: %s. Got: %v.", expected, ip)
}
rp := &net.IPNet{IP: net.IP{172, 28, 30, 97}, Mask: net.IPMask{255, 255, 0, 0}}
if err = a.ReleaseAddress(alloc.PoolID, rp.IP); err != nil {
t.Fatal(err)
}
if ip, _, err = a.RequestAddress(alloc.PoolID, nil, opts); err != nil {
t.Fatal(err)
}
if !types.CompareIPNet(rp, ip) {
t.Fatalf("Unexpected IP from subpool. Expected: %s. Got: %v.", rp, ip)
}
_, err = a.RequestPool(ipamapi.PoolRequest{AddressSpace: localAddressSpace, Pool: "10.0.0.0/8", SubPool: "10.0.0.0/16"})
if err != nil {
t.Fatal(err)
}
alloc, err = a.RequestPool(ipamapi.PoolRequest{AddressSpace: localAddressSpace, Pool: "10.0.0.0/16", SubPool: "10.0.0.0/24"})
if err != nil {
t.Fatal(err)
}
expected = &net.IPNet{IP: net.IP{10, 0, 0, 255}, Mask: net.IPMask{255, 255, 0, 0}}
for err == nil {
var c *net.IPNet
if c, _, err = a.RequestAddress(alloc.PoolID, nil, opts); err == nil {
ip = c
}
}
if !errors.Is(err, ipamapi.ErrNoAvailableIPs) {
t.Fatal(err)
}
if !types.CompareIPNet(expected, ip) {
t.Fatalf("Unexpected last IP from subpool. Expected: %s. Got: %v.", expected, ip)
}
rp = &net.IPNet{IP: net.IP{10, 0, 0, 79}, Mask: net.IPMask{255, 255, 0, 0}}
if err = a.ReleaseAddress(alloc.PoolID, rp.IP); err != nil {
t.Fatal(err)
}
if ip, _, err = a.RequestAddress(alloc.PoolID, nil, opts); err != nil {
t.Fatal(err)
}
if !types.CompareIPNet(rp, ip) {
t.Fatalf("Unexpected IP from subpool. Expected: %s. Got: %v.", rp, ip)
}
// Request any addresses from subpool after explicit address request
unoExp, _ := types.ParseCIDR("10.2.2.0/16")
dueExp, _ := types.ParseCIDR("10.2.2.2/16")
treExp, _ := types.ParseCIDR("10.2.2.1/16")
quaExp, _ := types.ParseCIDR("10.2.2.3/16")
fivExp, _ := types.ParseCIDR("10.2.2.4/16")
if alloc, err = a.RequestPool(ipamapi.PoolRequest{AddressSpace: localAddressSpace, Pool: "10.2.0.0/16", SubPool: "10.2.2.0/24"}); err != nil {
t.Fatal(err)
}
tre, _, err := a.RequestAddress(alloc.PoolID, treExp.IP, opts)
if err != nil {
t.Fatal(err)
}
if !types.CompareIPNet(tre, treExp) {
t.Fatalf("Unexpected address: want %v, got %v", treExp, tre)
}
uno, _, err := a.RequestAddress(alloc.PoolID, nil, opts)
if err != nil {
t.Fatal(err)
}
if !types.CompareIPNet(uno, unoExp) {
t.Fatalf("Unexpected address: %v", uno)
}
due, _, err := a.RequestAddress(alloc.PoolID, nil, opts)
if err != nil {
t.Fatal(err)
}
if !types.CompareIPNet(due, dueExp) {
t.Fatalf("Unexpected address: %v", due)
}
if err = a.ReleaseAddress(alloc.PoolID, uno.IP); err != nil {
t.Fatal(err)
}
uno, _, err = a.RequestAddress(alloc.PoolID, nil, opts)
if err != nil {
t.Fatal(err)
}
if !types.CompareIPNet(uno, quaExp) {
t.Fatalf("Unexpected address: %v", uno)
}
if err = a.ReleaseAddress(alloc.PoolID, tre.IP); err != nil {
t.Fatal(err)
}
tre, _, err = a.RequestAddress(alloc.PoolID, nil, opts)
if err != nil {
t.Fatal(err)
}
if !types.CompareIPNet(tre, fivExp) {
t.Fatalf("Unexpected address: %v", tre)
}
}
func TestGetAddress(t *testing.T) {
input := []string{
/*"10.0.0.0/8", "10.0.0.0/9", "10.0.0.0/10",*/ "10.0.0.0/11", "10.0.0.0/12", "10.0.0.0/13", "10.0.0.0/14",
"10.0.0.0/15", "10.0.0.0/16", "10.0.0.0/17", "10.0.0.0/18", "10.0.0.0/19", "10.0.0.0/20", "10.0.0.0/21",
"10.0.0.0/22", "10.0.0.0/23", "10.0.0.0/24", "10.0.0.0/25", "10.0.0.0/26", "10.0.0.0/27", "10.0.0.0/28",
"10.0.0.0/29", "10.0.0.0/30", "10.0.0.0/31",
}
for _, subnet := range input {
assertGetAddress(t, subnet)
}
}
func TestRequestSyntaxCheck(t *testing.T) {
var (
pool = "192.168.0.0/16"
subPool = "192.168.0.0/24"
)
a, err := NewAllocator(ipamutils.GetLocalScopeDefaultNetworks(), ipamutils.GetGlobalScopeDefaultNetworks())
assert.NilError(t, err)
_, err = a.RequestPool(ipamapi.PoolRequest{Pool: pool})
if err == nil {
t.Fatal("Failed to detect wrong request: empty address space")
}
_, err = a.RequestPool(ipamapi.PoolRequest{Pool: pool, SubPool: subPool})
if err == nil {
t.Fatal("Failed to detect wrong request: empty address space")
}
_, err = a.RequestPool(ipamapi.PoolRequest{AddressSpace: localAddressSpace, SubPool: subPool})
if err == nil {
t.Fatal("Failed to detect wrong request: subPool specified and no pool")
}
alloc, err := a.RequestPool(ipamapi.PoolRequest{AddressSpace: localAddressSpace, Pool: pool, SubPool: subPool})
if err != nil {
t.Fatalf("Unexpected failure: %v", err)
}
_, _, err = a.RequestAddress("", nil, nil)
if err == nil {
t.Fatal("Failed to detect wrong request: no pool id specified")
}
ip := net.ParseIP("172.17.0.23")
_, _, err = a.RequestAddress(alloc.PoolID, ip, nil)
if err == nil {
t.Fatal("Failed to detect wrong request: requested IP from different subnet")
}
ip = net.ParseIP("192.168.0.50")
_, _, err = a.RequestAddress(alloc.PoolID, ip, nil)
if err != nil {
t.Fatalf("Unexpected failure: %v", err)
}
err = a.ReleaseAddress("", ip)
if err == nil {
t.Fatal("Failed to detect wrong request: no pool id specified")
}
err = a.ReleaseAddress(alloc.PoolID, nil)
if err == nil {
t.Fatal("Failed to detect wrong request: no pool id specified")
}
err = a.ReleaseAddress(alloc.PoolID, ip)
if err != nil {
t.Fatalf("Unexpected failure: %v: %s, %s", err, alloc.PoolID, ip)
}
}
func TestRequest(t *testing.T) {
// Request N addresses from different size subnets, verifying last request
// returns expected address. Internal subnet host size is Allocator's default, 16
input := []struct {
subnet string
numReq int
lastIP string
}{
{"192.168.59.0/24", 254, "192.168.59.254"},
{"192.168.240.0/20", 255, "192.168.240.255"},
{"192.168.0.0/16", 255, "192.168.0.255"},
{"192.168.0.0/16", 256, "192.168.1.0"},
{"10.16.0.0/16", 255, "10.16.0.255"},
{"10.128.0.0/12", 255, "10.128.0.255"},
{"10.0.0.0/8", 256, "10.0.1.0"},
{"192.168.128.0/18", 4*256 - 1, "192.168.131.255"},
/*
{"192.168.240.0/20", 16*256 - 2, "192.168.255.254"},
{"192.168.0.0/16", 256*256 - 2, "192.168.255.254"},
{"10.0.0.0/8", 2 * 256, "10.0.2.0"},
{"10.0.0.0/8", 5 * 256, "10.0.5.0"},
{"10.0.0.0/8", 100 * 256 * 254, "10.99.255.254"},
*/
}
for _, d := range input {
assertNRequests(t, d.subnet, d.numReq, d.lastIP)
}
}
// TestOverlappingRequests tests that overlapping subnets cannot be allocated.
// Requests for subnets which are supersets or subsets of existing allocations,
// or which overlap at the beginning or end, should not be permitted.
func TestOverlappingRequests(t *testing.T) {
input := []struct {
environment []string
subnet string
ok bool
}{
// IPv4
// Previously allocated network does not overlap with request
{[]string{"10.0.0.0/8"}, "11.0.0.0/8", true},
{[]string{"74.0.0.0/7"}, "9.111.99.72/30", true},
{[]string{"110.192.0.0/10"}, "16.0.0.0/10", true},
// Previously allocated network entirely contains request
{[]string{"10.0.0.0/8"}, "10.0.0.0/8", false}, // exact overlap
{[]string{"0.0.0.0/1"}, "16.182.0.0/15", false},
{[]string{"16.0.0.0/4"}, "17.11.66.0/23", false},
// Previously allocated network overlaps beginning of request
{[]string{"0.0.0.0/1"}, "0.0.0.0/0", false},
{[]string{"64.0.0.0/6"}, "64.0.0.0/3", false},
{[]string{"112.0.0.0/6"}, "112.0.0.0/4", false},
// Previously allocated network overlaps end of request
{[]string{"96.0.0.0/3"}, "0.0.0.0/1", false},
{[]string{"192.0.0.0/2"}, "128.0.0.0/1", false},
{[]string{"95.0.0.0/8"}, "92.0.0.0/6", false},
// Previously allocated network entirely contained within request
{[]string{"10.0.0.0/8"}, "10.0.0.0/6", false}, // non-canonical
{[]string{"10.0.0.0/8"}, "8.0.0.0/6", false}, // canonical
{[]string{"25.173.144.0/20"}, "0.0.0.0/0", false},
// IPv6
// Previously allocated network entirely contains request
{[]string{"::/0"}, "f656:3484:c878:a05:e540:a6ed:4d70:3740/123", false},
{[]string{"8000::/1"}, "8fe8:e7c4:5779::/49", false},
{[]string{"f000::/4"}, "ffc7:6000::/19", false},
// Previously allocated network overlaps beginning of request
{[]string{"::/2"}, "::/0", false},
{[]string{"::/3"}, "::/1", false},
{[]string{"::/6"}, "::/5", false},
// Previously allocated network overlaps end of request
{[]string{"c000::/2"}, "8000::/1", false},
{[]string{"7c00::/6"}, "::/1", false},
{[]string{"cf80::/9"}, "c000::/4", false},
// Previously allocated network entirely contained within request
{[]string{"ff77:93f8::/29"}, "::/0", false},
{[]string{"9287:2e20:5134:fab6:9061:a0c6:bfe3:9400/119"}, "8000::/1", false},
{[]string{"3ea1:bfa9:8691:d1c6:8c46:519b:db6d:e700/120"}, "3000::/4", false},
}
for _, tc := range input {
a, err := NewAllocator(ipamutils.GetLocalScopeDefaultNetworks(), ipamutils.GetGlobalScopeDefaultNetworks())
assert.NilError(t, err)
// Set up some existing allocations. This should always succeed.
for _, env := range tc.environment {
_, err = a.RequestPool(ipamapi.PoolRequest{AddressSpace: localAddressSpace, Pool: env})
assert.NilError(t, err)
}
// Make the test allocation.
_, err = a.RequestPool(ipamapi.PoolRequest{AddressSpace: localAddressSpace, Pool: tc.subnet})
if tc.ok {
assert.NilError(t, err)
} else {
assert.Check(t, is.ErrorContains(err, ""))
}
}
}
func TestUnusualSubnets(t *testing.T) {
subnet := "192.168.0.2/31"
outsideTheRangeAddresses := []struct {
address string
}{
{"192.168.0.1"},
{"192.168.0.4"},
{"192.168.0.100"},
}
expectedAddresses := []struct {
address string
}{
{"192.168.0.2"},
{"192.168.0.3"},
}
allocator, err := NewAllocator(ipamutils.GetLocalScopeDefaultNetworks(), ipamutils.GetGlobalScopeDefaultNetworks())
if err != nil {
t.Fatal(err)
}
//
// IPv4 /31 blocks. See RFC 3021.
//
alloc, err := allocator.RequestPool(ipamapi.PoolRequest{AddressSpace: localAddressSpace, Pool: subnet})
if err != nil {
t.Fatal(err)
}
// Outside-the-range
for _, outside := range outsideTheRangeAddresses {
_, _, errx := allocator.RequestAddress(alloc.PoolID, net.ParseIP(outside.address), nil)
if !errors.Is(errx, ipamapi.ErrIPOutOfRange) {
t.Fatalf("Address %s failed to throw expected error: %s", outside.address, errx.Error())
}
}
// Should get just these two IPs followed by exhaustion on the next request
for _, expected := range expectedAddresses {
got, _, errx := allocator.RequestAddress(alloc.PoolID, nil, nil)
if errx != nil {
t.Fatalf("Failed to obtain the address: %s", errx.Error())
}
expectedIP := net.ParseIP(expected.address)
gotIP := got.IP
if !gotIP.Equal(expectedIP) {
t.Fatalf("Failed to obtain sequentialaddress. Expected: %s, Got: %s", expectedIP, gotIP)
}
}
_, _, err = allocator.RequestAddress(alloc.PoolID, nil, nil)
if !errors.Is(err, ipamapi.ErrNoAvailableIPs) {
t.Fatal("Did not get expected error when pool is exhausted.")
}
}
func TestRelease(t *testing.T) {
subnet := "192.168.0.0/23"
a, err := NewAllocator(ipamutils.GetLocalScopeDefaultNetworks(), ipamutils.GetGlobalScopeDefaultNetworks())
assert.NilError(t, err)
alloc, err := a.RequestPool(ipamapi.PoolRequest{AddressSpace: localAddressSpace, Pool: subnet})
if err != nil {
t.Fatal(err)
}
// Allocate all addresses
for !errors.Is(err, ipamapi.ErrNoAvailableIPs) {
_, _, err = a.RequestAddress(alloc.PoolID, nil, nil)
}
toRelease := []struct {
address string
}{
{"192.168.0.1"},
{"192.168.0.2"},
{"192.168.0.3"},
{"192.168.0.4"},
{"192.168.0.5"},
{"192.168.0.6"},
{"192.168.0.7"},
{"192.168.0.8"},
{"192.168.0.9"},
{"192.168.0.10"},
{"192.168.0.30"},
{"192.168.0.31"},
{"192.168.1.32"},
{"192.168.0.254"},
{"192.168.1.1"},
{"192.168.1.2"},
{"192.168.1.3"},
{"192.168.1.253"},
{"192.168.1.254"},
}
// One by one, release the address and request again. We should get the same IP
for _, inp := range toRelease {
ip0 := net.ParseIP(inp.address)
a.ReleaseAddress(alloc.PoolID, ip0)
nw, _, err := a.RequestAddress(alloc.PoolID, nil, nil)
if err != nil {
t.Fatalf("Failed to obtain the address: %s", err.Error())
}
ip := nw.IP
if !ip0.Equal(ip) {
t.Fatalf("Failed to obtain the same address. Expected: %s, Got: %s", ip0, ip)
}
}
}
func assertGetAddress(t *testing.T, subnet string) {
var (
err error
printTime = false
)
sub := netip.MustParsePrefix(subnet)
bm := addrset.New(sub)
start := time.Now()
run := 0
for !errors.Is(err, ipamapi.ErrNoAvailableIPs) {
_, err = getAddress(sub, bm, netip.Addr{}, netip.Prefix{}, false)
run++
}
if printTime {
fmt.Printf("\nTaken %v, to allocate all addresses on %s. (Runs: %d)", time.Since(start), subnet, run)
}
/*
if bm.Head.Block != expectedMax || bm.Head.Count != numBlocks {
t.Fatalf("Failed to effectively reserve all addresses on %s. Expected (0x%x, %d) as first sequence. Found (0x%x,%d)",
subnet, expectedMax, numBlocks, bm.Head.Block, bm.Head.Count)
}
*/
}
func assertNRequests(t *testing.T, subnet string, numReq int, lastExpectedIP string) {
var (
nw *net.IPNet
printTime = false
)
lastIP := net.ParseIP(lastExpectedIP)
a, err := NewAllocator(ipamutils.GetLocalScopeDefaultNetworks(), ipamutils.GetGlobalScopeDefaultNetworks())
assert.NilError(t, err)
alloc, err := a.RequestPool(ipamapi.PoolRequest{AddressSpace: localAddressSpace, Pool: subnet})
if err != nil {
t.Fatal(err)
}
i := 0
start := time.Now()
for ; i < numReq; i++ {
nw, _, err = a.RequestAddress(alloc.PoolID, nil, nil)
if err != nil {
t.Fatal(err)
}
}
if printTime {
fmt.Printf("\nTaken %v, to allocate %d addresses on %s\n", time.Since(start), numReq, subnet)
}
if !lastIP.Equal(nw.IP) {
t.Fatalf("Wrong last IP. Expected %s. Got: %s (err: %v, ind: %d)", lastExpectedIP, nw.IP.String(), err, i)
}
}
func benchmarkRequest(b *testing.B, a *Allocator, subnet string) {
alloc, err := a.RequestPool(ipamapi.PoolRequest{AddressSpace: localAddressSpace, Pool: subnet})
for !errors.Is(err, ipamapi.ErrNoAvailableIPs) {
_, _, err = a.RequestAddress(alloc.PoolID, nil, nil)
}
}
func BenchmarkRequest(b *testing.B) {
subnets := []string{
"10.0.0.0/24",
"10.0.0.0/16",
"10.0.0.0/8",
}
for _, subnet := range subnets {
name := fmt.Sprintf("%vSubnet", subnet)
b.Run(name, func(b *testing.B) {
a, _ := NewAllocator(ipamutils.GetLocalScopeDefaultNetworks(), ipamutils.GetGlobalScopeDefaultNetworks())
benchmarkRequest(b, a, subnet)
})
}
}
func TestAllocateRandomDeallocate(t *testing.T) {
for _, store := range []bool{false, true} {
testAllocateRandomDeallocate(t, "172.25.0.0/16", "", 384, store)
testAllocateRandomDeallocate(t, "172.25.0.0/16", "172.25.252.0/22", 384, store)
}
}
func testAllocateRandomDeallocate(t *testing.T, pool, subPool string, num int, store bool) {
a, err := NewAllocator(ipamutils.GetLocalScopeDefaultNetworks(), ipamutils.GetGlobalScopeDefaultNetworks())
if err != nil {
t.Fatal(err)
}
alloc, err := a.RequestPool(ipamapi.PoolRequest{AddressSpace: localAddressSpace, Pool: pool, SubPool: subPool})
if err != nil {
t.Fatal(err)
}
// Allocate num ip addresses
indices := make(map[int]*net.IPNet, num)
allocated := make(map[string]bool, num)
for i := 0; i < num; i++ {
ip, _, err := a.RequestAddress(alloc.PoolID, nil, nil)
if err != nil {
t.Fatal(err)
}
ips := ip.String()
if _, ok := allocated[ips]; ok {
t.Fatalf("Address %s is already allocated", ips)
}
allocated[ips] = true
indices[i] = ip
}
if len(indices) != len(allocated) || len(indices) != num {
t.Fatalf("Unexpected number of allocated addresses: (%d,%d).", len(indices), len(allocated))
}
seed := time.Now().Unix()
rng := rand.New(rand.NewSource(seed))
// Deallocate half of the allocated addresses following a random pattern
pattern := rng.Perm(num)
for i := 0; i < num/2; i++ {
idx := pattern[i]
ip := indices[idx]
err := a.ReleaseAddress(alloc.PoolID, ip.IP)
if err != nil {
t.Fatalf("Unexpected failure on deallocation of %s: %v.\nSeed: %d.", ip, err, seed)
}
delete(indices, idx)
delete(allocated, ip.String())
}
// Request a quarter of addresses
for i := 0; i < num/2; i++ {
ip, _, err := a.RequestAddress(alloc.PoolID, nil, nil)
if err != nil {
t.Fatal(err)
}
ips := ip.String()
if _, ok := allocated[ips]; ok {
t.Fatalf("\nAddress %s is already allocated.\nSeed: %d.", ips, seed)
}
allocated[ips] = true
}
if len(allocated) != num {
t.Fatalf("Unexpected number of allocated addresses: %d.\nSeed: %d.", len(allocated), seed)
}
}
const (
numInstances = 5
first = 0
)
var (
allocator *Allocator
start = make(chan struct{})
done sync.WaitGroup
pools = make([]*net.IPNet, numInstances)
)
func runParallelTests(t *testing.T, instance int) {
var err error
t.Parallel()
pTest := flag.Lookup("test.parallel")
if pTest == nil {
t.Skip("Skipped because test.parallel flag not set;")
}
numParallel, err := strconv.Atoi(pTest.Value.String())
if err != nil {
t.Fatal(err)
}
if numParallel < numInstances {
t.Skip("Skipped because t.parallel was less than ", numInstances)
}
// The first instance creates the allocator, gives the start
// and finally checks the pools each instance was assigned
if instance == first {
allocator, err = NewAllocator(ipamutils.GetLocalScopeDefaultNetworks(), ipamutils.GetGlobalScopeDefaultNetworks())
if err != nil {
t.Fatal(err)
}
done.Add(numInstances - 1)
close(start)
}
if instance != first {
<-start
defer done.Done()
}
alloc, err := allocator.RequestPool(ipamapi.PoolRequest{AddressSpace: localAddressSpace})
pools[instance] = netiputil.ToIPNet(alloc.Pool)
if err != nil {
t.Fatal(err)
}
if instance == first {
done.Wait()
// Now check each instance got a different pool
for i := 0; i < numInstances; i++ {
for j := i + 1; j < numInstances; j++ {
if types.CompareIPNet(pools[i], pools[j]) {
t.Errorf("Instance %d and %d were given the same predefined pool: %v", i, j, pools)
}
}
}
}
}
func TestRequestReleaseAddressDuplicate(t *testing.T) {
a, err := NewAllocator(ipamutils.GetLocalScopeDefaultNetworks(), ipamutils.GetGlobalScopeDefaultNetworks())
if err != nil {
t.Fatal(err)
}
type IP struct {
ip *net.IPNet
ref int
}
ips := []IP{}
allocatedIPs := []*net.IPNet{}
opts := map[string]string{
ipamapi.AllocSerialPrefix: "true",
}
var l sync.Mutex
alloc, err := a.RequestPool(ipamapi.PoolRequest{AddressSpace: localAddressSpace, Pool: "198.168.0.0/23"})
if err != nil {
t.Fatal(err)
}
seed := time.Now().Unix()
t.Logf("Random seed: %v", seed)
rng := rand.New(rand.NewSource(seed))
group, ctx := errgroup.WithContext(context.Background())
outer:
for n := 0; n < 10000; n++ {
var c *net.IPNet
for {
select {
case <-ctx.Done():
// One of group's goroutines returned an error.
break outer
default:
}
if c, _, err = a.RequestAddress(alloc.PoolID, nil, opts); err == nil {
break
}
// No addresses available. Spin until one is.
runtime.Gosched()
}
l.Lock()
ips = append(ips, IP{c, 1})
l.Unlock()
allocatedIPs = append(allocatedIPs, c)
if len(allocatedIPs) > 500 {
i := rng.Intn(len(allocatedIPs) - 1)
ip := allocatedIPs[i]
allocatedIPs = append(allocatedIPs[:i], allocatedIPs[i+1:]...)
group.Go(func() error {
// The lifetime of an allocated address begins when RequestAddress returns, and
// ends when ReleaseAddress is called. But we can't atomically call one of those
// methods and append to the log (ips slice) without also synchronizing the
// calls with each other. Synchronizing the calls would defeat the whole point
// of this test, which is to race ReleaseAddress against RequestAddress. We have
// no choice but to leave a small window of uncertainty open. Appending to the
// log after ReleaseAddress returns would allow the next RequestAddress call to
// race the log-release operation, which could result in the reallocate being
// logged before the release, despite the release happening before the
// reallocate: a false positive. Our only other option is to append the release
// to the log before calling ReleaseAddress, leaving a small race window for
// false negatives. False positives mean a flaky test, so let's err on the side
// of false negatives. Eventually we'll get lucky with a true-positive test
// failure or with Go's race detector if a concurrency bug exists.
l.Lock()
ips = append(ips, IP{ip, -1})
l.Unlock()
return a.ReleaseAddress(alloc.PoolID, ip.IP)
})
}
}
if err := group.Wait(); err != nil {
t.Fatal(err)
}
refMap := make(map[string]int)
for _, ip := range ips {
refMap[ip.ip.String()] = refMap[ip.ip.String()] + ip.ref
if refMap[ip.ip.String()] < 0 {
t.Fatalf("IP %s was previously released", ip.ip.String())
}
if refMap[ip.ip.String()] > 1 {
t.Fatalf("IP %s was previously allocated", ip.ip.String())
}
}
}
func TestParallelPredefinedRequest1(t *testing.T) {
runParallelTests(t, 0)
}
func TestParallelPredefinedRequest2(t *testing.T) {
runParallelTests(t, 1)
}
func TestParallelPredefinedRequest3(t *testing.T) {
runParallelTests(t, 2)
}
func TestParallelPredefinedRequest4(t *testing.T) {
runParallelTests(t, 3)
}
func TestParallelPredefinedRequest5(t *testing.T) {
runParallelTests(t, 4)
}
func BenchmarkPoolIDToString(b *testing.B) {
const poolIDString = "default/172.27.0.0/16/172.27.3.0/24"
k, err := PoolIDFromString(poolIDString)
if err != nil {
b.Fatal(err)
}
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = k.String()
}
}
func BenchmarkPoolIDFromString(b *testing.B) {
const poolIDString = "default/172.27.0.0/16/172.27.3.0/24"
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, err := PoolIDFromString(poolIDString)
if err != nil {
b.Fatal(err)
}
}
}
|