diff --git a/plugins/main/loopback/loopback.go b/plugins/main/loopback/loopback.go index d0e96462..b4e06bd9 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,11 +28,29 @@ func cmdAdd(args *skel.CmdArgs) error { return err // not tested } - return nil + result := types.Result{} + return result.Print() } 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 } diff --git a/plugins/main/loopback/loopback_test.go b/plugins/main/loopback/loopback_test.go index 9ae1198d..9436e9a9 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" ) @@ -18,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") }) @@ -51,9 +43,12 @@ 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()) + Eventually(session).Should(gbytes.Say(`{.*}`)) Eventually(session).Should(gexec.Exit(0)) var lo *net.Interface @@ -66,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)) + }) }) })