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