Merge pull request #142 from steveeJ/fix-loopback-result

Fix loopback result
This commit is contained in:
Stefan Junker 2016-03-03 09:03:51 +01:00
commit 36c6bcd106
2 changed files with 47 additions and 13 deletions

View File

@ -5,6 +5,7 @@ import (
"github.com/appc/cni/pkg/ns" "github.com/appc/cni/pkg/ns"
"github.com/appc/cni/pkg/skel" "github.com/appc/cni/pkg/skel"
"github.com/appc/cni/pkg/types"
"github.com/vishvananda/netlink" "github.com/vishvananda/netlink"
) )
@ -27,11 +28,29 @@ func cmdAdd(args *skel.CmdArgs) error {
return err // not tested return err // not tested
} }
return nil result := types.Result{}
return result.Print()
} }
func cmdDel(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 return nil
} }

View File

@ -10,6 +10,7 @@ import (
"github.com/appc/cni/pkg/ns" "github.com/appc/cni/pkg/ns"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
"github.com/onsi/gomega/gexec" "github.com/onsi/gomega/gexec"
) )
@ -18,30 +19,21 @@ var _ = Describe("Loopback", func() {
networkNS string networkNS string
containerID string containerID string
command *exec.Cmd command *exec.Cmd
environ []string
) )
BeforeEach(func() { BeforeEach(func() {
command = exec.Command(pathToLoPlugin) command = exec.Command(pathToLoPlugin)
environ := os.Environ()
containerID = "some-container-id" containerID = "some-container-id"
networkNS = makeNetworkNS(containerID) networkNS = makeNetworkNS(containerID)
cniEnvVars := []string{ environ = []string{
fmt.Sprintf("CNI_COMMAND=%s", "ADD"),
fmt.Sprintf("CNI_CONTAINERID=%s", containerID), fmt.Sprintf("CNI_CONTAINERID=%s", containerID),
fmt.Sprintf("CNI_NETNS=%s", networkNS), fmt.Sprintf("CNI_NETNS=%s", networkNS),
fmt.Sprintf("CNI_IFNAME=%s", "this is ignored"), fmt.Sprintf("CNI_IFNAME=%s", "this is ignored"),
fmt.Sprintf("CNI_ARGS=%s", "none"), fmt.Sprintf("CNI_ARGS=%s", "none"),
fmt.Sprintf("CNI_PATH=%s", "/some/test/path"), 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") command.Stdin = strings.NewReader("this doesn't matter")
}) })
@ -51,9 +43,12 @@ var _ = Describe("Loopback", func() {
Context("when given a network namespace", func() { Context("when given a network namespace", func() {
It("sets the lo device to UP", 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) session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Eventually(session).Should(gbytes.Say(`{.*}`))
Eventually(session).Should(gexec.Exit(0)) Eventually(session).Should(gexec.Exit(0))
var lo *net.Interface var lo *net.Interface
@ -66,5 +61,25 @@ var _ = Describe("Loopback", func() {
Expect(lo.Flags & net.FlagUp).To(Equal(net.FlagUp)) 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))
})
}) })
}) })