pkg/ip tests: cover SetupVeth and DelLinkByName*
authorStefan Junker <mail@stefanjunker.de>
Wed, 29 Jun 2016 02:52:08 +0000 (19:52 -0700)
committerStefan Junker <mail@stefanjunker.de>
Fri, 22 Jul 2016 22:34:53 +0000 (15:34 -0700)
pkg/ip/ip_suite_test.go [new file with mode: 0644]
pkg/ip/link_test.go [new file with mode: 0644]
test

diff --git a/pkg/ip/ip_suite_test.go b/pkg/ip/ip_suite_test.go
new file mode 100644 (file)
index 0000000..3fdd57e
--- /dev/null
@@ -0,0 +1,27 @@
+// Copyright 2016 CNI authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package ip_test
+
+import (
+       . "github.com/onsi/ginkgo"
+       . "github.com/onsi/gomega"
+
+       "testing"
+)
+
+func TestIp(t *testing.T) {
+       RegisterFailHandler(Fail)
+       RunSpecs(t, "Ip Suite")
+}
diff --git a/pkg/ip/link_test.go b/pkg/ip/link_test.go
new file mode 100644 (file)
index 0000000..cdf861a
--- /dev/null
@@ -0,0 +1,132 @@
+// Copyright 2016 CNI authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package ip_test
+
+import (
+       "fmt"
+       "net"
+
+       . "github.com/onsi/ginkgo"
+       . "github.com/onsi/gomega"
+
+       "github.com/containernetworking/cni/pkg/ip"
+       "github.com/containernetworking/cni/pkg/ns"
+
+       "github.com/vishvananda/netlink"
+       "github.com/vishvananda/netlink/nl"
+)
+
+var _ = Describe("Link", func() {
+       const (
+               ifaceFormatString string = "i%d"
+               mtu               int    = 1400
+       )
+       var (
+               hostNetNS         ns.NetNS
+               containerNetNS    ns.NetNS
+               ifaceCounter      int = 0
+               hostVethName      string
+               containerVethName string
+       )
+
+       BeforeEach(func() {
+               var err error
+
+               hostNetNS, err = ns.NewNS()
+               Expect(err).NotTo(HaveOccurred())
+
+               containerNetNS, err = ns.NewNS()
+               Expect(err).NotTo(HaveOccurred())
+
+               _ = containerNetNS.Do(func(ns.NetNS) error {
+                       defer GinkgoRecover()
+
+                       hostVeth, containerVeth, err := ip.SetupVeth(fmt.Sprintf(ifaceFormatString, ifaceCounter), mtu, hostNetNS)
+                       if err != nil {
+                               return err
+                       }
+                       Expect(err).NotTo(HaveOccurred())
+
+                       hostVethName = hostVeth.Attrs().Name
+                       containerVethName = containerVeth.Attrs().Name
+
+                       return nil
+               })
+       })
+
+       AfterEach(func() {
+               Expect(containerNetNS.Close()).To(Succeed())
+               Expect(hostNetNS.Close()).To(Succeed())
+               ifaceCounter++
+       })
+
+       It("SetupVeth must put the veth endpoints into the separate namespaces", func() {
+               _ = containerNetNS.Do(func(ns.NetNS) error {
+                       defer GinkgoRecover()
+
+                       _, err := netlink.LinkByName(containerVethName)
+                       Expect(err).NotTo(HaveOccurred())
+
+                       return nil
+               })
+
+               _ = hostNetNS.Do(func(ns.NetNS) error {
+                       defer GinkgoRecover()
+
+                       _, err := netlink.LinkByName(hostVethName)
+                       Expect(err).NotTo(HaveOccurred())
+
+                       return nil
+               })
+       })
+
+       It("DelLinkByName must delete the veth endpoints", func() {
+               _ = containerNetNS.Do(func(ns.NetNS) error {
+                       defer GinkgoRecover()
+
+                       // this will delete the host endpoint too
+                       err := ip.DelLinkByName(containerVethName)
+                       Expect(err).NotTo(HaveOccurred())
+
+                       _, err = netlink.LinkByName(containerVethName)
+                       Expect(err).To(HaveOccurred())
+
+                       return nil
+               })
+
+               _ = hostNetNS.Do(func(ns.NetNS) error {
+                       defer GinkgoRecover()
+
+                       _, err := netlink.LinkByName(hostVethName)
+                       Expect(err).To(HaveOccurred())
+
+                       return nil
+               })
+       })
+
+       It("DelLinkByNameAddr must throw an error for configured interfaces", func() {
+               _ = containerNetNS.Do(func(ns.NetNS) error {
+                       defer GinkgoRecover()
+
+                       // this will delete the host endpoint too
+                       addr, err := ip.DelLinkByNameAddr(containerVethName, nl.FAMILY_V4)
+                       Expect(err).To(HaveOccurred())
+
+                       var ipNetNil *net.IPNet
+                       Expect(addr).To(Equal(ipNetNil))
+                       return nil
+               })
+       })
+})
diff --git a/test b/test
index 9cd43e0..2f1a4b2 100755 (executable)
--- a/test
+++ b/test
@@ -11,8 +11,8 @@ set -e
 
 source ./build
 
-TESTABLE="libcni plugins/ipam/dhcp plugins/ipam/host-local plugins/main/loopback pkg/invoke pkg/ns pkg/skel pkg/types pkg/utils plugins/main/ipvlan plugins/main/macvlan plugins/main/bridge plugins/main/ptp plugins/test/noop"
-FORMATTABLE="$TESTABLE pkg/ip pkg/testutils plugins/meta/flannel plugins/meta/tuning"
+TESTABLE="libcni plugins/ipam/dhcp plugins/ipam/host-local plugins/main/loopback pkg/invoke pkg/ns pkg/skel pkg/types pkg/utils plugins/main/ipvlan plugins/main/macvlan plugins/main/bridge plugins/main/ptp plugins/test/noop pkg/utils/hwaddr pkg/ip"
+FORMATTABLE="$TESTABLE pkg/testutils plugins/meta/flannel plugins/meta/tuning"
 
 # user has not provided PKG override
 if [ -z "$PKG" ]; then