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