From e282f6939d8cb5dff2ede95cd1fc849b9d55c03e Mon Sep 17 00:00:00 2001 From: Stefan Junker Date: Wed, 2 Mar 2016 12:53:02 +0100 Subject: [PATCH 1/4] plugins/loopback: return empty result This is needed to conform to the specification and allow successful unmarshalling in the invoker. --- plugins/main/loopback/loopback.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plugins/main/loopback/loopback.go b/plugins/main/loopback/loopback.go index d0e96462..eb02eb19 100644 --- a/plugins/main/loopback/loopback.go +++ b/plugins/main/loopback/loopback.go @@ -5,6 +5,7 @@ import ( "github.com/appc/cni/pkg/ns" "github.com/appc/cni/pkg/skel" + "github.com/appc/cni/pkg/types" "github.com/vishvananda/netlink" ) @@ -27,7 +28,8 @@ func cmdAdd(args *skel.CmdArgs) error { return err // not tested } - return nil + result := types.Result{} + return result.Print() } func cmdDel(args *skel.CmdArgs) error { From 52be8aa6155854d053e272a080103ebec17dae28 Mon Sep 17 00:00:00 2001 From: Stefan Junker Date: Wed, 2 Mar 2016 12:54:43 +0100 Subject: [PATCH 2/4] plugins/loopback: take lo down on CmdDel --- plugins/main/loopback/loopback.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/plugins/main/loopback/loopback.go b/plugins/main/loopback/loopback.go index eb02eb19..b4e06bd9 100644 --- a/plugins/main/loopback/loopback.go +++ b/plugins/main/loopback/loopback.go @@ -33,7 +33,24 @@ func cmdAdd(args *skel.CmdArgs) error { } func cmdDel(args *skel.CmdArgs) error { - // del does nothing, we're going to destroy the device anyway + args.IfName = "lo" // ignore config, this only works for loopback + err := ns.WithNetNSPath(args.Netns, false, func(hostNS *os.File) error { + link, err := netlink.LinkByName(args.IfName) + if err != nil { + return err // not tested + } + + err = netlink.LinkSetDown(link) + if err != nil { + return err // not tested + } + + return nil + }) + if err != nil { + return err // not tested + } + return nil } From 80ab40470c3b3230840e34b12ddc2830893d52ce Mon Sep 17 00:00:00 2001 From: Stefan Junker Date: Wed, 2 Mar 2016 15:22:16 +0100 Subject: [PATCH 3/4] tests: loopback stdout must be JSON --- plugins/main/loopback/loopback_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plugins/main/loopback/loopback_test.go b/plugins/main/loopback/loopback_test.go index 9ae1198d..cf10e1a6 100644 --- a/plugins/main/loopback/loopback_test.go +++ b/plugins/main/loopback/loopback_test.go @@ -10,6 +10,7 @@ import ( "github.com/appc/cni/pkg/ns" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + "github.com/onsi/gomega/gbytes" "github.com/onsi/gomega/gexec" ) @@ -54,6 +55,7 @@ var _ = Describe("Loopback", func() { session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter) Expect(err).NotTo(HaveOccurred()) + Eventually(session).Should(gbytes.Say(`{.*}`)) Eventually(session).Should(gexec.Exit(0)) var lo *net.Interface From 4a5426b77a6496583b4908aca5f7375b2a0dbe7f Mon Sep 17 00:00:00 2001 From: Stefan Junker Date: Wed, 2 Mar 2016 17:56:44 +0100 Subject: [PATCH 4/4] tests: loopback DEL --- plugins/main/loopback/loopback_test.go | 35 ++++++++++++++++++-------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/plugins/main/loopback/loopback_test.go b/plugins/main/loopback/loopback_test.go index cf10e1a6..9436e9a9 100644 --- a/plugins/main/loopback/loopback_test.go +++ b/plugins/main/loopback/loopback_test.go @@ -19,30 +19,21 @@ var _ = Describe("Loopback", func() { networkNS string containerID string command *exec.Cmd + environ []string ) BeforeEach(func() { command = exec.Command(pathToLoPlugin) - - environ := os.Environ() - containerID = "some-container-id" networkNS = makeNetworkNS(containerID) - cniEnvVars := []string{ - fmt.Sprintf("CNI_COMMAND=%s", "ADD"), + environ = []string{ fmt.Sprintf("CNI_CONTAINERID=%s", containerID), fmt.Sprintf("CNI_NETNS=%s", networkNS), fmt.Sprintf("CNI_IFNAME=%s", "this is ignored"), fmt.Sprintf("CNI_ARGS=%s", "none"), fmt.Sprintf("CNI_PATH=%s", "/some/test/path"), } - - for _, v := range cniEnvVars { - environ = append(environ, v) - } - - command.Env = environ command.Stdin = strings.NewReader("this doesn't matter") }) @@ -52,6 +43,8 @@ var _ = Describe("Loopback", func() { Context("when given a network namespace", func() { It("sets the lo device to UP", func() { + command.Env = append(environ, fmt.Sprintf("CNI_COMMAND=%s", "ADD")) + session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter) Expect(err).NotTo(HaveOccurred()) @@ -68,5 +61,25 @@ var _ = Describe("Loopback", func() { Expect(lo.Flags & net.FlagUp).To(Equal(net.FlagUp)) }) + + It("sets the lo device to DOWN", func() { + command.Env = append(environ, fmt.Sprintf("CNI_COMMAND=%s", "DEL")) + + session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter) + Expect(err).NotTo(HaveOccurred()) + + Eventually(session).Should(gbytes.Say(``)) + Eventually(session).Should(gexec.Exit(0)) + + var lo *net.Interface + err = ns.WithNetNSPath(networkNS, true, func(hostNS *os.File) error { + var err error + lo, err = net.InterfaceByName("lo") + return err + }) + Expect(err).NotTo(HaveOccurred()) + + Expect(lo.Flags & net.FlagUp).NotTo(Equal(net.FlagUp)) + }) }) })