| 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 mainscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/sample`/ |
| 16 | |
| 17 | import ( |
| 18 | "fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/" |
| 19 | |
| 20 | . "github.com/onsi/ginkgo/v2scip-go gomod github.com/onsi/ginkgo/v2 v2.25.1 `github.com/onsi/ginkgo/v2`/" |
| 21 | . "github.com/onsi/gomegascip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega`/" |
| 22 | |
| 23 | "github.com/containernetworking/cni/pkg/skelscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/" |
| 24 | "github.com/containernetworking/plugins/pkg/nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/" |
| 25 | "github.com/containernetworking/plugins/pkg/testutilsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/testutils`/" |
| 26 | ) |
| 27 | |
| 28 | var _ = Describescip-go gomod github.com/onsi/ginkgo/v2 v2.25.1 `github.com/onsi/ginkgo/v2`/Describe().("sample test", func() { |
| 29 | var targetNsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/sample`/_:targetNs. nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/.NetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS# |
| 30 | |
| 31 | BeforeEachscip-go gomod github.com/onsi/ginkgo/v2 v2.25.1 `github.com/onsi/ginkgo/v2`/BeforeEach().(func() { |
| 32 | var errscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/sample`/_:err. error |
| 33 | targetNsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/sample`/_:targetNs., errscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/sample`/_:err. = testutilsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/testutils`/.NewNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/testutils`/NewNS().() |
| 34 | Expectscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega`/Expect().(errscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/sample`/_:err.).NotToscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega/types`/Assertion#NotTo().(HaveOccurredscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega`/HaveOccurred().()) |
| 35 | }) |
| 36 | |
| 37 | AfterEachscip-go gomod github.com/onsi/ginkgo/v2 v2.25.1 `github.com/onsi/ginkgo/v2`/AfterEach().(func() { |
| 38 | targetNsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/sample`/_:targetNs..Closescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Close.() |
| 39 | }) |
| 40 | |
| 41 | Itscip-go gomod github.com/onsi/ginkgo/v2 v2.25.1 `github.com/onsi/ginkgo/v2`/It().("Works with a 0.3.0 config", func() { |
| 42 | ifname := "eth0" |
| 43 | conf := `{ |
| 44 | "cniVersion": "0.3.0", |
| 45 | "name": "cni-plugin-sample-test", |
| 46 | "type": "sample", |
| 47 | "anotherAwesomeArg": "awesome", |
| 48 | "prevResult": { |
| 49 | "cniVersion": "0.3.0", |
| 50 | "interfaces": [ |
| 51 | { |
| 52 | "name": "%s", |
| 53 | "sandbox": "%s" |
| 54 | } |
| 55 | ], |
| 56 | "ips": [ |
| 57 | { |
| 58 | "version": "4", |
| 59 | "address": "10.0.0.2/24", |
| 60 | "gateway": "10.0.0.1", |
| 61 | "interface": 0 |
| 62 | } |
| 63 | ], |
| 64 | "routes": [] |
| 65 | } |
| 66 | }` |
| 67 | conf = fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Sprintf().(conf, ifname, targetNsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/sample`/_:targetNs..Pathscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Path.()) |
| 68 | args := &skelscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/.CmdArgsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#{ |
| 69 | ContainerIDscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#ContainerID.: "dummy", |
| 70 | Netnsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#Netns.: targetNsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/sample`/_:targetNs..Pathscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Path.(), |
| 71 | IfNamescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#IfName.: ifname, |
| 72 | StdinDatascip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#StdinData.: []byte(conf), |
| 73 | } |
| 74 | _, _, err := testutilsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/testutils`/.CmdAddWithArgsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/testutils`/CmdAddWithArgs().(args, func() error { return cmdAddscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/sample`/cmdAdd().(args) }) |
| 75 | Expectscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega`/Expect().(err).NotToscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega/types`/Assertion#NotTo().(HaveOccurredscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega`/HaveOccurred().()) |
| 76 | }) |
| 77 | |
| 78 | Itscip-go gomod github.com/onsi/ginkgo/v2 v2.25.1 `github.com/onsi/ginkgo/v2`/It().("fails an invalid config", func() { |
| 79 | conf := `{ |
| 80 | "cniVersion": "0.3.0", |
| 81 | "name": "cni-plugin-sample-test", |
| 82 | "type": "sample", |
| 83 | "prevResult": { |
| 84 | "interfaces": [ |
| 85 | { |
| 86 | "name": "eth0", |
| 87 | "sandbox": "/var/run/netns/test" |
| 88 | } |
| 89 | ], |
| 90 | "ips": [ |
| 91 | { |
| 92 | "version": "4", |
| 93 | "address": "10.0.0.2/24", |
| 94 | "gateway": "10.0.0.1", |
| 95 | "interface": 0 |
| 96 | } |
| 97 | ], |
| 98 | "routes": [] |
| 99 | } |
| 100 | }` |
| 101 | |
| 102 | args := &skelscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/.CmdArgsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#{ |
| 103 | ContainerIDscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#ContainerID.: "dummy", |
| 104 | Netnsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#Netns.: targetNsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/sample`/_:targetNs..Pathscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Path.(), |
| 105 | IfNamescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#IfName.: "eth0", |
| 106 | StdinDatascip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#StdinData.: []byte(conf), |
| 107 | } |
| 108 | _, _, err := testutilsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/testutils`/.CmdAddWithArgsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/testutils`/CmdAddWithArgs().(args, func() error { return cmdAddscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/sample`/cmdAdd().(args) }) |
| 109 | Expectscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega`/Expect().(err).Toscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega/types`/Assertion#To().(MatchErrorscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega`/MatchError().("anotherAwesomeArg must be specified")) |
| 110 | }) |
| 111 | |
| 112 | Itscip-go gomod github.com/onsi/ginkgo/v2 v2.25.1 `github.com/onsi/ginkgo/v2`/It().("fails with CNI spec versions that don't support plugin chaining", func() { |
| 113 | conf := `{ |
| 114 | "cniVersion": "0.2.0", |
| 115 | "name": "cni-plugin-sample-test", |
| 116 | "type": "sample", |
| 117 | "anotherAwesomeArg": "foo" |
| 118 | }` |
| 119 | |
| 120 | args := &skelscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/.CmdArgsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#{ |
| 121 | ContainerIDscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#ContainerID.: "dummy", |
| 122 | Netnsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#Netns.: targetNsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/sample`/_:targetNs..Pathscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#Path.(), |
| 123 | IfNamescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#IfName.: "eth0", |
| 124 | StdinDatascip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#StdinData.: []byte(conf), |
| 125 | } |
| 126 | _, _, err := testutilsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/testutils`/.CmdAddWithArgsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/testutils`/CmdAddWithArgs().(args, func() error { return cmdAddscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/sample`/cmdAdd().(args) }) |
| 127 | Expectscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega`/Expect().(err).Toscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega/types`/Assertion#To().(MatchErrorscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega`/MatchError().("must be called as chained plugin")) |
| 128 | }) |
| 129 | }) |