Skip to content

Commit 30bbfa2

Browse files
author
OpenShift Bot
authored
Merge pull request #11434 from xiangpengzhao/subnet_allocator_test
Merged by openshift-bot
2 parents b5156c2 + ff5fd54 commit 30bbfa2

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

pkg/util/netutils/subnet_allocator.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ func NewSubnetAllocator(network string, hostBits uint32, inUse []string) (*Subne
2525
}
2626

2727
netMaskSize, _ := netIP.Mask.Size()
28-
if hostBits > (32 - uint32(netMaskSize)) {
28+
if hostBits == 0 {
29+
return nil, fmt.Errorf("Host capacity cannot be zero.")
30+
} else if hostBits > (32 - uint32(netMaskSize)) {
2931
return nil, fmt.Errorf("Subnet capacity cannot be larger than number of networks available.")
3032
}
3133
subnetBits := 32 - uint32(netMaskSize) - hostBits

pkg/util/netutils/subnet_allocator_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,44 @@ func TestAllocateSubnetOverlapping(t *testing.T) {
144144
}
145145
}
146146

147+
// 10.1.HHHHHHHH.HHHHHHHH
148+
func TestAllocateSubnetNoSubnetBits(t *testing.T) {
149+
sna, err := NewSubnetAllocator("10.1.0.0/16", 16, nil)
150+
if err != nil {
151+
t.Fatal("Failed to initialize subnet allocator: ", err)
152+
}
153+
154+
sn, err := sna.GetNetwork()
155+
if err != nil {
156+
t.Fatal("Failed to get network: ", err)
157+
}
158+
if sn.String() != "10.1.0.0/16" {
159+
t.Fatalf("Did not get expected subnet (sn=%s)", sn.String())
160+
}
161+
162+
sn, err = sna.GetNetwork()
163+
if err == nil {
164+
t.Fatalf("Unexpectedly succeeded in getting network (sn=%s)", sn.String())
165+
}
166+
}
167+
168+
func TestAllocateSubnetInvalidHostBitsOrCIDR(t *testing.T) {
169+
_, err := NewSubnetAllocator("10.1.0.0/16", 18, nil)
170+
if err == nil {
171+
t.Fatal("Unexpectedly succeeded in initializing subnet allocator")
172+
}
173+
174+
_, err = NewSubnetAllocator("10.1.0.0/16", 0, nil)
175+
if err == nil {
176+
t.Fatal("Unexpectedly succeeded in initializing subnet allocator")
177+
}
178+
179+
_, err = NewSubnetAllocator("10.1.0.0/33", 16, nil)
180+
if err == nil {
181+
t.Fatal("Unexpectedly succeeded in initializing subnet allocator")
182+
}
183+
}
184+
147185
func TestAllocateSubnetInUse(t *testing.T) {
148186
inUse := []string{"10.1.0.0/24", "10.1.2.0/24", "10.2.2.2/24", "Invalid"}
149187
sna, err := NewSubnetAllocator("10.1.0.0/16", 8, inUse)

0 commit comments

Comments
 (0)