Skip to content

Commit 2d18087

Browse files
committed
adding test cases for the new func
1 parent e930db5 commit 2d18087

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

cloud/nodeipam/ipam/cloud_allocator_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,54 @@ type testCase struct {
5050
instance *linodego.Instance
5151
}
5252

53+
func TestComputeStableIPv6PodCIDR(t *testing.T) {
54+
for _, tc := range []struct {
55+
name string
56+
baseCIDR string
57+
desiredMask int
58+
wantCIDR string
59+
wantOK bool
60+
}{
61+
{name: "nil base", baseCIDR: "", desiredMask: 112, wantOK: false},
62+
{name: "non-/112 desired", baseCIDR: "2300:5800:2:1::/64", desiredMask: 120, wantOK: false},
63+
{name: "ipv4 base", baseCIDR: "10.0.0.0/24", desiredMask: 112, wantOK: false},
64+
{name: "non-/64 ipv6 base (/56)", baseCIDR: "2300:5800:2::/56", desiredMask: 112, wantOK: false},
65+
{name: "success /64 -> mnemonic /112", baseCIDR: "2300:5800:2:1::/64", desiredMask: 112, wantCIDR: "2300:5800:2:1:0:c::/112", wantOK: true},
66+
} {
67+
t.Run(tc.name, func(t *testing.T) {
68+
var baseNet *net.IPNet
69+
if tc.baseCIDR != "" {
70+
_, parsed, err := net.ParseCIDR(tc.baseCIDR)
71+
if err != nil {
72+
t.Fatalf("parse base cidr: %v", err)
73+
}
74+
baseNet = parsed
75+
}
76+
got, ok := getIPv6PodCIDR(baseNet, tc.desiredMask)
77+
if ok != tc.wantOK {
78+
t.Fatalf("ok mismatch: got %v want %v (gotCIDR=%v)", ok, tc.wantOK, func() string {
79+
if got != nil {
80+
return got.String()
81+
}
82+
return ""
83+
}())
84+
}
85+
if !tc.wantOK {
86+
if got != nil {
87+
t.Fatalf("expected nil cidr on failure, got %v", got.String())
88+
}
89+
return
90+
}
91+
if got == nil {
92+
t.Fatalf("expected non-nil cidr")
93+
}
94+
if got.String() != tc.wantCIDR {
95+
t.Fatalf("cidr mismatch: got %s want %s", got.String(), tc.wantCIDR)
96+
}
97+
})
98+
}
99+
}
100+
53101
func TestGetIPv6RangeFromLinodeInterface(t *testing.T) {
54102
for _, tc := range []struct {
55103
iface linodego.LinodeInterface

0 commit comments

Comments
 (0)