ipam/host-local/allocator tests: cover requested IP

Further:
* improve error handling
This commit is contained in:
Stefan Junker 2016-08-01 18:11:22 -07:00
parent f5ead7969a
commit 054fa9e42d

View File

@ -31,15 +31,26 @@ type AllocatorTestCase struct {
func (t AllocatorTestCase) run() (*types.IPConfig, error) { func (t AllocatorTestCase) run() (*types.IPConfig, error) {
subnet, err := types.ParseCIDR(t.subnet) subnet, err := types.ParseCIDR(t.subnet)
if err != nil {
return nil, err
}
conf := IPAMConfig{ conf := IPAMConfig{
Name: "test", Name: "test",
Type: "host-local", Type: "host-local",
Subnet: types.IPNet{IP: subnet.IP, Mask: subnet.Mask}, Subnet: types.IPNet{IP: subnet.IP, Mask: subnet.Mask},
} }
store := fakestore.NewFakeStore(t.ipmap, net.ParseIP(t.lastIP)) store := fakestore.NewFakeStore(t.ipmap, net.ParseIP(t.lastIP))
alloc, _ := NewIPAllocator(&conf, store) alloc, err := NewIPAllocator(&conf, store)
if err != nil {
return nil, err
}
res, err := alloc.Get("ID") res, err := alloc.Get("ID")
return res, err if err != nil {
return nil, err
}
return res, nil
} }
var _ = Describe("host-local ip allocator", func() { var _ = Describe("host-local ip allocator", func() {
@ -103,6 +114,26 @@ var _ = Describe("host-local ip allocator", func() {
Expect(res.IP.IP.String()).To(Equal(tc.expectResult)) Expect(res.IP.IP.String()).To(Equal(tc.expectResult))
} }
}) })
Context("when requesting a specific IP", func() {
It("must allocate the requested IP", func() {
subnet, err := types.ParseCIDR("10.0.0.0/29")
Expect(err).ToNot(HaveOccurred())
requestedIP := net.ParseIP("10.0.0.2")
ipmap := map[string]string{}
conf := IPAMConfig{
Name: "test",
Type: "host-local",
Subnet: types.IPNet{IP: subnet.IP, Mask: subnet.Mask},
Args: &IPAMArgs{IP: requestedIP},
}
store := fakestore.NewFakeStore(ipmap, nil)
alloc, _ := NewIPAllocator(&conf, store)
res, err := alloc.Get("ID")
Expect(err).ToNot(HaveOccurred())
Expect(res.IP.IP.String()).To(Equal(requestedIP.String()))
})
})
}) })
Context("when out of ips", func() { Context("when out of ips", func() {