From eb1ff18c4c87f2d57ee86c7a40712bd0a8d578af Mon Sep 17 00:00:00 2001 From: Bruce Ma Date: Sat, 1 Jun 2019 18:06:19 +0800 Subject: [PATCH] host-local: add some testcases for allocation idempotency Signed-off-by: Bruce Ma --- .../backend/allocator/allocator_test.go | 4 +- plugins/ipam/host-local/host_local_test.go | 100 ++++++++++++++++++ 2 files changed, 102 insertions(+), 2 deletions(-) diff --git a/plugins/ipam/host-local/backend/allocator/allocator_test.go b/plugins/ipam/host-local/backend/allocator/allocator_test.go index 5888c14b..a7027c15 100644 --- a/plugins/ipam/host-local/backend/allocator/allocator_test.go +++ b/plugins/ipam/host-local/backend/allocator/allocator_test.go @@ -221,14 +221,14 @@ var _ = Describe("host-local ip allocator", func() { It("should not allocate the broadcast address", func() { alloc := mkalloc() for i := 2; i < 7; i++ { - res, err := alloc.Get("ID", "eth0", nil) + res, err := alloc.Get(fmt.Sprintf("ID%d", i), "eth0", nil) Expect(err).ToNot(HaveOccurred()) s := fmt.Sprintf("192.168.1.%d/29", i) Expect(s).To(Equal(res.Address.String())) fmt.Fprintln(GinkgoWriter, "got ip", res.Address.String()) } - x, err := alloc.Get("ID", "eth0", nil) + x, err := alloc.Get("ID8", "eth0", nil) fmt.Fprintln(GinkgoWriter, "got ip", x) Expect(err).To(HaveOccurred()) }) diff --git a/plugins/ipam/host-local/host_local_test.go b/plugins/ipam/host-local/host_local_test.go index 46ab0b56..d3b487ba 100644 --- a/plugins/ipam/host-local/host_local_test.go +++ b/plugins/ipam/host-local/host_local_test.go @@ -250,6 +250,106 @@ var _ = Describe("host-local Operations", func() { Expect(err).To(HaveOccurred()) }) + It("repeat allocating addresses on specific interface for same container ID with ADD", func() { + const ifname string = "eth0" + const nspath string = "/some/where" + + tmpDir, err := getTmpDir() + Expect(err).NotTo(HaveOccurred()) + defer os.RemoveAll(tmpDir) + + conf := fmt.Sprintf(`{ + "cniVersion": "0.3.1", + "name": "mynet0", + "type": "ipvlan", + "master": "foo0", + "ipam": { + "type": "host-local", + "dataDir": "%s", + "ranges": [ + [{ "subnet": "10.1.2.0/24" }] + ] + } + }`, tmpDir) + + args := &skel.CmdArgs{ + ContainerID: "dummy", + Netns: nspath, + IfName: ifname, + StdinData: []byte(conf), + } + + args1 := &skel.CmdArgs{ + ContainerID: "dummy1", + Netns: nspath, + IfName: ifname, + StdinData: []byte(conf), + } + + // Allocate the IP + r0, raw, err := testutils.CmdAddWithArgs(args, func() error { + return cmdAdd(args) + }) + Expect(err).NotTo(HaveOccurred()) + Expect(strings.Index(string(raw), "\"version\":")).Should(BeNumerically(">", 0)) + + result0, err := current.GetResult(r0) + Expect(err).NotTo(HaveOccurred()) + Expect(len(result0.IPs)).Should(Equal(1)) + Expect(result0.IPs[0].Address.String()).Should(Equal("10.1.2.2/24")) + + // Allocate the IP with the same container ID + r1, raw, err := testutils.CmdAddWithArgs(args, func() error { + return cmdAdd(args) + }) + Expect(err).NotTo(HaveOccurred()) + Expect(strings.Index(string(raw), "\"version\":")).Should(BeNumerically(">", 0)) + + result1, err := current.GetResult(r1) + Expect(err).NotTo(HaveOccurred()) + Expect(len(result1.IPs)).Should(Equal(1)) + Expect(result1.IPs[0].Address.String()).Should(Equal("10.1.2.2/24")) + + // Allocate the IP with the another container ID + r2, raw, err := testutils.CmdAddWithArgs(args1, func() error { + return cmdAdd(args1) + }) + Expect(err).NotTo(HaveOccurred()) + Expect(strings.Index(string(raw), "\"version\":")).Should(BeNumerically(">", 0)) + + result2, err := current.GetResult(r2) + Expect(err).NotTo(HaveOccurred()) + Expect(len(result2.IPs)).Should(Equal(1)) + Expect(result2.IPs[0].Address.String()).Should(Equal("10.1.2.3/24")) + + // Allocate the IP with the same container ID again + r3, raw, err := testutils.CmdAddWithArgs(args, func() error { + return cmdAdd(args) + }) + Expect(err).NotTo(HaveOccurred()) + Expect(strings.Index(string(raw), "\"version\":")).Should(BeNumerically(">", 0)) + + result3, err := current.GetResult(r3) + Expect(err).NotTo(HaveOccurred()) + Expect(len(result3.IPs)).Should(Equal(1)) + Expect(result3.IPs[0].Address.String()).Should(Equal("10.1.2.2/24")) + + ipFilePath := filepath.Join(tmpDir, "mynet0", "10.1.2.2") + + // Release the IPs + err = testutils.CmdDelWithArgs(args, func() error { + return cmdDel(args) + }) + Expect(err).NotTo(HaveOccurred()) + _, err = os.Stat(ipFilePath) + Expect(err).To(HaveOccurred()) + + err = testutils.CmdDelWithArgs(args1, func() error { + return cmdDel(args1) + }) + Expect(err).NotTo(HaveOccurred()) + }) + It("Verify DEL works on backwards compatible allocate", func() { const nspath string = "/some/where" const ifname string = "eth0"