github.com/AbhinandanKurakure/podman/v3@v3.4.10/test/e2e/run_networking_test.go (about) 1 package integration 2 3 import ( 4 "fmt" 5 "os" 6 "strings" 7 8 . "github.com/containers/podman/v3/test/utils" 9 "github.com/containers/storage/pkg/stringid" 10 . "github.com/onsi/ginkgo" 11 . "github.com/onsi/gomega" 12 . "github.com/onsi/gomega/gexec" 13 "github.com/uber/jaeger-client-go/utils" 14 ) 15 16 var _ = Describe("Podman run networking", func() { 17 var ( 18 tempdir string 19 err error 20 podmanTest *PodmanTestIntegration 21 hostname, _ = os.Hostname() 22 ) 23 24 BeforeEach(func() { 25 tempdir, err = CreateTempDirInTempDir() 26 if err != nil { 27 os.Exit(1) 28 } 29 podmanTest = PodmanTestCreate(tempdir) 30 podmanTest.Setup() 31 podmanTest.SeedImages() 32 }) 33 34 AfterEach(func() { 35 podmanTest.Cleanup() 36 f := CurrentGinkgoTestDescription() 37 processTestResult(f) 38 39 }) 40 41 It("podman run network connection with default bridge", func() { 42 session := podmanTest.Podman([]string{"run", "-dt", ALPINE, "wget", "www.podman.io"}) 43 session.Wait(90) 44 Expect(session).Should(Exit(0)) 45 }) 46 47 It("podman run network connection with host", func() { 48 session := podmanTest.Podman([]string{"run", "-dt", "--network", "host", ALPINE, "wget", "www.podman.io"}) 49 session.Wait(90) 50 Expect(session).Should(Exit(0)) 51 }) 52 53 It("podman run network connection with default", func() { 54 session := podmanTest.Podman([]string{"run", "--network", "default", ALPINE, "wget", "www.podman.io"}) 55 session.WaitWithDefaultTimeout() 56 Expect(session).Should(Exit(0)) 57 }) 58 59 It("podman run network connection with none", func() { 60 session := podmanTest.Podman([]string{"run", "--network", "none", ALPINE, "wget", "www.podman.io"}) 61 session.WaitWithDefaultTimeout() 62 Expect(session).Should(Exit(1)) 63 Expect(session.ErrorToString()).To(ContainSubstring("wget: bad address 'www.podman.io'")) 64 }) 65 66 It("podman run network connection with private", func() { 67 session := podmanTest.Podman([]string{"run", "--network", "private", ALPINE, "wget", "www.podman.io"}) 68 session.WaitWithDefaultTimeout() 69 Expect(session).Should(Exit(0)) 70 }) 71 72 It("podman run network connection with loopback", func() { 73 session := podmanTest.Podman([]string{"run", "--network", "host", ALPINE, "wget", "www.podman.io"}) 74 session.WaitWithDefaultTimeout() 75 Expect(session).Should(Exit(0)) 76 }) 77 78 It("podman run network expose port 222", func() { 79 SkipIfRootless("iptables is not supported for rootless users") 80 session := podmanTest.Podman([]string{"run", "-dt", "--expose", "222-223", "-P", ALPINE, "/bin/sh"}) 81 session.Wait(30) 82 Expect(session).Should(Exit(0)) 83 results := SystemExec("iptables", []string{"-t", "nat", "-L"}) 84 Expect(results).Should(Exit(0)) 85 Expect(results.OutputToString()).To(ContainSubstring("222")) 86 Expect(results.OutputToString()).To(ContainSubstring("223")) 87 }) 88 89 It("podman run -p 80", func() { 90 name := "testctr" 91 session := podmanTest.Podman([]string{"create", "-t", "-p", "80", "--name", name, ALPINE, "/bin/sh"}) 92 session.WaitWithDefaultTimeout() 93 inspectOut := podmanTest.InspectContainer(name) 94 Expect(len(inspectOut)).To(Equal(1)) 95 Expect(len(inspectOut[0].NetworkSettings.Ports)).To(Equal(1)) 96 Expect(len(inspectOut[0].NetworkSettings.Ports["80/tcp"])).To(Equal(1)) 97 Expect(inspectOut[0].NetworkSettings.Ports["80/tcp"][0].HostPort).To(Not(Equal("80"))) 98 Expect(inspectOut[0].NetworkSettings.Ports["80/tcp"][0].HostIP).To(Equal("")) 99 }) 100 101 It("podman run -p 80-82 -p 8090:8090", func() { 102 name := "testctr" 103 session := podmanTest.Podman([]string{"create", "-t", "-p", "80-82", "-p", "8090:8090", "--name", name, ALPINE, "/bin/sh"}) 104 session.WaitWithDefaultTimeout() 105 inspectOut := podmanTest.InspectContainer(name) 106 Expect(len(inspectOut)).To(Equal(1)) 107 Expect(len(inspectOut[0].NetworkSettings.Ports)).To(Equal(4)) 108 Expect(len(inspectOut[0].NetworkSettings.Ports["80/tcp"])).To(Equal(1)) 109 Expect(inspectOut[0].NetworkSettings.Ports["80/tcp"][0].HostPort).To(Not(Equal("80"))) 110 Expect(inspectOut[0].NetworkSettings.Ports["80/tcp"][0].HostIP).To(Equal("")) 111 Expect(len(inspectOut[0].NetworkSettings.Ports["81/tcp"])).To(Equal(1)) 112 Expect(inspectOut[0].NetworkSettings.Ports["81/tcp"][0].HostPort).To(Not(Equal("81"))) 113 Expect(inspectOut[0].NetworkSettings.Ports["81/tcp"][0].HostIP).To(Equal("")) 114 Expect(len(inspectOut[0].NetworkSettings.Ports["82/tcp"])).To(Equal(1)) 115 Expect(inspectOut[0].NetworkSettings.Ports["82/tcp"][0].HostPort).To(Not(Equal("82"))) 116 Expect(inspectOut[0].NetworkSettings.Ports["82/tcp"][0].HostIP).To(Equal("")) 117 Expect(len(inspectOut[0].NetworkSettings.Ports["8090/tcp"])).To(Equal(1)) 118 Expect(inspectOut[0].NetworkSettings.Ports["8090/tcp"][0].HostPort).To(Equal("8090")) 119 Expect(inspectOut[0].NetworkSettings.Ports["8090/tcp"][0].HostIP).To(Equal("")) 120 }) 121 122 It("podman run -p 80-81 -p 8180-8181", func() { 123 name := "testctr" 124 session := podmanTest.Podman([]string{"create", "-t", "-p", "80-81", "-p", "8180-8181", "--name", name, ALPINE, "/bin/sh"}) 125 session.WaitWithDefaultTimeout() 126 inspectOut := podmanTest.InspectContainer(name) 127 Expect(len(inspectOut)).To(Equal(1)) 128 Expect(len(inspectOut[0].NetworkSettings.Ports)).To(Equal(4)) 129 Expect(len(inspectOut[0].NetworkSettings.Ports["80/tcp"])).To(Equal(1)) 130 Expect(inspectOut[0].NetworkSettings.Ports["80/tcp"][0].HostPort).To(Not(Equal("80"))) 131 Expect(inspectOut[0].NetworkSettings.Ports["80/tcp"][0].HostIP).To(Equal("")) 132 Expect(len(inspectOut[0].NetworkSettings.Ports["81/tcp"])).To(Equal(1)) 133 Expect(inspectOut[0].NetworkSettings.Ports["81/tcp"][0].HostPort).To(Not(Equal("81"))) 134 Expect(inspectOut[0].NetworkSettings.Ports["81/tcp"][0].HostIP).To(Equal("")) 135 Expect(len(inspectOut[0].NetworkSettings.Ports["8180/tcp"])).To(Equal(1)) 136 Expect(inspectOut[0].NetworkSettings.Ports["8180/tcp"][0].HostPort).To(Not(Equal("8180"))) 137 Expect(inspectOut[0].NetworkSettings.Ports["8180/tcp"][0].HostIP).To(Equal("")) 138 Expect(len(inspectOut[0].NetworkSettings.Ports["8181/tcp"])).To(Equal(1)) 139 Expect(inspectOut[0].NetworkSettings.Ports["8181/tcp"][0].HostPort).To(Not(Equal("8181"))) 140 Expect(inspectOut[0].NetworkSettings.Ports["8181/tcp"][0].HostIP).To(Equal("")) 141 }) 142 143 It("podman run -p 80 -p 8280-8282:8280-8282", func() { 144 name := "testctr" 145 session := podmanTest.Podman([]string{"create", "-t", "-p", "80", "-p", "8280-8282:8280-8282", "--name", name, ALPINE, "/bin/sh"}) 146 session.WaitWithDefaultTimeout() 147 inspectOut := podmanTest.InspectContainer(name) 148 Expect(len(inspectOut)).To(Equal(1)) 149 Expect(len(inspectOut[0].NetworkSettings.Ports)).To(Equal(4)) 150 Expect(len(inspectOut[0].NetworkSettings.Ports["80/tcp"])).To(Equal(1)) 151 Expect(inspectOut[0].NetworkSettings.Ports["80/tcp"][0].HostPort).To(Not(Equal("80"))) 152 Expect(inspectOut[0].NetworkSettings.Ports["80/tcp"][0].HostIP).To(Equal("")) 153 Expect(len(inspectOut[0].NetworkSettings.Ports["8280/tcp"])).To(Equal(1)) 154 Expect(inspectOut[0].NetworkSettings.Ports["8280/tcp"][0].HostPort).To(Equal("8280")) 155 Expect(inspectOut[0].NetworkSettings.Ports["8280/tcp"][0].HostIP).To(Equal("")) 156 Expect(len(inspectOut[0].NetworkSettings.Ports["8281/tcp"])).To(Equal(1)) 157 Expect(inspectOut[0].NetworkSettings.Ports["8281/tcp"][0].HostPort).To(Equal("8281")) 158 Expect(inspectOut[0].NetworkSettings.Ports["8281/tcp"][0].HostIP).To(Equal("")) 159 Expect(len(inspectOut[0].NetworkSettings.Ports["8282/tcp"])).To(Equal(1)) 160 Expect(inspectOut[0].NetworkSettings.Ports["8282/tcp"][0].HostPort).To(Equal("8282")) 161 Expect(inspectOut[0].NetworkSettings.Ports["8282/tcp"][0].HostIP).To(Equal("")) 162 }) 163 164 It("podman run -p 8380:80", func() { 165 name := "testctr" 166 session := podmanTest.Podman([]string{"create", "-t", "-p", "8380:80", "--name", name, ALPINE, "/bin/sh"}) 167 session.WaitWithDefaultTimeout() 168 inspectOut := podmanTest.InspectContainer(name) 169 Expect(len(inspectOut)).To(Equal(1)) 170 Expect(len(inspectOut[0].NetworkSettings.Ports)).To(Equal(1)) 171 Expect(len(inspectOut[0].NetworkSettings.Ports["80/tcp"])).To(Equal(1)) 172 Expect(inspectOut[0].NetworkSettings.Ports["80/tcp"][0].HostPort).To(Equal("8380")) 173 Expect(inspectOut[0].NetworkSettings.Ports["80/tcp"][0].HostIP).To(Equal("")) 174 }) 175 176 It("podman run -p 8480:80/TCP", func() { 177 name := "testctr" 178 // "TCP" in upper characters 179 session := podmanTest.Podman([]string{"create", "-t", "-p", "8480:80/TCP", "--name", name, ALPINE, "/bin/sh"}) 180 session.WaitWithDefaultTimeout() 181 inspectOut := podmanTest.InspectContainer(name) 182 Expect(len(inspectOut)).To(Equal(1)) 183 Expect(len(inspectOut[0].NetworkSettings.Ports)).To(Equal(1)) 184 // "tcp" in lower characters 185 Expect(len(inspectOut[0].NetworkSettings.Ports["80/tcp"])).To(Equal(1)) 186 Expect(inspectOut[0].NetworkSettings.Ports["80/tcp"][0].HostPort).To(Equal("8480")) 187 Expect(inspectOut[0].NetworkSettings.Ports["80/tcp"][0].HostIP).To(Equal("")) 188 }) 189 190 It("podman run -p 80/udp", func() { 191 name := "testctr" 192 session := podmanTest.Podman([]string{"create", "-t", "-p", "80/udp", "--name", name, ALPINE, "/bin/sh"}) 193 session.WaitWithDefaultTimeout() 194 inspectOut := podmanTest.InspectContainer(name) 195 Expect(len(inspectOut)).To(Equal(1)) 196 Expect(len(inspectOut[0].NetworkSettings.Ports)).To(Equal(1)) 197 Expect(len(inspectOut[0].NetworkSettings.Ports["80/udp"])).To(Equal(1)) 198 Expect(inspectOut[0].NetworkSettings.Ports["80/udp"][0].HostPort).To(Not(Equal("80"))) 199 Expect(inspectOut[0].NetworkSettings.Ports["80/udp"][0].HostIP).To(Equal("")) 200 }) 201 202 It("podman run -p 127.0.0.1:8580:80", func() { 203 name := "testctr" 204 session := podmanTest.Podman([]string{"create", "-t", "-p", "127.0.0.1:8580:80", "--name", name, ALPINE, "/bin/sh"}) 205 session.WaitWithDefaultTimeout() 206 inspectOut := podmanTest.InspectContainer(name) 207 Expect(len(inspectOut)).To(Equal(1)) 208 Expect(len(inspectOut[0].NetworkSettings.Ports)).To(Equal(1)) 209 Expect(len(inspectOut[0].NetworkSettings.Ports["80/tcp"])).To(Equal(1)) 210 Expect(inspectOut[0].NetworkSettings.Ports["80/tcp"][0].HostPort).To(Equal("8580")) 211 Expect(inspectOut[0].NetworkSettings.Ports["80/tcp"][0].HostIP).To(Equal("127.0.0.1")) 212 }) 213 214 It("podman run -p 127.0.0.1:8680:80/udp", func() { 215 name := "testctr" 216 session := podmanTest.Podman([]string{"create", "-t", "-p", "127.0.0.1:8680:80/udp", "--name", name, ALPINE, "/bin/sh"}) 217 session.WaitWithDefaultTimeout() 218 inspectOut := podmanTest.InspectContainer(name) 219 Expect(len(inspectOut)).To(Equal(1)) 220 Expect(len(inspectOut[0].NetworkSettings.Ports)).To(Equal(1)) 221 Expect(len(inspectOut[0].NetworkSettings.Ports["80/udp"])).To(Equal(1)) 222 Expect(inspectOut[0].NetworkSettings.Ports["80/udp"][0].HostPort).To(Equal("8680")) 223 Expect(inspectOut[0].NetworkSettings.Ports["80/udp"][0].HostIP).To(Equal("127.0.0.1")) 224 }) 225 226 It("podman run -p [::1]:8780:80/udp", func() { 227 name := "testctr" 228 session := podmanTest.Podman([]string{"create", "-t", "-p", "[::1]:8780:80/udp", "--name", name, ALPINE, "/bin/sh"}) 229 session.WaitWithDefaultTimeout() 230 inspectOut := podmanTest.InspectContainer(name) 231 Expect(len(inspectOut)).To(Equal(1)) 232 Expect(len(inspectOut[0].NetworkSettings.Ports)).To(Equal(1)) 233 Expect(len(inspectOut[0].NetworkSettings.Ports["80/udp"])).To(Equal(1)) 234 Expect(inspectOut[0].NetworkSettings.Ports["80/udp"][0].HostPort).To(Equal("8780")) 235 Expect(inspectOut[0].NetworkSettings.Ports["80/udp"][0].HostIP).To(Equal("::1")) 236 }) 237 238 It("podman run -p [::1]:8880:80/tcp", func() { 239 name := "testctr" 240 session := podmanTest.Podman([]string{"create", "-t", "-p", "[::1]:8880:80/tcp", "--name", name, ALPINE, "/bin/sh"}) 241 session.WaitWithDefaultTimeout() 242 inspectOut := podmanTest.InspectContainer(name) 243 Expect(len(inspectOut)).To(Equal(1)) 244 Expect(len(inspectOut[0].NetworkSettings.Ports)).To(Equal(1)) 245 Expect(len(inspectOut[0].NetworkSettings.Ports["80/tcp"])).To(Equal(1)) 246 Expect(inspectOut[0].NetworkSettings.Ports["80/tcp"][0].HostPort).To(Equal("8880")) 247 Expect(inspectOut[0].NetworkSettings.Ports["80/tcp"][0].HostIP).To(Equal("::1")) 248 }) 249 250 It("podman run --expose 80 -P", func() { 251 name := "testctr" 252 session := podmanTest.Podman([]string{"create", "-t", "--expose", "80", "-P", "--name", name, ALPINE, "/bin/sh"}) 253 session.WaitWithDefaultTimeout() 254 inspectOut := podmanTest.InspectContainer(name) 255 Expect(len(inspectOut)).To(Equal(1)) 256 Expect(len(inspectOut[0].NetworkSettings.Ports)).To(Equal(1)) 257 Expect(len(inspectOut[0].NetworkSettings.Ports["80/tcp"])).To(Equal(1)) 258 Expect(inspectOut[0].NetworkSettings.Ports["80/tcp"][0].HostPort).To(Not(Equal("0"))) 259 Expect(inspectOut[0].NetworkSettings.Ports["80/tcp"][0].HostIP).To(Equal("")) 260 }) 261 262 It("podman run --expose 80/udp -P", func() { 263 name := "testctr" 264 session := podmanTest.Podman([]string{"create", "-t", "--expose", "80/udp", "-P", "--name", name, ALPINE, "/bin/sh"}) 265 session.WaitWithDefaultTimeout() 266 inspectOut := podmanTest.InspectContainer(name) 267 Expect(len(inspectOut)).To(Equal(1)) 268 Expect(len(inspectOut[0].NetworkSettings.Ports)).To(Equal(1)) 269 Expect(len(inspectOut[0].NetworkSettings.Ports["80/udp"])).To(Equal(1)) 270 Expect(inspectOut[0].NetworkSettings.Ports["80/udp"][0].HostPort).To(Not(Equal("0"))) 271 Expect(inspectOut[0].NetworkSettings.Ports["80/udp"][0].HostIP).To(Equal("")) 272 }) 273 274 It("podman run --expose 80 -p 80", func() { 275 name := "testctr" 276 session := podmanTest.Podman([]string{"create", "-t", "--expose", "80", "-p", "80", "--name", name, ALPINE, "/bin/sh"}) 277 session.WaitWithDefaultTimeout() 278 inspectOut := podmanTest.InspectContainer(name) 279 Expect(len(inspectOut)).To(Equal(1)) 280 Expect(len(inspectOut[0].NetworkSettings.Ports)).To(Equal(1)) 281 Expect(len(inspectOut[0].NetworkSettings.Ports["80/tcp"])).To(Equal(1)) 282 Expect(inspectOut[0].NetworkSettings.Ports["80/tcp"][0].HostPort).To(Not(Equal("80"))) 283 Expect(inspectOut[0].NetworkSettings.Ports["80/tcp"][0].HostIP).To(Equal("")) 284 }) 285 286 It("podman run -p 127.0.0.1::8980/udp", func() { 287 name := "testctr" 288 session := podmanTest.Podman([]string{"create", "-t", "-p", "127.0.0.1::8980/udp", "--name", name, ALPINE, "/bin/sh"}) 289 session.WaitWithDefaultTimeout() 290 inspectOut := podmanTest.InspectContainer(name) 291 Expect(len(inspectOut)).To(Equal(1)) 292 Expect(len(inspectOut[0].NetworkSettings.Ports)).To(Equal(1)) 293 Expect(len(inspectOut[0].NetworkSettings.Ports["8980/udp"])).To(Equal(1)) 294 Expect(inspectOut[0].NetworkSettings.Ports["8980/udp"][0].HostPort).To(Not(Equal("8980"))) 295 Expect(inspectOut[0].NetworkSettings.Ports["8980/udp"][0].HostIP).To(Equal("127.0.0.1")) 296 }) 297 298 It("podman run -p :8181", func() { 299 name := "testctr" 300 session := podmanTest.Podman([]string{"create", "-t", "-p", ":8181", "--name", name, ALPINE, "/bin/sh"}) 301 session.WaitWithDefaultTimeout() 302 inspectOut := podmanTest.InspectContainer(name) 303 Expect(len(inspectOut)).To(Equal(1)) 304 Expect(len(inspectOut[0].NetworkSettings.Ports)).To(Equal(1)) 305 Expect(len(inspectOut[0].NetworkSettings.Ports["8181/tcp"])).To(Equal(1)) 306 Expect(inspectOut[0].NetworkSettings.Ports["8181/tcp"][0].HostPort).To(Not(Equal("8181"))) 307 Expect(inspectOut[0].NetworkSettings.Ports["8181/tcp"][0].HostIP).To(Equal("")) 308 }) 309 310 It("podman run -p xxx:8080 -p yyy:8080", func() { 311 name := "testctr" 312 session := podmanTest.Podman([]string{"create", "-t", "-p", "4444:8080", "-p", "5555:8080", "--name", name, ALPINE, "/bin/sh"}) 313 session.WaitWithDefaultTimeout() 314 inspectOut := podmanTest.InspectContainer(name) 315 Expect(len(inspectOut)).To(Equal(1)) 316 Expect(len(inspectOut[0].NetworkSettings.Ports)).To(Equal(1)) 317 Expect(len(inspectOut[0].NetworkSettings.Ports["8080/tcp"])).To(Equal(2)) 318 319 hp1 := inspectOut[0].NetworkSettings.Ports["8080/tcp"][0].HostPort 320 hp2 := inspectOut[0].NetworkSettings.Ports["8080/tcp"][1].HostPort 321 322 // We can't guarantee order 323 Expect((hp1 == "4444" && hp2 == "5555") || (hp1 == "5555" && hp2 == "4444")).To(BeTrue()) 324 }) 325 326 It("podman run -p 0.0.0.0:9280:80", func() { 327 name := "testctr" 328 session := podmanTest.Podman([]string{"create", "-t", "-p", "0.0.0.0:9280:80", "--name", name, ALPINE, "/bin/sh"}) 329 session.WaitWithDefaultTimeout() 330 inspectOut := podmanTest.InspectContainer(name) 331 Expect(len(inspectOut)).To(Equal(1)) 332 Expect(len(inspectOut[0].NetworkSettings.Ports)).To(Equal(1)) 333 Expect(len(inspectOut[0].NetworkSettings.Ports["80/tcp"])).To(Equal(1)) 334 Expect(inspectOut[0].NetworkSettings.Ports["80/tcp"][0].HostPort).To(Equal("9280")) 335 Expect(inspectOut[0].NetworkSettings.Ports["80/tcp"][0].HostIP).To(Equal("")) 336 }) 337 338 It("podman run network expose host port 80 to container port 8000", func() { 339 SkipIfRootless("iptables is not supported for rootless users") 340 session := podmanTest.Podman([]string{"run", "-dt", "-p", "80:8000", ALPINE, "/bin/sh"}) 341 session.Wait(30) 342 Expect(session).Should(Exit(0)) 343 results := SystemExec("iptables", []string{"-t", "nat", "-L"}) 344 Expect(results).Should(Exit(0)) 345 Expect(results.OutputToString()).To(ContainSubstring("8000")) 346 347 ncBusy := SystemExec("nc", []string{"-l", "-p", "80"}) 348 Expect(ncBusy).To(ExitWithError()) 349 }) 350 351 It("podman run network expose host port 18081 to container port 8000 using rootlesskit port handler", func() { 352 session := podmanTest.Podman([]string{"run", "--network", "slirp4netns:port_handler=rootlesskit", "-dt", "-p", "18081:8000", ALPINE, "/bin/sh"}) 353 session.Wait(30) 354 Expect(session).Should(Exit(0)) 355 356 ncBusy := SystemExec("nc", []string{"-l", "-p", "18081"}) 357 Expect(ncBusy).To(ExitWithError()) 358 }) 359 360 It("podman run slirp4netns verify net.ipv6.conf.default.accept_dad=0", func() { 361 session := podmanTest.Podman([]string{"run", "--network", "slirp4netns:enable_ipv6=true", ALPINE, "ip", "addr"}) 362 session.Wait(30) 363 Expect(session).Should(Exit(0)) 364 // check the ipv6 setup id done without delay (https://github.com/containers/podman/issues/11062) 365 Expect(session.OutputToString()).To(ContainSubstring("inet6 fd00::")) 366 367 const ipv6ConfDefaultAcceptDadSysctl = "/proc/sys/net/ipv6/conf/all/accept_dad" 368 369 cat := SystemExec("cat", []string{ipv6ConfDefaultAcceptDadSysctl}) 370 cat.Wait(30) 371 Expect(cat).Should(Exit(0)) 372 sysctlValue := cat.OutputToString() 373 374 session = podmanTest.Podman([]string{"run", "--network", "slirp4netns:enable_ipv6=true", ALPINE, "cat", ipv6ConfDefaultAcceptDadSysctl}) 375 session.Wait(30) 376 Expect(session).Should(Exit(0)) 377 Expect(session.OutputToString()).To(Equal(sysctlValue)) 378 }) 379 380 It("podman run network expose host port 18082 to container port 8000 using slirp4netns port handler", func() { 381 session := podmanTest.Podman([]string{"run", "--network", "slirp4netns:port_handler=slirp4netns", "-dt", "-p", "18082:8000", ALPINE, "/bin/sh"}) 382 session.Wait(30) 383 Expect(session).Should(Exit(0)) 384 ncBusy := SystemExec("nc", []string{"-l", "-p", "18082"}) 385 Expect(ncBusy).To(ExitWithError()) 386 }) 387 388 It("podman run network expose host port 8080 to container port 8000 using invalid port handler", func() { 389 session := podmanTest.Podman([]string{"run", "--network", "slirp4netns:port_handler=invalid", "-dt", "-p", "8080:8000", ALPINE, "/bin/sh"}) 390 session.Wait(30) 391 Expect(session).To(ExitWithError()) 392 }) 393 394 It("podman run slirp4netns network with host loopback", func() { 395 session := podmanTest.Podman([]string{"run", "--network", "slirp4netns:allow_host_loopback=true", ALPINE, "ping", "-c1", "10.0.2.2"}) 396 session.Wait(30) 397 Expect(session).Should(Exit(0)) 398 }) 399 400 It("podman run slirp4netns network with mtu", func() { 401 session := podmanTest.Podman([]string{"run", "--network", "slirp4netns:mtu=9000", ALPINE, "ip", "addr"}) 402 session.Wait(30) 403 Expect(session).Should(Exit(0)) 404 Expect(session.OutputToString()).To(ContainSubstring("mtu 9000")) 405 }) 406 407 It("podman run slirp4netns network with different cidr", func() { 408 slirp4netnsHelp := SystemExec("slirp4netns", []string{"--help"}) 409 Expect(slirp4netnsHelp).Should(Exit(0)) 410 411 networkConfiguration := "slirp4netns:cidr=192.168.0.0/24,allow_host_loopback=true" 412 session := podmanTest.Podman([]string{"run", "--network", networkConfiguration, ALPINE, "ping", "-c1", "192.168.0.2"}) 413 session.Wait(30) 414 415 if strings.Contains(slirp4netnsHelp.OutputToString(), "cidr") { 416 Expect(session).Should(Exit(0)) 417 } else { 418 Expect(session).To(ExitWithError()) 419 Expect(session.ErrorToString()).To(ContainSubstring("cidr not supported")) 420 } 421 }) 422 423 It("podman run network bind to 127.0.0.1", func() { 424 slirp4netnsHelp := SystemExec("slirp4netns", []string{"--help"}) 425 Expect(slirp4netnsHelp).Should(Exit(0)) 426 networkConfiguration := "slirp4netns:outbound_addr=127.0.0.1,allow_host_loopback=true" 427 428 if strings.Contains(slirp4netnsHelp.OutputToString(), "outbound-addr") { 429 ncListener := StartSystemExec("nc", []string{"-v", "-n", "-l", "-p", "8083"}) 430 session := podmanTest.Podman([]string{"run", "--network", networkConfiguration, "-dt", ALPINE, "nc", "-w", "2", "10.0.2.2", "8083"}) 431 session.Wait(30) 432 ncListener.Wait(30) 433 434 Expect(session).Should(Exit(0)) 435 Expect(ncListener).Should(Exit(0)) 436 Expect(ncListener.ErrorToString()).To(ContainSubstring("127.0.0.1")) 437 } else { 438 session := podmanTest.Podman([]string{"run", "--network", networkConfiguration, "-dt", ALPINE, "nc", "-w", "2", "10.0.2.2", "8083"}) 439 session.Wait(30) 440 Expect(session).To(ExitWithError()) 441 Expect(session.ErrorToString()).To(ContainSubstring("outbound_addr not supported")) 442 } 443 }) 444 445 It("podman run network bind to HostIP", func() { 446 ip, err := utils.HostIP() 447 Expect(err).To(BeNil()) 448 449 slirp4netnsHelp := SystemExec("slirp4netns", []string{"--help"}) 450 Expect(slirp4netnsHelp).Should(Exit(0)) 451 networkConfiguration := fmt.Sprintf("slirp4netns:outbound_addr=%s,allow_host_loopback=true", ip.String()) 452 453 if strings.Contains(slirp4netnsHelp.OutputToString(), "outbound-addr") { 454 ncListener := StartSystemExec("nc", []string{"-v", "-n", "-l", "-p", "8084"}) 455 session := podmanTest.Podman([]string{"run", "--network", networkConfiguration, "-dt", ALPINE, "nc", "-w", "2", "10.0.2.2", "8084"}) 456 session.Wait(30) 457 ncListener.Wait(30) 458 459 Expect(session).Should(Exit(0)) 460 Expect(ncListener).Should(Exit(0)) 461 Expect(ncListener.ErrorToString()).To(ContainSubstring(ip.String())) 462 } else { 463 session := podmanTest.Podman([]string{"run", "--network", networkConfiguration, "-dt", ALPINE, "nc", "-w", "2", "10.0.2.2", "8084"}) 464 session.Wait(30) 465 Expect(session).To(ExitWithError()) 466 Expect(session.ErrorToString()).To(ContainSubstring("outbound_addr not supported")) 467 } 468 }) 469 470 It("podman run network expose ports in image metadata", func() { 471 session := podmanTest.Podman([]string{"create", "--name", "test", "-t", "-P", nginx}) 472 session.Wait(90) 473 Expect(session).Should(Exit(0)) 474 results := podmanTest.Podman([]string{"inspect", "test"}) 475 results.Wait(30) 476 Expect(results).Should(Exit(0)) 477 Expect(results.OutputToString()).To(ContainSubstring(`"80/tcp":`)) 478 }) 479 480 It("podman run network expose duplicate host port results in error", func() { 481 SkipIfRootless("FIXME we should be able to run this test in rootless mode with different ports") 482 483 session := podmanTest.Podman([]string{"run", "--name", "test", "-dt", "-p", "80", ALPINE, "/bin/sh"}) 484 session.WaitWithDefaultTimeout() 485 Expect(session).Should(Exit(0)) 486 487 inspect := podmanTest.Podman([]string{"inspect", "test"}) 488 inspect.WaitWithDefaultTimeout() 489 Expect(inspect).Should(Exit(0)) 490 491 containerConfig := inspect.InspectContainerToJSON() 492 Expect(containerConfig[0].NetworkSettings.Ports).To(Not(BeNil())) 493 Expect(containerConfig[0].NetworkSettings.Ports["80/tcp"]).To(Not(BeNil())) 494 Expect(containerConfig[0].NetworkSettings.Ports["80/tcp"][0].HostPort).ToNot(Equal(80)) 495 }) 496 497 It("podman run hostname test", func() { 498 session := podmanTest.Podman([]string{"run", "--rm", ALPINE, "printenv", "HOSTNAME"}) 499 session.WaitWithDefaultTimeout() 500 Expect(session).Should(Exit(0)) 501 match, _ := session.GrepString(hostname) 502 Expect(match).Should(BeFalse()) 503 }) 504 505 It("podman run --net host hostname test", func() { 506 session := podmanTest.Podman([]string{"run", "--rm", "--net", "host", ALPINE, "printenv", "HOSTNAME"}) 507 session.WaitWithDefaultTimeout() 508 Expect(session).Should(Exit(0)) 509 match, _ := session.GrepString(hostname) 510 Expect(match).Should(BeTrue()) 511 }) 512 It("podman run --net host --uts host hostname test", func() { 513 session := podmanTest.Podman([]string{"run", "--rm", "--net", "host", "--uts", "host", ALPINE, "printenv", "HOSTNAME"}) 514 session.WaitWithDefaultTimeout() 515 Expect(session).Should(Exit(0)) 516 match, _ := session.GrepString(hostname) 517 Expect(match).Should(BeTrue()) 518 }) 519 It("podman run --uts host hostname test", func() { 520 session := podmanTest.Podman([]string{"run", "--rm", "--uts", "host", ALPINE, "printenv", "HOSTNAME"}) 521 session.WaitWithDefaultTimeout() 522 Expect(session).Should(Exit(0)) 523 match, _ := session.GrepString(hostname) 524 Expect(match).Should(BeTrue()) 525 }) 526 527 It("podman run --net host --hostname ... hostname test", func() { 528 session := podmanTest.Podman([]string{"run", "--rm", "--net", "host", "--hostname", "foobar", ALPINE, "printenv", "HOSTNAME"}) 529 session.WaitWithDefaultTimeout() 530 Expect(session).Should(Exit(0)) 531 match, _ := session.GrepString("foobar") 532 Expect(match).Should(BeTrue()) 533 }) 534 535 It("podman run --hostname ... hostname test", func() { 536 session := podmanTest.Podman([]string{"run", "--rm", "--hostname", "foobar", ALPINE, "printenv", "HOSTNAME"}) 537 session.WaitWithDefaultTimeout() 538 Expect(session).Should(Exit(0)) 539 match, _ := session.GrepString("foobar") 540 Expect(match).Should(BeTrue()) 541 }) 542 543 It("podman run --net container: and --uts container:", func() { 544 ctrName := "ctrToJoin" 545 ctr1 := podmanTest.RunTopContainer(ctrName) 546 ctr1.WaitWithDefaultTimeout() 547 Expect(ctr1).Should(Exit(0)) 548 549 ctr2 := podmanTest.Podman([]string{"run", "-d", "--net=container:" + ctrName, "--uts=container:" + ctrName, ALPINE, "true"}) 550 ctr2.WaitWithDefaultTimeout() 551 Expect(ctr2).Should(Exit(0)) 552 }) 553 554 It("podman run --net container: copies hosts and resolv", func() { 555 ctrName := "ctr1" 556 ctr1 := podmanTest.RunTopContainer(ctrName) 557 ctr1.WaitWithDefaultTimeout() 558 Expect(ctr1).Should(Exit(0)) 559 560 // Exec in and modify /etc/resolv.conf and /etc/hosts 561 exec1 := podmanTest.Podman([]string{"exec", ctrName, "sh", "-c", "echo nameserver 192.0.2.1 > /etc/resolv.conf"}) 562 exec1.WaitWithDefaultTimeout() 563 Expect(exec1).Should(Exit(0)) 564 565 exec2 := podmanTest.Podman([]string{"exec", ctrName, "sh", "-c", "echo 192.0.2.2 test1 > /etc/hosts"}) 566 exec2.WaitWithDefaultTimeout() 567 Expect(exec2).Should(Exit(0)) 568 569 ctrName2 := "ctr2" 570 ctr2 := podmanTest.Podman([]string{"run", "-d", "--net=container:" + ctrName, "--name", ctrName2, ALPINE, "top"}) 571 ctr2.WaitWithDefaultTimeout() 572 Expect(ctr2).Should(Exit(0)) 573 574 exec3 := podmanTest.Podman([]string{"exec", "-i", ctrName2, "cat", "/etc/resolv.conf"}) 575 exec3.WaitWithDefaultTimeout() 576 Expect(exec3).Should(Exit(0)) 577 Expect(exec3.OutputToString()).To(ContainSubstring("nameserver 192.0.2.1")) 578 579 exec4 := podmanTest.Podman([]string{"exec", "-i", ctrName2, "cat", "/etc/hosts"}) 580 exec4.WaitWithDefaultTimeout() 581 Expect(exec4).Should(Exit(0)) 582 Expect(exec4.OutputToString()).To(ContainSubstring("192.0.2.2 test1")) 583 }) 584 585 It("podman run /etc/hosts contains --hostname", func() { 586 session := podmanTest.Podman([]string{"run", "--rm", "--hostname", "foohostname", ALPINE, "grep", "foohostname", "/etc/hosts"}) 587 session.WaitWithDefaultTimeout() 588 Expect(session).Should(Exit(0)) 589 }) 590 591 It("podman run --uidmap /etc/hosts contains --hostname", func() { 592 SkipIfRootless("uidmap population of cninetworks not supported for rootless users") 593 session := podmanTest.Podman([]string{"run", "--uidmap", "0:100000:1000", "--rm", "--hostname", "foohostname", ALPINE, "grep", "foohostname", "/etc/hosts"}) 594 session.WaitWithDefaultTimeout() 595 Expect(session).Should(Exit(0)) 596 597 session = podmanTest.Podman([]string{"run", "--uidmap", "0:100000:1000", "--rm", "--hostname", "foohostname", "-v", "/etc/hosts:/etc/hosts", ALPINE, "grep", "foohostname", "/etc/hosts"}) 598 session.WaitWithDefaultTimeout() 599 Expect(session).Should(Exit(1)) 600 }) 601 602 It("podman run network in user created network namespace", func() { 603 SkipIfRootless("ip netns is not supported for rootless users") 604 if Containerized() { 605 Skip("Cannot be run within a container.") 606 } 607 addXXX := SystemExec("ip", []string{"netns", "add", "xxx"}) 608 Expect(addXXX).Should(Exit(0)) 609 defer func() { 610 delXXX := SystemExec("ip", []string{"netns", "delete", "xxx"}) 611 Expect(delXXX).Should(Exit(0)) 612 }() 613 614 session := podmanTest.Podman([]string{"run", "-dt", "--net", "ns:/run/netns/xxx", ALPINE, "wget", "www.podman.io"}) 615 session.Wait(90) 616 Expect(session).Should(Exit(0)) 617 }) 618 619 It("podman run n user created network namespace with resolv.conf", func() { 620 SkipIfRootless("ip netns is not supported for rootless users") 621 if Containerized() { 622 Skip("Cannot be run within a container.") 623 } 624 addXXX2 := SystemExec("ip", []string{"netns", "add", "xxx2"}) 625 Expect(addXXX2).Should(Exit(0)) 626 defer func() { 627 delXXX2 := SystemExec("ip", []string{"netns", "delete", "xxx2"}) 628 Expect(delXXX2).Should(Exit(0)) 629 }() 630 631 mdXXX2 := SystemExec("mkdir", []string{"-p", "/etc/netns/xxx2"}) 632 Expect(mdXXX2).Should(Exit(0)) 633 defer os.RemoveAll("/etc/netns/xxx2") 634 635 nsXXX2 := SystemExec("bash", []string{"-c", "echo nameserver 11.11.11.11 > /etc/netns/xxx2/resolv.conf"}) 636 Expect(nsXXX2).Should(Exit(0)) 637 638 session := podmanTest.Podman([]string{"run", "--net", "ns:/run/netns/xxx2", ALPINE, "cat", "/etc/resolv.conf"}) 639 session.Wait(90) 640 Expect(session).Should(Exit(0)) 641 Expect(session.OutputToString()).To(ContainSubstring("11.11.11.11")) 642 }) 643 644 It("podman run network in bogus user created network namespace", func() { 645 session := podmanTest.Podman([]string{"run", "-dt", "--net", "ns:/run/netns/xxy", ALPINE, "wget", "www.podman.io"}) 646 session.Wait(90) 647 Expect(session).To(ExitWithError()) 648 Expect(session.ErrorToString()).To(ContainSubstring("stat /run/netns/xxy: no such file or directory")) 649 }) 650 651 It("podman run in custom CNI network with --static-ip", func() { 652 netName := stringid.GenerateNonCryptoID() 653 ipAddr := "10.25.30.128" 654 create := podmanTest.Podman([]string{"network", "create", "--subnet", "10.25.30.0/24", netName}) 655 create.WaitWithDefaultTimeout() 656 Expect(create).Should(Exit(0)) 657 defer podmanTest.removeCNINetwork(netName) 658 659 run := podmanTest.Podman([]string{"run", "-t", "-i", "--rm", "--net", netName, "--ip", ipAddr, ALPINE, "ip", "addr"}) 660 run.WaitWithDefaultTimeout() 661 Expect(run).Should(Exit(0)) 662 Expect(run.OutputToString()).To(ContainSubstring(ipAddr)) 663 }) 664 665 It("podman cni network works across user ns", func() { 666 netName := stringid.GenerateNonCryptoID() 667 create := podmanTest.Podman([]string{"network", "create", netName}) 668 create.WaitWithDefaultTimeout() 669 Expect(create).Should(Exit(0)) 670 defer podmanTest.removeCNINetwork(netName) 671 672 name := "nc-server" 673 run := podmanTest.Podman([]string{"run", "--log-driver", "k8s-file", "-d", "--name", name, "--net", netName, ALPINE, "nc", "-l", "-p", "9480"}) 674 run.WaitWithDefaultTimeout() 675 Expect(run).Should(Exit(0)) 676 677 // NOTE: we force the k8s-file log driver to make sure the 678 // tests are passing inside a container. 679 run = podmanTest.Podman([]string{"run", "--log-driver", "k8s-file", "--rm", "--net", netName, "--uidmap", "0:1:4096", ALPINE, "sh", "-c", fmt.Sprintf("echo podman | nc -w 1 %s.dns.podman 9480", name)}) 680 run.WaitWithDefaultTimeout() 681 Expect(run).Should(Exit(0)) 682 683 log := podmanTest.Podman([]string{"logs", name}) 684 log.WaitWithDefaultTimeout() 685 Expect(log).Should(Exit(0)) 686 Expect(log.OutputToString()).To(Equal("podman")) 687 }) 688 689 It("podman run with new:pod and static-ip", func() { 690 netName := stringid.GenerateNonCryptoID() 691 ipAddr := "10.25.40.128" 692 podname := "testpod" 693 create := podmanTest.Podman([]string{"network", "create", "--subnet", "10.25.40.0/24", netName}) 694 create.WaitWithDefaultTimeout() 695 Expect(create).Should(Exit(0)) 696 defer podmanTest.removeCNINetwork(netName) 697 698 run := podmanTest.Podman([]string{"run", "-t", "-i", "--rm", "--pod", "new:" + podname, "--net", netName, "--ip", ipAddr, ALPINE, "ip", "addr"}) 699 run.WaitWithDefaultTimeout() 700 Expect(run).Should(Exit(0)) 701 Expect(run.OutputToString()).To(ContainSubstring(ipAddr)) 702 703 podrm := podmanTest.Podman([]string{"pod", "rm", "-f", podname}) 704 podrm.WaitWithDefaultTimeout() 705 Expect(podrm).Should(Exit(0)) 706 }) 707 708 It("podman run with --net=host and --hostname sets correct hostname", func() { 709 hostname := "testctr" 710 run := podmanTest.Podman([]string{"run", "--net=host", "--hostname", hostname, ALPINE, "hostname"}) 711 run.WaitWithDefaultTimeout() 712 Expect(run).Should(Exit(0)) 713 Expect(strings.Contains(run.OutputToString(), hostname)).To(BeTrue()) 714 }) 715 716 It("podman run with --net=none sets hostname", func() { 717 hostname := "testctr" 718 run := podmanTest.Podman([]string{"run", "--net=none", "--hostname", hostname, ALPINE, "hostname"}) 719 run.WaitWithDefaultTimeout() 720 Expect(run).Should(Exit(0)) 721 Expect(strings.Contains(run.OutputToString(), hostname)).To(BeTrue()) 722 }) 723 724 It("podman run with --net=none adds hostname to /etc/hosts", func() { 725 hostname := "testctr" 726 run := podmanTest.Podman([]string{"run", "--net=none", "--hostname", hostname, ALPINE, "cat", "/etc/hosts"}) 727 run.WaitWithDefaultTimeout() 728 Expect(run).Should(Exit(0)) 729 Expect(strings.Contains(run.OutputToString(), hostname)).To(BeTrue()) 730 }) 731 732 It("podman run with pod does not add extra 127 entry to /etc/hosts", func() { 733 pod := "testpod" 734 hostname := "test-hostname" 735 run := podmanTest.Podman([]string{"pod", "create", "--hostname", hostname, "--name", pod}) 736 run.WaitWithDefaultTimeout() 737 Expect(run).Should(Exit(0)) 738 run = podmanTest.Podman([]string{"run", "--pod", pod, ALPINE, "cat", "/etc/hosts"}) 739 run.WaitWithDefaultTimeout() 740 Expect(run).Should(Exit(0)) 741 Expect(run.OutputToString()).ToNot(ContainSubstring("127.0.0.1 %s", hostname)) 742 }) 743 744 ping_test := func(netns string) { 745 hostname := "testctr" 746 run := podmanTest.Podman([]string{"run", netns, "--hostname", hostname, ALPINE, "ping", "-c", "1", hostname}) 747 run.WaitWithDefaultTimeout() 748 Expect(run).Should(Exit(0)) 749 750 run = podmanTest.Podman([]string{"run", netns, "--hostname", hostname, "--name", "test", ALPINE, "ping", "-c", "1", "test"}) 751 run.WaitWithDefaultTimeout() 752 Expect(run).Should(Exit(0)) 753 } 754 755 It("podman attempt to ping container name and hostname --net=none", func() { 756 ping_test("--net=none") 757 }) 758 759 It("podman attempt to ping container name and hostname --net=private", func() { 760 ping_test("--net=private") 761 }) 762 763 It("podman run check dnsname plugin", func() { 764 pod := "testpod" 765 session := podmanTest.Podman([]string{"pod", "create", "--name", pod}) 766 session.WaitWithDefaultTimeout() 767 Expect(session).Should(Exit(0)) 768 769 net := "IntTest" + stringid.GenerateNonCryptoID() 770 session = podmanTest.Podman([]string{"network", "create", net}) 771 session.WaitWithDefaultTimeout() 772 defer podmanTest.removeCNINetwork(net) 773 Expect(session).Should(Exit(0)) 774 775 pod2 := "testpod2" 776 session = podmanTest.Podman([]string{"pod", "create", "--network", net, "--name", pod2}) 777 session.WaitWithDefaultTimeout() 778 Expect(session).Should(Exit(0)) 779 780 session = podmanTest.Podman([]string{"run", "--name", "con1", "--network", net, ALPINE, "nslookup", "con1"}) 781 session.WaitWithDefaultTimeout() 782 Expect(session).Should(Exit(0)) 783 784 session = podmanTest.Podman([]string{"run", "--name", "con2", "--pod", pod, "--network", net, ALPINE, "nslookup", "con2"}) 785 session.WaitWithDefaultTimeout() 786 Expect(session).Should(Exit(0)) 787 788 session = podmanTest.Podman([]string{"run", "--name", "con3", "--pod", pod2, ALPINE, "nslookup", "con1"}) 789 session.WaitWithDefaultTimeout() 790 Expect(session).Should(Exit(1)) 791 Expect(session.ErrorToString()).To(ContainSubstring("can't resolve 'con1'")) 792 793 session = podmanTest.Podman([]string{"run", "--name", "con4", "--network", net, ALPINE, "nslookup", pod2 + ".dns.podman"}) 794 session.WaitWithDefaultTimeout() 795 Expect(session).Should(Exit(0)) 796 }) 797 798 It("podman run check dnsname adds dns search domain", func() { 799 net := "dnsname" + stringid.GenerateNonCryptoID() 800 session := podmanTest.Podman([]string{"network", "create", net}) 801 session.WaitWithDefaultTimeout() 802 defer podmanTest.removeCNINetwork(net) 803 Expect(session).Should(Exit(0)) 804 805 session = podmanTest.Podman([]string{"run", "--network", net, ALPINE, "cat", "/etc/resolv.conf"}) 806 session.WaitWithDefaultTimeout() 807 Expect(session).Should(Exit(0)) 808 Expect(session.OutputToString()).To(ContainSubstring("search dns.podman")) 809 }) 810 811 It("Rootless podman run with --net=bridge works and connects to default network", func() { 812 // This is harmless when run as root, so we'll just let it run. 813 ctrName := "testctr" 814 ctr := podmanTest.Podman([]string{"run", "-d", "--net=bridge", "--name", ctrName, ALPINE, "top"}) 815 ctr.WaitWithDefaultTimeout() 816 Expect(ctr).Should(Exit(0)) 817 818 inspectOut := podmanTest.InspectContainer(ctrName) 819 Expect(len(inspectOut)).To(Equal(1)) 820 Expect(len(inspectOut[0].NetworkSettings.Networks)).To(Equal(1)) 821 _, ok := inspectOut[0].NetworkSettings.Networks["podman"] 822 Expect(ok).To(BeTrue()) 823 }) 824 })