| 1 | // Copyright 2018 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 | package integration_test |
| 15 | |
| 16 | import ( |
| 17 | "bytes" |
| 18 | "fmt" |
| 19 | "io" |
| 20 | "log" |
| 21 | "math/rand" |
| 22 | "net" |
| 23 | "os" |
| 24 | "os/exec" |
| 25 | "path/filepath" |
| 26 | "regexp" |
| 27 | "strconv" |
| 28 | "strings" |
| 29 | "time" |
| 30 | |
| 31 | . "github.com/onsi/ginkgo/v2" |
| 32 | . "github.com/onsi/gomega" |
| 33 | "github.com/onsi/gomega/gbytes" |
| 34 | "github.com/onsi/gomega/gexec" |
| 35 | ) |
| 36 | |
| 37 | var _ = Describe("Basic PTP using cnitool", func() { |
| 38 | var ( |
| 39 | cnitoolBin string |
| 40 | cniPath string |
| 41 | ) |
| 42 | |
| 43 | BeforeEach(func() { |
| 44 | var err error |
| 45 | cniPath, err = filepath.Abs("../bin") |
| 46 | Expect(err).NotTo(HaveOccurred()) |
| 47 | cnitoolBin, err = exec.LookPath("cnitool") |
| 48 | Expect(err).NotTo(HaveOccurred(), "expected to find cnitool in your PATH") |
| 49 | }) |
| 50 | |
| 51 | Context("basic cases", func() { |
| 52 | var ( |
| 53 | env TestEnv |
| 54 | hostNS Namespace |
| 55 | contNS Namespace |
| 56 | ) |
| 57 | |
| 58 | BeforeEach(func() { |
| 59 | var err error |
| 60 | |
| 61 | netConfPath, err := filepath.Abs("./testdata") |
| 62 | Expect(err).NotTo(HaveOccurred()) |
| 63 | |
| 64 | // Flush ipam stores to avoid conflicts |
| 65 | err = os.RemoveAll("/tmp/chained-ptp-bandwidth-test") |
| 66 | Expect(err).NotTo(HaveOccurred()) |
| 67 | |
| 68 | err = os.RemoveAll("/tmp/basic-ptp-test") |
| 69 | Expect(err).NotTo(HaveOccurred()) |
| 70 | |
| 71 | env = TestEnv([]string{ |
| 72 | "CNI_PATH=" + cniPath, |
| 73 | "NETCONFPATH=" + netConfPath, |
| 74 | "PATH=" + os.Getenv("PATH"), |
| 75 | }) |
| 76 | |
| 77 | hostNS = Namespace(fmt.Sprintf("cni-test-host-%x", rand.Int31())) |
| 78 | hostNS.Add() |
| 79 | |
| 80 | contNS = Namespace(fmt.Sprintf("cni-test-cont-%x", rand.Int31())) |
| 81 | contNS.Add() |
| 82 | }) |
| 83 | |
| 84 | AfterEach(func() { |
| 85 | contNS.Del() |
| 86 | hostNS.Del() |
| 87 | }) |
| 88 | |
| 89 | basicAssertion := func(netName, expectedIPPrefix string) { |
| 90 | env.runInNS(hostNS, cnitoolBin, "add", netName, contNS.LongName()) |
| 91 | |
| 92 | addrOutput := env.runInNS(contNS, "ip", "addr") |
| 93 | |
| 94 | Expect(addrOutput).To(ContainSubstring(expectedIPPrefix)) |
| 95 | |
| 96 | env.runInNS(hostNS, cnitoolBin, "del", netName, contNS.LongName()) |
| 97 | } |
| 98 | |
| 99 | It("supports basic network add and del operations", func() { |
| 100 | basicAssertion("basic-ptp", "10.1.2.") |
| 101 | }) |
| 102 | |
| 103 | It("supports add and del with ptp + bandwidth", func() { |
| 104 | basicAssertion("chained-ptp-bandwidth", "10.9.2.") |
| 105 | }) |
| 106 | }) |
| 107 | |
| 108 | Context("when the bandwidth plugin is chained with a plugin that returns multiple adapters", func() { |
| 109 | var ( |
| 110 | hostNS Namespace |
| 111 | contNS1 Namespace |
| 112 | contNS2 Namespace |
| 113 | basicBridgeEnv TestEnv |
| 114 | chainedBridgeBandwidthEnv TestEnv |
| 115 | chainedBridgeBandwidthSession, basicBridgeSession *gexec.Session |
| 116 | ) |
| 117 | |
| 118 | BeforeEach(func() { |
| 119 | hostNS = Namespace(fmt.Sprintf("cni-test-host-%x", rand.Int31())) |
| 120 | hostNS.Add() |
| 121 | |
| 122 | contNS1 = Namespace(fmt.Sprintf("cni-test-cont1-%x", rand.Int31())) |
| 123 | contNS1.Add() |
| 124 | |
| 125 | contNS2 = Namespace(fmt.Sprintf("cni-test-cont2-%x", rand.Int31())) |
| 126 | contNS2.Add() |
| 127 | |
| 128 | basicBridgeNetConfPath, err := filepath.Abs("./testdata/basic-bridge") |
| 129 | Expect(err).NotTo(HaveOccurred()) |
| 130 | |
| 131 | basicBridgeEnv = TestEnv([]string{ |
| 132 | "CNI_PATH=" + cniPath, |
| 133 | "NETCONFPATH=" + basicBridgeNetConfPath, |
| 134 | "PATH=" + os.Getenv("PATH"), |
| 135 | }) |
| 136 | |
| 137 | chainedBridgeBandwidthNetConfPath, err := filepath.Abs("./testdata/chained-bridge-bandwidth") |
| 138 | Expect(err).NotTo(HaveOccurred()) |
| 139 | |
| 140 | chainedBridgeBandwidthEnv = TestEnv([]string{ |
| 141 | "CNI_PATH=" + cniPath, |
| 142 | "NETCONFPATH=" + chainedBridgeBandwidthNetConfPath, |
| 143 | "PATH=" + os.Getenv("PATH"), |
| 144 | }) |
| 145 | }) |
| 146 | |
| 147 | AfterEach(func() { |
| 148 | if chainedBridgeBandwidthSession != nil { |
| 149 | chainedBridgeBandwidthSession.Kill() |
| 150 | } |
| 151 | if basicBridgeSession != nil { |
| 152 | basicBridgeSession.Kill() |
| 153 | } |
| 154 | |
| 155 | chainedBridgeBandwidthEnv.runInNS(hostNS, cnitoolBin, "del", "network-chain-test", contNS1.LongName()) |
| 156 | basicBridgeEnv.runInNS(hostNS, cnitoolBin, "del", "network-chain-test", contNS2.LongName()) |
| 157 | |
| 158 | contNS1.Del() |
| 159 | contNS2.Del() |
| 160 | hostNS.Del() |
| 161 | }) |
| 162 | |
| 163 | It("limits traffic only on the restricted bandwidth veth device", func() { |
| 164 | ipRegexp := regexp.MustCompile(`10\.1[12]\.2\.\d{1,3}`) |
| 165 | |
| 166 | By(fmt.Sprintf("adding %s to %s\n\n", "chained-bridge-bandwidth", contNS1.ShortName())) |
| 167 | chainedBridgeBandwidthEnv.runInNS(hostNS, cnitoolBin, "add", "network-chain-test", contNS1.LongName()) |
| 168 | chainedBridgeIP := ipRegexp.FindString(chainedBridgeBandwidthEnv.runInNS(contNS1, "ip", "addr")) |
| 169 | Expect(chainedBridgeIP).To(ContainSubstring("10.12.2.")) |
| 170 | |
| 171 | By(fmt.Sprintf("adding %s to %s\n\n", "basic-bridge", contNS2.ShortName())) |
| 172 | basicBridgeEnv.runInNS(hostNS, cnitoolBin, "add", "network-chain-test", contNS2.LongName()) |
| 173 | basicBridgeIP := ipRegexp.FindString(basicBridgeEnv.runInNS(contNS2, "ip", "addr")) |
| 174 | Expect(basicBridgeIP).To(ContainSubstring("10.11.2.")) |
| 175 | |
| 176 | var chainedBridgeBandwidthPort, basicBridgePort int |
| 177 | |
| 178 | By(fmt.Sprintf("starting echo server in %s\n\n", contNS1.ShortName())) |
| 179 | chainedBridgeBandwidthPort, chainedBridgeBandwidthSession = startEchoServerInNamespace(contNS1) |
| 180 | |
| 181 | By(fmt.Sprintf("starting echo server in %s\n\n", contNS2.ShortName())) |
| 182 | basicBridgePort, basicBridgeSession = startEchoServerInNamespace(contNS2) |
| 183 | |
| 184 | packetInBytes := 20000 // The shaper needs to 'warm'. Send enough to cause it to throttle, |
| 185 | // balanced by run time. |
| 186 | |
| 187 | By(fmt.Sprintf("sending tcp traffic to the chained, bridged, traffic shaped container on ip address '%s:%d'\n\n", chainedBridgeIP, chainedBridgeBandwidthPort)) |
| 188 | start := time.Now() |
| 189 | makeTCPClientInNS(hostNS.ShortName(), chainedBridgeIP, chainedBridgeBandwidthPort, packetInBytes) |
| 190 | runtimeWithLimit := time.Since(start) |
| 191 | log.Printf("Runtime with qos limit %.2f seconds", runtimeWithLimit.Seconds()) |
| 192 | |
| 193 | By(fmt.Sprintf("sending tcp traffic to the basic bridged container on ip address '%s:%d'\n\n", basicBridgeIP, basicBridgePort)) |
| 194 | start = time.Now() |
| 195 | makeTCPClientInNS(hostNS.ShortName(), basicBridgeIP, basicBridgePort, packetInBytes) |
| 196 | runtimeWithoutLimit := time.Since(start) |
| 197 | log.Printf("Runtime without qos limit %.2f seconds", runtimeWithoutLimit.Seconds()) |
| 198 | |
| 199 | Expect(runtimeWithLimit).To(BeNumerically(">", runtimeWithoutLimit+1000*time.Millisecond)) |
| 200 | }) |
| 201 | }) |
| 202 | }) |
| 203 | |
| 204 | type TestEnv []string |
| 205 | |
| 206 | func (e TestEnv) run(bin string, args ...string) string { |
| 207 | cmd := exec.Command(bin, args...) |
| 208 | cmd.Env = e |
| 209 | session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter) |
| 210 | Expect(err).NotTo(HaveOccurred()) |
| 211 | Eventually(session, "5s").Should(gexec.Exit(0)) |
| 212 | return string(session.Out.Contents()) |
| 213 | } |
| 214 | |
| 215 | func (e TestEnv) runInNS(nsShortName Namespace, bin string, args ...string) string { |
| 216 | a := append([]string{"netns", "exec", string(nsShortName), bin}, args...) |
| 217 | return e.run("ip", a...) |
| 218 | } |
| 219 | |
| 220 | type Namespace string |
| 221 | |
| 222 | func (n Namespace) LongName() string { |
| 223 | return fmt.Sprintf("/var/run/netns/%s", n) |
| 224 | } |
| 225 | |
| 226 | func (n Namespace) ShortName() string { |
| 227 | return string(n) |
| 228 | } |
| 229 | |
| 230 | func (n Namespace) Add() { |
| 231 | (TestEnv{}).run("ip", "netns", "add", string(n)) |
| 232 | } |
| 233 | |
| 234 | func (n Namespace) Del() { |
| 235 | (TestEnv{}).run("ip", "netns", "del", string(n)) |
| 236 | } |
| 237 | |
| 238 | func makeTCPClientInNS(netns string, address string, port int, numBytes int) { |
| 239 | payload := bytes.Repeat([]byte{'a'}, numBytes) |
| 240 | message := string(payload) |
| 241 | |
| 242 | var cmd *exec.Cmd |
| 243 | if netns != "" { |
| 244 | netns = filepath.Base(netns) |
| 245 | cmd = exec.Command("ip", "netns", "exec", netns, echoClientBinaryPath, "--target", fmt.Sprintf("%s:%d", address, port), "--message", message) |
| 246 | } else { |
| 247 | cmd = exec.Command(echoClientBinaryPath, "--target", fmt.Sprintf("%s:%d", address, port), "--message", message) |
| 248 | } |
| 249 | cmd.Stdin = bytes.NewBuffer([]byte(message)) |
| 250 | cmd.Stderr = GinkgoWriter |
| 251 | out, err := cmd.Output() |
| 252 | |
| 253 | Expect(err).NotTo(HaveOccurred()) |
| 254 | Expect(string(out)).To(Equal(message)) |
| 255 | } |
| 256 | |
| 257 | func startEchoServerInNamespace(netNS Namespace) (int, *gexec.Session) { |
| 258 | session, err := startInNetNS(echoServerBinaryPath, netNS) |
| 259 | Expect(err).NotTo(HaveOccurred()) |
| 260 | |
| 261 | // wait for it to print it's address on stdout |
| 262 | Eventually(session.Out).Should(gbytes.Say("\n")) |
| 263 | _, portString, err := net.SplitHostPort(strings.TrimSpace(string(session.Out.Contents()))) |
| 264 | Expect(err).NotTo(HaveOccurred()) |
| 265 | |
| 266 | port, err := strconv.Atoi(portString) |
| 267 | Expect(err).NotTo(HaveOccurred()) |
| 268 | |
| 269 | go func() { |
| 270 | // print out echoserver output to ginkgo to capture any errors that might be occurring. |
| 271 | io.Copy(GinkgoWriter, io.MultiReader(session.Out, session.Err)) |
| 272 | }() |
| 273 | |
| 274 | return port, session |
| 275 | } |
| 276 | |
| 277 | func startInNetNS(binPath string, namespace Namespace) (*gexec.Session, error) { |
| 278 | cmd := exec.Command("ip", "netns", "exec", namespace.ShortName(), binPath) |
| 279 | return gexec.Start(cmd, GinkgoWriter, GinkgoWriter) |
| 280 | } |