host-local: increase test coverage to 1.0.0 and older spec versions

Signed-off-by: Dan Williams <dcbw@redhat.com>
This commit is contained in:
Dan Williams 2021-01-28 15:02:24 -06:00
parent f534133ec7
commit 02cdaafe93

View File

@ -24,8 +24,7 @@ import (
"github.com/containernetworking/cni/pkg/skel" "github.com/containernetworking/cni/pkg/skel"
"github.com/containernetworking/cni/pkg/types" "github.com/containernetworking/cni/pkg/types"
"github.com/containernetworking/cni/pkg/types/020" "github.com/containernetworking/cni/pkg/types/100"
current "github.com/containernetworking/cni/pkg/types/100"
"github.com/containernetworking/plugins/pkg/testutils" "github.com/containernetworking/plugins/pkg/testutils"
"github.com/containernetworking/plugins/plugins/ipam/host-local/backend/disk" "github.com/containernetworking/plugins/plugins/ipam/host-local/backend/disk"
@ -36,19 +35,34 @@ import (
const LineBreak = "\r\n" const LineBreak = "\r\n"
var _ = Describe("host-local Operations", func() { var _ = Describe("host-local Operations", func() {
It("allocates and releases addresses with ADD/DEL", func() { var tmpDir string
const ifname string = "eth0" const (
const nspath string = "/some/where" 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()) Expect(err).NotTo(HaveOccurred())
defer os.RemoveAll(tmpDir) tmpDir = filepath.ToSlash(tmpDir)
})
err = ioutil.WriteFile(filepath.Join(tmpDir, "resolv.conf"), []byte("nameserver 192.0.2.3"), 0644) 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()) Expect(err).NotTo(HaveOccurred())
conf := fmt.Sprintf(`{ conf := fmt.Sprintf(`{
"cniVersion": "0.3.1", "cniVersion": "%s",
"name": "mynet", "name": "mynet",
"type": "ipvlan", "type": "ipvlan",
"master": "foo0", "master": "foo0",
@ -67,7 +81,7 @@ var _ = Describe("host-local Operations", func() {
{"dst": "2001:db8:2::0/64", "gw": "2001:db8:3::1"} {"dst": "2001:db8:2::0/64", "gw": "2001:db8:3::1"}
] ]
} }
}`, tmpDir, tmpDir) }`, ver, tmpDir, tmpDir)
args := &skel.CmdArgs{ args := &skel.CmdArgs{
ContainerID: "dummy", ContainerID: "dummy",
@ -81,32 +95,43 @@ var _ = Describe("host-local Operations", func() {
return cmdAdd(args) return cmdAdd(args)
}) })
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
if testutils.SpecVersionHasIPVersion(ver) {
Expect(strings.Index(string(raw), "\"version\":")).Should(BeNumerically(">", 0)) Expect(strings.Index(string(raw), "\"version\":")).Should(BeNumerically(">", 0))
}
result, err := current.GetResult(r) result, err := types100.GetResult(r)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
// Gomega is cranky about slices with different caps // Gomega is cranky about slices with different caps
Expect(*result.IPs[0]).To(Equal( Expect(*result.IPs[0]).To(Equal(
current.IPConfig{ types100.IPConfig{
Address: mustCIDR("10.1.2.2/24"), Address: mustCIDR("10.1.2.2/24"),
Gateway: net.ParseIP("10.1.2.1"), Gateway: net.ParseIP("10.1.2.1"),
})) }))
Expect(*result.IPs[1]).To(Equal( Expect(*result.IPs[1]).To(Equal(
current.IPConfig{ types100.IPConfig{
Address: mustCIDR("2001:db8:1::2/64"), Address: mustCIDR("2001:db8:1::2/64"),
Gateway: net.ParseIP("2001:db8:1::1"), Gateway: net.ParseIP("2001:db8:1::1"),
}, },
)) ))
Expect(len(result.IPs)).To(Equal(2)) Expect(len(result.IPs)).To(Equal(2))
Expect(result.Routes).To(Equal([]*types.Route{ for _, expectedRoute := range []*types.Route{
{Dst: mustCIDR("0.0.0.0/0"), GW: nil}, {Dst: mustCIDR("0.0.0.0/0"), GW: nil},
{Dst: mustCIDR("::/0"), GW: nil}, {Dst: mustCIDR("::/0"), GW: nil},
{Dst: mustCIDR("192.168.0.0/16"), GW: net.ParseIP("1.1.1.1")}, {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")}, {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") ipFilePath1 := filepath.Join(tmpDir, "mynet", "10.1.2.2")
contents, err := ioutil.ReadFile(ipFilePath1) contents, err := ioutil.ReadFile(ipFilePath1)
@ -139,20 +164,14 @@ var _ = Describe("host-local Operations", func() {
Expect(err).To(HaveOccurred()) Expect(err).To(HaveOccurred())
}) })
It("allocates and releases addresses on specific interface with ADD/DEL", func() { It(fmt.Sprintf("[%s] allocates and releases addresses on specific interface with ADD/DEL", ver), func() {
const ifname0 string = "eth0"
const ifname1 string = "eth1" const ifname1 string = "eth1"
const nspath string = "/some/where"
tmpDir, err := getTmpDir() err := ioutil.WriteFile(filepath.Join(tmpDir, "resolv.conf"), []byte("nameserver 192.0.2.3"), 0644)
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()) Expect(err).NotTo(HaveOccurred())
conf0 := fmt.Sprintf(`{ conf0 := fmt.Sprintf(`{
"cniVersion": "0.3.1", "cniVersion": "%s",
"name": "mynet0", "name": "mynet0",
"type": "ipvlan", "type": "ipvlan",
"master": "foo0", "master": "foo0",
@ -164,10 +183,10 @@ var _ = Describe("host-local Operations", func() {
[{ "subnet": "10.1.2.0/24" }] [{ "subnet": "10.1.2.0/24" }]
] ]
} }
}`, tmpDir, tmpDir) }`, ver, tmpDir, tmpDir)
conf1 := fmt.Sprintf(`{ conf1 := fmt.Sprintf(`{
"cniVersion": "0.3.1", "cniVersion": "%s",
"name": "mynet1", "name": "mynet1",
"type": "ipvlan", "type": "ipvlan",
"master": "foo1", "master": "foo1",
@ -179,12 +198,12 @@ var _ = Describe("host-local Operations", func() {
[{ "subnet": "10.2.2.0/24" }] [{ "subnet": "10.2.2.0/24" }]
] ]
} }
}`, tmpDir, tmpDir) }`, ver, tmpDir, tmpDir)
args0 := &skel.CmdArgs{ args0 := &skel.CmdArgs{
ContainerID: "dummy", ContainerID: "dummy",
Netns: nspath, Netns: nspath,
IfName: ifname0, IfName: ifname,
StdinData: []byte(conf0), StdinData: []byte(conf0),
} }
@ -193,9 +212,11 @@ var _ = Describe("host-local Operations", func() {
return cmdAdd(args0) return cmdAdd(args0)
}) })
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
if testutils.SpecVersionHasIPVersion(ver) {
Expect(strings.Index(string(raw), "\"version\":")).Should(BeNumerically(">", 0)) Expect(strings.Index(string(raw), "\"version\":")).Should(BeNumerically(">", 0))
}
_, err = current.GetResult(r0) _, err = types100.GetResult(r0)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
args1 := &skel.CmdArgs{ args1 := &skel.CmdArgs{
@ -210,22 +231,24 @@ var _ = Describe("host-local Operations", func() {
return cmdAdd(args1) return cmdAdd(args1)
}) })
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
if testutils.SpecVersionHasIPVersion(ver) {
Expect(strings.Index(string(raw), "\"version\":")).Should(BeNumerically(">", 0)) Expect(strings.Index(string(raw), "\"version\":")).Should(BeNumerically(">", 0))
}
_, err = current.GetResult(r1) _, err = types100.GetResult(r1)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
ipFilePath0 := filepath.Join(tmpDir, "mynet0", "10.1.2.2") ipFilePath0 := filepath.Join(tmpDir, "mynet0", "10.1.2.2")
contents, err := ioutil.ReadFile(ipFilePath0) contents, err := ioutil.ReadFile(ipFilePath0)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(string(contents)).To(Equal(args0.ContainerID + LineBreak + ifname0)) Expect(string(contents)).To(Equal(args0.ContainerID + LineBreak + ifname))
ipFilePath1 := filepath.Join(tmpDir, "mynet1", "10.2.2.2") ipFilePath1 := filepath.Join(tmpDir, "mynet1", "10.2.2.2")
contents, err = ioutil.ReadFile(ipFilePath1) contents, err = ioutil.ReadFile(ipFilePath1)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(string(contents)).To(Equal(args1.ContainerID + LineBreak + ifname1)) Expect(string(contents)).To(Equal(args1.ContainerID + LineBreak + ifname1))
// Release the IP on ifname0 // Release the IP on ifname
err = testutils.CmdDelWithArgs(args0, func() error { err = testutils.CmdDelWithArgs(args0, func() error {
return cmdDel(args0) return cmdDel(args0)
}) })
@ -248,16 +271,9 @@ var _ = Describe("host-local Operations", func() {
Expect(err).To(HaveOccurred()) Expect(err).To(HaveOccurred())
}) })
It("repeat allocating addresses on specific interface for same container ID with ADD", func() { It(fmt.Sprintf("[%s] repeat allocating addresses on specific interface for same container ID with ADD", ver), func() {
const ifname string = "eth0"
const nspath string = "/some/where"
tmpDir, err := getTmpDir()
Expect(err).NotTo(HaveOccurred())
defer os.RemoveAll(tmpDir)
conf := fmt.Sprintf(`{ conf := fmt.Sprintf(`{
"cniVersion": "0.3.1", "cniVersion": "%s",
"name": "mynet0", "name": "mynet0",
"type": "ipvlan", "type": "ipvlan",
"master": "foo0", "master": "foo0",
@ -268,7 +284,7 @@ var _ = Describe("host-local Operations", func() {
[{ "subnet": "10.1.2.0/24" }] [{ "subnet": "10.1.2.0/24" }]
] ]
} }
}`, tmpDir) }`, ver, tmpDir)
args := &skel.CmdArgs{ args := &skel.CmdArgs{
ContainerID: "dummy", ContainerID: "dummy",
@ -289,9 +305,11 @@ var _ = Describe("host-local Operations", func() {
return cmdAdd(args) return cmdAdd(args)
}) })
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
if testutils.SpecVersionHasIPVersion(ver) {
Expect(strings.Index(string(raw), "\"version\":")).Should(BeNumerically(">", 0)) Expect(strings.Index(string(raw), "\"version\":")).Should(BeNumerically(">", 0))
}
result0, err := current.GetResult(r0) result0, err := types100.GetResult(r0)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(len(result0.IPs)).Should(Equal(1)) Expect(len(result0.IPs)).Should(Equal(1))
Expect(result0.IPs[0].Address.String()).Should(Equal("10.1.2.2/24")) Expect(result0.IPs[0].Address.String()).Should(Equal("10.1.2.2/24"))
@ -307,9 +325,11 @@ var _ = Describe("host-local Operations", func() {
return cmdAdd(args1) return cmdAdd(args1)
}) })
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
if testutils.SpecVersionHasIPVersion(ver) {
Expect(strings.Index(string(raw), "\"version\":")).Should(BeNumerically(">", 0)) Expect(strings.Index(string(raw), "\"version\":")).Should(BeNumerically(">", 0))
}
result1, err := current.GetResult(r1) result1, err := types100.GetResult(r1)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(len(result1.IPs)).Should(Equal(1)) Expect(len(result1.IPs)).Should(Equal(1))
Expect(result1.IPs[0].Address.String()).Should(Equal("10.1.2.3/24")) Expect(result1.IPs[0].Address.String()).Should(Equal("10.1.2.3/24"))
@ -336,19 +356,12 @@ var _ = Describe("host-local Operations", func() {
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
}) })
It("Verify DEL works on backwards compatible allocate", func() { It(fmt.Sprintf("[%s] verify DEL works on backwards compatible allocate", ver), func() {
const nspath string = "/some/where" err := ioutil.WriteFile(filepath.Join(tmpDir, "resolv.conf"), []byte("nameserver 192.0.2.3"), 0644)
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()) Expect(err).NotTo(HaveOccurred())
conf := fmt.Sprintf(`{ conf := fmt.Sprintf(`{
"cniVersion": "0.3.1", "cniVersion": "%s",
"name": "mynet", "name": "mynet",
"type": "ipvlan", "type": "ipvlan",
"master": "foo", "master": "foo",
@ -360,7 +373,7 @@ var _ = Describe("host-local Operations", func() {
[{ "subnet": "10.1.2.0/24" }] [{ "subnet": "10.1.2.0/24" }]
] ]
} }
}`, tmpDir, tmpDir) }`, ver, tmpDir, tmpDir)
args := &skel.CmdArgs{ args := &skel.CmdArgs{
ContainerID: "dummy", ContainerID: "dummy",
@ -374,9 +387,11 @@ var _ = Describe("host-local Operations", func() {
return cmdAdd(args) return cmdAdd(args)
}) })
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
if testutils.SpecVersionHasIPVersion(ver) {
Expect(strings.Index(string(raw), "\"version\":")).Should(BeNumerically(">", 0)) Expect(strings.Index(string(raw), "\"version\":")).Should(BeNumerically(">", 0))
}
_, err = current.GetResult(r) _, err = types100.GetResult(r)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
ipFilePath := filepath.Join(tmpDir, "mynet", "10.1.2.2") ipFilePath := filepath.Join(tmpDir, "mynet", "10.1.2.2")
@ -394,16 +409,9 @@ var _ = Describe("host-local Operations", func() {
Expect(err).To(HaveOccurred()) Expect(err).To(HaveOccurred())
}) })
It("doesn't error when passed an unknown ID on DEL", func() { It(fmt.Sprintf("[%s] doesn't error when passed an unknown ID on DEL", ver), func() {
const ifname string = "eth0"
const nspath string = "/some/where"
tmpDir, err := getTmpDir()
Expect(err).NotTo(HaveOccurred())
defer os.RemoveAll(tmpDir)
conf := fmt.Sprintf(`{ conf := fmt.Sprintf(`{
"cniVersion": "0.3.0", "cniVersion": "%s",
"name": "mynet", "name": "mynet",
"type": "ipvlan", "type": "ipvlan",
"master": "foo0", "master": "foo0",
@ -412,7 +420,7 @@ var _ = Describe("host-local Operations", func() {
"subnet": "10.1.2.0/24", "subnet": "10.1.2.0/24",
"dataDir": "%s" "dataDir": "%s"
} }
}`, tmpDir) }`, ver, tmpDir)
args := &skel.CmdArgs{ args := &skel.CmdArgs{
ContainerID: "dummy", ContainerID: "dummy",
@ -422,91 +430,15 @@ var _ = Describe("host-local Operations", func() {
} }
// Release the IP // Release the IP
err = testutils.CmdDelWithArgs(args, func() error { err := testutils.CmdDelWithArgs(args, func() error {
return cmdDel(args) return cmdDel(args)
}) })
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
}) })
It("allocates and releases an address with ADD/DEL and 0.1.0 config", func() { It(fmt.Sprintf("[%s] ignores whitespace in disk files", ver), 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(`{ conf := fmt.Sprintf(`{
"cniVersion": "0.1.0", "cniVersion": "%s",
"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", "name": "mynet",
"type": "ipvlan", "type": "ipvlan",
"master": "foo0", "master": "foo0",
@ -515,7 +447,7 @@ var _ = Describe("host-local Operations", func() {
"subnet": "10.1.2.0/24", "subnet": "10.1.2.0/24",
"dataDir": "%s" "dataDir": "%s"
} }
}`, tmpDir) }`, ver, tmpDir)
args := &skel.CmdArgs{ args := &skel.CmdArgs{
ContainerID: " dummy\n ", ContainerID: " dummy\n ",
@ -530,7 +462,7 @@ var _ = Describe("host-local Operations", func() {
}) })
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
result, err := current.GetResult(r) result, err := types100.GetResult(r)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
ipFilePath := filepath.Join(tmpDir, "mynet", result.IPs[0].Address.IP.String()) ipFilePath := filepath.Join(tmpDir, "mynet", result.IPs[0].Address.IP.String())
@ -548,16 +480,9 @@ var _ = Describe("host-local Operations", func() {
Expect(err).To(HaveOccurred()) Expect(err).To(HaveOccurred())
}) })
It("does not output an error message upon initial subnet creation", func() { It(fmt.Sprintf("[%s] does not output an error message upon initial subnet creation", ver), func() {
const ifname string = "eth0"
const nspath string = "/some/where"
tmpDir, err := getTmpDir()
Expect(err).NotTo(HaveOccurred())
defer os.RemoveAll(tmpDir)
conf := fmt.Sprintf(`{ conf := fmt.Sprintf(`{
"cniVersion": "0.2.0", "cniVersion": "%s",
"name": "mynet", "name": "mynet",
"type": "ipvlan", "type": "ipvlan",
"master": "foo0", "master": "foo0",
@ -566,7 +491,7 @@ var _ = Describe("host-local Operations", func() {
"subnet": "10.1.2.0/24", "subnet": "10.1.2.0/24",
"dataDir": "%s" "dataDir": "%s"
} }
}`, tmpDir) }`, ver, tmpDir)
args := &skel.CmdArgs{ args := &skel.CmdArgs{
ContainerID: "testing", ContainerID: "testing",
@ -583,16 +508,9 @@ var _ = Describe("host-local Operations", func() {
Expect(strings.Index(string(out), "Error retriving last reserved ip")).To(Equal(-1)) Expect(strings.Index(string(out), "Error retriving last reserved ip")).To(Equal(-1))
}) })
It("allocates a custom IP when requested by config args", func() { It(fmt.Sprintf("[%s] allocates a custom IP when requested by config args", ver), func() {
const ifname string = "eth0"
const nspath string = "/some/where"
tmpDir, err := getTmpDir()
Expect(err).NotTo(HaveOccurred())
defer os.RemoveAll(tmpDir)
conf := fmt.Sprintf(`{ conf := fmt.Sprintf(`{
"cniVersion": "0.3.1", "cniVersion": "%s",
"name": "mynet", "name": "mynet",
"type": "ipvlan", "type": "ipvlan",
"master": "foo0", "master": "foo0",
@ -608,7 +526,7 @@ var _ = Describe("host-local Operations", func() {
"ips": ["10.1.2.88"] "ips": ["10.1.2.88"]
} }
} }
}`, tmpDir) }`, ver, tmpDir)
args := &skel.CmdArgs{ args := &skel.CmdArgs{
ContainerID: "dummy", ContainerID: "dummy",
@ -622,25 +540,18 @@ var _ = Describe("host-local Operations", func() {
return cmdAdd(args) return cmdAdd(args)
}) })
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
result, err := current.GetResult(r) result, err := types100.GetResult(r)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(result.IPs).To(HaveLen(1)) Expect(result.IPs).To(HaveLen(1))
Expect(result.IPs[0].Address.IP).To(Equal(net.ParseIP("10.1.2.88"))) Expect(result.IPs[0].Address.IP).To(Equal(net.ParseIP("10.1.2.88")))
}) })
It("allocates custom IPs from multiple ranges", func() { It(fmt.Sprintf("[%s] allocates custom IPs from multiple ranges", ver), func() {
const ifname string = "eth0" err := ioutil.WriteFile(filepath.Join(tmpDir, "resolv.conf"), []byte("nameserver 192.0.2.3"), 0644)
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()) Expect(err).NotTo(HaveOccurred())
conf := fmt.Sprintf(`{ conf := fmt.Sprintf(`{
"cniVersion": "0.3.1", "cniVersion": "%s",
"name": "mynet", "name": "mynet",
"type": "ipvlan", "type": "ipvlan",
"master": "foo0", "master": "foo0",
@ -657,7 +568,7 @@ var _ = Describe("host-local Operations", func() {
"ips": ["10.1.2.88", "10.1.3.77"] "ips": ["10.1.2.88", "10.1.3.77"]
} }
} }
}`, tmpDir) }`, ver, tmpDir)
args := &skel.CmdArgs{ args := &skel.CmdArgs{
ContainerID: "dummy", ContainerID: "dummy",
@ -670,27 +581,25 @@ var _ = Describe("host-local Operations", func() {
r, _, err := testutils.CmdAddWithArgs(args, func() error { r, _, err := testutils.CmdAddWithArgs(args, func() error {
return cmdAdd(args) 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()) Expect(err).NotTo(HaveOccurred())
result, err := current.GetResult(r) result, err := types100.GetResult(r)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(result.IPs).To(HaveLen(2)) Expect(result.IPs).To(HaveLen(2))
Expect(result.IPs[0].Address.IP).To(Equal(net.ParseIP("10.1.2.88"))) 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"))) Expect(result.IPs[1].Address.IP).To(Equal(net.ParseIP("10.1.3.77")))
}
}) })
It("allocates custom IPs from multiple protocols", func() { It(fmt.Sprintf("[%s] allocates custom IPs from multiple protocols", ver), func() {
const ifname string = "eth0" err := ioutil.WriteFile(filepath.Join(tmpDir, "resolv.conf"), []byte("nameserver 192.0.2.3"), 0644)
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()) Expect(err).NotTo(HaveOccurred())
conf := fmt.Sprintf(`{ conf := fmt.Sprintf(`{
"cniVersion": "0.3.1", "cniVersion": "%s",
"name": "mynet", "name": "mynet",
"type": "ipvlan", "type": "ipvlan",
"master": "foo0", "master": "foo0",
@ -707,7 +616,7 @@ var _ = Describe("host-local Operations", func() {
"ips": ["10.1.2.88", "2001:db8:1::999"] "ips": ["10.1.2.88", "2001:db8:1::999"]
} }
} }
}`, tmpDir) }`, ver, tmpDir)
args := &skel.CmdArgs{ args := &skel.CmdArgs{
ContainerID: "dummy", ContainerID: "dummy",
@ -721,23 +630,16 @@ var _ = Describe("host-local Operations", func() {
return cmdAdd(args) return cmdAdd(args)
}) })
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
result, err := current.GetResult(r) result, err := types100.GetResult(r)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(result.IPs).To(HaveLen(2)) Expect(result.IPs).To(HaveLen(2))
Expect(result.IPs[0].Address.IP).To(Equal(net.ParseIP("10.1.2.88"))) 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"))) 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() { It(fmt.Sprintf("[%s] fails if a requested custom IP is not used", ver), func() {
const ifname string = "eth0"
const nspath string = "/some/where"
tmpDir, err := getTmpDir()
Expect(err).NotTo(HaveOccurred())
defer os.RemoveAll(tmpDir)
conf := fmt.Sprintf(`{ conf := fmt.Sprintf(`{
"cniVersion": "0.3.1", "cniVersion": "%s",
"name": "mynet", "name": "mynet",
"type": "ipvlan", "type": "ipvlan",
"master": "foo0", "master": "foo0",
@ -754,7 +656,7 @@ var _ = Describe("host-local Operations", func() {
"ips": ["10.1.2.88", "10.1.2.77"] "ips": ["10.1.2.88", "10.1.2.77"]
} }
} }
}`, tmpDir) }`, ver, tmpDir)
args := &skel.CmdArgs{ args := &skel.CmdArgs{
ContainerID: "dummy", ContainerID: "dummy",
@ -764,23 +666,20 @@ var _ = Describe("host-local Operations", func() {
} }
// Allocate the IP // Allocate the IP
_, _, err = testutils.CmdAddWithArgs(args, func() error { _, _, err := testutils.CmdAddWithArgs(args, func() error {
return cmdAdd(args) 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()) Expect(err).To(HaveOccurred())
// Need to match prefix, because ordering is not guaranteed // Need to match prefix, because ordering is not guaranteed
Expect(err.Error()).To(HavePrefix("failed to allocate all requested IPs: 10.1.2.")) 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
} }
})
func mustCIDR(s string) net.IPNet { func mustCIDR(s string) net.IPNet {
ip, n, err := net.ParseCIDR(s) ip, n, err := net.ParseCIDR(s)