github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/test/e2e/network_test.go (about) 1 package integration 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "os" 7 "time" 8 9 "github.com/containers/common/libnetwork/types" 10 "github.com/hanks177/podman/v4/pkg/rootless" 11 . "github.com/hanks177/podman/v4/test/utils" 12 "github.com/containers/storage/pkg/stringid" 13 . "github.com/onsi/ginkgo" 14 . "github.com/onsi/gomega" 15 . "github.com/onsi/gomega/gexec" 16 ) 17 18 var _ = Describe("Podman network", func() { 19 var ( 20 tempdir string 21 err error 22 podmanTest *PodmanTestIntegration 23 ) 24 25 BeforeEach(func() { 26 tempdir, err = CreateTempDirInTempDir() 27 if err != nil { 28 os.Exit(1) 29 } 30 podmanTest = PodmanTestCreate(tempdir) 31 podmanTest.Setup() 32 }) 33 34 AfterEach(func() { 35 podmanTest.Cleanup() 36 f := CurrentGinkgoTestDescription() 37 processTestResult(f) 38 39 }) 40 41 It("podman --cni-config-dir backwards compat", func() { 42 SkipIfRemote("--cni-config-dir only works locally") 43 netDir, err := CreateTempDirInTempDir() 44 Expect(err).ToNot(HaveOccurred()) 45 defer os.RemoveAll(netDir) 46 session := podmanTest.Podman([]string{"--cni-config-dir", netDir, "network", "ls", "--noheading"}) 47 session.WaitWithDefaultTimeout() 48 Expect(session).Should(Exit(0)) 49 // default network always exists 50 Expect(session.OutputToStringArray()).To(HaveLen(1)) 51 52 // check that the only file in the directory is the network lockfile 53 dir, err := os.Open(netDir) 54 Expect(err).ToNot(HaveOccurred()) 55 names, err := dir.Readdirnames(5) 56 Expect(err).ToNot(HaveOccurred()) 57 Expect(names).To(HaveLen(1)) 58 Expect(names[0]).To(Or(Equal("netavark.lock"), Equal("cni.lock"))) 59 }) 60 61 It("podman network list", func() { 62 name, path := generateNetworkConfig(podmanTest) 63 defer removeConf(path) 64 65 session := podmanTest.Podman([]string{"network", "ls"}) 66 session.WaitWithDefaultTimeout() 67 Expect(session).Should(Exit(0)) 68 Expect(session.OutputToString()).To(ContainSubstring(name)) 69 }) 70 71 It("podman network list -q", func() { 72 name, path := generateNetworkConfig(podmanTest) 73 defer removeConf(path) 74 75 session := podmanTest.Podman([]string{"network", "ls", "--quiet"}) 76 session.WaitWithDefaultTimeout() 77 Expect(session).Should(Exit(0)) 78 Expect(session.OutputToString()).To(ContainSubstring(name)) 79 }) 80 81 It("podman network list --filter success", func() { 82 name, path := generateNetworkConfig(podmanTest) 83 defer removeConf(path) 84 85 session := podmanTest.Podman([]string{"network", "ls", "--filter", "driver=bridge"}) 86 session.WaitWithDefaultTimeout() 87 Expect(session).Should(Exit(0)) 88 Expect(session.OutputToString()).To(ContainSubstring(name)) 89 }) 90 91 It("podman network list --filter driver and name", func() { 92 name, path := generateNetworkConfig(podmanTest) 93 defer removeConf(path) 94 95 session := podmanTest.Podman([]string{"network", "ls", "--filter", "driver=bridge", "--filter", "name=" + name}) 96 session.WaitWithDefaultTimeout() 97 Expect(session).Should(Exit(0)) 98 Expect(session.OutputToString()).To(ContainSubstring(name)) 99 }) 100 101 It("podman network list --filter two names", func() { 102 name1, path1 := generateNetworkConfig(podmanTest) 103 defer removeConf(path1) 104 105 name2, path2 := generateNetworkConfig(podmanTest) 106 defer removeConf(path2) 107 108 session := podmanTest.Podman([]string{"network", "ls", "--filter", "name=" + name1, "--filter", "name=" + name2}) 109 session.WaitWithDefaultTimeout() 110 Expect(session).Should(Exit(0)) 111 Expect(session.OutputToString()).To(ContainSubstring(name1)) 112 Expect(session.OutputToString()).To(ContainSubstring(name2)) 113 }) 114 115 It("podman network list --filter labels", func() { 116 net1 := "labelnet" + stringid.GenerateNonCryptoID() 117 label1 := "testlabel1=abc" 118 label2 := "abcdef" 119 session := podmanTest.Podman([]string{"network", "create", "--label", label1, net1}) 120 session.WaitWithDefaultTimeout() 121 defer podmanTest.removeNetwork(net1) 122 Expect(session).Should(Exit(0)) 123 124 net2 := "labelnet" + stringid.GenerateNonCryptoID() 125 session = podmanTest.Podman([]string{"network", "create", "--label", label1, "--label", label2, net2}) 126 session.WaitWithDefaultTimeout() 127 defer podmanTest.removeNetwork(net2) 128 Expect(session).Should(Exit(0)) 129 130 session = podmanTest.Podman([]string{"network", "ls", "--filter", "label=" + label1}) 131 session.WaitWithDefaultTimeout() 132 Expect(session).Should(Exit(0)) 133 Expect(session.OutputToString()).To(ContainSubstring(net1)) 134 Expect(session.OutputToString()).To(ContainSubstring(net2)) 135 136 session = podmanTest.Podman([]string{"network", "ls", "--filter", "label=" + label1, "--filter", "label=" + label2}) 137 session.WaitWithDefaultTimeout() 138 Expect(session).Should(Exit(0)) 139 Expect(session.OutputToString()).ToNot(ContainSubstring(net1)) 140 Expect(session.OutputToString()).To(ContainSubstring(net2)) 141 }) 142 143 It("podman network list --filter invalid value", func() { 144 net := "net" + stringid.GenerateNonCryptoID() 145 session := podmanTest.Podman([]string{"network", "create", net}) 146 session.WaitWithDefaultTimeout() 147 defer podmanTest.removeNetwork(net) 148 Expect(session).Should(Exit(0)) 149 150 session = podmanTest.Podman([]string{"network", "ls", "--filter", "namr=ab"}) 151 session.WaitWithDefaultTimeout() 152 Expect(session).To(ExitWithError()) 153 Expect(session.ErrorToString()).To(ContainSubstring(`invalid filter "namr"`)) 154 }) 155 156 It("podman network list --filter failure", func() { 157 name, path := generateNetworkConfig(podmanTest) 158 defer removeConf(path) 159 160 session := podmanTest.Podman([]string{"network", "ls", "--filter", "label=abc"}) 161 session.WaitWithDefaultTimeout() 162 Expect(session).Should(Exit(0)) 163 Expect(session.OutputToString()).To(Not(ContainSubstring(name))) 164 }) 165 166 It("podman network ID test", func() { 167 net := "networkIDTest" 168 // the network id should be the sha256 hash of the network name 169 netID := "6073aefe03cdf8f29be5b23ea9795c431868a3a22066a6290b187691614fee84" 170 session := podmanTest.Podman([]string{"network", "create", net}) 171 session.WaitWithDefaultTimeout() 172 defer podmanTest.removeNetwork(net) 173 Expect(session).Should(Exit(0)) 174 175 if podmanTest.NetworkBackend == Netavark { 176 // netavark uses a different algo for determining the id and it is not repeatable 177 getid := podmanTest.Podman([]string{"network", "inspect", net, "--format", "{{.ID}}"}) 178 getid.WaitWithDefaultTimeout() 179 Expect(getid).Should(Exit(0)) 180 netID = getid.OutputToString() 181 } 182 // Tests Default Table Output 183 session = podmanTest.Podman([]string{"network", "ls", "--filter", "id=" + netID}) 184 session.WaitWithDefaultTimeout() 185 Expect(session).Should(Exit(0)) 186 expectedTable := "NETWORK ID NAME DRIVER" 187 Expect(session.OutputToString()).To(ContainSubstring(expectedTable)) 188 189 session = podmanTest.Podman([]string{"network", "ls", "--format", "{{.Name}} {{.ID}}", "--filter", "id=" + netID}) 190 session.WaitWithDefaultTimeout() 191 Expect(session).Should(Exit(0)) 192 Expect(session.OutputToString()).To(ContainSubstring(net + " " + netID[:12])) 193 194 session = podmanTest.Podman([]string{"network", "ls", "--format", "{{.Name}} {{.ID}}", "--filter", "id=" + netID[10:50], "--no-trunc"}) 195 session.WaitWithDefaultTimeout() 196 Expect(session).Should(Exit(0)) 197 Expect(session.OutputToString()).To(ContainSubstring(net + " " + netID)) 198 199 session = podmanTest.Podman([]string{"network", "inspect", netID[:40]}) 200 session.WaitWithDefaultTimeout() 201 Expect(session).Should(Exit(0)) 202 Expect(session.OutputToString()).To(ContainSubstring(net)) 203 204 session = podmanTest.Podman([]string{"network", "inspect", netID[1:]}) 205 session.WaitWithDefaultTimeout() 206 Expect(session).Should(ExitWithError()) 207 Expect(session.ErrorToString()).To(ContainSubstring("network not found")) 208 209 session = podmanTest.Podman([]string{"network", "rm", netID}) 210 session.WaitWithDefaultTimeout() 211 Expect(session).Should(Exit(0)) 212 }) 213 214 rmFunc := func(rm string) { 215 It(fmt.Sprintf("podman network %s no args", rm), func() { 216 session := podmanTest.Podman([]string{"network", rm}) 217 session.WaitWithDefaultTimeout() 218 Expect(session).Should(ExitWithError()) 219 220 }) 221 222 It(fmt.Sprintf("podman network %s", rm), func() { 223 name, path := generateNetworkConfig(podmanTest) 224 defer removeConf(path) 225 226 session := podmanTest.Podman([]string{"network", "ls", "--quiet"}) 227 session.WaitWithDefaultTimeout() 228 Expect(session).Should(Exit(0)) 229 Expect(session.OutputToString()).To(ContainSubstring(name)) 230 231 rm := podmanTest.Podman([]string{"network", rm, name}) 232 rm.WaitWithDefaultTimeout() 233 Expect(rm).Should(Exit(0)) 234 235 results := podmanTest.Podman([]string{"network", "ls", "--quiet"}) 236 results.WaitWithDefaultTimeout() 237 Expect(results).Should(Exit(0)) 238 Expect(results.OutputToString()).To(Not(ContainSubstring(name))) 239 }) 240 } 241 242 rmFunc("rm") 243 rmFunc("remove") 244 245 It("podman network inspect no args", func() { 246 session := podmanTest.Podman([]string{"network", "inspect"}) 247 session.WaitWithDefaultTimeout() 248 Expect(session).Should(ExitWithError()) 249 }) 250 251 It("podman network inspect", func() { 252 name, path := generateNetworkConfig(podmanTest) 253 defer removeConf(path) 254 255 expectedNetworks := []string{name} 256 if !rootless.IsRootless() { 257 // rootful image contains "podman/cni/87-podman-bridge.conflist" for "podman" network 258 expectedNetworks = append(expectedNetworks, "podman") 259 } 260 session := podmanTest.Podman(append([]string{"network", "inspect"}, expectedNetworks...)) 261 session.WaitWithDefaultTimeout() 262 Expect(session).Should(Exit(0)) 263 Expect(session.OutputToString()).To(BeValidJSON()) 264 }) 265 266 It("podman network inspect", func() { 267 name, path := generateNetworkConfig(podmanTest) 268 defer removeConf(path) 269 270 session := podmanTest.Podman([]string{"network", "inspect", name, "--format", "{{.Driver}}"}) 271 session.WaitWithDefaultTimeout() 272 Expect(session).Should(Exit(0)) 273 Expect(session.OutputToString()).To(ContainSubstring("bridge")) 274 }) 275 276 It("podman inspect container single CNI network", func() { 277 netName := "net-" + stringid.GenerateNonCryptoID() 278 network := podmanTest.Podman([]string{"network", "create", "--subnet", "10.50.50.0/24", netName}) 279 network.WaitWithDefaultTimeout() 280 defer podmanTest.removeNetwork(netName) 281 Expect(network).Should(Exit(0)) 282 283 ctrName := "testCtr" 284 container := podmanTest.Podman([]string{"run", "-dt", "--network", netName, "--name", ctrName, ALPINE, "top"}) 285 container.WaitWithDefaultTimeout() 286 Expect(container).Should(Exit(0)) 287 288 inspect := podmanTest.Podman([]string{"inspect", ctrName}) 289 inspect.WaitWithDefaultTimeout() 290 Expect(inspect).Should(Exit(0)) 291 conData := inspect.InspectContainerToJSON() 292 Expect(conData).To(HaveLen(1)) 293 Expect(conData[0].NetworkSettings.Networks).To(HaveLen(1)) 294 net, ok := conData[0].NetworkSettings.Networks[netName] 295 Expect(ok).To(BeTrue()) 296 Expect(net).To(HaveField("NetworkID", netName)) 297 Expect(net).To(HaveField("IPPrefixLen", 24)) 298 Expect(net.IPAddress).To(HavePrefix("10.50.50.")) 299 300 // Necessary to ensure the CNI network is removed cleanly 301 rmAll := podmanTest.Podman([]string{"rm", "-t", "0", "-f", ctrName}) 302 rmAll.WaitWithDefaultTimeout() 303 Expect(rmAll).Should(Exit(0)) 304 }) 305 306 It("podman inspect container two CNI networks (container not running)", func() { 307 netName1 := "net1-" + stringid.GenerateNonCryptoID() 308 network1 := podmanTest.Podman([]string{"network", "create", netName1}) 309 network1.WaitWithDefaultTimeout() 310 defer podmanTest.removeNetwork(netName1) 311 Expect(network1).Should(Exit(0)) 312 313 netName2 := "net2-" + stringid.GenerateNonCryptoID() 314 network2 := podmanTest.Podman([]string{"network", "create", netName2}) 315 network2.WaitWithDefaultTimeout() 316 defer podmanTest.removeNetwork(netName2) 317 Expect(network2).Should(Exit(0)) 318 319 ctrName := "testCtr" 320 container := podmanTest.Podman([]string{"create", "--network", fmt.Sprintf("%s,%s", netName1, netName2), "--name", ctrName, ALPINE, "top"}) 321 container.WaitWithDefaultTimeout() 322 Expect(container).Should(Exit(0)) 323 324 inspect := podmanTest.Podman([]string{"inspect", ctrName}) 325 inspect.WaitWithDefaultTimeout() 326 Expect(inspect).Should(Exit(0)) 327 conData := inspect.InspectContainerToJSON() 328 Expect(conData).To(HaveLen(1)) 329 Expect(conData[0].NetworkSettings.Networks).To(HaveLen(2)) 330 net1, ok := conData[0].NetworkSettings.Networks[netName1] 331 Expect(ok).To(BeTrue()) 332 Expect(net1).To(HaveField("NetworkID", netName1)) 333 net2, ok := conData[0].NetworkSettings.Networks[netName2] 334 Expect(ok).To(BeTrue()) 335 Expect(net2).To(HaveField("NetworkID", netName2)) 336 337 // Necessary to ensure the CNI network is removed cleanly 338 rmAll := podmanTest.Podman([]string{"rm", "-t", "0", "-f", ctrName}) 339 rmAll.WaitWithDefaultTimeout() 340 Expect(rmAll).Should(Exit(0)) 341 }) 342 343 It("podman inspect container two CNI networks", func() { 344 netName1 := "net1-" + stringid.GenerateNonCryptoID() 345 network1 := podmanTest.Podman([]string{"network", "create", "--subnet", "10.50.51.0/25", netName1}) 346 network1.WaitWithDefaultTimeout() 347 defer podmanTest.removeNetwork(netName1) 348 Expect(network1).Should(Exit(0)) 349 350 netName2 := "net2-" + stringid.GenerateNonCryptoID() 351 network2 := podmanTest.Podman([]string{"network", "create", "--subnet", "10.50.51.128/26", netName2}) 352 network2.WaitWithDefaultTimeout() 353 defer podmanTest.removeNetwork(netName2) 354 Expect(network2).Should(Exit(0)) 355 356 ctrName := "testCtr" 357 container := podmanTest.Podman([]string{"run", "-dt", "--network", fmt.Sprintf("%s,%s", netName1, netName2), "--name", ctrName, ALPINE, "top"}) 358 container.WaitWithDefaultTimeout() 359 Expect(container).Should(Exit(0)) 360 361 inspect := podmanTest.Podman([]string{"inspect", ctrName}) 362 inspect.WaitWithDefaultTimeout() 363 Expect(inspect).Should(Exit(0)) 364 conData := inspect.InspectContainerToJSON() 365 Expect(conData).To(HaveLen(1)) 366 Expect(conData[0].NetworkSettings.Networks).To(HaveLen(2)) 367 net1, ok := conData[0].NetworkSettings.Networks[netName1] 368 Expect(ok).To(BeTrue()) 369 Expect(net1).To(HaveField("NetworkID", netName1)) 370 Expect(net1).To(HaveField("IPPrefixLen", 25)) 371 Expect(net1.IPAddress).To(HavePrefix("10.50.51.")) 372 net2, ok := conData[0].NetworkSettings.Networks[netName2] 373 Expect(ok).To(BeTrue()) 374 Expect(net2).To(HaveField("NetworkID", netName2)) 375 Expect(net2).To(HaveField("IPPrefixLen", 26)) 376 Expect(net2.IPAddress).To(HavePrefix("10.50.51.")) 377 378 // Necessary to ensure the CNI network is removed cleanly 379 rmAll := podmanTest.Podman([]string{"rm", "-t", "0", "-f", ctrName}) 380 rmAll.WaitWithDefaultTimeout() 381 Expect(rmAll).Should(Exit(0)) 382 }) 383 384 It("podman network remove after disconnect when container initially created with the network", func() { 385 container := "test" 386 network := "foo" + stringid.GenerateNonCryptoID() 387 388 session := podmanTest.Podman([]string{"network", "create", network}) 389 session.WaitWithDefaultTimeout() 390 defer podmanTest.removeNetwork(network) 391 Expect(session).Should(Exit(0)) 392 393 session = podmanTest.Podman([]string{"run", "--name", container, "--network", network, "-d", ALPINE, "top"}) 394 session.WaitWithDefaultTimeout() 395 Expect(session).Should(Exit(0)) 396 397 session = podmanTest.Podman([]string{"network", "disconnect", network, container}) 398 session.WaitWithDefaultTimeout() 399 Expect(session).Should(Exit(0)) 400 401 session = podmanTest.Podman([]string{"network", "rm", network}) 402 session.WaitWithDefaultTimeout() 403 Expect(session).Should(Exit(0)) 404 }) 405 406 It("podman network remove bogus", func() { 407 session := podmanTest.Podman([]string{"network", "rm", "bogus"}) 408 session.WaitWithDefaultTimeout() 409 Expect(session).Should(Exit(1)) 410 }) 411 412 It("podman network remove --force with pod", func() { 413 netName := "net-" + stringid.GenerateNonCryptoID() 414 session := podmanTest.Podman([]string{"network", "create", netName}) 415 session.WaitWithDefaultTimeout() 416 defer podmanTest.removeNetwork(netName) 417 Expect(session).Should(Exit(0)) 418 419 session = podmanTest.Podman([]string{"pod", "create", "--network", netName}) 420 session.WaitWithDefaultTimeout() 421 Expect(session).Should(Exit(0)) 422 podID := session.OutputToString() 423 424 session = podmanTest.Podman([]string{"create", "--pod", podID, ALPINE}) 425 session.WaitWithDefaultTimeout() 426 Expect(session).Should(Exit(0)) 427 428 session = podmanTest.Podman([]string{"network", "rm", netName}) 429 session.WaitWithDefaultTimeout() 430 Expect(session).Should(Exit(2)) 431 432 session = podmanTest.Podman([]string{"network", "rm", "-t", "0", "--force", netName}) 433 session.WaitWithDefaultTimeout() 434 Expect(session).Should(Exit(0)) 435 436 // check if pod is deleted 437 session = podmanTest.Podman([]string{"pod", "exists", podID}) 438 session.WaitWithDefaultTimeout() 439 Expect(session).Should(Exit(1)) 440 441 // check if net is deleted 442 session = podmanTest.Podman([]string{"network", "ls"}) 443 session.WaitWithDefaultTimeout() 444 Expect(session).Should(Exit(0)) 445 Expect(session.OutputToString()).To(Not(ContainSubstring(netName))) 446 }) 447 448 It("podman network remove with two networks", func() { 449 netName1 := "net1-" + stringid.GenerateNonCryptoID() 450 session := podmanTest.Podman([]string{"network", "create", netName1}) 451 session.WaitWithDefaultTimeout() 452 defer podmanTest.removeNetwork(netName1) 453 Expect(session).Should(Exit(0)) 454 455 netName2 := "net2-" + stringid.GenerateNonCryptoID() 456 session = podmanTest.Podman([]string{"network", "create", netName2}) 457 session.WaitWithDefaultTimeout() 458 defer podmanTest.removeNetwork(netName2) 459 Expect(session).Should(Exit(0)) 460 461 session = podmanTest.Podman([]string{"network", "rm", netName1, netName2}) 462 session.WaitWithDefaultTimeout() 463 Expect(session).Should(Exit(0)) 464 lines := session.OutputToStringArray() 465 Expect(lines[0]).To(Equal(netName1)) 466 Expect(lines[1]).To(Equal(netName2)) 467 }) 468 469 It("podman CNI network with multiple aliases", func() { 470 SkipIfNetavark(podmanTest) 471 var worked bool 472 netName := createNetworkName("aliasTest") 473 session := podmanTest.Podman([]string{"network", "create", netName}) 474 session.WaitWithDefaultTimeout() 475 defer podmanTest.removeNetwork(netName) 476 Expect(session).Should(Exit(0)) 477 478 interval := 250 * time.Millisecond 479 for i := 0; i < 6; i++ { 480 n := podmanTest.Podman([]string{"network", "exists", netName}) 481 n.WaitWithDefaultTimeout() 482 worked = n.ExitCode() == 0 483 if worked { 484 break 485 } 486 time.Sleep(interval) 487 interval *= 2 488 } 489 490 top := podmanTest.Podman([]string{"run", "-dt", "--name=web", "--network=" + netName, "--network-alias=web1", "--network-alias=web2", nginx}) 491 top.WaitWithDefaultTimeout() 492 Expect(top).Should(Exit(0)) 493 interval = 250 * time.Millisecond 494 // Wait for the nginx service to be running 495 for i := 0; i < 6; i++ { 496 // Test curl against the container's name 497 c1 := podmanTest.Podman([]string{"run", "--dns-search", "dns.podman", "--network=" + netName, nginx, "curl", "web"}) 498 c1.WaitWithDefaultTimeout() 499 worked = c1.ExitCode() == 0 500 if worked { 501 break 502 } 503 time.Sleep(interval) 504 interval *= 2 505 } 506 Expect(worked).To(BeTrue()) 507 508 // Nginx is now running so no need to do a loop 509 // Test against the first alias 510 c2 := podmanTest.Podman([]string{"run", "--dns-search", "dns.podman", "--network=" + netName, nginx, "curl", "web1"}) 511 c2.WaitWithDefaultTimeout() 512 Expect(c2).Should(Exit(0)) 513 514 // Test against the second alias 515 c3 := podmanTest.Podman([]string{"run", "--dns-search", "dns.podman", "--network=" + netName, nginx, "curl", "web2"}) 516 c3.WaitWithDefaultTimeout() 517 Expect(c3).Should(Exit(0)) 518 }) 519 520 It("podman Netavark network with multiple aliases", func() { 521 SkipIfCNI(podmanTest) 522 var worked bool 523 netName := createNetworkName("aliasTest") 524 session := podmanTest.Podman([]string{"network", "create", netName}) 525 session.WaitWithDefaultTimeout() 526 defer podmanTest.removeNetwork(netName) 527 Expect(session).Should(Exit(0)) 528 529 interval := 250 * time.Millisecond 530 for i := 0; i < 6; i++ { 531 n := podmanTest.Podman([]string{"network", "exists", netName}) 532 n.WaitWithDefaultTimeout() 533 worked = n.ExitCode() == 0 534 if worked { 535 break 536 } 537 time.Sleep(interval) 538 interval *= 2 539 } 540 541 top := podmanTest.Podman([]string{"run", "-dt", "--name=web", "--network=" + netName, "--network-alias=web1", "--network-alias=web2", nginx}) 542 top.WaitWithDefaultTimeout() 543 Expect(top).Should(Exit(0)) 544 interval = 250 * time.Millisecond 545 // Wait for the nginx service to be running 546 for i := 0; i < 6; i++ { 547 // Test curl against the container's name 548 c1 := podmanTest.Podman([]string{"run", "--dns-search", "dns.podman", "--network=" + netName, nginx, "curl", "web"}) 549 c1.WaitWithDefaultTimeout() 550 worked = c1.ExitCode() == 0 551 if worked { 552 break 553 } 554 time.Sleep(interval) 555 interval *= 2 556 } 557 Expect(worked).To(BeTrue()) 558 559 // Nginx is now running so no need to do a loop 560 // Test against the first alias 561 c2 := podmanTest.Podman([]string{"run", "--dns-search", "dns.podman", "--network=" + netName, nginx, "curl", "web1"}) 562 c2.WaitWithDefaultTimeout() 563 Expect(c2).Should(Exit(0)) 564 565 // Test against the second alias 566 c3 := podmanTest.Podman([]string{"run", "--dns-search", "dns.podman", "--network=" + netName, nginx, "curl", "web2"}) 567 c3.WaitWithDefaultTimeout() 568 Expect(c3).Should(Exit(0)) 569 }) 570 571 It("podman network create/remove macvlan", func() { 572 // Netavark currently does not do dhcp so the this test fails 573 SkipIfNetavark(podmanTest) 574 net := "macvlan" + stringid.GenerateNonCryptoID() 575 nc := podmanTest.Podman([]string{"network", "create", "--macvlan", "lo", net}) 576 nc.WaitWithDefaultTimeout() 577 defer podmanTest.removeNetwork(net) 578 Expect(nc).Should(Exit(0)) 579 580 nc = podmanTest.Podman([]string{"network", "rm", net}) 581 nc.WaitWithDefaultTimeout() 582 Expect(nc).Should(Exit(0)) 583 }) 584 585 It("podman network create/remove macvlan as driver (-d) no device name", func() { 586 // Netavark currently does not do dhcp so the this test fails 587 SkipIfNetavark(podmanTest) 588 net := "macvlan" + stringid.GenerateNonCryptoID() 589 nc := podmanTest.Podman([]string{"network", "create", "-d", "macvlan", net}) 590 nc.WaitWithDefaultTimeout() 591 defer podmanTest.removeNetwork(net) 592 Expect(nc).Should(Exit(0)) 593 594 inspect := podmanTest.Podman([]string{"network", "inspect", net}) 595 inspect.WaitWithDefaultTimeout() 596 Expect(inspect).Should(Exit(0)) 597 598 // JSON the network configuration into something usable 599 var results []types.Network 600 err := json.Unmarshal([]byte(inspect.OutputToString()), &results) 601 Expect(err).To(BeNil()) 602 Expect(results).To(HaveLen(1)) 603 result := results[0] 604 Expect(result).To(HaveField("NetworkInterface", "")) 605 Expect(result.IPAMOptions).To(HaveKeyWithValue("driver", "dhcp")) 606 607 nc = podmanTest.Podman([]string{"network", "rm", net}) 608 nc.WaitWithDefaultTimeout() 609 Expect(nc).Should(Exit(0)) 610 }) 611 612 It("podman network create/remove macvlan as driver (-d) with device name", func() { 613 // Netavark currently does not do dhcp so the this test fails 614 SkipIfNetavark(podmanTest) 615 net := "macvlan" + stringid.GenerateNonCryptoID() 616 nc := podmanTest.Podman([]string{"network", "create", "-d", "macvlan", "-o", "parent=lo", net}) 617 nc.WaitWithDefaultTimeout() 618 defer podmanTest.removeNetwork(net) 619 Expect(nc).Should(Exit(0)) 620 621 inspect := podmanTest.Podman([]string{"network", "inspect", net}) 622 inspect.WaitWithDefaultTimeout() 623 Expect(inspect).Should(Exit(0)) 624 625 var results []types.Network 626 err := json.Unmarshal([]byte(inspect.OutputToString()), &results) 627 Expect(err).To(BeNil()) 628 Expect(results).To(HaveLen(1)) 629 result := results[0] 630 631 Expect(result).To(HaveField("Driver", "macvlan")) 632 Expect(result).To(HaveField("NetworkInterface", "lo")) 633 Expect(result.IPAMOptions).To(HaveKeyWithValue("driver", "dhcp")) 634 Expect(result.Subnets).To(HaveLen(0)) 635 636 nc = podmanTest.Podman([]string{"network", "rm", net}) 637 nc.WaitWithDefaultTimeout() 638 Expect(nc).Should(Exit(0)) 639 }) 640 641 It("podman network exists", func() { 642 net := "net" + stringid.GenerateNonCryptoID() 643 session := podmanTest.Podman([]string{"network", "create", net}) 644 session.WaitWithDefaultTimeout() 645 defer podmanTest.removeNetwork(net) 646 Expect(session).Should(Exit(0)) 647 648 session = podmanTest.Podman([]string{"network", "exists", net}) 649 session.WaitWithDefaultTimeout() 650 Expect(session).Should(Exit(0)) 651 652 session = podmanTest.Podman([]string{"network", "exists", stringid.GenerateNonCryptoID()}) 653 session.WaitWithDefaultTimeout() 654 Expect(session).Should(Exit(1)) 655 }) 656 657 It("podman network create macvlan with network info and options", func() { 658 net := "macvlan" + stringid.GenerateNonCryptoID() 659 nc := podmanTest.Podman([]string{"network", "create", "-d", "macvlan", "-o", "parent=lo", "-o", "mtu=1500", "--gateway", "192.168.1.254", "--subnet", "192.168.1.0/24", net}) 660 nc.WaitWithDefaultTimeout() 661 defer podmanTest.removeNetwork(net) 662 Expect(nc).Should(Exit(0)) 663 664 inspect := podmanTest.Podman([]string{"network", "inspect", net}) 665 inspect.WaitWithDefaultTimeout() 666 Expect(inspect).Should(Exit(0)) 667 668 var results []types.Network 669 err := json.Unmarshal([]byte(inspect.OutputToString()), &results) 670 Expect(err).To(BeNil()) 671 Expect(results).To(HaveLen(1)) 672 result := results[0] 673 674 Expect(result.Options).To(HaveKeyWithValue("mtu", "1500")) 675 Expect(result).To(HaveField("Driver", "macvlan")) 676 Expect(result).To(HaveField("NetworkInterface", "lo")) 677 Expect(result.IPAMOptions).To(HaveKeyWithValue("driver", "host-local")) 678 679 Expect(result.Subnets).To(HaveLen(1)) 680 Expect(result.Subnets[0].Subnet.String()).To(Equal("192.168.1.0/24")) 681 Expect(result.Subnets[0].Gateway.String()).To(Equal("192.168.1.254")) 682 683 nc = podmanTest.Podman([]string{"network", "rm", net}) 684 nc.WaitWithDefaultTimeout() 685 Expect(nc).Should(Exit(0)) 686 }) 687 688 It("podman network prune --filter", func() { 689 // set custom cni directory to prevent flakes 690 podmanTest.NetworkConfigDir = tempdir 691 if IsRemote() { 692 podmanTest.RestartRemoteService() 693 } 694 net1 := "macvlan" + stringid.GenerateNonCryptoID() + "net1" 695 696 nc := podmanTest.Podman([]string{"network", "create", net1}) 697 nc.WaitWithDefaultTimeout() 698 defer podmanTest.removeNetwork(net1) 699 Expect(nc).Should(Exit(0)) 700 701 list := podmanTest.Podman([]string{"network", "ls", "--format", "{{.Name}}"}) 702 list.WaitWithDefaultTimeout() 703 Expect(list).Should(Exit(0)) 704 Expect(list.OutputToStringArray()).Should(HaveLen(2)) 705 706 Expect(list.OutputToStringArray()).Should(ContainElement(net1)) 707 Expect(list.OutputToStringArray()).Should(ContainElement("podman")) 708 709 // -f needed only to skip y/n question 710 prune := podmanTest.Podman([]string{"network", "prune", "-f", "--filter", "until=50"}) 711 prune.WaitWithDefaultTimeout() 712 Expect(prune).Should(Exit(0)) 713 714 listAgain := podmanTest.Podman([]string{"network", "ls", "--format", "{{.Name}}"}) 715 listAgain.WaitWithDefaultTimeout() 716 Expect(listAgain).Should(Exit(0)) 717 Expect(listAgain.OutputToStringArray()).Should(HaveLen(2)) 718 719 Expect(listAgain.OutputToStringArray()).Should(ContainElement(net1)) 720 Expect(listAgain.OutputToStringArray()).Should(ContainElement("podman")) 721 722 // -f needed only to skip y/n question 723 prune = podmanTest.Podman([]string{"network", "prune", "-f", "--filter", "until=5000000000000"}) 724 prune.WaitWithDefaultTimeout() 725 Expect(prune).Should(Exit(0)) 726 727 listAgain = podmanTest.Podman([]string{"network", "ls", "--format", "{{.Name}}"}) 728 listAgain.WaitWithDefaultTimeout() 729 Expect(listAgain).Should(Exit(0)) 730 Expect(listAgain.OutputToStringArray()).Should(HaveLen(1)) 731 732 Expect(listAgain.OutputToStringArray()).ShouldNot(ContainElement(net1)) 733 Expect(listAgain.OutputToStringArray()).Should(ContainElement("podman")) 734 }) 735 736 It("podman network prune", func() { 737 // set custom cni directory to prevent flakes 738 podmanTest.NetworkConfigDir = tempdir 739 if IsRemote() { 740 podmanTest.RestartRemoteService() 741 } 742 // Create two networks 743 // Check they are there 744 // Run a container on one of them 745 // Network Prune 746 // Check that one has been pruned, other remains 747 net := "macvlan" + stringid.GenerateNonCryptoID() 748 net1 := net + "1" 749 net2 := net + "2" 750 nc := podmanTest.Podman([]string{"network", "create", net1}) 751 nc.WaitWithDefaultTimeout() 752 defer podmanTest.removeNetwork(net1) 753 Expect(nc).Should(Exit(0)) 754 755 nc2 := podmanTest.Podman([]string{"network", "create", net2}) 756 nc2.WaitWithDefaultTimeout() 757 defer podmanTest.removeNetwork(net2) 758 Expect(nc2).Should(Exit(0)) 759 760 list := podmanTest.Podman([]string{"network", "ls", "--format", "{{.Name}}"}) 761 list.WaitWithDefaultTimeout() 762 Expect(list.OutputToStringArray()).Should(HaveLen(3)) 763 764 Expect(list.OutputToStringArray()).Should(ContainElement(net1)) 765 Expect(list.OutputToStringArray()).Should(ContainElement(net2)) 766 Expect(list.OutputToStringArray()).Should(ContainElement("podman")) 767 768 session := podmanTest.Podman([]string{"run", "-dt", "--net", net2, ALPINE, "top"}) 769 session.WaitWithDefaultTimeout() 770 Expect(session).Should(Exit(0)) 771 772 prune := podmanTest.Podman([]string{"network", "prune", "-f"}) 773 prune.WaitWithDefaultTimeout() 774 Expect(prune).Should(Exit(0)) 775 776 listAgain := podmanTest.Podman([]string{"network", "ls", "--format", "{{.Name}}"}) 777 listAgain.WaitWithDefaultTimeout() 778 Expect(listAgain).Should(Exit(0)) 779 Expect(listAgain.OutputToStringArray()).Should(HaveLen(2)) 780 781 Expect(listAgain.OutputToStringArray()).ShouldNot(ContainElement(net1)) 782 Expect(listAgain.OutputToStringArray()).Should(ContainElement(net2)) 783 Expect(listAgain.OutputToStringArray()).Should(ContainElement("podman")) 784 }) 785 })