github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/test/e2e/network_connect_disconnect_test.go (about) 1 package integration 2 3 import ( 4 "os" 5 6 . "github.com/hanks177/podman/v4/test/utils" 7 "github.com/containers/storage/pkg/stringid" 8 . "github.com/onsi/ginkgo" 9 . "github.com/onsi/gomega" 10 . "github.com/onsi/gomega/gexec" 11 "github.com/onsi/gomega/types" 12 ) 13 14 var _ = Describe("Podman network connect and disconnect", func() { 15 var ( 16 tempdir string 17 err error 18 podmanTest *PodmanTestIntegration 19 ) 20 21 BeforeEach(func() { 22 tempdir, err = CreateTempDirInTempDir() 23 if err != nil { 24 os.Exit(1) 25 } 26 podmanTest = PodmanTestCreate(tempdir) 27 podmanTest.Setup() 28 }) 29 30 AfterEach(func() { 31 podmanTest.Cleanup() 32 f := CurrentGinkgoTestDescription() 33 processTestResult(f) 34 35 }) 36 37 It("bad network name in disconnect should result in error", func() { 38 dis := podmanTest.Podman([]string{"network", "disconnect", "foobar", "test"}) 39 dis.WaitWithDefaultTimeout() 40 Expect(dis).Should(ExitWithError()) 41 }) 42 43 It("bad container name in network disconnect should result in error", func() { 44 netName := "aliasTest" + stringid.GenerateNonCryptoID() 45 session := podmanTest.Podman([]string{"network", "create", netName}) 46 session.WaitWithDefaultTimeout() 47 Expect(session).Should(Exit(0)) 48 defer podmanTest.removeNetwork(netName) 49 50 dis := podmanTest.Podman([]string{"network", "disconnect", netName, "foobar"}) 51 dis.WaitWithDefaultTimeout() 52 Expect(dis).Should(ExitWithError()) 53 }) 54 55 It("network disconnect with net mode slirp4netns should result in error", func() { 56 netName := "slirp" + stringid.GenerateNonCryptoID() 57 session := podmanTest.Podman([]string{"network", "create", netName}) 58 session.WaitWithDefaultTimeout() 59 Expect(session).Should(Exit(0)) 60 defer podmanTest.removeNetwork(netName) 61 62 session = podmanTest.Podman([]string{"create", "--name", "test", "--network", "slirp4netns", ALPINE}) 63 session.WaitWithDefaultTimeout() 64 Expect(session).Should(Exit(0)) 65 defer podmanTest.removeNetwork(netName) 66 67 con := podmanTest.Podman([]string{"network", "disconnect", netName, "test"}) 68 con.WaitWithDefaultTimeout() 69 Expect(con).Should(ExitWithError()) 70 Expect(con.ErrorToString()).To(ContainSubstring(`"slirp4netns" is not supported: invalid network mode`)) 71 }) 72 73 It("podman network disconnect", func() { 74 netName := "aliasTest" + stringid.GenerateNonCryptoID() 75 session := podmanTest.Podman([]string{"network", "create", netName}) 76 session.WaitWithDefaultTimeout() 77 Expect(session).Should(Exit(0)) 78 defer podmanTest.removeNetwork(netName) 79 80 gw := podmanTest.Podman([]string{"network", "inspect", netName, "--format", "{{(index .Subnets 0).Gateway}}"}) 81 gw.WaitWithDefaultTimeout() 82 Expect(gw).Should(Exit(0)) 83 ns := gw.OutputToString() 84 85 ctr := podmanTest.Podman([]string{"run", "-dt", "--name", "test", "--network", netName, ALPINE, "top"}) 86 ctr.WaitWithDefaultTimeout() 87 Expect(ctr).Should(Exit(0)) 88 89 exec := podmanTest.Podman([]string{"exec", "-it", "test", "ip", "addr", "show", "eth0"}) 90 exec.WaitWithDefaultTimeout() 91 Expect(exec).Should(Exit(0)) 92 93 exec2 := podmanTest.Podman([]string{"exec", "-it", "test", "cat", "/etc/resolv.conf"}) 94 exec2.WaitWithDefaultTimeout() 95 Expect(exec2).Should(Exit(0)) 96 Expect(exec2.OutputToString()).To(ContainSubstring(ns)) 97 98 dis := podmanTest.Podman([]string{"network", "disconnect", netName, "test"}) 99 dis.WaitWithDefaultTimeout() 100 Expect(dis).Should(Exit(0)) 101 Expect(dis.ErrorToString()).Should(Equal("")) 102 103 inspect := podmanTest.Podman([]string{"container", "inspect", "test", "--format", "{{len .NetworkSettings.Networks}}"}) 104 inspect.WaitWithDefaultTimeout() 105 Expect(inspect).Should(Exit(0)) 106 Expect(inspect.OutputToString()).To(Equal("0")) 107 108 exec = podmanTest.Podman([]string{"exec", "-it", "test", "ip", "addr", "show", "eth0"}) 109 exec.WaitWithDefaultTimeout() 110 Expect(exec).Should(ExitWithError()) 111 112 exec3 := podmanTest.Podman([]string{"exec", "-it", "test", "cat", "/etc/resolv.conf"}) 113 exec3.WaitWithDefaultTimeout() 114 Expect(exec3).Should(Exit(0)) 115 Expect(exec3.OutputToString()).ToNot(ContainSubstring(ns)) 116 117 // make sure stats still works https://github.com/containers/podman/issues/13824 118 stats := podmanTest.Podman([]string{"stats", "test", "--no-stream"}) 119 stats.WaitWithDefaultTimeout() 120 Expect(stats).Should(Exit(0)) 121 }) 122 123 It("bad network name in connect should result in error", func() { 124 dis := podmanTest.Podman([]string{"network", "connect", "foobar", "test"}) 125 dis.WaitWithDefaultTimeout() 126 Expect(dis).Should(ExitWithError()) 127 }) 128 129 It("bad container name in network connect should result in error", func() { 130 netName := "aliasTest" + stringid.GenerateNonCryptoID() 131 session := podmanTest.Podman([]string{"network", "create", netName}) 132 session.WaitWithDefaultTimeout() 133 Expect(session).Should(Exit(0)) 134 defer podmanTest.removeNetwork(netName) 135 136 dis := podmanTest.Podman([]string{"network", "connect", netName, "foobar"}) 137 dis.WaitWithDefaultTimeout() 138 Expect(dis).Should(ExitWithError()) 139 }) 140 141 It("network connect with net mode slirp4netns should result in error", func() { 142 netName := "slirp" + stringid.GenerateNonCryptoID() 143 session := podmanTest.Podman([]string{"network", "create", netName}) 144 session.WaitWithDefaultTimeout() 145 Expect(session).Should(Exit(0)) 146 defer podmanTest.removeNetwork(netName) 147 148 session = podmanTest.Podman([]string{"create", "--name", "test", "--network", "slirp4netns", ALPINE}) 149 session.WaitWithDefaultTimeout() 150 Expect(session).Should(Exit(0)) 151 defer podmanTest.removeNetwork(netName) 152 153 con := podmanTest.Podman([]string{"network", "connect", netName, "test"}) 154 con.WaitWithDefaultTimeout() 155 Expect(con).Should(ExitWithError()) 156 Expect(con.ErrorToString()).To(ContainSubstring(`"slirp4netns" is not supported: invalid network mode`)) 157 }) 158 159 It("podman connect on a container that already is connected to the network should error", func() { 160 netName := "aliasTest" + stringid.GenerateNonCryptoID() 161 session := podmanTest.Podman([]string{"network", "create", netName}) 162 session.WaitWithDefaultTimeout() 163 Expect(session).Should(Exit(0)) 164 defer podmanTest.removeNetwork(netName) 165 166 ctr := podmanTest.Podman([]string{"create", "--name", "test", "--network", netName, ALPINE, "top"}) 167 ctr.WaitWithDefaultTimeout() 168 Expect(ctr).Should(Exit(0)) 169 cid := ctr.OutputToString() 170 171 // network alias container short id is always added and shown in inspect 172 inspect := podmanTest.Podman([]string{"container", "inspect", "test", "--format", "{{(index .NetworkSettings.Networks \"" + netName + "\").Aliases}}"}) 173 inspect.WaitWithDefaultTimeout() 174 Expect(inspect).Should(Exit(0)) 175 Expect(inspect.OutputToString()).To(Equal("[" + cid[0:12] + "]")) 176 177 con := podmanTest.Podman([]string{"network", "connect", netName, "test"}) 178 con.WaitWithDefaultTimeout() 179 Expect(con).Should(ExitWithError()) 180 }) 181 182 It("podman network connect", func() { 183 netName := "aliasTest" + stringid.GenerateNonCryptoID() 184 session := podmanTest.Podman([]string{"network", "create", netName}) 185 session.WaitWithDefaultTimeout() 186 Expect(session).Should(Exit(0)) 187 defer podmanTest.removeNetwork(netName) 188 189 ctr := podmanTest.Podman([]string{"run", "-dt", "--name", "test", "--network", netName, ALPINE, "top"}) 190 ctr.WaitWithDefaultTimeout() 191 Expect(ctr).Should(Exit(0)) 192 cid := ctr.OutputToString() 193 194 exec := podmanTest.Podman([]string{"exec", "-it", "test", "ip", "addr", "show", "eth0"}) 195 exec.WaitWithDefaultTimeout() 196 Expect(exec).Should(Exit(0)) 197 198 // Create a second network 199 newNetName := "aliasTest" + stringid.GenerateNonCryptoID() 200 session = podmanTest.Podman([]string{"network", "create", newNetName, "--subnet", "10.11.100.0/24"}) 201 session.WaitWithDefaultTimeout() 202 Expect(session).Should(Exit(0)) 203 defer podmanTest.removeNetwork(newNetName) 204 205 gw := podmanTest.Podman([]string{"network", "inspect", newNetName, "--format", "{{(index .Subnets 0).Gateway}}"}) 206 gw.WaitWithDefaultTimeout() 207 Expect(gw).Should(Exit(0)) 208 ns := gw.OutputToString() 209 210 exec2 := podmanTest.Podman([]string{"exec", "-it", "test", "cat", "/etc/resolv.conf"}) 211 exec2.WaitWithDefaultTimeout() 212 Expect(exec2).Should(Exit(0)) 213 Expect(exec2.OutputToString()).ToNot(ContainSubstring(ns)) 214 215 ip := "10.11.100.99" 216 mac := "44:11:44:11:44:11" 217 connect := podmanTest.Podman([]string{"network", "connect", "--ip", ip, "--mac-address", mac, newNetName, "test"}) 218 connect.WaitWithDefaultTimeout() 219 Expect(connect).Should(Exit(0)) 220 Expect(connect.ErrorToString()).Should(Equal("")) 221 222 inspect := podmanTest.Podman([]string{"container", "inspect", "test", "--format", "{{len .NetworkSettings.Networks}}"}) 223 inspect.WaitWithDefaultTimeout() 224 Expect(inspect).Should(Exit(0)) 225 Expect(inspect.OutputToString()).To(Equal("2")) 226 227 // network alias container short id is always added and shown in inspect 228 inspect = podmanTest.Podman([]string{"container", "inspect", "test", "--format", "{{(index .NetworkSettings.Networks \"" + newNetName + "\").Aliases}}"}) 229 inspect.WaitWithDefaultTimeout() 230 Expect(inspect).Should(Exit(0)) 231 Expect(inspect.OutputToString()).To(Equal("[" + cid[0:12] + "]")) 232 233 exec = podmanTest.Podman([]string{"exec", "-it", "test", "ip", "addr", "show", "eth1"}) 234 exec.WaitWithDefaultTimeout() 235 Expect(exec).Should(Exit(0)) 236 Expect(exec.OutputToString()).Should(ContainSubstring(ip)) 237 Expect(exec.OutputToString()).Should(ContainSubstring(mac)) 238 239 exec3 := podmanTest.Podman([]string{"exec", "-it", "test", "cat", "/etc/resolv.conf"}) 240 exec3.WaitWithDefaultTimeout() 241 Expect(exec3).Should(Exit(0)) 242 Expect(exec3.OutputToString()).To(ContainSubstring(ns)) 243 244 // make sure stats works https://github.com/containers/podman/issues/13824 245 stats := podmanTest.Podman([]string{"stats", "test", "--no-stream"}) 246 stats.WaitWithDefaultTimeout() 247 Expect(stats).Should(Exit(0)) 248 249 // make sure no logrus errors are shown https://github.com/containers/podman/issues/9602 250 rm := podmanTest.Podman([]string{"rm", "--time=0", "-f", "test"}) 251 rm.WaitWithDefaultTimeout() 252 Expect(rm).Should(Exit(0)) 253 Expect(rm.ErrorToString()).To(Equal("")) 254 }) 255 256 It("podman network connect when not running", func() { 257 netName1 := "connect1" + stringid.GenerateNonCryptoID() 258 session := podmanTest.Podman([]string{"network", "create", netName1}) 259 session.WaitWithDefaultTimeout() 260 Expect(session).Should(Exit(0)) 261 defer podmanTest.removeNetwork(netName1) 262 263 netName2 := "connect2" + stringid.GenerateNonCryptoID() 264 session = podmanTest.Podman([]string{"network", "create", netName2}) 265 session.WaitWithDefaultTimeout() 266 Expect(session).Should(Exit(0)) 267 defer podmanTest.removeNetwork(netName2) 268 269 ctr := podmanTest.Podman([]string{"create", "--name", "test", "--network", netName1, ALPINE, "top"}) 270 ctr.WaitWithDefaultTimeout() 271 Expect(ctr).Should(Exit(0)) 272 273 dis := podmanTest.Podman([]string{"network", "connect", netName2, "test"}) 274 dis.WaitWithDefaultTimeout() 275 Expect(dis).Should(Exit(0)) 276 277 inspect := podmanTest.Podman([]string{"container", "inspect", "test", "--format", "{{len .NetworkSettings.Networks}}"}) 278 inspect.WaitWithDefaultTimeout() 279 Expect(inspect).Should(Exit(0)) 280 Expect(inspect.OutputToString()).To(Equal("2")) 281 282 start := podmanTest.Podman([]string{"start", "test"}) 283 start.WaitWithDefaultTimeout() 284 Expect(start).Should(Exit(0)) 285 286 exec := podmanTest.Podman([]string{"exec", "-it", "test", "ip", "addr", "show", "eth0"}) 287 exec.WaitWithDefaultTimeout() 288 Expect(exec).Should(Exit(0)) 289 290 exec = podmanTest.Podman([]string{"exec", "-it", "test", "ip", "addr", "show", "eth1"}) 291 exec.WaitWithDefaultTimeout() 292 Expect(exec).Should(Exit(0)) 293 }) 294 295 It("podman network connect and run with network ID", func() { 296 netName := "ID" + stringid.GenerateNonCryptoID() 297 session := podmanTest.Podman([]string{"network", "create", netName}) 298 session.WaitWithDefaultTimeout() 299 Expect(session).Should(Exit(0)) 300 defer podmanTest.removeNetwork(netName) 301 302 session = podmanTest.Podman([]string{"network", "ls", "--format", "{{.ID}}", "--filter", "name=" + netName}) 303 session.WaitWithDefaultTimeout() 304 Expect(session).Should(Exit(0)) 305 netID := session.OutputToString() 306 307 ctr := podmanTest.Podman([]string{"run", "-dt", "--name", "test", "--network", netID, "--network-alias", "somealias", ALPINE, "top"}) 308 ctr.WaitWithDefaultTimeout() 309 Expect(ctr).Should(Exit(0)) 310 311 exec := podmanTest.Podman([]string{"exec", "-it", "test", "ip", "addr", "show", "eth0"}) 312 exec.WaitWithDefaultTimeout() 313 Expect(exec).Should(Exit(0)) 314 315 // Create a second network 316 newNetName := "ID2" + stringid.GenerateNonCryptoID() 317 session = podmanTest.Podman([]string{"network", "create", newNetName}) 318 session.WaitWithDefaultTimeout() 319 Expect(session).Should(Exit(0)) 320 defer podmanTest.removeNetwork(newNetName) 321 322 session = podmanTest.Podman([]string{"network", "ls", "--format", "{{.ID}}", "--filter", "name=" + newNetName}) 323 session.WaitWithDefaultTimeout() 324 Expect(session).Should(Exit(0)) 325 newNetID := session.OutputToString() 326 327 connect := podmanTest.Podman([]string{"network", "connect", "--alias", "secondalias", newNetID, "test"}) 328 connect.WaitWithDefaultTimeout() 329 Expect(connect).Should(Exit(0)) 330 331 inspect := podmanTest.Podman([]string{"container", "inspect", "test", "--format", "{{.NetworkSettings.Networks}}"}) 332 inspect.WaitWithDefaultTimeout() 333 Expect(inspect).Should(Exit(0)) 334 Expect(inspect.OutputToString()).To(ContainSubstring(netName)) 335 Expect(inspect.OutputToString()).To(ContainSubstring(newNetName)) 336 337 exec = podmanTest.Podman([]string{"exec", "-it", "test", "ip", "addr", "show", "eth1"}) 338 exec.WaitWithDefaultTimeout() 339 Expect(exec).Should(Exit(0)) 340 }) 341 342 It("podman network disconnect when not running", func() { 343 netName1 := "aliasTest" + stringid.GenerateNonCryptoID() 344 session := podmanTest.Podman([]string{"network", "create", netName1}) 345 session.WaitWithDefaultTimeout() 346 Expect(session).Should(Exit(0)) 347 defer podmanTest.removeNetwork(netName1) 348 349 netName2 := "aliasTest" + stringid.GenerateNonCryptoID() 350 session2 := podmanTest.Podman([]string{"network", "create", netName2}) 351 session2.WaitWithDefaultTimeout() 352 Expect(session2).Should(Exit(0)) 353 defer podmanTest.removeNetwork(netName2) 354 355 ctr := podmanTest.Podman([]string{"create", "--name", "test", "--network", netName1 + "," + netName2, ALPINE, "top"}) 356 ctr.WaitWithDefaultTimeout() 357 Expect(ctr).Should(Exit(0)) 358 359 dis := podmanTest.Podman([]string{"network", "disconnect", netName1, "test"}) 360 dis.WaitWithDefaultTimeout() 361 Expect(dis).Should(Exit(0)) 362 363 inspect := podmanTest.Podman([]string{"container", "inspect", "test", "--format", "{{len .NetworkSettings.Networks}}"}) 364 inspect.WaitWithDefaultTimeout() 365 Expect(inspect).Should(Exit(0)) 366 Expect(inspect.OutputToString()).To(Equal("1")) 367 368 start := podmanTest.Podman([]string{"start", "test"}) 369 start.WaitWithDefaultTimeout() 370 Expect(start).Should(Exit(0)) 371 372 exec := podmanTest.Podman([]string{"exec", "-it", "test", "ip", "addr", "show", "eth0"}) 373 exec.WaitWithDefaultTimeout() 374 375 // because the network interface order is not guaranteed to be the same we have to check both eth0 and eth1 376 // if eth0 did not exists eth1 has to exists 377 var exitMatcher types.GomegaMatcher = ExitWithError() 378 if exec.ExitCode() > 0 { 379 exitMatcher = Exit(0) 380 } 381 382 exec = podmanTest.Podman([]string{"exec", "-it", "test", "ip", "addr", "show", "eth1"}) 383 exec.WaitWithDefaultTimeout() 384 Expect(exec).Should(exitMatcher) 385 }) 386 387 It("podman network disconnect and run with network ID", func() { 388 netName := "aliasTest" + stringid.GenerateNonCryptoID() 389 session := podmanTest.Podman([]string{"network", "create", netName}) 390 session.WaitWithDefaultTimeout() 391 Expect(session).Should(Exit(0)) 392 defer podmanTest.removeNetwork(netName) 393 394 session = podmanTest.Podman([]string{"network", "ls", "--format", "{{.ID}}", "--filter", "name=" + netName}) 395 session.WaitWithDefaultTimeout() 396 Expect(session).Should(Exit(0)) 397 netID := session.OutputToString() 398 399 ctr := podmanTest.Podman([]string{"run", "-dt", "--name", "test", "--network", netID, ALPINE, "top"}) 400 ctr.WaitWithDefaultTimeout() 401 Expect(ctr).Should(Exit(0)) 402 403 exec := podmanTest.Podman([]string{"exec", "-it", "test", "ip", "addr", "show", "eth0"}) 404 exec.WaitWithDefaultTimeout() 405 Expect(exec).Should(Exit(0)) 406 407 dis := podmanTest.Podman([]string{"network", "disconnect", netID, "test"}) 408 dis.WaitWithDefaultTimeout() 409 Expect(dis).Should(Exit(0)) 410 411 inspect := podmanTest.Podman([]string{"container", "inspect", "test", "--format", "{{len .NetworkSettings.Networks}}"}) 412 inspect.WaitWithDefaultTimeout() 413 Expect(inspect).Should(Exit(0)) 414 Expect(inspect.OutputToString()).To(Equal("0")) 415 416 exec = podmanTest.Podman([]string{"exec", "-it", "test", "ip", "addr", "show", "eth0"}) 417 exec.WaitWithDefaultTimeout() 418 Expect(exec).Should(ExitWithError()) 419 }) 420 })