github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/test/e2e/run_networking_test.go (about) 1 // +build !remoteclient 2 3 package integration 4 5 import ( 6 "os" 7 8 . "github.com/containers/libpod/test/utils" 9 . "github.com/onsi/ginkgo" 10 . "github.com/onsi/gomega" 11 ) 12 13 var _ = Describe("Podman run networking", func() { 14 var ( 15 tempdir string 16 err error 17 podmanTest *PodmanTestIntegration 18 hostname, _ = os.Hostname() 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 podmanTest.SeedImages() 29 }) 30 31 AfterEach(func() { 32 podmanTest.Cleanup() 33 f := CurrentGinkgoTestDescription() 34 processTestResult(f) 35 36 }) 37 38 It("podman run network connection with default bridge", func() { 39 session := podmanTest.Podman([]string{"run", "-dt", ALPINE, "wget", "www.podman.io"}) 40 session.Wait(90) 41 Expect(session.ExitCode()).To(Equal(0)) 42 }) 43 44 It("podman run network connection with host", func() { 45 session := podmanTest.Podman([]string{"run", "-dt", "--network", "host", ALPINE, "wget", "www.podman.io"}) 46 session.Wait(90) 47 Expect(session.ExitCode()).To(Equal(0)) 48 }) 49 50 It("podman run network connection with loopback", func() { 51 session := podmanTest.Podman([]string{"run", "-dt", "--network", "host", ALPINE, "wget", "www.podman.io"}) 52 session.Wait(90) 53 Expect(session.ExitCode()).To(Equal(0)) 54 }) 55 56 It("podman run network expose port 222", func() { 57 SkipIfRootless() 58 session := podmanTest.Podman([]string{"run", "-dt", "--expose", "222-223", "-P", ALPINE, "/bin/sh"}) 59 session.Wait(30) 60 Expect(session.ExitCode()).To(Equal(0)) 61 results := SystemExec("iptables", []string{"-t", "nat", "-L"}) 62 Expect(results.ExitCode()).To(Equal(0)) 63 Expect(results.OutputToString()).To(ContainSubstring("222")) 64 Expect(results.OutputToString()).To(ContainSubstring("223")) 65 }) 66 67 It("podman run network expose host port 80 to container port 8000", func() { 68 SkipIfRootless() 69 session := podmanTest.Podman([]string{"run", "-dt", "-p", "80:8000", ALPINE, "/bin/sh"}) 70 session.Wait(30) 71 Expect(session.ExitCode()).To(Equal(0)) 72 results := SystemExec("iptables", []string{"-t", "nat", "-L"}) 73 Expect(results.ExitCode()).To(Equal(0)) 74 Expect(results.OutputToString()).To(ContainSubstring("8000")) 75 76 ncBusy := SystemExec("nc", []string{"-l", "-p", "80"}) 77 Expect(ncBusy).To(ExitWithError()) 78 }) 79 80 It("podman run network expose ports in image metadata", func() { 81 session := podmanTest.Podman([]string{"create", "-dt", "-P", nginx}) 82 session.Wait(90) 83 Expect(session.ExitCode()).To(Equal(0)) 84 results := podmanTest.Podman([]string{"inspect", "-l"}) 85 results.Wait(30) 86 Expect(results.ExitCode()).To(Equal(0)) 87 Expect(results.OutputToString()).To(ContainSubstring(": 80,")) 88 }) 89 90 It("podman run network expose duplicate host port results in error", func() { 91 session := podmanTest.Podman([]string{"run", "-dt", "-p", "80", ALPINE, "/bin/sh"}) 92 session.WaitWithDefaultTimeout() 93 Expect(session.ExitCode()).To(Equal(0)) 94 95 inspect := podmanTest.Podman([]string{"inspect", "-l"}) 96 inspect.WaitWithDefaultTimeout() 97 Expect(inspect.ExitCode()).To(Equal(0)) 98 99 containerConfig := inspect.InspectContainerToJSON() 100 Expect(containerConfig[0].NetworkSettings.Ports[0].HostPort).ToNot(Equal("80")) 101 }) 102 103 It("podman run hostname test", func() { 104 session := podmanTest.Podman([]string{"run", "--rm", ALPINE, "printenv", "HOSTNAME"}) 105 session.WaitWithDefaultTimeout() 106 Expect(session.ExitCode()).To(Equal(0)) 107 match, _ := session.GrepString(hostname) 108 Expect(match).Should(BeFalse()) 109 }) 110 111 It("podman run --net host hostname test", func() { 112 session := podmanTest.Podman([]string{"run", "--rm", "--net", "host", ALPINE, "printenv", "HOSTNAME"}) 113 session.WaitWithDefaultTimeout() 114 Expect(session.ExitCode()).To(Equal(0)) 115 match, _ := session.GrepString(hostname) 116 Expect(match).Should(BeTrue()) 117 }) 118 It("podman run --net host --uts host hostname test", func() { 119 session := podmanTest.Podman([]string{"run", "--rm", "--net", "host", "--uts", "host", ALPINE, "printenv", "HOSTNAME"}) 120 session.WaitWithDefaultTimeout() 121 Expect(session.ExitCode()).To(Equal(0)) 122 match, _ := session.GrepString(hostname) 123 Expect(match).Should(BeTrue()) 124 }) 125 It("podman run --uts host hostname test", func() { 126 session := podmanTest.Podman([]string{"run", "--rm", "--uts", "host", ALPINE, "printenv", "HOSTNAME"}) 127 session.WaitWithDefaultTimeout() 128 Expect(session.ExitCode()).To(Equal(0)) 129 match, _ := session.GrepString(hostname) 130 Expect(match).Should(BeTrue()) 131 }) 132 133 It("podman run --net host --hostname ... hostname test", func() { 134 session := podmanTest.Podman([]string{"run", "--rm", "--net", "host", "--hostname", "foobar", ALPINE, "printenv", "HOSTNAME"}) 135 session.WaitWithDefaultTimeout() 136 Expect(session.ExitCode()).To(Equal(0)) 137 match, _ := session.GrepString("foobar") 138 Expect(match).Should(BeTrue()) 139 }) 140 141 It("podman run --hostname ... hostname test", func() { 142 session := podmanTest.Podman([]string{"run", "--rm", "--hostname", "foobar", ALPINE, "printenv", "HOSTNAME"}) 143 session.WaitWithDefaultTimeout() 144 Expect(session.ExitCode()).To(Equal(0)) 145 match, _ := session.GrepString("foobar") 146 Expect(match).Should(BeTrue()) 147 }) 148 149 It("podman run --net container: and --uts container:", func() { 150 ctrName := "ctrToJoin" 151 ctr1 := podmanTest.RunTopContainer(ctrName) 152 ctr1.WaitWithDefaultTimeout() 153 Expect(ctr1.ExitCode()).To(Equal(0)) 154 155 ctr2 := podmanTest.Podman([]string{"run", "-d", "--net=container:" + ctrName, "--uts=container:" + ctrName, ALPINE, "true"}) 156 ctr2.WaitWithDefaultTimeout() 157 Expect(ctr2.ExitCode()).To(Equal(0)) 158 }) 159 160 It("podman run --net container: copies hosts and resolv", func() { 161 SkipIfRootless() 162 ctrName := "ctr1" 163 ctr1 := podmanTest.RunTopContainer(ctrName) 164 ctr1.WaitWithDefaultTimeout() 165 Expect(ctr1.ExitCode()).To(Equal(0)) 166 167 // Exec in and modify /etc/resolv.conf and /etc/hosts 168 exec1 := podmanTest.Podman([]string{"exec", ctrName, "sh", "-c", "echo nameserver 192.0.2.1 > /etc/resolv.conf"}) 169 exec1.WaitWithDefaultTimeout() 170 Expect(exec1.ExitCode()).To(Equal(0)) 171 172 exec2 := podmanTest.Podman([]string{"exec", ctrName, "sh", "-c", "echo 192.0.2.2 test1 > /etc/hosts"}) 173 exec2.WaitWithDefaultTimeout() 174 Expect(exec2.ExitCode()).To(Equal(0)) 175 176 ctrName2 := "ctr2" 177 ctr2 := podmanTest.Podman([]string{"run", "-d", "--net=container:" + ctrName, "--name", ctrName2, ALPINE, "top"}) 178 ctr2.WaitWithDefaultTimeout() 179 Expect(ctr2.ExitCode()).To(Equal(0)) 180 181 exec3 := podmanTest.Podman([]string{"exec", "-i", ctrName2, "cat", "/etc/resolv.conf"}) 182 exec3.WaitWithDefaultTimeout() 183 Expect(exec3.ExitCode()).To(Equal(0)) 184 Expect(exec3.OutputToString()).To(ContainSubstring("nameserver 192.0.2.1")) 185 186 exec4 := podmanTest.Podman([]string{"exec", "-i", ctrName2, "cat", "/etc/hosts"}) 187 exec4.WaitWithDefaultTimeout() 188 Expect(exec4.ExitCode()).To(Equal(0)) 189 Expect(exec4.OutputToString()).To(ContainSubstring("192.0.2.2 test1")) 190 }) 191 192 It("podman run /etc/hosts contains --hostname", func() { 193 session := podmanTest.Podman([]string{"run", "--rm", "--hostname", "foohostname", ALPINE, "grep", "foohostname", "/etc/hosts"}) 194 session.WaitWithDefaultTimeout() 195 Expect(session.ExitCode()).To(Equal(0)) 196 }) 197 198 It("podman run network in user created network namespace", func() { 199 SkipIfRootless() 200 if Containerized() { 201 Skip("Can not be run within a container.") 202 } 203 addXXX := SystemExec("ip", []string{"netns", "add", "xxx"}) 204 Expect(addXXX.ExitCode()).To(Equal(0)) 205 defer func() { 206 delXXX := SystemExec("ip", []string{"netns", "delete", "xxx"}) 207 Expect(delXXX.ExitCode()).To(Equal(0)) 208 }() 209 210 session := podmanTest.Podman([]string{"run", "-dt", "--net", "ns:/run/netns/xxx", ALPINE, "wget", "www.podman.io"}) 211 session.Wait(90) 212 Expect(session.ExitCode()).To(Equal(0)) 213 }) 214 215 It("podman run n user created network namespace with resolv.conf", func() { 216 SkipIfRootless() 217 if Containerized() { 218 Skip("Can not be run within a container.") 219 } 220 addXXX2 := SystemExec("ip", []string{"netns", "add", "xxx2"}) 221 Expect(addXXX2.ExitCode()).To(Equal(0)) 222 defer func() { 223 delXXX2 := SystemExec("ip", []string{"netns", "delete", "xxx2"}) 224 Expect(delXXX2.ExitCode()).To(Equal(0)) 225 }() 226 227 mdXXX2 := SystemExec("mkdir", []string{"-p", "/etc/netns/xxx2"}) 228 Expect(mdXXX2.ExitCode()).To(Equal(0)) 229 defer os.RemoveAll("/etc/netns/xxx2") 230 231 nsXXX2 := SystemExec("bash", []string{"-c", "echo nameserver 11.11.11.11 > /etc/netns/xxx2/resolv.conf"}) 232 Expect(nsXXX2.ExitCode()).To(Equal(0)) 233 234 session := podmanTest.Podman([]string{"run", "--net", "ns:/run/netns/xxx2", ALPINE, "cat", "/etc/resolv.conf"}) 235 session.Wait(90) 236 Expect(session.ExitCode()).To(Equal(0)) 237 Expect(session.OutputToString()).To(ContainSubstring("11.11.11.11")) 238 }) 239 240 It("podman run network in bogus user created network namespace", func() { 241 session := podmanTest.Podman([]string{"run", "-dt", "--net", "ns:/run/netns/xxy", ALPINE, "wget", "www.podman.io"}) 242 session.Wait(90) 243 Expect(session).To(ExitWithError()) 244 Expect(session.ErrorToString()).To(ContainSubstring("stat /run/netns/xxy: no such file or directory")) 245 }) 246 247 It("podman run in custom CNI network with --static-ip", func() { 248 SkipIfRootless() 249 netName := "podmantestnetwork" 250 ipAddr := "10.20.30.128" 251 create := podmanTest.Podman([]string{"network", "create", "--subnet", "10.20.30.0/24", netName}) 252 create.WaitWithDefaultTimeout() 253 Expect(create.ExitCode()).To(BeZero()) 254 255 run := podmanTest.Podman([]string{"run", "-t", "-i", "--rm", "--net", netName, "--ip", ipAddr, ALPINE, "ip", "addr"}) 256 run.WaitWithDefaultTimeout() 257 Expect(run.ExitCode()).To(BeZero()) 258 Expect(run.OutputToString()).To(ContainSubstring(ipAddr)) 259 }) 260 })