| 1 | // Copyright 2016 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 | "fmt" |
| 19 | "net" |
| 20 | "os" |
| 21 | "path/filepath" |
| 22 | "strings" |
| 23 | |
| 24 | . "github.com/onsi/ginkgo/v2" |
| 25 | . "github.com/onsi/gomega" |
| 26 | |
| 27 | "github.com/containernetworking/cni/pkg/skel" |
| 28 | "github.com/containernetworking/cni/pkg/types" |
| 29 | types100 "github.com/containernetworking/cni/pkg/types/100" |
| 30 | "github.com/containernetworking/plugins/pkg/testutils" |
| 31 | "github.com/containernetworking/plugins/plugins/ipam/host-local/backend/disk" |
| 32 | ) |
| 33 | |
| 34 | const LineBreak = "\r\n" |
| 35 | |
| 36 | var _ = Describe("host-local Operations", func() { |
| 37 | var tmpDir string |
| 38 | const ( |
| 39 | ifname string = "eth0" |
| 40 | nspath string = "/some/where" |
| 41 | ) |
| 42 | |
| 43 | BeforeEach(func() { |
| 44 | var err error |
| 45 | tmpDir, err = os.MkdirTemp("", "host-local_test") |
| 46 | Expect(err).NotTo(HaveOccurred()) |
| 47 | tmpDir = filepath.ToSlash(tmpDir) |
| 48 | }) |
| 49 | |
| 50 | AfterEach(func() { |
| 51 | os.RemoveAll(tmpDir) |
| 52 | }) |
| 53 | |
| 54 | for _, ver := range testutils.AllSpecVersions { |
| 55 | // Redefine ver inside for scope so real value is picked up by each dynamically defined It() |
| 56 | // See Gingkgo's "Patterns for dynamically generating tests" documentation. |
| 57 | ver := ver |
| 58 | |
| 59 | It(fmt.Sprintf("[%s] allocates and releases addresses with ADD/DEL", ver), func() { |
| 60 | err := os.WriteFile(filepath.Join(tmpDir, "resolv.conf"), []byte("nameserver 192.0.2.3"), 0o644) |
| 61 | Expect(err).NotTo(HaveOccurred()) |
| 62 | |
| 63 | conf := fmt.Sprintf(`{ |
| 64 | "cniVersion": "%s", |
| 65 | "name": "mynet", |
| 66 | "type": "ipvlan", |
| 67 | "master": "foo0", |
| 68 | "ipam": { |
| 69 | "type": "host-local", |
| 70 | "dataDir": "%s", |
| 71 | "resolvConf": "%s/resolv.conf", |
| 72 | "ranges": [ |
| 73 | [{ "subnet": "10.1.2.0/24" }, {"subnet": "10.2.2.0/24"}], |
| 74 | [{ "subnet": "2001:db8:1::0/64" }] |
| 75 | ], |
| 76 | "routes": [ |
| 77 | {"dst": "0.0.0.0/0"}, |
| 78 | {"dst": "::/0"}, |
| 79 | {"dst": "192.168.0.0/16", "gw": "1.1.1.1"}, |
| 80 | {"dst": "2001:db8:2::0/64", "gw": "2001:db8:3::1"} |
| 81 | ] |
| 82 | } |
| 83 | }`, ver, tmpDir, tmpDir) |
| 84 | |
| 85 | args := &skel.CmdArgs{ |
| 86 | ContainerID: "dummy", |
| 87 | Netns: nspath, |
| 88 | IfName: ifname, |
| 89 | StdinData: []byte(conf), |
| 90 | } |
| 91 | |
| 92 | // Allocate the IP |
| 93 | r, raw, err := testutils.CmdAddWithArgs(args, func() error { |
| 94 | return cmdAdd(args) |
| 95 | }) |
| 96 | Expect(err).NotTo(HaveOccurred()) |
| 97 | if testutils.SpecVersionHasIPVersion(ver) { |
| 98 | Expect(strings.Index(string(raw), "\"version\":")).Should(BeNumerically(">", 0)) |
| 99 | } |
| 100 | |
| 101 | result, err := types100.GetResult(r) |
| 102 | Expect(err).NotTo(HaveOccurred()) |
| 103 | |
| 104 | // Gomega is cranky about slices with different caps |
| 105 | Expect(*result.IPs[0]).To(Equal( |
| 106 | types100.IPConfig{ |
| 107 | Address: mustCIDR("10.1.2.2/24"), |
| 108 | Gateway: net.ParseIP("10.1.2.1"), |
| 109 | })) |
| 110 | |
| 111 | Expect(*result.IPs[1]).To(Equal( |
| 112 | types100.IPConfig{ |
| 113 | Address: mustCIDR("2001:db8:1::2/64"), |
| 114 | Gateway: net.ParseIP("2001:db8:1::1"), |
| 115 | }, |
| 116 | )) |
| 117 | Expect(result.IPs).To(HaveLen(2)) |
| 118 | |
| 119 | for _, expectedRoute := range []*types.Route{ |
| 120 | {Dst: mustCIDR("0.0.0.0/0"), GW: nil}, |
| 121 | {Dst: mustCIDR("::/0"), GW: nil}, |
| 122 | {Dst: mustCIDR("192.168.0.0/16"), GW: net.ParseIP("1.1.1.1")}, |
| 123 | {Dst: mustCIDR("2001:db8:2::0/64"), GW: net.ParseIP("2001:db8:3::1")}, |
| 124 | } { |
| 125 | found := false |
| 126 | for _, foundRoute := range result.Routes { |
| 127 | if foundRoute.String() == expectedRoute.String() { |
| 128 | found = true |
| 129 | break |
| 130 | } |
| 131 | } |
| 132 | Expect(found).To(BeTrue()) |
| 133 | } |
| 134 | |
| 135 | ipFilePath1 := filepath.Join(tmpDir, "mynet", "10.1.2.2") |
| 136 | contents, err := os.ReadFile(ipFilePath1) |
| 137 | Expect(err).NotTo(HaveOccurred()) |
| 138 | Expect(string(contents)).To(Equal(args.ContainerID + LineBreak + ifname)) |
| 139 | |
| 140 | ipFilePath2 := filepath.Join(tmpDir, disk.GetEscapedPath("mynet", "2001:db8:1::2")) |
| 141 | contents, err = os.ReadFile(ipFilePath2) |
| 142 | Expect(err).NotTo(HaveOccurred()) |
| 143 | Expect(string(contents)).To(Equal(args.ContainerID + LineBreak + ifname)) |
| 144 | |
| 145 | lastFilePath1 := filepath.Join(tmpDir, "mynet", "last_reserved_ip.0") |
| 146 | contents, err = os.ReadFile(lastFilePath1) |
| 147 | Expect(err).NotTo(HaveOccurred()) |
| 148 | Expect(string(contents)).To(Equal("10.1.2.2")) |
| 149 | |
| 150 | lastFilePath2 := filepath.Join(tmpDir, "mynet", "last_reserved_ip.1") |
| 151 | contents, err = os.ReadFile(lastFilePath2) |
| 152 | Expect(err).NotTo(HaveOccurred()) |
| 153 | Expect(string(contents)).To(Equal("2001:db8:1::2")) |
| 154 | // Release the IP |
| 155 | err = testutils.CmdDelWithArgs(args, func() error { |
| 156 | return cmdDel(args) |
| 157 | }) |
| 158 | Expect(err).NotTo(HaveOccurred()) |
| 159 | |
| 160 | _, err = os.Stat(ipFilePath1) |
| 161 | Expect(err).To(HaveOccurred()) |
| 162 | _, err = os.Stat(ipFilePath2) |
| 163 | Expect(err).To(HaveOccurred()) |
| 164 | }) |
| 165 | |
| 166 | It(fmt.Sprintf("[%s] allocates and releases addresses on specific interface with ADD/DEL", ver), func() { |
| 167 | const ifname1 string = "eth1" |
| 168 | |
| 169 | err := os.WriteFile(filepath.Join(tmpDir, "resolv.conf"), []byte("nameserver 192.0.2.3"), 0o644) |
| 170 | Expect(err).NotTo(HaveOccurred()) |
| 171 | |
| 172 | conf0 := fmt.Sprintf(`{ |
| 173 | "cniVersion": "%s", |
| 174 | "name": "mynet0", |
| 175 | "type": "ipvlan", |
| 176 | "master": "foo0", |
| 177 | "ipam": { |
| 178 | "type": "host-local", |
| 179 | "dataDir": "%s", |
| 180 | "resolvConf": "%s/resolv.conf", |
| 181 | "ranges": [ |
| 182 | [{ "subnet": "10.1.2.0/24" }] |
| 183 | ] |
| 184 | } |
| 185 | }`, ver, tmpDir, tmpDir) |
| 186 | |
| 187 | conf1 := fmt.Sprintf(`{ |
| 188 | "cniVersion": "%s", |
| 189 | "name": "mynet1", |
| 190 | "type": "ipvlan", |
| 191 | "master": "foo1", |
| 192 | "ipam": { |
| 193 | "type": "host-local", |
| 194 | "dataDir": "%s", |
| 195 | "resolvConf": "%s/resolv.conf", |
| 196 | "ranges": [ |
| 197 | [{ "subnet": "10.2.2.0/24" }] |
| 198 | ] |
| 199 | } |
| 200 | }`, ver, tmpDir, tmpDir) |
| 201 | |
| 202 | args0 := &skel.CmdArgs{ |
| 203 | ContainerID: "dummy", |
| 204 | Netns: nspath, |
| 205 | IfName: ifname, |
| 206 | StdinData: []byte(conf0), |
| 207 | } |
| 208 | |
| 209 | // Allocate the IP |
| 210 | r0, raw, err := testutils.CmdAddWithArgs(args0, func() error { |
| 211 | return cmdAdd(args0) |
| 212 | }) |
| 213 | Expect(err).NotTo(HaveOccurred()) |
| 214 | if testutils.SpecVersionHasIPVersion(ver) { |
| 215 | Expect(strings.Index(string(raw), "\"version\":")).Should(BeNumerically(">", 0)) |
| 216 | } |
| 217 | |
| 218 | _, err = types100.GetResult(r0) |
| 219 | Expect(err).NotTo(HaveOccurred()) |
| 220 | |
| 221 | args1 := &skel.CmdArgs{ |
| 222 | ContainerID: "dummy", |
| 223 | Netns: nspath, |
| 224 | IfName: ifname1, |
| 225 | StdinData: []byte(conf1), |
| 226 | } |
| 227 | |
| 228 | // Allocate the IP |
| 229 | r1, raw, err := testutils.CmdAddWithArgs(args1, func() error { |
| 230 | return cmdAdd(args1) |
| 231 | }) |
| 232 | Expect(err).NotTo(HaveOccurred()) |
| 233 | if testutils.SpecVersionHasIPVersion(ver) { |
| 234 | Expect(strings.Index(string(raw), "\"version\":")).Should(BeNumerically(">", 0)) |
| 235 | } |
| 236 | |
| 237 | _, err = types100.GetResult(r1) |
| 238 | Expect(err).NotTo(HaveOccurred()) |
| 239 | |
| 240 | ipFilePath0 := filepath.Join(tmpDir, "mynet0", "10.1.2.2") |
| 241 | contents, err := os.ReadFile(ipFilePath0) |
| 242 | Expect(err).NotTo(HaveOccurred()) |
| 243 | Expect(string(contents)).To(Equal(args0.ContainerID + LineBreak + ifname)) |
| 244 | |
| 245 | ipFilePath1 := filepath.Join(tmpDir, "mynet1", "10.2.2.2") |
| 246 | contents, err = os.ReadFile(ipFilePath1) |
| 247 | Expect(err).NotTo(HaveOccurred()) |
| 248 | Expect(string(contents)).To(Equal(args1.ContainerID + LineBreak + ifname1)) |
| 249 | |
| 250 | // Release the IP on ifname |
| 251 | err = testutils.CmdDelWithArgs(args0, func() error { |
| 252 | return cmdDel(args0) |
| 253 | }) |
| 254 | Expect(err).NotTo(HaveOccurred()) |
| 255 | _, err = os.Stat(ipFilePath0) |
| 256 | Expect(err).To(HaveOccurred()) |
| 257 | |
| 258 | // reread ipFilePath1, ensure that ifname1 didn't get deleted |
| 259 | contents, err = os.ReadFile(ipFilePath1) |
| 260 | Expect(err).NotTo(HaveOccurred()) |
| 261 | Expect(string(contents)).To(Equal(args1.ContainerID + LineBreak + ifname1)) |
| 262 | |
| 263 | // Release the IP on ifname1 |
| 264 | err = testutils.CmdDelWithArgs(args1, func() error { |
| 265 | return cmdDel(args1) |
| 266 | }) |
| 267 | Expect(err).NotTo(HaveOccurred()) |
| 268 | |
| 269 | _, err = os.Stat(ipFilePath1) |
| 270 | Expect(err).To(HaveOccurred()) |
| 271 | }) |
| 272 | |
| 273 | It(fmt.Sprintf("[%s] repeat allocating addresses on specific interface for same container ID with ADD", ver), func() { |
| 274 | conf := fmt.Sprintf(`{ |
| 275 | "cniVersion": "%s", |
| 276 | "name": "mynet0", |
| 277 | "type": "ipvlan", |
| 278 | "master": "foo0", |
| 279 | "ipam": { |
| 280 | "type": "host-local", |
| 281 | "dataDir": "%s", |
| 282 | "ranges": [ |
| 283 | [{ "subnet": "10.1.2.0/24" }] |
| 284 | ] |
| 285 | } |
| 286 | }`, ver, tmpDir) |
| 287 | |
| 288 | args := &skel.CmdArgs{ |
| 289 | ContainerID: "dummy", |
| 290 | Netns: nspath, |
| 291 | IfName: ifname, |
| 292 | StdinData: []byte(conf), |
| 293 | } |
| 294 | |
| 295 | args1 := &skel.CmdArgs{ |
| 296 | ContainerID: "dummy1", |
| 297 | Netns: nspath, |
| 298 | IfName: ifname, |
| 299 | StdinData: []byte(conf), |
| 300 | } |
| 301 | |
| 302 | // Allocate the IP |
| 303 | r0, raw, err := testutils.CmdAddWithArgs(args, func() error { |
| 304 | return cmdAdd(args) |
| 305 | }) |
| 306 | Expect(err).NotTo(HaveOccurred()) |
| 307 | if testutils.SpecVersionHasIPVersion(ver) { |
| 308 | Expect(strings.Index(string(raw), "\"version\":")).Should(BeNumerically(">", 0)) |
| 309 | } |
| 310 | |
| 311 | result0, err := types100.GetResult(r0) |
| 312 | Expect(err).NotTo(HaveOccurred()) |
| 313 | Expect(result0.IPs).Should(HaveLen(1)) |
| 314 | Expect(result0.IPs[0].Address.String()).Should(Equal("10.1.2.2/24")) |
| 315 | |
| 316 | // Allocate the IP with the same container ID |
| 317 | _, _, err = testutils.CmdAddWithArgs(args, func() error { |
| 318 | return cmdAdd(args) |
| 319 | }) |
| 320 | Expect(err).To(HaveOccurred()) |
| 321 | |
| 322 | // Allocate the IP with the another container ID |
| 323 | r1, raw, err := testutils.CmdAddWithArgs(args1, func() error { |
| 324 | return cmdAdd(args1) |
| 325 | }) |
| 326 | Expect(err).NotTo(HaveOccurred()) |
| 327 | if testutils.SpecVersionHasIPVersion(ver) { |
| 328 | Expect(strings.Index(string(raw), "\"version\":")).Should(BeNumerically(">", 0)) |
| 329 | } |
| 330 | |
| 331 | result1, err := types100.GetResult(r1) |
| 332 | Expect(err).NotTo(HaveOccurred()) |
| 333 | Expect(result1.IPs).Should(HaveLen(1)) |
| 334 | Expect(result1.IPs[0].Address.String()).Should(Equal("10.1.2.3/24")) |
| 335 | |
| 336 | // Allocate the IP with the same container ID again |
| 337 | _, _, err = testutils.CmdAddWithArgs(args, func() error { |
| 338 | return cmdAdd(args) |
| 339 | }) |
| 340 | Expect(err).To(HaveOccurred()) |
| 341 | |
| 342 | ipFilePath := filepath.Join(tmpDir, "mynet0", "10.1.2.2") |
| 343 | |
| 344 | // Release the IPs |
| 345 | err = testutils.CmdDelWithArgs(args, func() error { |
| 346 | return cmdDel(args) |
| 347 | }) |
| 348 | Expect(err).NotTo(HaveOccurred()) |
| 349 | _, err = os.Stat(ipFilePath) |
| 350 | Expect(err).To(HaveOccurred()) |
| 351 | |
| 352 | err = testutils.CmdDelWithArgs(args1, func() error { |
| 353 | return cmdDel(args1) |
| 354 | }) |
| 355 | Expect(err).NotTo(HaveOccurred()) |
| 356 | }) |
| 357 | |
| 358 | It(fmt.Sprintf("[%s] verify DEL works on backwards compatible allocate", ver), func() { |
| 359 | err := os.WriteFile(filepath.Join(tmpDir, "resolv.conf"), []byte("nameserver 192.0.2.3"), 0o644) |
| 360 | Expect(err).NotTo(HaveOccurred()) |
| 361 | |
| 362 | conf := fmt.Sprintf(`{ |
| 363 | "cniVersion": "%s", |
| 364 | "name": "mynet", |
| 365 | "type": "ipvlan", |
| 366 | "master": "foo", |
| 367 | "ipam": { |
| 368 | "type": "host-local", |
| 369 | "dataDir": "%s", |
| 370 | "resolvConf": "%s/resolv.conf", |
| 371 | "ranges": [ |
| 372 | [{ "subnet": "10.1.2.0/24" }] |
| 373 | ] |
| 374 | } |
| 375 | }`, ver, tmpDir, tmpDir) |
| 376 | |
| 377 | args := &skel.CmdArgs{ |
| 378 | ContainerID: "dummy", |
| 379 | Netns: nspath, |
| 380 | IfName: ifname, |
| 381 | StdinData: []byte(conf), |
| 382 | } |
| 383 | |
| 384 | // Allocate the IP |
| 385 | r, raw, err := testutils.CmdAddWithArgs(args, func() error { |
| 386 | return cmdAdd(args) |
| 387 | }) |
| 388 | Expect(err).NotTo(HaveOccurred()) |
| 389 | if testutils.SpecVersionHasIPVersion(ver) { |
| 390 | Expect(strings.Index(string(raw), "\"version\":")).Should(BeNumerically(">", 0)) |
| 391 | } |
| 392 | |
| 393 | _, err = types100.GetResult(r) |
| 394 | Expect(err).NotTo(HaveOccurred()) |
| 395 | |
| 396 | ipFilePath := filepath.Join(tmpDir, "mynet", "10.1.2.2") |
| 397 | contents, err := os.ReadFile(ipFilePath) |
| 398 | Expect(err).NotTo(HaveOccurred()) |
| 399 | Expect(string(contents)).To(Equal(args.ContainerID + LineBreak + ifname)) |
| 400 | err = os.WriteFile(ipFilePath, []byte(strings.TrimSpace(args.ContainerID)), 0o644) |
| 401 | Expect(err).NotTo(HaveOccurred()) |
| 402 | |
| 403 | err = testutils.CmdDelWithArgs(args, func() error { |
| 404 | return cmdDel(args) |
| 405 | }) |
| 406 | Expect(err).NotTo(HaveOccurred()) |
| 407 | _, err = os.Stat(ipFilePath) |
| 408 | Expect(err).To(HaveOccurred()) |
| 409 | }) |
| 410 | |
| 411 | It(fmt.Sprintf("[%s] doesn't error when passed an unknown ID on DEL", ver), func() { |
| 412 | conf := fmt.Sprintf(`{ |
| 413 | "cniVersion": "%s", |
| 414 | "name": "mynet", |
| 415 | "type": "ipvlan", |
| 416 | "master": "foo0", |
| 417 | "ipam": { |
| 418 | "type": "host-local", |
| 419 | "subnet": "10.1.2.0/24", |
| 420 | "dataDir": "%s" |
| 421 | } |
| 422 | }`, ver, tmpDir) |
| 423 | |
| 424 | args := &skel.CmdArgs{ |
| 425 | ContainerID: "dummy", |
| 426 | Netns: nspath, |
| 427 | IfName: ifname, |
| 428 | StdinData: []byte(conf), |
| 429 | } |
| 430 | |
| 431 | // Release the IP |
| 432 | err := testutils.CmdDelWithArgs(args, func() error { |
| 433 | return cmdDel(args) |
| 434 | }) |
| 435 | Expect(err).NotTo(HaveOccurred()) |
| 436 | }) |
| 437 | |
| 438 | It(fmt.Sprintf("[%s] ignores whitespace in disk files", ver), func() { |
| 439 | conf := fmt.Sprintf(`{ |
| 440 | "cniVersion": "%s", |
| 441 | "name": "mynet", |
| 442 | "type": "ipvlan", |
| 443 | "master": "foo0", |
| 444 | "ipam": { |
| 445 | "type": "host-local", |
| 446 | "subnet": "10.1.2.0/24", |
| 447 | "dataDir": "%s" |
| 448 | } |
| 449 | }`, ver, tmpDir) |
| 450 | |
| 451 | args := &skel.CmdArgs{ |
| 452 | ContainerID: " dummy\n ", |
| 453 | Netns: nspath, |
| 454 | IfName: ifname, |
| 455 | StdinData: []byte(conf), |
| 456 | } |
| 457 | |
| 458 | // Allocate the IP |
| 459 | r, _, err := testutils.CmdAddWithArgs(args, func() error { |
| 460 | return cmdAdd(args) |
| 461 | }) |
| 462 | Expect(err).NotTo(HaveOccurred()) |
| 463 | |
| 464 | result, err := types100.GetResult(r) |
| 465 | Expect(err).NotTo(HaveOccurred()) |
| 466 | |
| 467 | ipFilePath := filepath.Join(tmpDir, "mynet", result.IPs[0].Address.IP.String()) |
| 468 | contents, err := os.ReadFile(ipFilePath) |
| 469 | Expect(err).NotTo(HaveOccurred()) |
| 470 | Expect(string(contents)).To(Equal("dummy" + LineBreak + ifname)) |
| 471 | |
| 472 | // Release the IP |
| 473 | err = testutils.CmdDelWithArgs(args, func() error { |
| 474 | return cmdDel(args) |
| 475 | }) |
| 476 | Expect(err).NotTo(HaveOccurred()) |
| 477 | |
| 478 | _, err = os.Stat(ipFilePath) |
| 479 | Expect(err).To(HaveOccurred()) |
| 480 | }) |
| 481 | |
| 482 | It(fmt.Sprintf("[%s] does not output an error message upon initial subnet creation", ver), func() { |
| 483 | conf := fmt.Sprintf(`{ |
| 484 | "cniVersion": "%s", |
| 485 | "name": "mynet", |
| 486 | "type": "ipvlan", |
| 487 | "master": "foo0", |
| 488 | "ipam": { |
| 489 | "type": "host-local", |
| 490 | "subnet": "10.1.2.0/24", |
| 491 | "dataDir": "%s" |
| 492 | } |
| 493 | }`, ver, tmpDir) |
| 494 | |
| 495 | args := &skel.CmdArgs{ |
| 496 | ContainerID: "testing", |
| 497 | Netns: nspath, |
| 498 | IfName: ifname, |
| 499 | StdinData: []byte(conf), |
| 500 | } |
| 501 | |
| 502 | // Allocate the IP |
| 503 | _, out, err := testutils.CmdAddWithArgs(args, func() error { |
| 504 | return cmdAdd(args) |
| 505 | }) |
| 506 | Expect(err).NotTo(HaveOccurred()) |
| 507 | Expect(strings.Index(string(out), "Error retrieving last reserved ip")).To(Equal(-1)) |
| 508 | }) |
| 509 | |
| 510 | It(fmt.Sprintf("[%s] allocates a custom IP when requested by config args", ver), func() { |
| 511 | conf := fmt.Sprintf(`{ |
| 512 | "cniVersion": "%s", |
| 513 | "name": "mynet", |
| 514 | "type": "ipvlan", |
| 515 | "master": "foo0", |
| 516 | "ipam": { |
| 517 | "type": "host-local", |
| 518 | "dataDir": "%s", |
| 519 | "ranges": [ |
| 520 | [{ "subnet": "10.1.2.0/24" }] |
| 521 | ] |
| 522 | }, |
| 523 | "args": { |
| 524 | "cni": { |
| 525 | "ips": ["10.1.2.88"] |
| 526 | } |
| 527 | } |
| 528 | }`, ver, tmpDir) |
| 529 | |
| 530 | args := &skel.CmdArgs{ |
| 531 | ContainerID: "dummy", |
| 532 | Netns: nspath, |
| 533 | IfName: ifname, |
| 534 | StdinData: []byte(conf), |
| 535 | } |
| 536 | |
| 537 | // Allocate the IP |
| 538 | r, _, err := testutils.CmdAddWithArgs(args, func() error { |
| 539 | return cmdAdd(args) |
| 540 | }) |
| 541 | Expect(err).NotTo(HaveOccurred()) |
| 542 | result, err := types100.GetResult(r) |
| 543 | Expect(err).NotTo(HaveOccurred()) |
| 544 | Expect(result.IPs).To(HaveLen(1)) |
| 545 | Expect(result.IPs[0].Address.IP).To(Equal(net.ParseIP("10.1.2.88"))) |
| 546 | }) |
| 547 | |
| 548 | It(fmt.Sprintf("[%s] allocates custom IPs from multiple ranges", ver), func() { |
| 549 | err := os.WriteFile(filepath.Join(tmpDir, "resolv.conf"), []byte("nameserver 192.0.2.3"), 0o644) |
| 550 | Expect(err).NotTo(HaveOccurred()) |
| 551 | |
| 552 | conf := fmt.Sprintf(`{ |
| 553 | "cniVersion": "%s", |
| 554 | "name": "mynet", |
| 555 | "type": "ipvlan", |
| 556 | "master": "foo0", |
| 557 | "ipam": { |
| 558 | "type": "host-local", |
| 559 | "dataDir": "%s", |
| 560 | "ranges": [ |
| 561 | [{ "subnet": "10.1.2.0/24" }], |
| 562 | [{ "subnet": "10.1.3.0/24" }] |
| 563 | ] |
| 564 | }, |
| 565 | "args": { |
| 566 | "cni": { |
| 567 | "ips": ["10.1.2.88", "10.1.3.77"] |
| 568 | } |
| 569 | } |
| 570 | }`, ver, tmpDir) |
| 571 | |
| 572 | args := &skel.CmdArgs{ |
| 573 | ContainerID: "dummy", |
| 574 | Netns: nspath, |
| 575 | IfName: ifname, |
| 576 | StdinData: []byte(conf), |
| 577 | } |
| 578 | |
| 579 | // Allocate the IP |
| 580 | r, _, err := testutils.CmdAddWithArgs(args, func() error { |
| 581 | return cmdAdd(args) |
| 582 | }) |
| 583 | if !testutils.SpecVersionHasMultipleIPs(ver) { |
| 584 | errStr := fmt.Sprintf("CNI version %s does not support more than 1 address per family", ver) |
| 585 | Expect(err).To(MatchError(errStr)) |
| 586 | } else { |
| 587 | Expect(err).NotTo(HaveOccurred()) |
| 588 | result, err := types100.GetResult(r) |
| 589 | Expect(err).NotTo(HaveOccurred()) |
| 590 | Expect(result.IPs).To(HaveLen(2)) |
| 591 | Expect(result.IPs[0].Address.IP).To(Equal(net.ParseIP("10.1.2.88"))) |
| 592 | Expect(result.IPs[1].Address.IP).To(Equal(net.ParseIP("10.1.3.77"))) |
| 593 | } |
| 594 | }) |
| 595 | |
| 596 | It(fmt.Sprintf("[%s] allocates custom IPs from multiple protocols", ver), func() { |
| 597 | err := os.WriteFile(filepath.Join(tmpDir, "resolv.conf"), []byte("nameserver 192.0.2.3"), 0o644) |
| 598 | Expect(err).NotTo(HaveOccurred()) |
| 599 | |
| 600 | conf := fmt.Sprintf(`{ |
| 601 | "cniVersion": "%s", |
| 602 | "name": "mynet", |
| 603 | "type": "ipvlan", |
| 604 | "master": "foo0", |
| 605 | "ipam": { |
| 606 | "type": "host-local", |
| 607 | "dataDir": "%s", |
| 608 | "ranges": [ |
| 609 | [{"subnet":"172.16.1.0/24"}, { "subnet": "10.1.2.0/24" }], |
| 610 | [{ "subnet": "2001:db8:1::/48" }] |
| 611 | ] |
| 612 | }, |
| 613 | "args": { |
| 614 | "cni": { |
| 615 | "ips": ["10.1.2.88", "2001:db8:1::999"] |
| 616 | } |
| 617 | } |
| 618 | }`, ver, tmpDir) |
| 619 | |
| 620 | args := &skel.CmdArgs{ |
| 621 | ContainerID: "dummy", |
| 622 | Netns: nspath, |
| 623 | IfName: ifname, |
| 624 | StdinData: []byte(conf), |
| 625 | } |
| 626 | |
| 627 | // Allocate the IP |
| 628 | r, _, err := testutils.CmdAddWithArgs(args, func() error { |
| 629 | return cmdAdd(args) |
| 630 | }) |
| 631 | Expect(err).NotTo(HaveOccurred()) |
| 632 | result, err := types100.GetResult(r) |
| 633 | Expect(err).NotTo(HaveOccurred()) |
| 634 | Expect(result.IPs).To(HaveLen(2)) |
| 635 | Expect(result.IPs[0].Address.IP).To(Equal(net.ParseIP("10.1.2.88"))) |
| 636 | Expect(result.IPs[1].Address.IP).To(Equal(net.ParseIP("2001:db8:1::999"))) |
| 637 | }) |
| 638 | |
| 639 | It(fmt.Sprintf("[%s] fails if a requested custom IP is not used", ver), func() { |
| 640 | conf := fmt.Sprintf(`{ |
| 641 | "cniVersion": "%s", |
| 642 | "name": "mynet", |
| 643 | "type": "ipvlan", |
| 644 | "master": "foo0", |
| 645 | "ipam": { |
| 646 | "type": "host-local", |
| 647 | "dataDir": "%s", |
| 648 | "ranges": [ |
| 649 | [{ "subnet": "10.1.2.0/24" }], |
| 650 | [{ "subnet": "10.1.3.0/24" }] |
| 651 | ] |
| 652 | }, |
| 653 | "args": { |
| 654 | "cni": { |
| 655 | "ips": ["10.1.2.88", "10.1.2.77"] |
| 656 | } |
| 657 | } |
| 658 | }`, ver, tmpDir) |
| 659 | |
| 660 | args := &skel.CmdArgs{ |
| 661 | ContainerID: "dummy", |
| 662 | Netns: nspath, |
| 663 | IfName: ifname, |
| 664 | StdinData: []byte(conf), |
| 665 | } |
| 666 | |
| 667 | // Allocate the IP |
| 668 | _, _, err := testutils.CmdAddWithArgs(args, func() error { |
| 669 | return cmdAdd(args) |
| 670 | }) |
| 671 | if !testutils.SpecVersionHasMultipleIPs(ver) { |
| 672 | errStr := fmt.Sprintf("CNI version %s does not support more than 1 address per family", ver) |
| 673 | Expect(err).To(MatchError(errStr)) |
| 674 | } else { |
| 675 | Expect(err).To(HaveOccurred()) |
| 676 | // Need to match prefix, because ordering is not guaranteed |
| 677 | Expect(err.Error()).To(HavePrefix("failed to allocate all requested IPs: 10.1.2.")) |
| 678 | } |
| 679 | }) |
| 680 | } |
| 681 | }) |
| 682 | |
| 683 | func mustCIDR(s string) net.IPNet { |
| 684 | ip, n, err := net.ParseCIDR(s) |
| 685 | n.IP = ip |
| 686 | if err != nil { |
| 687 | Fail(err.Error()) |
| 688 | } |
| 689 | |
| 690 | return *n |
| 691 | } |