|
| 1 | +// Copyright 2017 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 |
| 16 | + |
| 17 | +import ( |
| 18 | + "github.com/containernetworking/cni/pkg/skel" |
| 19 | + "github.com/containernetworking/cni/pkg/types/current" |
| 20 | + "github.com/containernetworking/plugins/pkg/ns" |
| 21 | + "github.com/containernetworking/plugins/pkg/testutils" |
| 22 | + |
| 23 | + "github.com/vishvananda/netlink" |
| 24 | + |
| 25 | + . "github.com/onsi/ginkgo" |
| 26 | + . "github.com/onsi/gomega" |
| 27 | +) |
| 28 | + |
| 29 | +var _ = Describe("tuning plugin", func() { |
| 30 | + var originalNS ns.NetNS |
| 31 | + const IFNAME string = "dummy0" |
| 32 | + |
| 33 | + BeforeEach(func() { |
| 34 | + // Create a new NetNS so we don't modify the host |
| 35 | + var err error |
| 36 | + originalNS, err = ns.NewNS() |
| 37 | + Expect(err).NotTo(HaveOccurred()) |
| 38 | + |
| 39 | + err = originalNS.Do(func(ns.NetNS) error { |
| 40 | + defer GinkgoRecover() |
| 41 | + |
| 42 | + err = netlink.LinkAdd(&netlink.Dummy{ |
| 43 | + LinkAttrs: netlink.LinkAttrs{ |
| 44 | + Name: IFNAME, |
| 45 | + }, |
| 46 | + }) |
| 47 | + Expect(err).NotTo(HaveOccurred()) |
| 48 | + _, err = netlink.LinkByName(IFNAME) |
| 49 | + Expect(err).NotTo(HaveOccurred()) |
| 50 | + return nil |
| 51 | + }) |
| 52 | + Expect(err).NotTo(HaveOccurred()) |
| 53 | + }) |
| 54 | + |
| 55 | + AfterEach(func() { |
| 56 | + Expect(originalNS.Close()).To(Succeed()) |
| 57 | + }) |
| 58 | + |
| 59 | + It("passes prevResult through unchanged", func() { |
| 60 | + conf := []byte(`{ |
| 61 | + "name": "test", |
| 62 | + "type": "tuning", |
| 63 | + "cniVersion": "0.3.1", |
| 64 | + "sysctl": { |
| 65 | + "net.ipv4.conf.all.log_martians": "1" |
| 66 | + }, |
| 67 | + "prevResult": { |
| 68 | + "interfaces": [ |
| 69 | + {"name": "dummy0", "sandbox":"netns"} |
| 70 | + ], |
| 71 | + "ips": [ |
| 72 | + { |
| 73 | + "version": "4", |
| 74 | + "address": "10.0.0.2/24", |
| 75 | + "gateway": "10.0.0.1", |
| 76 | + "interface": 0 |
| 77 | + } |
| 78 | + ] |
| 79 | + } |
| 80 | +}`) |
| 81 | + |
| 82 | + targetNs, err := ns.NewNS() |
| 83 | + Expect(err).NotTo(HaveOccurred()) |
| 84 | + defer targetNs.Close() |
| 85 | + |
| 86 | + args := &skel.CmdArgs{ |
| 87 | + ContainerID: "dummy", |
| 88 | + Netns: targetNs.Path(), |
| 89 | + IfName: IFNAME, |
| 90 | + StdinData: conf, |
| 91 | + } |
| 92 | + |
| 93 | + err = originalNS.Do(func(ns.NetNS) error { |
| 94 | + defer GinkgoRecover() |
| 95 | + |
| 96 | + r, _, err := testutils.CmdAddWithResult(targetNs.Path(), IFNAME, []byte(conf), func() error { |
| 97 | + return cmdAdd(args) |
| 98 | + }) |
| 99 | + Expect(err).NotTo(HaveOccurred()) |
| 100 | + |
| 101 | + result, err := current.GetResult(r) |
| 102 | + Expect(err).NotTo(HaveOccurred()) |
| 103 | + |
| 104 | + Expect(len(result.Interfaces)).To(Equal(1)) |
| 105 | + Expect(result.Interfaces[0].Name).To(Equal(IFNAME)) |
| 106 | + Expect(len(result.IPs)).To(Equal(1)) |
| 107 | + Expect(result.IPs[0].Address.String()).To(Equal("10.0.0.2/24")) |
| 108 | + return nil |
| 109 | + }) |
| 110 | + Expect(err).NotTo(HaveOccurred()) |
| 111 | + }) |
| 112 | +}) |
0 commit comments