| 1 | // Copyright 2022 Arista Networks |
| 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 | "encoding/json" |
| 19 | "errors" |
| 20 | "fmt" |
| 21 | "net" |
| 22 | "os" |
| 23 | "strings" |
| 24 | "syscall" |
| 25 | |
| 26 | . "github.com/onsi/ginkgo/v2" |
| 27 | . "github.com/onsi/gomega" |
| 28 | "github.com/vishvananda/netlink" |
| 29 | |
| 30 | "github.com/containernetworking/cni/pkg/skel" |
| 31 | "github.com/containernetworking/cni/pkg/types" |
| 32 | types020 "github.com/containernetworking/cni/pkg/types/020" |
| 33 | types040 "github.com/containernetworking/cni/pkg/types/040" |
| 34 | types100 "github.com/containernetworking/cni/pkg/types/100" |
| 35 | "github.com/containernetworking/plugins/pkg/netlinksafe" |
| 36 | "github.com/containernetworking/plugins/pkg/ns" |
| 37 | "github.com/containernetworking/plugins/pkg/testutils" |
| 38 | "github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator" |
| 39 | ) |
| 40 | |
| 41 | const MASTER_NAME = "eth0" |
| 42 | |
| 43 | type Net struct { |
| 44 | Name string `json:"name"` |
| 45 | CNIVersion string `json:"cniVersion"` |
| 46 | Type string `json:"type,omitempty"` |
| 47 | IPAM *allocator.IPAMConfig `json:"ipam"` |
| 48 | RawPrevResult map[string]interface{} `json:"prevResult,omitempty"` |
| 49 | PrevResult types100.Result `json:"-"` |
| 50 | } |
| 51 | |
| 52 | func buildOneConfig(netName string, cniVersion string, orig *Net, prevResult types.Result) (*Net, error) { |
| 53 | var err error |
| 54 | |
| 55 | inject := map[string]interface{}{ |
| 56 | "name": netName, |
| 57 | "cniVersion": cniVersion, |
| 58 | } |
| 59 | // Add previous plugin result |
| 60 | if prevResult != nil { |
| 61 | inject["prevResult"] = prevResult |
| 62 | } |
| 63 | |
| 64 | // Ensure every config uses the same name and version |
| 65 | config := make(map[string]interface{}) |
| 66 | |
| 67 | confBytes, err := json.Marshal(orig) |
| 68 | if err != nil { |
| 69 | return nil, err |
| 70 | } |
| 71 | |
| 72 | err = json.Unmarshal(confBytes, &config) |
| 73 | if err != nil { |
| 74 | return nil, fmt.Errorf("unmarshal existing network bytes: %s", err) |
| 75 | } |
| 76 | |
| 77 | for key, value := range inject { |
| 78 | config[key] = value |
| 79 | } |
| 80 | |
| 81 | newBytes, err := json.Marshal(config) |
| 82 | if err != nil { |
| 83 | return nil, err |
| 84 | } |
| 85 | |
| 86 | conf := &Net{} |
| 87 | if err := json.Unmarshal(newBytes, &conf); err != nil { |
| 88 | return nil, fmt.Errorf("error parsing configuration: %s", err) |
| 89 | } |
| 90 | |
| 91 | return conf, nil |
| 92 | } |
| 93 | |
| 94 | type tester interface { |
| 95 | // verifyResult minimally verifies the Result and returns the interface's MAC address |
| 96 | verifyResult(result types.Result, name string) string |
| 97 | } |
| 98 | |
| 99 | type testerBase struct{} |
| 100 | |
| 101 | type ( |
| 102 | testerV10x testerBase |
| 103 | testerV04x testerBase |
| 104 | testerV03x testerBase |
| 105 | testerV01xOr02x testerBase |
| 106 | ) |
| 107 | |
| 108 | func newTesterByVersion(version string) tester { |
| 109 | switch { |
| 110 | case strings.HasPrefix(version, "1."): |
| 111 | return &testerV10x{} |
| 112 | case strings.HasPrefix(version, "0.4."): |
| 113 | return &testerV04x{} |
| 114 | case strings.HasPrefix(version, "0.3."): |
| 115 | return &testerV03x{} |
| 116 | default: |
| 117 | return &testerV01xOr02x{} |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // verifyResult minimally verifies the Result and returns the interface's MAC address |
| 122 | func (t *testerV10x) verifyResult(result types.Result, name string) string { |
| 123 | r, err := types100.GetResult(result) |
| 124 | Expect(err).NotTo(HaveOccurred()) |
| 125 | |
| 126 | Expect(r.Interfaces).To(HaveLen(1)) |
| 127 | Expect(r.Interfaces[0].Name).To(Equal(name)) |
| 128 | Expect(r.IPs).To(HaveLen(1)) |
| 129 | |
| 130 | return r.Interfaces[0].Mac |
| 131 | } |
| 132 | |
| 133 | func verify0403(result types.Result, name string) string { |
| 134 | r, err := types040.GetResult(result) |
| 135 | Expect(err).NotTo(HaveOccurred()) |
| 136 | |
| 137 | Expect(r.Interfaces).To(HaveLen(1)) |
| 138 | Expect(r.Interfaces[0].Name).To(Equal(name)) |
| 139 | Expect(r.IPs).To(HaveLen(1)) |
| 140 | |
| 141 | return r.Interfaces[0].Mac |
| 142 | } |
| 143 | |
| 144 | // verifyResult minimally verifies the Result and returns the interface's MAC address |
| 145 | func (t *testerV04x) verifyResult(result types.Result, name string) string { |
| 146 | return verify0403(result, name) |
| 147 | } |
| 148 | |
| 149 | // verifyResult minimally verifies the Result and returns the interface's MAC address |
| 150 | func (t *testerV03x) verifyResult(result types.Result, name string) string { |
| 151 | return verify0403(result, name) |
| 152 | } |
| 153 | |
| 154 | // verifyResult minimally verifies the Result and returns the interface's MAC address |
| 155 | func (t *testerV01xOr02x) verifyResult(result types.Result, _ string) string { |
| 156 | r, err := types020.GetResult(result) |
| 157 | Expect(err).NotTo(HaveOccurred()) |
| 158 | |
| 159 | Expect(r.IP4.IP.IP).NotTo(BeNil()) |
| 160 | Expect(r.IP6).To(BeNil()) |
| 161 | |
| 162 | // 0.2 and earlier don't return MAC address |
| 163 | return "" |
| 164 | } |
| 165 | |
| 166 | var _ = Describe("dummy Operations", func() { |
| 167 | var originalNS, targetNS ns.NetNS |
| 168 | var dataDir string |
| 169 | |
| 170 | BeforeEach(func() { |
| 171 | // Create a new NetNS so we don't modify the host |
| 172 | var err error |
| 173 | originalNS, err = testutils.NewNS() |
| 174 | Expect(err).NotTo(HaveOccurred()) |
| 175 | targetNS, err = testutils.NewNS() |
| 176 | Expect(err).NotTo(HaveOccurred()) |
| 177 | |
| 178 | dataDir, err = os.MkdirTemp("", "dummy_test") |
| 179 | Expect(err).NotTo(HaveOccurred()) |
| 180 | |
| 181 | err = originalNS.Do(func(ns.NetNS) error { |
| 182 | defer GinkgoRecover() |
| 183 | |
| 184 | linkAttrs := netlink.NewLinkAttrs() |
| 185 | linkAttrs.Name = MASTER_NAME |
| 186 | // Add master |
| 187 | err = netlink.LinkAdd(&netlink.Dummy{ |
| 188 | LinkAttrs: linkAttrs, |
| 189 | }) |
| 190 | Expect(err).NotTo(HaveOccurred()) |
| 191 | m, err := netlinksafe.LinkByName(MASTER_NAME) |
| 192 | Expect(err).NotTo(HaveOccurred()) |
| 193 | err = netlink.LinkSetUp(m) |
| 194 | Expect(err).NotTo(HaveOccurred()) |
| 195 | return nil |
| 196 | }) |
| 197 | Expect(err).NotTo(HaveOccurred()) |
| 198 | }) |
| 199 | |
| 200 | AfterEach(func() { |
| 201 | Expect(os.RemoveAll(dataDir)).To(Succeed()) |
| 202 | Expect(originalNS.Close()).To(Succeed()) |
| 203 | Expect(testutils.UnmountNS(originalNS)).To(Succeed()) |
| 204 | Expect(targetNS.Close()).To(Succeed()) |
| 205 | Expect(testutils.UnmountNS(targetNS)).To(Succeed()) |
| 206 | }) |
| 207 | |
| 208 | for _, ver := range testutils.AllSpecVersions { |
| 209 | // Redefine ver inside for scope so real value is picked up by each dynamically defined It() |
| 210 | // See Gingkgo's "Patterns for dynamically generating tests" documentation. |
| 211 | ver := ver |
| 212 | |
| 213 | It(fmt.Sprintf("[%s] creates an dummy link in a non-default namespace", ver), func() { |
| 214 | // Create dummy in other namespace |
| 215 | err := originalNS.Do(func(ns.NetNS) error { |
| 216 | defer GinkgoRecover() |
| 217 | |
| 218 | _, err := createDummy("foobar0", targetNS) |
| 219 | Expect(err).NotTo(HaveOccurred()) |
| 220 | return nil |
| 221 | }) |
| 222 | Expect(err).NotTo(HaveOccurred()) |
| 223 | |
| 224 | // Make sure dummy link exists in the target namespace |
| 225 | err = targetNS.Do(func(ns.NetNS) error { |
| 226 | defer GinkgoRecover() |
| 227 | |
| 228 | link, err := netlinksafe.LinkByName("foobar0") |
| 229 | Expect(err).NotTo(HaveOccurred()) |
| 230 | Expect(link.Attrs().Name).To(Equal("foobar0")) |
| 231 | return nil |
| 232 | }) |
| 233 | Expect(err).NotTo(HaveOccurred()) |
| 234 | }) |
| 235 | |
| 236 | It(fmt.Sprintf("[%s] creates an dummy link in a non-default namespace with conflicting name in original namespace", ver), func() { |
| 237 | ifName := "foobar0" |
| 238 | err := originalNS.Do(func(ns.NetNS) error { |
| 239 | defer GinkgoRecover() |
| 240 | // Create dummy in the original namespace, with the same name |
| 241 | linkAttrs := netlink.NewLinkAttrs() |
| 242 | linkAttrs.Name = ifName |
| 243 | dm := &netlink.Dummy{ |
| 244 | LinkAttrs: linkAttrs, |
| 245 | } |
| 246 | err := netlink.LinkAdd(dm) |
| 247 | Expect(err).NotTo(HaveOccurred()) |
| 248 | |
| 249 | _, err = createDummy(ifName, targetNS) |
| 250 | Expect(err).NotTo(HaveOccurred()) |
| 251 | return nil |
| 252 | }) |
| 253 | Expect(err).NotTo(HaveOccurred()) |
| 254 | }) |
| 255 | |
| 256 | It(fmt.Sprintf("[%s] configures and deconfigures a dummy link with ADD/CHECK/DEL", ver), func() { |
| 257 | const IFNAME = "dummy0" |
| 258 | |
| 259 | conf := fmt.Sprintf(`{ |
| 260 | "cniVersion": "%s", |
| 261 | "name": "dummyTestv4", |
| 262 | "type": "dummy", |
| 263 | "ipam": { |
| 264 | "type": "host-local", |
| 265 | "subnet": "10.1.2.0/24", |
| 266 | "dataDir": "%s" |
| 267 | } |
| 268 | }`, ver, dataDir) |
| 269 | |
| 270 | args := &skel.CmdArgs{ |
| 271 | ContainerID: "contDummy", |
| 272 | Netns: targetNS.Path(), |
| 273 | IfName: IFNAME, |
| 274 | StdinData: []byte(conf), |
| 275 | } |
| 276 | |
| 277 | t := newTesterByVersion(ver) |
| 278 | |
| 279 | var result types.Result |
| 280 | var macAddress string |
| 281 | err := originalNS.Do(func(ns.NetNS) error { |
| 282 | defer GinkgoRecover() |
| 283 | |
| 284 | var err error |
| 285 | if testutils.SpecVersionHasSTATUS(ver) { |
| 286 | err = testutils.CmdStatus(func() error { |
| 287 | return cmdStatus(args) |
| 288 | }) |
| 289 | Expect(err).NotTo(HaveOccurred()) |
| 290 | } |
| 291 | |
| 292 | result, _, err = testutils.CmdAddWithArgs(args, func() error { |
| 293 | return cmdAdd(args) |
| 294 | }) |
| 295 | Expect(err).NotTo(HaveOccurred()) |
| 296 | |
| 297 | macAddress = t.verifyResult(result, IFNAME) |
| 298 | return nil |
| 299 | }) |
| 300 | Expect(err).NotTo(HaveOccurred()) |
| 301 | // Make sure dummy link exists in the target namespace |
| 302 | err = targetNS.Do(func(ns.NetNS) error { |
| 303 | defer GinkgoRecover() |
| 304 | |
| 305 | link, err := netlinksafe.LinkByName(IFNAME) |
| 306 | Expect(err).NotTo(HaveOccurred()) |
| 307 | Expect(link.Attrs().Name).To(Equal(IFNAME)) |
| 308 | |
| 309 | if macAddress != "" { |
| 310 | hwaddr, err := net.ParseMAC(macAddress) |
| 311 | Expect(err).NotTo(HaveOccurred()) |
| 312 | Expect(link.Attrs().HardwareAddr).To(Equal(hwaddr)) |
| 313 | } |
| 314 | |
| 315 | addrs, err := netlinksafe.AddrList(link, syscall.AF_INET) |
| 316 | Expect(err).NotTo(HaveOccurred()) |
| 317 | Expect(addrs).To(HaveLen(1)) |
| 318 | return nil |
| 319 | }) |
| 320 | Expect(err).NotTo(HaveOccurred()) |
| 321 | |
| 322 | // call CmdCheck |
| 323 | n := &Net{} |
| 324 | err = json.Unmarshal([]byte(conf), &n) |
| 325 | Expect(err).NotTo(HaveOccurred()) |
| 326 | |
| 327 | n.IPAM, _, err = allocator.LoadIPAMConfig([]byte(conf), "") |
| 328 | Expect(err).NotTo(HaveOccurred()) |
| 329 | |
| 330 | newConf, err := buildOneConfig("dummyTestv4", ver, n, result) |
| 331 | Expect(err).NotTo(HaveOccurred()) |
| 332 | |
| 333 | confString, err := json.Marshal(newConf) |
| 334 | Expect(err).NotTo(HaveOccurred()) |
| 335 | |
| 336 | args.StdinData = confString |
| 337 | // CNI Check dummy in the target namespace |
| 338 | err = originalNS.Do(func(ns.NetNS) error { |
| 339 | defer GinkgoRecover() |
| 340 | return testutils.CmdCheckWithArgs(args, func() error { return cmdCheck(args) }) |
| 341 | }) |
| 342 | if testutils.SpecVersionHasCHECK(ver) { |
| 343 | Expect(err).NotTo(HaveOccurred()) |
| 344 | } else { |
| 345 | Expect(err).To(MatchError("config version does not allow CHECK")) |
| 346 | } |
| 347 | |
| 348 | args.StdinData = []byte(conf) |
| 349 | |
| 350 | err = originalNS.Do(func(ns.NetNS) error { |
| 351 | defer GinkgoRecover() |
| 352 | |
| 353 | err = testutils.CmdDelWithArgs(args, func() error { |
| 354 | return cmdDel(args) |
| 355 | }) |
| 356 | Expect(err).NotTo(HaveOccurred()) |
| 357 | return nil |
| 358 | }) |
| 359 | Expect(err).NotTo(HaveOccurred()) |
| 360 | |
| 361 | // Make sure dummy link has been deleted |
| 362 | err = targetNS.Do(func(ns.NetNS) error { |
| 363 | defer GinkgoRecover() |
| 364 | |
| 365 | link, err := netlinksafe.LinkByName(IFNAME) |
| 366 | Expect(err).To(HaveOccurred()) |
| 367 | Expect(link).To(BeNil()) |
| 368 | return nil |
| 369 | }) |
| 370 | Expect(err).NotTo(HaveOccurred()) |
| 371 | |
| 372 | // DEL can be called multiple times, make sure no error is returned |
| 373 | // if the device is already removed. |
| 374 | err = originalNS.Do(func(ns.NetNS) error { |
| 375 | defer GinkgoRecover() |
| 376 | |
| 377 | err = testutils.CmdDelWithArgs(args, func() error { |
| 378 | return cmdDel(args) |
| 379 | }) |
| 380 | Expect(err).NotTo(HaveOccurred()) |
| 381 | return nil |
| 382 | }) |
| 383 | Expect(err).NotTo(HaveOccurred()) |
| 384 | }) |
| 385 | |
| 386 | It(fmt.Sprintf("[%s] fails to create dummy link with no ipam", ver), func() { |
| 387 | var err error |
| 388 | |
| 389 | const confFmt = `{ |
| 390 | "cniVersion": "%s", |
| 391 | "name": "mynet", |
| 392 | "type": "dummy" |
| 393 | }` |
| 394 | |
| 395 | args := &skel.CmdArgs{ |
| 396 | ContainerID: "dummyCont", |
| 397 | Netns: "/var/run/netns/test", |
| 398 | IfName: "dummy0", |
| 399 | StdinData: []byte(fmt.Sprintf(confFmt, ver)), |
| 400 | } |
| 401 | |
| 402 | _ = originalNS.Do(func(_ ns.NetNS) error { |
| 403 | defer GinkgoRecover() |
| 404 | |
| 405 | _, _, err = testutils.CmdAddWithArgs(args, func() error { |
| 406 | return cmdAdd(args) |
| 407 | }) |
| 408 | Expect(err).To(Equal(errors.New("dummy interface requires an IPAM configuration"))) |
| 409 | return nil |
| 410 | }) |
| 411 | }) |
| 412 | } |
| 413 | }) |