diff --git a/plugins/ipam/host-local/host_local_test.go b/plugins/ipam/host-local/host_local_test.go index 123e05ef..5cba3d40 100644 --- a/plugins/ipam/host-local/host_local_test.go +++ b/plugins/ipam/host-local/host_local_test.go @@ -24,8 +24,7 @@ import ( "github.com/containernetworking/cni/pkg/skel" "github.com/containernetworking/cni/pkg/types" - "github.com/containernetworking/cni/pkg/types/020" - current "github.com/containernetworking/cni/pkg/types/100" + "github.com/containernetworking/cni/pkg/types/100" "github.com/containernetworking/plugins/pkg/testutils" "github.com/containernetworking/plugins/plugins/ipam/host-local/backend/disk" @@ -36,751 +35,651 @@ import ( const LineBreak = "\r\n" var _ = Describe("host-local Operations", func() { - It("allocates and releases addresses with ADD/DEL", func() { - const ifname string = "eth0" - const nspath string = "/some/where" + var tmpDir string + const ( + ifname string = "eth0" + nspath string = "/some/where" + ) - tmpDir, err := getTmpDir() + BeforeEach(func() { + var err error + tmpDir, err = ioutil.TempDir("", "host-local_test") Expect(err).NotTo(HaveOccurred()) - defer os.RemoveAll(tmpDir) - - err = ioutil.WriteFile(filepath.Join(tmpDir, "resolv.conf"), []byte("nameserver 192.0.2.3"), 0644) - Expect(err).NotTo(HaveOccurred()) - - conf := fmt.Sprintf(`{ - "cniVersion": "0.3.1", - "name": "mynet", - "type": "ipvlan", - "master": "foo0", - "ipam": { - "type": "host-local", - "dataDir": "%s", - "resolvConf": "%s/resolv.conf", - "ranges": [ - [{ "subnet": "10.1.2.0/24" }, {"subnet": "10.2.2.0/24"}], - [{ "subnet": "2001:db8:1::0/64" }] - ], - "routes": [ - {"dst": "0.0.0.0/0"}, - {"dst": "::/0"}, - {"dst": "192.168.0.0/16", "gw": "1.1.1.1"}, - {"dst": "2001:db8:2::0/64", "gw": "2001:db8:3::1"} - ] - } - }`, tmpDir, tmpDir) - - args := &skel.CmdArgs{ - ContainerID: "dummy", - Netns: nspath, - IfName: ifname, - StdinData: []byte(conf), - } - - // Allocate the IP - r, raw, err := testutils.CmdAddWithArgs(args, func() error { - return cmdAdd(args) - }) - Expect(err).NotTo(HaveOccurred()) - Expect(strings.Index(string(raw), "\"version\":")).Should(BeNumerically(">", 0)) - - result, err := current.GetResult(r) - Expect(err).NotTo(HaveOccurred()) - - // Gomega is cranky about slices with different caps - Expect(*result.IPs[0]).To(Equal( - current.IPConfig{ - Address: mustCIDR("10.1.2.2/24"), - Gateway: net.ParseIP("10.1.2.1"), - })) - - Expect(*result.IPs[1]).To(Equal( - current.IPConfig{ - Address: mustCIDR("2001:db8:1::2/64"), - Gateway: net.ParseIP("2001:db8:1::1"), - }, - )) - Expect(len(result.IPs)).To(Equal(2)) - - Expect(result.Routes).To(Equal([]*types.Route{ - {Dst: mustCIDR("0.0.0.0/0"), GW: nil}, - {Dst: mustCIDR("::/0"), GW: nil}, - {Dst: mustCIDR("192.168.0.0/16"), GW: net.ParseIP("1.1.1.1")}, - {Dst: mustCIDR("2001:db8:2::0/64"), GW: net.ParseIP("2001:db8:3::1")}, - })) - - ipFilePath1 := filepath.Join(tmpDir, "mynet", "10.1.2.2") - contents, err := ioutil.ReadFile(ipFilePath1) - Expect(err).NotTo(HaveOccurred()) - Expect(string(contents)).To(Equal(args.ContainerID + LineBreak + ifname)) - - ipFilePath2 := filepath.Join(tmpDir, disk.GetEscapedPath("mynet", "2001:db8:1::2")) - contents, err = ioutil.ReadFile(ipFilePath2) - Expect(err).NotTo(HaveOccurred()) - Expect(string(contents)).To(Equal(args.ContainerID + LineBreak + ifname)) - - lastFilePath1 := filepath.Join(tmpDir, "mynet", "last_reserved_ip.0") - contents, err = ioutil.ReadFile(lastFilePath1) - Expect(err).NotTo(HaveOccurred()) - Expect(string(contents)).To(Equal("10.1.2.2")) - - lastFilePath2 := filepath.Join(tmpDir, "mynet", "last_reserved_ip.1") - contents, err = ioutil.ReadFile(lastFilePath2) - Expect(err).NotTo(HaveOccurred()) - Expect(string(contents)).To(Equal("2001:db8:1::2")) - // Release the IP - err = testutils.CmdDelWithArgs(args, func() error { - return cmdDel(args) - }) - Expect(err).NotTo(HaveOccurred()) - - _, err = os.Stat(ipFilePath1) - Expect(err).To(HaveOccurred()) - _, err = os.Stat(ipFilePath2) - Expect(err).To(HaveOccurred()) - }) - - It("allocates and releases addresses on specific interface with ADD/DEL", func() { - const ifname0 string = "eth0" - const ifname1 string = "eth1" - const nspath string = "/some/where" - - tmpDir, err := getTmpDir() - Expect(err).NotTo(HaveOccurred()) - defer os.RemoveAll(tmpDir) - - err = ioutil.WriteFile(filepath.Join(tmpDir, "resolv.conf"), []byte("nameserver 192.0.2.3"), 0644) - Expect(err).NotTo(HaveOccurred()) - - conf0 := fmt.Sprintf(`{ - "cniVersion": "0.3.1", - "name": "mynet0", - "type": "ipvlan", - "master": "foo0", - "ipam": { - "type": "host-local", - "dataDir": "%s", - "resolvConf": "%s/resolv.conf", - "ranges": [ - [{ "subnet": "10.1.2.0/24" }] - ] - } - }`, tmpDir, tmpDir) - - conf1 := fmt.Sprintf(`{ - "cniVersion": "0.3.1", - "name": "mynet1", - "type": "ipvlan", - "master": "foo1", - "ipam": { - "type": "host-local", - "dataDir": "%s", - "resolvConf": "%s/resolv.conf", - "ranges": [ - [{ "subnet": "10.2.2.0/24" }] - ] - } - }`, tmpDir, tmpDir) - - args0 := &skel.CmdArgs{ - ContainerID: "dummy", - Netns: nspath, - IfName: ifname0, - StdinData: []byte(conf0), - } - - // Allocate the IP - r0, raw, err := testutils.CmdAddWithArgs(args0, func() error { - return cmdAdd(args0) - }) - Expect(err).NotTo(HaveOccurred()) - Expect(strings.Index(string(raw), "\"version\":")).Should(BeNumerically(">", 0)) - - _, err = current.GetResult(r0) - Expect(err).NotTo(HaveOccurred()) - - args1 := &skel.CmdArgs{ - ContainerID: "dummy", - Netns: nspath, - IfName: ifname1, - StdinData: []byte(conf1), - } - - // Allocate the IP - r1, raw, err := testutils.CmdAddWithArgs(args1, func() error { - return cmdAdd(args1) - }) - Expect(err).NotTo(HaveOccurred()) - Expect(strings.Index(string(raw), "\"version\":")).Should(BeNumerically(">", 0)) - - _, err = current.GetResult(r1) - Expect(err).NotTo(HaveOccurred()) - - ipFilePath0 := filepath.Join(tmpDir, "mynet0", "10.1.2.2") - contents, err := ioutil.ReadFile(ipFilePath0) - Expect(err).NotTo(HaveOccurred()) - Expect(string(contents)).To(Equal(args0.ContainerID + LineBreak + ifname0)) - - ipFilePath1 := filepath.Join(tmpDir, "mynet1", "10.2.2.2") - contents, err = ioutil.ReadFile(ipFilePath1) - Expect(err).NotTo(HaveOccurred()) - Expect(string(contents)).To(Equal(args1.ContainerID + LineBreak + ifname1)) - - // Release the IP on ifname0 - err = testutils.CmdDelWithArgs(args0, func() error { - return cmdDel(args0) - }) - Expect(err).NotTo(HaveOccurred()) - _, err = os.Stat(ipFilePath0) - Expect(err).To(HaveOccurred()) - - // reread ipFilePath1, ensure that ifname1 didn't get deleted - contents, err = ioutil.ReadFile(ipFilePath1) - Expect(err).NotTo(HaveOccurred()) - Expect(string(contents)).To(Equal(args1.ContainerID + LineBreak + ifname1)) - - // Release the IP on ifname1 - err = testutils.CmdDelWithArgs(args1, func() error { - return cmdDel(args1) - }) - Expect(err).NotTo(HaveOccurred()) - - _, err = os.Stat(ipFilePath1) - 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 - _, _, err = testutils.CmdAddWithArgs(args, func() error { - return cmdAdd(args) - }) - Expect(err).To(HaveOccurred()) - - // Allocate the IP with the another container ID - r1, raw, err := testutils.CmdAddWithArgs(args1, func() error { - return cmdAdd(args1) - }) - 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.3/24")) - - // Allocate the IP with the same container ID again - _, _, err = testutils.CmdAddWithArgs(args, func() error { - return cmdAdd(args) - }) - Expect(err).To(HaveOccurred()) - - 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" - - tmpDir, err := getTmpDir() - Expect(err).NotTo(HaveOccurred()) - defer os.RemoveAll(tmpDir) - - err = ioutil.WriteFile(filepath.Join(tmpDir, "resolv.conf"), []byte("nameserver 192.0.2.3"), 0644) - Expect(err).NotTo(HaveOccurred()) - - conf := fmt.Sprintf(`{ - "cniVersion": "0.3.1", - "name": "mynet", - "type": "ipvlan", - "master": "foo", - "ipam": { - "type": "host-local", - "dataDir": "%s", - "resolvConf": "%s/resolv.conf", - "ranges": [ - [{ "subnet": "10.1.2.0/24" }] - ] - } - }`, tmpDir, tmpDir) - - args := &skel.CmdArgs{ - ContainerID: "dummy", - Netns: nspath, - IfName: ifname, - StdinData: []byte(conf), - } - - // Allocate the IP - r, raw, err := testutils.CmdAddWithArgs(args, func() error { - return cmdAdd(args) - }) - Expect(err).NotTo(HaveOccurred()) - Expect(strings.Index(string(raw), "\"version\":")).Should(BeNumerically(">", 0)) - - _, err = current.GetResult(r) - Expect(err).NotTo(HaveOccurred()) - - ipFilePath := filepath.Join(tmpDir, "mynet", "10.1.2.2") - contents, err := ioutil.ReadFile(ipFilePath) - Expect(err).NotTo(HaveOccurred()) - Expect(string(contents)).To(Equal(args.ContainerID + LineBreak + ifname)) - err = ioutil.WriteFile(ipFilePath, []byte(strings.TrimSpace(args.ContainerID)), 0644) - Expect(err).NotTo(HaveOccurred()) - - err = testutils.CmdDelWithArgs(args, func() error { - return cmdDel(args) - }) - Expect(err).NotTo(HaveOccurred()) - _, err = os.Stat(ipFilePath) - Expect(err).To(HaveOccurred()) - }) - - It("doesn't error when passed an unknown ID on DEL", 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.0", - "name": "mynet", - "type": "ipvlan", - "master": "foo0", - "ipam": { - "type": "host-local", - "subnet": "10.1.2.0/24", - "dataDir": "%s" - } - }`, tmpDir) - - args := &skel.CmdArgs{ - ContainerID: "dummy", - Netns: nspath, - IfName: ifname, - StdinData: []byte(conf), - } - - // Release the IP - err = testutils.CmdDelWithArgs(args, func() error { - return cmdDel(args) - }) - Expect(err).NotTo(HaveOccurred()) - }) - - It("allocates and releases an address with ADD/DEL and 0.1.0 config", func() { - const ifname string = "eth0" - const nspath string = "/some/where" - - tmpDir, err := getTmpDir() - Expect(err).NotTo(HaveOccurred()) - defer os.RemoveAll(tmpDir) - - err = ioutil.WriteFile(filepath.Join(tmpDir, "resolv.conf"), []byte("nameserver 192.0.2.3"), 0644) - Expect(err).NotTo(HaveOccurred()) - - conf := fmt.Sprintf(`{ - "cniVersion": "0.1.0", - "name": "mynet", - "type": "ipvlan", - "master": "foo0", - "ipam": { - "type": "host-local", - "subnet": "10.1.2.0/24", - "dataDir": "%s", - "resolvConf": "%s/resolv.conf" - } - }`, tmpDir, tmpDir) - - args := &skel.CmdArgs{ - ContainerID: "dummy", - Netns: nspath, - IfName: ifname, - StdinData: []byte(conf), - } - - // Allocate the IP - r, raw, err := testutils.CmdAddWithArgs(args, func() error { - return cmdAdd(args) - }) - Expect(err).NotTo(HaveOccurred()) - Expect(strings.Index(string(raw), "\"ip4\":")).Should(BeNumerically(">", 0)) - - result, err := types020.GetResult(r) - Expect(err).NotTo(HaveOccurred()) - - expectedAddress, err := types.ParseCIDR("10.1.2.2/24") - Expect(err).NotTo(HaveOccurred()) - expectedAddress.IP = expectedAddress.IP.To16() - Expect(result.IP4.IP).To(Equal(*expectedAddress)) - Expect(result.IP4.Gateway).To(Equal(net.ParseIP("10.1.2.1"))) - - ipFilePath := filepath.Join(tmpDir, "mynet", "10.1.2.2") - contents, err := ioutil.ReadFile(ipFilePath) - Expect(err).NotTo(HaveOccurred()) - Expect(string(contents)).To(Equal(args.ContainerID + LineBreak + ifname)) - - lastFilePath := filepath.Join(tmpDir, "mynet", "last_reserved_ip.0") - contents, err = ioutil.ReadFile(lastFilePath) - Expect(err).NotTo(HaveOccurred()) - Expect(string(contents)).To(Equal("10.1.2.2")) - - Expect(result.DNS.Nameservers).To(Equal([]string{"192.0.2.3"})) - - // Release the IP - err = testutils.CmdDelWithArgs(args, func() error { - return cmdDel(args) - }) - Expect(err).NotTo(HaveOccurred()) - - _, err = os.Stat(ipFilePath) - Expect(err).To(HaveOccurred()) - }) - - It("ignores whitespace in disk files", 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": "mynet", - "type": "ipvlan", - "master": "foo0", - "ipam": { - "type": "host-local", - "subnet": "10.1.2.0/24", - "dataDir": "%s" - } - }`, tmpDir) - - args := &skel.CmdArgs{ - ContainerID: " dummy\n ", - Netns: nspath, - IfName: ifname, - StdinData: []byte(conf), - } - - // Allocate the IP - r, _, err := testutils.CmdAddWithArgs(args, func() error { - return cmdAdd(args) - }) - Expect(err).NotTo(HaveOccurred()) - - result, err := current.GetResult(r) - Expect(err).NotTo(HaveOccurred()) - - ipFilePath := filepath.Join(tmpDir, "mynet", result.IPs[0].Address.IP.String()) - contents, err := ioutil.ReadFile(ipFilePath) - Expect(err).NotTo(HaveOccurred()) - Expect(string(contents)).To(Equal("dummy" + LineBreak + ifname)) - - // Release the IP - err = testutils.CmdDelWithArgs(args, func() error { - return cmdDel(args) - }) - Expect(err).NotTo(HaveOccurred()) - - _, err = os.Stat(ipFilePath) - Expect(err).To(HaveOccurred()) - }) - - It("does not output an error message upon initial subnet creation", 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.2.0", - "name": "mynet", - "type": "ipvlan", - "master": "foo0", - "ipam": { - "type": "host-local", - "subnet": "10.1.2.0/24", - "dataDir": "%s" - } - }`, tmpDir) - - args := &skel.CmdArgs{ - ContainerID: "testing", - Netns: nspath, - IfName: ifname, - StdinData: []byte(conf), - } - - // Allocate the IP - _, out, err := testutils.CmdAddWithArgs(args, func() error { - return cmdAdd(args) - }) - Expect(err).NotTo(HaveOccurred()) - Expect(strings.Index(string(out), "Error retriving last reserved ip")).To(Equal(-1)) - }) - - It("allocates a custom IP when requested by config args", 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": "mynet", - "type": "ipvlan", - "master": "foo0", - "ipam": { - "type": "host-local", - "dataDir": "%s", - "ranges": [ - [{ "subnet": "10.1.2.0/24" }] - ] - }, - "args": { - "cni": { - "ips": ["10.1.2.88"] - } - } - }`, tmpDir) - - args := &skel.CmdArgs{ - ContainerID: "dummy", - Netns: nspath, - IfName: ifname, - StdinData: []byte(conf), - } - - // Allocate the IP - r, _, err := testutils.CmdAddWithArgs(args, func() error { - return cmdAdd(args) - }) - Expect(err).NotTo(HaveOccurred()) - result, err := current.GetResult(r) - Expect(err).NotTo(HaveOccurred()) - Expect(result.IPs).To(HaveLen(1)) - Expect(result.IPs[0].Address.IP).To(Equal(net.ParseIP("10.1.2.88"))) - }) - - It("allocates custom IPs from multiple ranges", func() { - const ifname string = "eth0" - const nspath string = "/some/where" - - tmpDir, err := getTmpDir() - Expect(err).NotTo(HaveOccurred()) - defer os.RemoveAll(tmpDir) - - err = ioutil.WriteFile(filepath.Join(tmpDir, "resolv.conf"), []byte("nameserver 192.0.2.3"), 0644) - Expect(err).NotTo(HaveOccurred()) - - conf := fmt.Sprintf(`{ - "cniVersion": "0.3.1", - "name": "mynet", - "type": "ipvlan", - "master": "foo0", - "ipam": { - "type": "host-local", - "dataDir": "%s", - "ranges": [ - [{ "subnet": "10.1.2.0/24" }], - [{ "subnet": "10.1.3.0/24" }] - ] - }, - "args": { - "cni": { - "ips": ["10.1.2.88", "10.1.3.77"] - } - } - }`, tmpDir) - - args := &skel.CmdArgs{ - ContainerID: "dummy", - Netns: nspath, - IfName: ifname, - StdinData: []byte(conf), - } - - // Allocate the IP - r, _, err := testutils.CmdAddWithArgs(args, func() error { - return cmdAdd(args) - }) - Expect(err).NotTo(HaveOccurred()) - result, err := current.GetResult(r) - Expect(err).NotTo(HaveOccurred()) - Expect(result.IPs).To(HaveLen(2)) - Expect(result.IPs[0].Address.IP).To(Equal(net.ParseIP("10.1.2.88"))) - Expect(result.IPs[1].Address.IP).To(Equal(net.ParseIP("10.1.3.77"))) - }) - - It("allocates custom IPs from multiple protocols", func() { - const ifname string = "eth0" - const nspath string = "/some/where" - - tmpDir, err := getTmpDir() - Expect(err).NotTo(HaveOccurred()) - defer os.RemoveAll(tmpDir) - - err = ioutil.WriteFile(filepath.Join(tmpDir, "resolv.conf"), []byte("nameserver 192.0.2.3"), 0644) - Expect(err).NotTo(HaveOccurred()) - - conf := fmt.Sprintf(`{ - "cniVersion": "0.3.1", - "name": "mynet", - "type": "ipvlan", - "master": "foo0", - "ipam": { - "type": "host-local", - "dataDir": "%s", - "ranges": [ - [{"subnet":"172.16.1.0/24"}, { "subnet": "10.1.2.0/24" }], - [{ "subnet": "2001:db8:1::/48" }] - ] - }, - "args": { - "cni": { - "ips": ["10.1.2.88", "2001:db8:1::999"] - } - } - }`, tmpDir) - - args := &skel.CmdArgs{ - ContainerID: "dummy", - Netns: nspath, - IfName: ifname, - StdinData: []byte(conf), - } - - // Allocate the IP - r, _, err := testutils.CmdAddWithArgs(args, func() error { - return cmdAdd(args) - }) - Expect(err).NotTo(HaveOccurred()) - result, err := current.GetResult(r) - Expect(err).NotTo(HaveOccurred()) - Expect(result.IPs).To(HaveLen(2)) - Expect(result.IPs[0].Address.IP).To(Equal(net.ParseIP("10.1.2.88"))) - Expect(result.IPs[1].Address.IP).To(Equal(net.ParseIP("2001:db8:1::999"))) - }) - - It("fails if a requested custom IP is not used", 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": "mynet", - "type": "ipvlan", - "master": "foo0", - "ipam": { - "type": "host-local", - "dataDir": "%s", - "ranges": [ - [{ "subnet": "10.1.2.0/24" }], - [{ "subnet": "10.1.3.0/24" }] - ] - }, - "args": { - "cni": { - "ips": ["10.1.2.88", "10.1.2.77"] - } - } - }`, tmpDir) - - args := &skel.CmdArgs{ - ContainerID: "dummy", - Netns: nspath, - IfName: ifname, - StdinData: []byte(conf), - } - - // Allocate the IP - _, _, err = testutils.CmdAddWithArgs(args, func() error { - return cmdAdd(args) - }) - Expect(err).To(HaveOccurred()) - // Need to match prefix, because ordering is not guaranteed - Expect(err.Error()).To(HavePrefix("failed to allocate all requested IPs: 10.1.2.")) - }) -}) - -func getTmpDir() (string, error) { - tmpDir, err := ioutil.TempDir("", "host_local_artifacts") - if err == nil { tmpDir = filepath.ToSlash(tmpDir) - } + }) - return tmpDir, err -} + AfterEach(func() { + os.RemoveAll(tmpDir) + }) + + for _, ver := range testutils.AllSpecVersions { + // Redefine ver inside for scope so real value is picked up by each dynamically defined It() + // See Gingkgo's "Patterns for dynamically generating tests" documentation. + ver := ver + + It(fmt.Sprintf("[%s] allocates and releases addresses with ADD/DEL", ver), func() { + err := ioutil.WriteFile(filepath.Join(tmpDir, "resolv.conf"), []byte("nameserver 192.0.2.3"), 0644) + Expect(err).NotTo(HaveOccurred()) + + conf := fmt.Sprintf(`{ + "cniVersion": "%s", + "name": "mynet", + "type": "ipvlan", + "master": "foo0", + "ipam": { + "type": "host-local", + "dataDir": "%s", + "resolvConf": "%s/resolv.conf", + "ranges": [ + [{ "subnet": "10.1.2.0/24" }, {"subnet": "10.2.2.0/24"}], + [{ "subnet": "2001:db8:1::0/64" }] + ], + "routes": [ + {"dst": "0.0.0.0/0"}, + {"dst": "::/0"}, + {"dst": "192.168.0.0/16", "gw": "1.1.1.1"}, + {"dst": "2001:db8:2::0/64", "gw": "2001:db8:3::1"} + ] + } + }`, ver, tmpDir, tmpDir) + + args := &skel.CmdArgs{ + ContainerID: "dummy", + Netns: nspath, + IfName: ifname, + StdinData: []byte(conf), + } + + // Allocate the IP + r, raw, err := testutils.CmdAddWithArgs(args, func() error { + return cmdAdd(args) + }) + Expect(err).NotTo(HaveOccurred()) + if testutils.SpecVersionHasIPVersion(ver) { + Expect(strings.Index(string(raw), "\"version\":")).Should(BeNumerically(">", 0)) + } + + result, err := types100.GetResult(r) + Expect(err).NotTo(HaveOccurred()) + + // Gomega is cranky about slices with different caps + Expect(*result.IPs[0]).To(Equal( + types100.IPConfig{ + Address: mustCIDR("10.1.2.2/24"), + Gateway: net.ParseIP("10.1.2.1"), + })) + + Expect(*result.IPs[1]).To(Equal( + types100.IPConfig{ + Address: mustCIDR("2001:db8:1::2/64"), + Gateway: net.ParseIP("2001:db8:1::1"), + }, + )) + Expect(len(result.IPs)).To(Equal(2)) + + for _, expectedRoute := range []*types.Route{ + {Dst: mustCIDR("0.0.0.0/0"), GW: nil}, + {Dst: mustCIDR("::/0"), GW: nil}, + {Dst: mustCIDR("192.168.0.0/16"), GW: net.ParseIP("1.1.1.1")}, + {Dst: mustCIDR("2001:db8:2::0/64"), GW: net.ParseIP("2001:db8:3::1")}, + } { + found := false + for _, foundRoute := range result.Routes { + if foundRoute.String() == expectedRoute.String() { + found = true + break + } + } + Expect(found).To(BeTrue()) + } + + ipFilePath1 := filepath.Join(tmpDir, "mynet", "10.1.2.2") + contents, err := ioutil.ReadFile(ipFilePath1) + Expect(err).NotTo(HaveOccurred()) + Expect(string(contents)).To(Equal(args.ContainerID + LineBreak + ifname)) + + ipFilePath2 := filepath.Join(tmpDir, disk.GetEscapedPath("mynet", "2001:db8:1::2")) + contents, err = ioutil.ReadFile(ipFilePath2) + Expect(err).NotTo(HaveOccurred()) + Expect(string(contents)).To(Equal(args.ContainerID + LineBreak + ifname)) + + lastFilePath1 := filepath.Join(tmpDir, "mynet", "last_reserved_ip.0") + contents, err = ioutil.ReadFile(lastFilePath1) + Expect(err).NotTo(HaveOccurred()) + Expect(string(contents)).To(Equal("10.1.2.2")) + + lastFilePath2 := filepath.Join(tmpDir, "mynet", "last_reserved_ip.1") + contents, err = ioutil.ReadFile(lastFilePath2) + Expect(err).NotTo(HaveOccurred()) + Expect(string(contents)).To(Equal("2001:db8:1::2")) + // Release the IP + err = testutils.CmdDelWithArgs(args, func() error { + return cmdDel(args) + }) + Expect(err).NotTo(HaveOccurred()) + + _, err = os.Stat(ipFilePath1) + Expect(err).To(HaveOccurred()) + _, err = os.Stat(ipFilePath2) + Expect(err).To(HaveOccurred()) + }) + + It(fmt.Sprintf("[%s] allocates and releases addresses on specific interface with ADD/DEL", ver), func() { + const ifname1 string = "eth1" + + err := ioutil.WriteFile(filepath.Join(tmpDir, "resolv.conf"), []byte("nameserver 192.0.2.3"), 0644) + Expect(err).NotTo(HaveOccurred()) + + conf0 := fmt.Sprintf(`{ + "cniVersion": "%s", + "name": "mynet0", + "type": "ipvlan", + "master": "foo0", + "ipam": { + "type": "host-local", + "dataDir": "%s", + "resolvConf": "%s/resolv.conf", + "ranges": [ + [{ "subnet": "10.1.2.0/24" }] + ] + } + }`, ver, tmpDir, tmpDir) + + conf1 := fmt.Sprintf(`{ + "cniVersion": "%s", + "name": "mynet1", + "type": "ipvlan", + "master": "foo1", + "ipam": { + "type": "host-local", + "dataDir": "%s", + "resolvConf": "%s/resolv.conf", + "ranges": [ + [{ "subnet": "10.2.2.0/24" }] + ] + } + }`, ver, tmpDir, tmpDir) + + args0 := &skel.CmdArgs{ + ContainerID: "dummy", + Netns: nspath, + IfName: ifname, + StdinData: []byte(conf0), + } + + // Allocate the IP + r0, raw, err := testutils.CmdAddWithArgs(args0, func() error { + return cmdAdd(args0) + }) + Expect(err).NotTo(HaveOccurred()) + if testutils.SpecVersionHasIPVersion(ver) { + Expect(strings.Index(string(raw), "\"version\":")).Should(BeNumerically(">", 0)) + } + + _, err = types100.GetResult(r0) + Expect(err).NotTo(HaveOccurred()) + + args1 := &skel.CmdArgs{ + ContainerID: "dummy", + Netns: nspath, + IfName: ifname1, + StdinData: []byte(conf1), + } + + // Allocate the IP + r1, raw, err := testutils.CmdAddWithArgs(args1, func() error { + return cmdAdd(args1) + }) + Expect(err).NotTo(HaveOccurred()) + if testutils.SpecVersionHasIPVersion(ver) { + Expect(strings.Index(string(raw), "\"version\":")).Should(BeNumerically(">", 0)) + } + + _, err = types100.GetResult(r1) + Expect(err).NotTo(HaveOccurred()) + + ipFilePath0 := filepath.Join(tmpDir, "mynet0", "10.1.2.2") + contents, err := ioutil.ReadFile(ipFilePath0) + Expect(err).NotTo(HaveOccurred()) + Expect(string(contents)).To(Equal(args0.ContainerID + LineBreak + ifname)) + + ipFilePath1 := filepath.Join(tmpDir, "mynet1", "10.2.2.2") + contents, err = ioutil.ReadFile(ipFilePath1) + Expect(err).NotTo(HaveOccurred()) + Expect(string(contents)).To(Equal(args1.ContainerID + LineBreak + ifname1)) + + // Release the IP on ifname + err = testutils.CmdDelWithArgs(args0, func() error { + return cmdDel(args0) + }) + Expect(err).NotTo(HaveOccurred()) + _, err = os.Stat(ipFilePath0) + Expect(err).To(HaveOccurred()) + + // reread ipFilePath1, ensure that ifname1 didn't get deleted + contents, err = ioutil.ReadFile(ipFilePath1) + Expect(err).NotTo(HaveOccurred()) + Expect(string(contents)).To(Equal(args1.ContainerID + LineBreak + ifname1)) + + // Release the IP on ifname1 + err = testutils.CmdDelWithArgs(args1, func() error { + return cmdDel(args1) + }) + Expect(err).NotTo(HaveOccurred()) + + _, err = os.Stat(ipFilePath1) + Expect(err).To(HaveOccurred()) + }) + + It(fmt.Sprintf("[%s] repeat allocating addresses on specific interface for same container ID with ADD", ver), func() { + conf := fmt.Sprintf(`{ + "cniVersion": "%s", + "name": "mynet0", + "type": "ipvlan", + "master": "foo0", + "ipam": { + "type": "host-local", + "dataDir": "%s", + "ranges": [ + [{ "subnet": "10.1.2.0/24" }] + ] + } + }`, ver, 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()) + if testutils.SpecVersionHasIPVersion(ver) { + Expect(strings.Index(string(raw), "\"version\":")).Should(BeNumerically(">", 0)) + } + + result0, err := types100.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 + _, _, err = testutils.CmdAddWithArgs(args, func() error { + return cmdAdd(args) + }) + Expect(err).To(HaveOccurred()) + + // Allocate the IP with the another container ID + r1, raw, err := testutils.CmdAddWithArgs(args1, func() error { + return cmdAdd(args1) + }) + Expect(err).NotTo(HaveOccurred()) + if testutils.SpecVersionHasIPVersion(ver) { + Expect(strings.Index(string(raw), "\"version\":")).Should(BeNumerically(">", 0)) + } + + result1, err := types100.GetResult(r1) + Expect(err).NotTo(HaveOccurred()) + Expect(len(result1.IPs)).Should(Equal(1)) + Expect(result1.IPs[0].Address.String()).Should(Equal("10.1.2.3/24")) + + // Allocate the IP with the same container ID again + _, _, err = testutils.CmdAddWithArgs(args, func() error { + return cmdAdd(args) + }) + Expect(err).To(HaveOccurred()) + + 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(fmt.Sprintf("[%s] verify DEL works on backwards compatible allocate", ver), func() { + err := ioutil.WriteFile(filepath.Join(tmpDir, "resolv.conf"), []byte("nameserver 192.0.2.3"), 0644) + Expect(err).NotTo(HaveOccurred()) + + conf := fmt.Sprintf(`{ + "cniVersion": "%s", + "name": "mynet", + "type": "ipvlan", + "master": "foo", + "ipam": { + "type": "host-local", + "dataDir": "%s", + "resolvConf": "%s/resolv.conf", + "ranges": [ + [{ "subnet": "10.1.2.0/24" }] + ] + } + }`, ver, tmpDir, tmpDir) + + args := &skel.CmdArgs{ + ContainerID: "dummy", + Netns: nspath, + IfName: ifname, + StdinData: []byte(conf), + } + + // Allocate the IP + r, raw, err := testutils.CmdAddWithArgs(args, func() error { + return cmdAdd(args) + }) + Expect(err).NotTo(HaveOccurred()) + if testutils.SpecVersionHasIPVersion(ver) { + Expect(strings.Index(string(raw), "\"version\":")).Should(BeNumerically(">", 0)) + } + + _, err = types100.GetResult(r) + Expect(err).NotTo(HaveOccurred()) + + ipFilePath := filepath.Join(tmpDir, "mynet", "10.1.2.2") + contents, err := ioutil.ReadFile(ipFilePath) + Expect(err).NotTo(HaveOccurred()) + Expect(string(contents)).To(Equal(args.ContainerID + LineBreak + ifname)) + err = ioutil.WriteFile(ipFilePath, []byte(strings.TrimSpace(args.ContainerID)), 0644) + Expect(err).NotTo(HaveOccurred()) + + err = testutils.CmdDelWithArgs(args, func() error { + return cmdDel(args) + }) + Expect(err).NotTo(HaveOccurred()) + _, err = os.Stat(ipFilePath) + Expect(err).To(HaveOccurred()) + }) + + It(fmt.Sprintf("[%s] doesn't error when passed an unknown ID on DEL", ver), func() { + conf := fmt.Sprintf(`{ + "cniVersion": "%s", + "name": "mynet", + "type": "ipvlan", + "master": "foo0", + "ipam": { + "type": "host-local", + "subnet": "10.1.2.0/24", + "dataDir": "%s" + } + }`, ver, tmpDir) + + args := &skel.CmdArgs{ + ContainerID: "dummy", + Netns: nspath, + IfName: ifname, + StdinData: []byte(conf), + } + + // Release the IP + err := testutils.CmdDelWithArgs(args, func() error { + return cmdDel(args) + }) + Expect(err).NotTo(HaveOccurred()) + }) + + It(fmt.Sprintf("[%s] ignores whitespace in disk files", ver), func() { + conf := fmt.Sprintf(`{ + "cniVersion": "%s", + "name": "mynet", + "type": "ipvlan", + "master": "foo0", + "ipam": { + "type": "host-local", + "subnet": "10.1.2.0/24", + "dataDir": "%s" + } + }`, ver, tmpDir) + + args := &skel.CmdArgs{ + ContainerID: " dummy\n ", + Netns: nspath, + IfName: ifname, + StdinData: []byte(conf), + } + + // Allocate the IP + r, _, err := testutils.CmdAddWithArgs(args, func() error { + return cmdAdd(args) + }) + Expect(err).NotTo(HaveOccurred()) + + result, err := types100.GetResult(r) + Expect(err).NotTo(HaveOccurred()) + + ipFilePath := filepath.Join(tmpDir, "mynet", result.IPs[0].Address.IP.String()) + contents, err := ioutil.ReadFile(ipFilePath) + Expect(err).NotTo(HaveOccurred()) + Expect(string(contents)).To(Equal("dummy" + LineBreak + ifname)) + + // Release the IP + err = testutils.CmdDelWithArgs(args, func() error { + return cmdDel(args) + }) + Expect(err).NotTo(HaveOccurred()) + + _, err = os.Stat(ipFilePath) + Expect(err).To(HaveOccurred()) + }) + + It(fmt.Sprintf("[%s] does not output an error message upon initial subnet creation", ver), func() { + conf := fmt.Sprintf(`{ + "cniVersion": "%s", + "name": "mynet", + "type": "ipvlan", + "master": "foo0", + "ipam": { + "type": "host-local", + "subnet": "10.1.2.0/24", + "dataDir": "%s" + } + }`, ver, tmpDir) + + args := &skel.CmdArgs{ + ContainerID: "testing", + Netns: nspath, + IfName: ifname, + StdinData: []byte(conf), + } + + // Allocate the IP + _, out, err := testutils.CmdAddWithArgs(args, func() error { + return cmdAdd(args) + }) + Expect(err).NotTo(HaveOccurred()) + Expect(strings.Index(string(out), "Error retriving last reserved ip")).To(Equal(-1)) + }) + + It(fmt.Sprintf("[%s] allocates a custom IP when requested by config args", ver), func() { + conf := fmt.Sprintf(`{ + "cniVersion": "%s", + "name": "mynet", + "type": "ipvlan", + "master": "foo0", + "ipam": { + "type": "host-local", + "dataDir": "%s", + "ranges": [ + [{ "subnet": "10.1.2.0/24" }] + ] + }, + "args": { + "cni": { + "ips": ["10.1.2.88"] + } + } + }`, ver, tmpDir) + + args := &skel.CmdArgs{ + ContainerID: "dummy", + Netns: nspath, + IfName: ifname, + StdinData: []byte(conf), + } + + // Allocate the IP + r, _, err := testutils.CmdAddWithArgs(args, func() error { + return cmdAdd(args) + }) + Expect(err).NotTo(HaveOccurred()) + result, err := types100.GetResult(r) + Expect(err).NotTo(HaveOccurred()) + Expect(result.IPs).To(HaveLen(1)) + Expect(result.IPs[0].Address.IP).To(Equal(net.ParseIP("10.1.2.88"))) + }) + + It(fmt.Sprintf("[%s] allocates custom IPs from multiple ranges", ver), func() { + err := ioutil.WriteFile(filepath.Join(tmpDir, "resolv.conf"), []byte("nameserver 192.0.2.3"), 0644) + Expect(err).NotTo(HaveOccurred()) + + conf := fmt.Sprintf(`{ + "cniVersion": "%s", + "name": "mynet", + "type": "ipvlan", + "master": "foo0", + "ipam": { + "type": "host-local", + "dataDir": "%s", + "ranges": [ + [{ "subnet": "10.1.2.0/24" }], + [{ "subnet": "10.1.3.0/24" }] + ] + }, + "args": { + "cni": { + "ips": ["10.1.2.88", "10.1.3.77"] + } + } + }`, ver, tmpDir) + + args := &skel.CmdArgs{ + ContainerID: "dummy", + Netns: nspath, + IfName: ifname, + StdinData: []byte(conf), + } + + // Allocate the IP + r, _, err := testutils.CmdAddWithArgs(args, func() error { + return cmdAdd(args) + }) + if !testutils.SpecVersionHasMultipleIPs(ver) { + errStr := fmt.Sprintf("CNI version %s does not support more than 1 address per family", ver) + Expect(err).To(MatchError(errStr)) + } else { + Expect(err).NotTo(HaveOccurred()) + result, err := types100.GetResult(r) + Expect(err).NotTo(HaveOccurred()) + Expect(result.IPs).To(HaveLen(2)) + Expect(result.IPs[0].Address.IP).To(Equal(net.ParseIP("10.1.2.88"))) + Expect(result.IPs[1].Address.IP).To(Equal(net.ParseIP("10.1.3.77"))) + } + }) + + It(fmt.Sprintf("[%s] allocates custom IPs from multiple protocols", ver), func() { + err := ioutil.WriteFile(filepath.Join(tmpDir, "resolv.conf"), []byte("nameserver 192.0.2.3"), 0644) + Expect(err).NotTo(HaveOccurred()) + + conf := fmt.Sprintf(`{ + "cniVersion": "%s", + "name": "mynet", + "type": "ipvlan", + "master": "foo0", + "ipam": { + "type": "host-local", + "dataDir": "%s", + "ranges": [ + [{"subnet":"172.16.1.0/24"}, { "subnet": "10.1.2.0/24" }], + [{ "subnet": "2001:db8:1::/48" }] + ] + }, + "args": { + "cni": { + "ips": ["10.1.2.88", "2001:db8:1::999"] + } + } + }`, ver, tmpDir) + + args := &skel.CmdArgs{ + ContainerID: "dummy", + Netns: nspath, + IfName: ifname, + StdinData: []byte(conf), + } + + // Allocate the IP + r, _, err := testutils.CmdAddWithArgs(args, func() error { + return cmdAdd(args) + }) + Expect(err).NotTo(HaveOccurred()) + result, err := types100.GetResult(r) + Expect(err).NotTo(HaveOccurred()) + Expect(result.IPs).To(HaveLen(2)) + Expect(result.IPs[0].Address.IP).To(Equal(net.ParseIP("10.1.2.88"))) + Expect(result.IPs[1].Address.IP).To(Equal(net.ParseIP("2001:db8:1::999"))) + }) + + It(fmt.Sprintf("[%s] fails if a requested custom IP is not used", ver), func() { + conf := fmt.Sprintf(`{ + "cniVersion": "%s", + "name": "mynet", + "type": "ipvlan", + "master": "foo0", + "ipam": { + "type": "host-local", + "dataDir": "%s", + "ranges": [ + [{ "subnet": "10.1.2.0/24" }], + [{ "subnet": "10.1.3.0/24" }] + ] + }, + "args": { + "cni": { + "ips": ["10.1.2.88", "10.1.2.77"] + } + } + }`, ver, tmpDir) + + args := &skel.CmdArgs{ + ContainerID: "dummy", + Netns: nspath, + IfName: ifname, + StdinData: []byte(conf), + } + + // Allocate the IP + _, _, err := testutils.CmdAddWithArgs(args, func() error { + return cmdAdd(args) + }) + if !testutils.SpecVersionHasMultipleIPs(ver) { + errStr := fmt.Sprintf("CNI version %s does not support more than 1 address per family", ver) + Expect(err).To(MatchError(errStr)) + } else { + Expect(err).To(HaveOccurred()) + // Need to match prefix, because ordering is not guaranteed + Expect(err.Error()).To(HavePrefix("failed to allocate all requested IPs: 10.1.2.")) + } + }) + } +}) func mustCIDR(s string) net.IPNet { ip, n, err := net.ParseCIDR(s)