ipam/host-local/allocator tests: cover requested IP
authorStefan Junker <mail@stefanjunker.de>
Tue, 2 Aug 2016 01:11:22 +0000 (18:11 -0700)
committerStefan Junker <mail@stefanjunker.de>
Tue, 2 Aug 2016 01:13:15 +0000 (18:13 -0700)
Further:
* improve error handling

plugins/ipam/host-local/allocator_test.go

index b0402af..200618a 100644 (file)
@@ -31,15 +31,26 @@ type AllocatorTestCase struct {
 
 func (t AllocatorTestCase) run() (*types.IPConfig, error) {
        subnet, err := types.ParseCIDR(t.subnet)
+       if err != nil {
+               return nil, err
+       }
+
        conf := IPAMConfig{
                Name:   "test",
                Type:   "host-local",
                Subnet: types.IPNet{IP: subnet.IP, Mask: subnet.Mask},
        }
        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")
-       return res, err
+       if err != nil {
+               return nil, err
+       }
+
+       return res, nil
 }
 
 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))
                        }
                })
+
+               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() {