Skip to content

Commit

Permalink
plugins/testutils: pass CNI_CONTAINERID to plugins in testcases
Browse files Browse the repository at this point in the history
Recent CNI specification changes require the container ID on ADD/DEL,
which the testcases were not providing.  Fix that up so things work
when this repo gets CNI revendored.
  • Loading branch information
dcbw committed Apr 26, 2018
1 parent 1df359a commit 7312980
Show file tree
Hide file tree
Showing 15 changed files with 84 additions and 68 deletions.
16 changes: 14 additions & 2 deletions pkg/testutils/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"io/ioutil"
"os"

"github.com/containernetworking/cni/pkg/skel"
"github.com/containernetworking/cni/pkg/types"
"github.com/containernetworking/cni/pkg/version"
)
Expand All @@ -27,13 +28,15 @@ func envCleanup() {
os.Unsetenv("CNI_PATH")
os.Unsetenv("CNI_NETNS")
os.Unsetenv("CNI_IFNAME")
os.Unsetenv("CNI_CONTAINERID")
}

func CmdAddWithResult(cniNetns, cniIfname string, conf []byte, f func() error) (types.Result, []byte, error) {
func CmdAdd(cniNetns, cniContainerID, cniIfname string, conf []byte, f func() error) (types.Result, []byte, error) {
os.Setenv("CNI_COMMAND", "ADD")
os.Setenv("CNI_PATH", os.Getenv("PATH"))
os.Setenv("CNI_NETNS", cniNetns)
os.Setenv("CNI_IFNAME", cniIfname)
os.Setenv("CNI_CONTAINERID", cniContainerID)
defer envCleanup()

// Redirect stdout to capture plugin result
Expand Down Expand Up @@ -74,12 +77,21 @@ func CmdAddWithResult(cniNetns, cniIfname string, conf []byte, f func() error) (
return result, out, nil
}

func CmdDelWithResult(cniNetns, cniIfname string, f func() error) error {
func CmdAddWithArgs(args *skel.CmdArgs, f func() error) (types.Result, []byte, error) {
return CmdAdd(args.Netns, args.ContainerID, args.IfName, args.StdinData, f)
}

func CmdDel(cniNetns, cniContainerID, cniIfname string, f func() error) error {
os.Setenv("CNI_COMMAND", "DEL")
os.Setenv("CNI_PATH", os.Getenv("PATH"))
os.Setenv("CNI_NETNS", cniNetns)
os.Setenv("CNI_IFNAME", cniIfname)
os.Setenv("CNI_CONTAINERID", cniContainerID)
defer envCleanup()

return f()
}

func CmdDelWithArgs(args *skel.CmdArgs, f func() error) error {
return CmdDel(args.Netns, args.ContainerID, args.IfName, f)
}
10 changes: 5 additions & 5 deletions plugins/ipam/dhcp/dhcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ var _ = Describe("DHCP Operations", func() {
err := originalNS.Do(func(ns.NetNS) error {
defer GinkgoRecover()

r, _, err := testutils.CmdAddWithResult(targetNS.Path(), contVethName, []byte(conf), func() error {
r, _, err := testutils.CmdAddWithArgs(args, func() error {
return cmdAdd(args)
})
Expect(err).NotTo(HaveOccurred())
Expand All @@ -245,7 +245,7 @@ var _ = Describe("DHCP Operations", func() {
Expect(err).NotTo(HaveOccurred())

err = originalNS.Do(func(ns.NetNS) error {
return testutils.CmdDelWithResult(targetNS.Path(), contVethName, func() error {
return testutils.CmdDelWithArgs(args, func() error {
return cmdDel(args)
})
})
Expand Down Expand Up @@ -273,7 +273,7 @@ var _ = Describe("DHCP Operations", func() {
err := originalNS.Do(func(ns.NetNS) error {
defer GinkgoRecover()

r, _, err := testutils.CmdAddWithResult(targetNS.Path(), contVethName, []byte(conf), func() error {
r, _, err := testutils.CmdAddWithArgs(args, func() error {
return cmdAdd(args)
})
Expect(err).NotTo(HaveOccurred())
Expand All @@ -299,7 +299,7 @@ var _ = Describe("DHCP Operations", func() {
started.Wait()

err = originalNS.Do(func(ns.NetNS) error {
return testutils.CmdDelWithResult(targetNS.Path(), contVethName, func() error {
return testutils.CmdDelWithArgs(args, func() error {
return cmdDel(args)
})
})
Expand All @@ -310,7 +310,7 @@ var _ = Describe("DHCP Operations", func() {
wg.Wait()

err = originalNS.Do(func(ns.NetNS) error {
return testutils.CmdDelWithResult(targetNS.Path(), contVethName, func() error {
return testutils.CmdDelWithArgs(args, func() error {
return cmdDel(args)
})
})
Expand Down
30 changes: 15 additions & 15 deletions plugins/ipam/host-local/host_local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ var _ = Describe("host-local Operations", func() {
}

// Allocate the IP
r, raw, err := testutils.CmdAddWithResult(nspath, ifname, []byte(conf), func() error {
r, raw, err := testutils.CmdAddWithArgs(args, func() error {
return cmdAdd(args)
})
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -111,12 +111,12 @@ var _ = Describe("host-local Operations", func() {
ipFilePath1 := filepath.Join(tmpDir, "mynet", "10.1.2.2")
contents, err := ioutil.ReadFile(ipFilePath1)
Expect(err).NotTo(HaveOccurred())
Expect(string(contents)).To(Equal("dummy"))
Expect(string(contents)).To(Equal(args.ContainerID))

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("dummy"))
Expect(string(contents)).To(Equal(args.ContainerID))

lastFilePath1 := filepath.Join(tmpDir, "mynet", "last_reserved_ip.0")
contents, err = ioutil.ReadFile(lastFilePath1)
Expand All @@ -128,7 +128,7 @@ var _ = Describe("host-local Operations", func() {
Expect(err).NotTo(HaveOccurred())
Expect(string(contents)).To(Equal("2001:db8:1::2"))
// Release the IP
err = testutils.CmdDelWithResult(nspath, ifname, func() error {
err = testutils.CmdDelWithArgs(args, func() error {
return cmdDel(args)
})
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -167,7 +167,7 @@ var _ = Describe("host-local Operations", func() {
}

// Release the IP
err = testutils.CmdDelWithResult(nspath, ifname, func() error {
err = testutils.CmdDelWithArgs(args, func() error {
return cmdDel(args)
})
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -205,7 +205,7 @@ var _ = Describe("host-local Operations", func() {
}

// Allocate the IP
r, raw, err := testutils.CmdAddWithResult(nspath, ifname, []byte(conf), func() error {
r, raw, err := testutils.CmdAddWithArgs(args, func() error {
return cmdAdd(args)
})
Expect(err).NotTo(HaveOccurred())
Expand All @@ -223,7 +223,7 @@ var _ = Describe("host-local Operations", func() {
ipFilePath := filepath.Join(tmpDir, "mynet", "10.1.2.2")
contents, err := ioutil.ReadFile(ipFilePath)
Expect(err).NotTo(HaveOccurred())
Expect(string(contents)).To(Equal("dummy"))
Expect(string(contents)).To(Equal(args.ContainerID))

lastFilePath := filepath.Join(tmpDir, "mynet", "last_reserved_ip.0")
contents, err = ioutil.ReadFile(lastFilePath)
Expand All @@ -233,7 +233,7 @@ var _ = Describe("host-local Operations", func() {
Expect(result.DNS).To(Equal(types.DNS{Nameservers: []string{"192.0.2.3"}}))

// Release the IP
err = testutils.CmdDelWithResult(nspath, ifname, func() error {
err = testutils.CmdDelWithArgs(args, func() error {
return cmdDel(args)
})
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -270,7 +270,7 @@ var _ = Describe("host-local Operations", func() {
}

// Allocate the IP
r, _, err := testutils.CmdAddWithResult(nspath, ifname, []byte(conf), func() error {
r, _, err := testutils.CmdAddWithArgs(args, func() error {
return cmdAdd(args)
})
Expect(err).NotTo(HaveOccurred())
Expand All @@ -284,7 +284,7 @@ var _ = Describe("host-local Operations", func() {
Expect(string(contents)).To(Equal("dummy"))

// Release the IP
err = testutils.CmdDelWithResult(nspath, ifname, func() error {
err = testutils.CmdDelWithArgs(args, func() error {
return cmdDel(args)
})
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -321,7 +321,7 @@ var _ = Describe("host-local Operations", func() {
}

// Allocate the IP
_, out, err := testutils.CmdAddWithResult(nspath, ifname, []byte(conf), func() error {
_, out, err := testutils.CmdAddWithArgs(args, func() error {
return cmdAdd(args)
})
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -363,7 +363,7 @@ var _ = Describe("host-local Operations", func() {
}

// Allocate the IP
r, _, err := testutils.CmdAddWithResult(nspath, ifname, []byte(conf), func() error {
r, _, err := testutils.CmdAddWithArgs(args, func() error {
return cmdAdd(args)
})
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -412,7 +412,7 @@ var _ = Describe("host-local Operations", func() {
}

// Allocate the IP
r, _, err := testutils.CmdAddWithResult(nspath, ifname, []byte(conf), func() error {
r, _, err := testutils.CmdAddWithArgs(args, func() error {
return cmdAdd(args)
})
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -462,7 +462,7 @@ var _ = Describe("host-local Operations", func() {
}

// Allocate the IP
r, _, err := testutils.CmdAddWithResult(nspath, ifname, []byte(conf), func() error {
r, _, err := testutils.CmdAddWithArgs(args, func() error {
return cmdAdd(args)
})
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -509,7 +509,7 @@ var _ = Describe("host-local Operations", func() {
}

// Allocate the IP
_, _, err = testutils.CmdAddWithResult(nspath, ifname, []byte(conf), func() error {
_, _, err = testutils.CmdAddWithArgs(args, func() error {
return cmdAdd(args)
})
Expect(err).To(HaveOccurred())
Expand Down
6 changes: 3 additions & 3 deletions plugins/ipam/static/static_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ var _ = Describe("static Operations", func() {
}

// Allocate the IP
r, raw, err := testutils.CmdAddWithResult(nspath, ifname, []byte(conf), func() error {
r, raw, err := testutils.CmdAddWithArgs(args, func() error {
return cmdAdd(args)
})
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -100,7 +100,7 @@ var _ = Describe("static Operations", func() {
}))

// Release the IP
err = testutils.CmdDelWithResult(nspath, ifname, func() error {
err = testutils.CmdDelWithArgs(args, func() error {
return cmdDel(args)
})
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -143,7 +143,7 @@ var _ = Describe("static Operations", func() {
}

// Release the IP
err := testutils.CmdDelWithResult(nspath, ifname, func() error {
err := testutils.CmdDelWithArgs(args, func() error {
return cmdDel(args)
})
Expect(err).NotTo(HaveOccurred())
Expand Down
13 changes: 8 additions & 5 deletions plugins/main/bridge/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,15 @@ func (tc testCase) rangesConfig() string {
return conf + rangesEndStr
}

var counter uint

// createCmdArgs generates network configuration and creates command
// arguments for a test case.
func (tc testCase) createCmdArgs(targetNS ns.NetNS) *skel.CmdArgs {
conf := tc.netConfJSON()
defer func() { counter += 1 }()
return &skel.CmdArgs{
ContainerID: "dummy",
ContainerID: fmt.Sprintf("dummy-%d", counter),
Netns: targetNS.Path(),
IfName: IFNAME,
StdinData: []byte(conf),
Expand Down Expand Up @@ -246,7 +249,7 @@ func (tester *testerV03x) cmdAddTest(tc testCase) {
err := tester.testNS.Do(func(ns.NetNS) error {
defer GinkgoRecover()

r, raw, err := testutils.CmdAddWithResult(tester.targetNS.Path(), IFNAME, tester.args.StdinData, func() error {
r, raw, err := testutils.CmdAddWithArgs(tester.args, func() error {
return cmdAdd(tester.args)
})
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -374,7 +377,7 @@ func (tester *testerV03x) cmdDelTest(tc testCase) {
err := tester.testNS.Do(func(ns.NetNS) error {
defer GinkgoRecover()

err := testutils.CmdDelWithResult(tester.targetNS.Path(), IFNAME, func() error {
err := testutils.CmdDelWithArgs(tester.args, func() error {
return cmdDel(tester.args)
})
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -424,7 +427,7 @@ func (tester *testerV01xOr02x) cmdAddTest(tc testCase) {
err := tester.testNS.Do(func(ns.NetNS) error {
defer GinkgoRecover()

r, raw, err := testutils.CmdAddWithResult(tester.targetNS.Path(), IFNAME, tester.args.StdinData, func() error {
r, raw, err := testutils.CmdAddWithArgs(tester.args, func() error {
return cmdAdd(tester.args)
})
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -513,7 +516,7 @@ func (tester *testerV01xOr02x) cmdDelTest(tc testCase) {
err := tester.testNS.Do(func(ns.NetNS) error {
defer GinkgoRecover()

err := testutils.CmdDelWithResult(tester.targetNS.Path(), IFNAME, func() error {
err := testutils.CmdDelWithArgs(tester.args, func() error {
return cmdDel(tester.args)
})
Expect(err).NotTo(HaveOccurred())
Expand Down
8 changes: 5 additions & 3 deletions plugins/main/host-device/host-device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ var _ = Describe("base functionality", func() {
err = originalNS.Do(func(ns.NetNS) error {
defer GinkgoRecover()
var err error
resI, _, err = testutils.CmdAddWithResult(targetNS.Path(), CNI_IFNAME, []byte(conf), func() error { return cmdAdd(args) })
resI, _, err = testutils.CmdAddWithArgs(args, func() error { return cmdAdd(args) })
return err
})
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -123,7 +123,9 @@ var _ = Describe("base functionality", func() {
// Check that deleting the device moves it back and restores the name
err = originalNS.Do(func(ns.NetNS) error {
defer GinkgoRecover()
err = testutils.CmdDelWithResult(targetNS.Path(), CNI_IFNAME, func() error { return cmdDel(args) })
err = testutils.CmdDelWithArgs(args, func() error {
return cmdDel(args)
})
Expect(err).NotTo(HaveOccurred())

_, err := netlink.LinkByName(ifname)
Expand All @@ -147,7 +149,7 @@ var _ = Describe("base functionality", func() {
IfName: ifname,
StdinData: []byte(conf),
}
_, _, err := testutils.CmdAddWithResult(originalNS.Path(), ifname, []byte(conf), func() error { return cmdAdd(args) })
_, _, err := testutils.CmdAddWithArgs(args, func() error { return cmdAdd(args) })
Expect(err).To(MatchError(`specify either "device", "hwaddr" or "kernelpath"`))

})
Expand Down
6 changes: 3 additions & 3 deletions plugins/main/ipvlan/ipvlan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func ipvlanAddDelTest(conf, IFNAME string, originalNS ns.NetNS) {
err = originalNS.Do(func(ns.NetNS) error {
defer GinkgoRecover()

r, _, err := testutils.CmdAddWithResult(targetNs.Path(), IFNAME, []byte(conf), func() error {
r, _, err := testutils.CmdAddWithArgs(args, func() error {
return cmdAdd(args)
})
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -86,7 +86,7 @@ func ipvlanAddDelTest(conf, IFNAME string, originalNS ns.NetNS) {
err = originalNS.Do(func(ns.NetNS) error {
defer GinkgoRecover()

err = testutils.CmdDelWithResult(targetNs.Path(), IFNAME, func() error {
err = testutils.CmdDelWithArgs(args, func() error {
return cmdDel(args)
})
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -248,7 +248,7 @@ var _ = Describe("ipvlan Operations", func() {
err = originalNS.Do(func(ns.NetNS) error {
defer GinkgoRecover()

err = testutils.CmdDelWithResult(targetNs.Path(), IFNAME, func() error {
err = testutils.CmdDelWithArgs(args, func() error {
return cmdDel(args)
})
Expect(err).NotTo(HaveOccurred())
Expand Down
9 changes: 4 additions & 5 deletions plugins/main/loopback/loopback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ import (

var _ = Describe("Loopback", func() {
var (
networkNS ns.NetNS
containerID string
command *exec.Cmd
environ []string
networkNS ns.NetNS
command *exec.Cmd
environ []string
)

BeforeEach(func() {
Expand All @@ -44,7 +43,7 @@ var _ = Describe("Loopback", func() {
Expect(err).NotTo(HaveOccurred())

environ = []string{
fmt.Sprintf("CNI_CONTAINERID=%s", containerID),
fmt.Sprintf("CNI_CONTAINERID=%s", "dummy"),
fmt.Sprintf("CNI_NETNS=%s", networkNS.Path()),
fmt.Sprintf("CNI_IFNAME=%s", "this is ignored"),
fmt.Sprintf("CNI_ARGS=%s", "none"),
Expand Down
Loading

0 comments on commit 7312980

Please sign in to comment.