| 1 | // Copyright 2016 CNI authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package main_test |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "net" |
| 20 | "os/exec" |
| 21 | "strings" |
| 22 | |
| 23 | . "github.com/onsi/ginkgo/v2" |
| 24 | . "github.com/onsi/gomega" |
| 25 | "github.com/onsi/gomega/gbytes" |
| 26 | "github.com/onsi/gomega/gexec" |
| 27 | |
| 28 | "github.com/containernetworking/plugins/pkg/ns" |
| 29 | "github.com/containernetworking/plugins/pkg/testutils" |
| 30 | ) |
| 31 | |
| 32 | func generateConfig(cniVersion string) *strings.Reader { |
| 33 | return strings.NewReader(fmt.Sprintf(`{ "name": "loopback-test", "cniVersion": "%s" }`, cniVersion)) |
| 34 | } |
| 35 | |
| 36 | var _ = Describe("Loopback", func() { |
| 37 | var ( |
| 38 | networkNS ns.NetNS |
| 39 | command *exec.Cmd |
| 40 | environ []string |
| 41 | ) |
| 42 | |
| 43 | BeforeEach(func() { |
| 44 | command = exec.Command(pathToLoPlugin) |
| 45 | |
| 46 | var err error |
| 47 | networkNS, err = testutils.NewNS() |
| 48 | Expect(err).NotTo(HaveOccurred()) |
| 49 | |
| 50 | environ = []string{ |
| 51 | fmt.Sprintf("CNI_CONTAINERID=%s", "dummy"), |
| 52 | fmt.Sprintf("CNI_NETNS=%s", networkNS.Path()), |
| 53 | fmt.Sprintf("CNI_IFNAME=%s", "lo"), |
| 54 | fmt.Sprintf("CNI_ARGS=%s", "none"), |
| 55 | fmt.Sprintf("CNI_PATH=%s", "/some/test/path"), |
| 56 | } |
| 57 | }) |
| 58 | |
| 59 | AfterEach(func() { |
| 60 | Expect(networkNS.Close()).To(Succeed()) |
| 61 | Expect(testutils.UnmountNS(networkNS)).To(Succeed()) |
| 62 | }) |
| 63 | |
| 64 | for _, ver := range testutils.AllSpecVersions { |
| 65 | // Redefine ver inside for scope so real value is picked up by each dynamically defined It() |
| 66 | // See Gingkgo's "Patterns for dynamically generating tests" documentation. |
| 67 | ver := ver |
| 68 | |
| 69 | Context("when given a network namespace", func() { |
| 70 | It(fmt.Sprintf("[%s] sets the lo device to UP", ver), func() { |
| 71 | command.Stdin = generateConfig(ver) |
| 72 | command.Env = append(environ, fmt.Sprintf("CNI_COMMAND=%s", "ADD")) |
| 73 | |
| 74 | session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter) |
| 75 | Expect(err).NotTo(HaveOccurred()) |
| 76 | |
| 77 | // "(?s)" turns on the "s" flag, making "." match newlines too. |
| 78 | Eventually(session).Should(gbytes.Say(`(?s){.*}`)) |
| 79 | Eventually(session).Should(gexec.Exit(0)) |
| 80 | |
| 81 | var lo *net.Interface |
| 82 | err = networkNS.Do(func(ns.NetNS) error { |
| 83 | var err error |
| 84 | lo, err = net.InterfaceByName("lo") |
| 85 | return err |
| 86 | }) |
| 87 | Expect(err).NotTo(HaveOccurred()) |
| 88 | |
| 89 | Expect(lo.Flags & net.FlagUp).To(Equal(net.FlagUp)) |
| 90 | }) |
| 91 | |
| 92 | It(fmt.Sprintf("[%s] sets the lo device to DOWN", ver), func() { |
| 93 | command.Stdin = generateConfig(ver) |
| 94 | command.Env = append(environ, fmt.Sprintf("CNI_COMMAND=%s", "DEL")) |
| 95 | |
| 96 | session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter) |
| 97 | Expect(err).NotTo(HaveOccurred()) |
| 98 | |
| 99 | Eventually(session).Should(gbytes.Say(``)) |
| 100 | Eventually(session).Should(gexec.Exit(0)) |
| 101 | |
| 102 | var lo *net.Interface |
| 103 | err = networkNS.Do(func(ns.NetNS) error { |
| 104 | var err error |
| 105 | lo, err = net.InterfaceByName("lo") |
| 106 | return err |
| 107 | }) |
| 108 | Expect(err).NotTo(HaveOccurred()) |
| 109 | |
| 110 | Expect(lo.Flags & net.FlagUp).NotTo(Equal(net.FlagUp)) |
| 111 | }) |
| 112 | }) |
| 113 | } |
| 114 | }) |