github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/test/e2e/pod_create_test.go (about) 1 package integration 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "strings" 9 10 "github.com/containers/podman/v2/pkg/rootless" 11 . "github.com/containers/podman/v2/test/utils" 12 . "github.com/onsi/ginkgo" 13 . "github.com/onsi/gomega" 14 ) 15 16 var _ = Describe("Podman pod create", func() { 17 var ( 18 tempdir string 19 err error 20 podmanTest *PodmanTestIntegration 21 ) 22 23 BeforeEach(func() { 24 tempdir, err = CreateTempDirInTempDir() 25 if err != nil { 26 os.Exit(1) 27 } 28 podmanTest = PodmanTestCreate(tempdir) 29 podmanTest.Setup() 30 podmanTest.SeedImages() 31 }) 32 33 AfterEach(func() { 34 podmanTest.Cleanup() 35 f := CurrentGinkgoTestDescription() 36 processTestResult(f) 37 38 }) 39 40 It("podman create pod", func() { 41 _, ec, podID := podmanTest.CreatePod("") 42 Expect(ec).To(Equal(0)) 43 44 check := podmanTest.Podman([]string{"pod", "ps", "-q", "--no-trunc"}) 45 check.WaitWithDefaultTimeout() 46 match, _ := check.GrepString(podID) 47 Expect(match).To(BeTrue()) 48 Expect(len(check.OutputToStringArray())).To(Equal(1)) 49 }) 50 51 It("podman create pod with name", func() { 52 name := "test" 53 _, ec, _ := podmanTest.CreatePod(name) 54 Expect(ec).To(Equal(0)) 55 56 check := podmanTest.Podman([]string{"pod", "ps", "--no-trunc"}) 57 check.WaitWithDefaultTimeout() 58 match, _ := check.GrepString(name) 59 Expect(match).To(BeTrue()) 60 }) 61 62 It("podman create pod with doubled name", func() { 63 name := "test" 64 _, ec, _ := podmanTest.CreatePod(name) 65 Expect(ec).To(Equal(0)) 66 67 _, ec2, _ := podmanTest.CreatePod(name) 68 Expect(ec2).To(Not(Equal(0))) 69 70 check := podmanTest.Podman([]string{"pod", "ps", "-q"}) 71 check.WaitWithDefaultTimeout() 72 Expect(len(check.OutputToStringArray())).To(Equal(1)) 73 }) 74 75 It("podman create pod with same name as ctr", func() { 76 name := "test" 77 session := podmanTest.Podman([]string{"create", "--name", name, ALPINE, "ls"}) 78 session.WaitWithDefaultTimeout() 79 Expect(session.ExitCode()).To(Equal(0)) 80 81 _, ec, _ := podmanTest.CreatePod(name) 82 Expect(ec).To(Not(Equal(0))) 83 84 check := podmanTest.Podman([]string{"pod", "ps", "-q"}) 85 check.WaitWithDefaultTimeout() 86 Expect(len(check.OutputToStringArray())).To(Equal(0)) 87 }) 88 89 It("podman create pod without network portbindings", func() { 90 name := "test" 91 session := podmanTest.Podman([]string{"pod", "create", "--name", name}) 92 session.WaitWithDefaultTimeout() 93 Expect(session.ExitCode()).To(Equal(0)) 94 pod := session.OutputToString() 95 96 webserver := podmanTest.Podman([]string{"run", "--pod", pod, "-dt", nginx}) 97 webserver.WaitWithDefaultTimeout() 98 Expect(webserver.ExitCode()).To(Equal(0)) 99 100 check := SystemExec("nc", []string{"-z", "localhost", "80"}) 101 Expect(check.ExitCode()).To(Equal(1)) 102 }) 103 104 It("podman create pod with network portbindings", func() { 105 name := "test" 106 session := podmanTest.Podman([]string{"pod", "create", "--name", name, "-p", "8080:80"}) 107 session.WaitWithDefaultTimeout() 108 Expect(session.ExitCode()).To(Equal(0)) 109 pod := session.OutputToString() 110 111 webserver := podmanTest.Podman([]string{"run", "--pod", pod, "-dt", nginx}) 112 webserver.WaitWithDefaultTimeout() 113 Expect(webserver.ExitCode()).To(Equal(0)) 114 115 check := SystemExec("nc", []string{"-z", "localhost", "8080"}) 116 Expect(check.ExitCode()).To(Equal(0)) 117 }) 118 119 It("podman create pod with no infra but portbindings should fail", func() { 120 name := "test" 121 session := podmanTest.Podman([]string{"pod", "create", "--infra=false", "--name", name, "-p", "80:80"}) 122 session.WaitWithDefaultTimeout() 123 Expect(session.ExitCode()).To(Equal(125)) 124 }) 125 126 It("podman create pod with --no-hosts", func() { 127 name := "test" 128 podCreate := podmanTest.Podman([]string{"pod", "create", "--no-hosts", "--name", name}) 129 podCreate.WaitWithDefaultTimeout() 130 Expect(podCreate.ExitCode()).To(Equal(0)) 131 132 alpineResolvConf := podmanTest.Podman([]string{"run", "-ti", "--rm", "--no-hosts", ALPINE, "cat", "/etc/hosts"}) 133 alpineResolvConf.WaitWithDefaultTimeout() 134 Expect(alpineResolvConf.ExitCode()).To(Equal(0)) 135 136 podResolvConf := podmanTest.Podman([]string{"run", "--pod", name, "-ti", "--rm", ALPINE, "cat", "/etc/hosts"}) 137 podResolvConf.WaitWithDefaultTimeout() 138 Expect(podResolvConf.ExitCode()).To(Equal(0)) 139 Expect(podResolvConf.OutputToString()).To(Equal(alpineResolvConf.OutputToString())) 140 }) 141 142 It("podman create pod with --no-hosts and no infra should fail", func() { 143 name := "test" 144 podCreate := podmanTest.Podman([]string{"pod", "create", "--no-hosts", "--name", name, "--infra=false"}) 145 podCreate.WaitWithDefaultTimeout() 146 Expect(podCreate.ExitCode()).To(Equal(125)) 147 }) 148 149 It("podman create pod with --add-host", func() { 150 name := "test" 151 podCreate := podmanTest.Podman([]string{"pod", "create", "--add-host", "test.example.com:12.34.56.78", "--name", name}) 152 podCreate.WaitWithDefaultTimeout() 153 Expect(podCreate.ExitCode()).To(Equal(0)) 154 155 podResolvConf := podmanTest.Podman([]string{"run", "--pod", name, "-ti", "--rm", ALPINE, "cat", "/etc/hosts"}) 156 podResolvConf.WaitWithDefaultTimeout() 157 Expect(podResolvConf.ExitCode()).To(Equal(0)) 158 Expect(strings.Contains(podResolvConf.OutputToString(), "12.34.56.78 test.example.com")).To(BeTrue()) 159 }) 160 161 It("podman create pod with --add-host and no infra should fail", func() { 162 name := "test" 163 podCreate := podmanTest.Podman([]string{"pod", "create", "--add-host", "test.example.com:12.34.56.78", "--name", name, "--infra=false"}) 164 podCreate.WaitWithDefaultTimeout() 165 Expect(podCreate.ExitCode()).To(Equal(125)) 166 }) 167 168 It("podman create pod with DNS server set", func() { 169 name := "test" 170 server := "12.34.56.78" 171 podCreate := podmanTest.Podman([]string{"pod", "create", "--dns", server, "--name", name}) 172 podCreate.WaitWithDefaultTimeout() 173 Expect(podCreate.ExitCode()).To(Equal(0)) 174 175 podResolvConf := podmanTest.Podman([]string{"run", "--pod", name, "-ti", "--rm", ALPINE, "cat", "/etc/resolv.conf"}) 176 podResolvConf.WaitWithDefaultTimeout() 177 Expect(podResolvConf.ExitCode()).To(Equal(0)) 178 Expect(strings.Contains(podResolvConf.OutputToString(), fmt.Sprintf("nameserver %s", server))).To(BeTrue()) 179 }) 180 181 It("podman create pod with DNS server set and no infra should fail", func() { 182 name := "test" 183 server := "12.34.56.78" 184 podCreate := podmanTest.Podman([]string{"pod", "create", "--dns", server, "--name", name, "--infra=false"}) 185 podCreate.WaitWithDefaultTimeout() 186 Expect(podCreate.ExitCode()).To(Equal(125)) 187 }) 188 189 It("podman create pod with DNS option set", func() { 190 name := "test" 191 option := "attempts:5" 192 podCreate := podmanTest.Podman([]string{"pod", "create", "--dns-opt", option, "--name", name}) 193 podCreate.WaitWithDefaultTimeout() 194 Expect(podCreate.ExitCode()).To(Equal(0)) 195 196 podResolvConf := podmanTest.Podman([]string{"run", "--pod", name, "-ti", "--rm", ALPINE, "cat", "/etc/resolv.conf"}) 197 podResolvConf.WaitWithDefaultTimeout() 198 Expect(podResolvConf.ExitCode()).To(Equal(0)) 199 Expect(strings.Contains(podResolvConf.OutputToString(), fmt.Sprintf("options %s", option))).To(BeTrue()) 200 }) 201 202 It("podman create pod with DNS option set and no infra should fail", func() { 203 name := "test" 204 option := "attempts:5" 205 podCreate := podmanTest.Podman([]string{"pod", "create", "--dns-opt", option, "--name", name, "--infra=false"}) 206 podCreate.WaitWithDefaultTimeout() 207 Expect(podCreate.ExitCode()).To(Equal(125)) 208 }) 209 210 It("podman create pod with DNS search domain set", func() { 211 name := "test" 212 search := "example.com" 213 podCreate := podmanTest.Podman([]string{"pod", "create", "--dns-search", search, "--name", name}) 214 podCreate.WaitWithDefaultTimeout() 215 Expect(podCreate.ExitCode()).To(Equal(0)) 216 217 podResolvConf := podmanTest.Podman([]string{"run", "--pod", name, "-ti", "--rm", ALPINE, "cat", "/etc/resolv.conf"}) 218 podResolvConf.WaitWithDefaultTimeout() 219 Expect(podResolvConf.ExitCode()).To(Equal(0)) 220 Expect(strings.Contains(podResolvConf.OutputToString(), fmt.Sprintf("search %s", search))).To(BeTrue()) 221 }) 222 223 It("podman create pod with DNS search domain set and no infra should fail", func() { 224 name := "test" 225 search := "example.com" 226 podCreate := podmanTest.Podman([]string{"pod", "create", "--dns-search", search, "--name", name, "--infra=false"}) 227 podCreate.WaitWithDefaultTimeout() 228 Expect(podCreate.ExitCode()).To(Equal(125)) 229 }) 230 231 It("podman create pod with IP address", func() { 232 name := "test" 233 ip := GetRandomIPAddress() 234 podCreate := podmanTest.Podman([]string{"pod", "create", "--ip", ip, "--name", name}) 235 podCreate.WaitWithDefaultTimeout() 236 // Rootless should error 237 if rootless.IsRootless() { 238 Expect(podCreate.ExitCode()).To(Equal(125)) 239 } else { 240 Expect(podCreate.ExitCode()).To(Equal(0)) 241 podResolvConf := podmanTest.Podman([]string{"run", "--pod", name, "-ti", "--rm", ALPINE, "ip", "addr"}) 242 podResolvConf.WaitWithDefaultTimeout() 243 Expect(podResolvConf.ExitCode()).To(Equal(0)) 244 Expect(strings.Contains(podResolvConf.OutputToString(), ip)).To(BeTrue()) 245 } 246 }) 247 248 It("podman container in pod with IP address shares IP address", func() { 249 SkipIfRootless("Rootless does not support --ip") 250 podName := "test" 251 ctrName := "testCtr" 252 ip := GetRandomIPAddress() 253 podCreate := podmanTest.Podman([]string{"pod", "create", "--ip", ip, "--name", podName}) 254 podCreate.WaitWithDefaultTimeout() 255 Expect(podCreate.ExitCode()).To(Equal(0)) 256 podCtr := podmanTest.Podman([]string{"run", "--name", ctrName, "--pod", podName, "-d", "-t", ALPINE, "top"}) 257 podCtr.WaitWithDefaultTimeout() 258 Expect(podCtr.ExitCode()).To(Equal(0)) 259 ctrInspect := podmanTest.Podman([]string{"inspect", ctrName}) 260 ctrInspect.WaitWithDefaultTimeout() 261 Expect(ctrInspect.ExitCode()).To(Equal(0)) 262 ctrJSON := ctrInspect.InspectContainerToJSON() 263 Expect(ctrJSON[0].NetworkSettings.IPAddress).To(Equal(ip)) 264 }) 265 266 It("podman create pod with IP address and no infra should fail", func() { 267 name := "test" 268 ip := GetRandomIPAddress() 269 podCreate := podmanTest.Podman([]string{"pod", "create", "--ip", ip, "--name", name, "--infra=false"}) 270 podCreate.WaitWithDefaultTimeout() 271 Expect(podCreate.ExitCode()).To(Equal(125)) 272 }) 273 274 It("podman create pod with MAC address", func() { 275 name := "test" 276 mac := "92:d0:c6:0a:29:35" 277 podCreate := podmanTest.Podman([]string{"pod", "create", "--mac-address", mac, "--name", name}) 278 podCreate.WaitWithDefaultTimeout() 279 // Rootless should error 280 if rootless.IsRootless() { 281 Expect(podCreate.ExitCode()).To(Equal(125)) 282 } else { 283 Expect(podCreate.ExitCode()).To(Equal(0)) 284 podResolvConf := podmanTest.Podman([]string{"run", "--pod", name, "-ti", "--rm", ALPINE, "ip", "addr"}) 285 podResolvConf.WaitWithDefaultTimeout() 286 Expect(podResolvConf.ExitCode()).To(Equal(0)) 287 Expect(strings.Contains(podResolvConf.OutputToString(), mac)).To(BeTrue()) 288 } 289 }) 290 291 It("podman create pod with MAC address and no infra should fail", func() { 292 name := "test" 293 mac := "92:d0:c6:0a:29:35" 294 podCreate := podmanTest.Podman([]string{"pod", "create", "--mac-address", mac, "--name", name, "--infra=false"}) 295 podCreate.WaitWithDefaultTimeout() 296 Expect(podCreate.ExitCode()).To(Equal(125)) 297 }) 298 299 It("podman create pod and print id to external file", func() { 300 // Switch to temp dir and restore it afterwards 301 cwd, err := os.Getwd() 302 Expect(err).To(BeNil()) 303 Expect(os.Chdir(os.TempDir())).To(BeNil()) 304 targetPath, err := CreateTempDirInTempDir() 305 if err != nil { 306 os.Exit(1) 307 } 308 targetFile := filepath.Join(targetPath, "idFile") 309 defer Expect(os.RemoveAll(targetFile)).To(BeNil()) 310 defer Expect(os.Chdir(cwd)).To(BeNil()) 311 312 session := podmanTest.Podman([]string{"pod", "create", "--name=abc", "--pod-id-file", targetFile}) 313 session.WaitWithDefaultTimeout() 314 Expect(session.ExitCode()).To(Equal(0)) 315 316 id, _ := ioutil.ReadFile(targetFile) 317 check := podmanTest.Podman([]string{"pod", "inspect", "abc"}) 318 check.WaitWithDefaultTimeout() 319 data := check.InspectPodToJSON() 320 Expect(data.ID).To(Equal(string(id))) 321 }) 322 323 It("podman pod create --replace", func() { 324 // Make sure we error out with --name. 325 session := podmanTest.Podman([]string{"pod", "create", "--replace", ALPINE, "/bin/sh"}) 326 session.WaitWithDefaultTimeout() 327 Expect(session.ExitCode()).To(Equal(125)) 328 329 // Create and replace 5 times in a row the "same" pod. 330 podName := "testCtr" 331 for i := 0; i < 5; i++ { 332 session = podmanTest.Podman([]string{"pod", "create", "--replace", "--name", podName}) 333 session.WaitWithDefaultTimeout() 334 Expect(session.ExitCode()).To(Equal(0)) 335 } 336 }) 337 338 It("podman create pod with defaults", func() { 339 name := "test" 340 session := podmanTest.Podman([]string{"pod", "create", "--name", name}) 341 session.WaitWithDefaultTimeout() 342 Expect(session.ExitCode()).To(Equal(0)) 343 344 check := podmanTest.Podman([]string{"pod", "inspect", name}) 345 check.WaitWithDefaultTimeout() 346 Expect(check.ExitCode()).To(Equal(0)) 347 data := check.InspectPodToJSON() 348 349 check1 := podmanTest.Podman([]string{"container", "inspect", "--format", "{{.Config.Entrypoint}}", data.Containers[0].ID}) 350 check1.WaitWithDefaultTimeout() 351 Expect(check1.ExitCode()).To(Equal(0)) 352 Expect(check1.OutputToString()).To(Equal("/pause")) 353 354 // check the Path and Args 355 check2 := podmanTest.Podman([]string{"container", "inspect", "--format", "{{.Path}}:{{.Args}}", data.Containers[0].ID}) 356 check2.WaitWithDefaultTimeout() 357 Expect(check2.ExitCode()).To(Equal(0)) 358 Expect(check2.OutputToString()).To(Equal("/pause:[/pause]")) 359 }) 360 361 It("podman create pod with --infra-command", func() { 362 name := "test" 363 session := podmanTest.Podman([]string{"pod", "create", "--infra-command", "/pause1", "--name", name}) 364 session.WaitWithDefaultTimeout() 365 Expect(session.ExitCode()).To(Equal(0)) 366 367 check := podmanTest.Podman([]string{"pod", "inspect", name}) 368 check.WaitWithDefaultTimeout() 369 Expect(check.ExitCode()).To(Equal(0)) 370 data := check.InspectPodToJSON() 371 372 check1 := podmanTest.Podman([]string{"container", "inspect", "--format", "{{.Config.Entrypoint}}", data.Containers[0].ID}) 373 check1.WaitWithDefaultTimeout() 374 Expect(check1.ExitCode()).To(Equal(0)) 375 Expect(check1.OutputToString()).To(Equal("/pause1")) 376 377 // check the Path and Args 378 check2 := podmanTest.Podman([]string{"container", "inspect", "--format", "{{.Path}}:{{.Args}}", data.Containers[0].ID}) 379 check2.WaitWithDefaultTimeout() 380 Expect(check2.ExitCode()).To(Equal(0)) 381 Expect(check2.OutputToString()).To(Equal("/pause1:[/pause1]")) 382 }) 383 384 It("podman create pod with --infra-image", func() { 385 dockerfile := `FROM quay.io/libpod/alpine:latest 386 entrypoint ["/fromimage"] 387 ` 388 podmanTest.BuildImage(dockerfile, "localhost/infra", "false") 389 name := "test" 390 session := podmanTest.Podman([]string{"pod", "create", "--infra-image", "localhost/infra", "--name", name}) 391 session.WaitWithDefaultTimeout() 392 Expect(session.ExitCode()).To(Equal(0)) 393 394 check := podmanTest.Podman([]string{"pod", "inspect", name}) 395 check.WaitWithDefaultTimeout() 396 Expect(check.ExitCode()).To(Equal(0)) 397 data := check.InspectPodToJSON() 398 399 check1 := podmanTest.Podman([]string{"container", "inspect", "--format", "{{.Config.Entrypoint}}", data.Containers[0].ID}) 400 check1.WaitWithDefaultTimeout() 401 Expect(check1.ExitCode()).To(Equal(0)) 402 Expect(check1.OutputToString()).To(Equal("/fromimage")) 403 404 // check the Path and Args 405 check2 := podmanTest.Podman([]string{"container", "inspect", "--format", "{{.Path}}:{{.Args}}", data.Containers[0].ID}) 406 check2.WaitWithDefaultTimeout() 407 Expect(check2.ExitCode()).To(Equal(0)) 408 Expect(check2.OutputToString()).To(Equal("/fromimage:[/fromimage]")) 409 }) 410 411 It("podman create pod with --infra-command --infra-image", func() { 412 dockerfile := `FROM quay.io/libpod/alpine:latest 413 entrypoint ["/fromimage"] 414 ` 415 podmanTest.BuildImage(dockerfile, "localhost/infra", "false") 416 name := "test" 417 session := podmanTest.Podman([]string{"pod", "create", "--infra-image", "localhost/infra", "--infra-command", "/fromcommand", "--name", name}) 418 session.WaitWithDefaultTimeout() 419 Expect(session.ExitCode()).To(Equal(0)) 420 421 check := podmanTest.Podman([]string{"pod", "inspect", name}) 422 check.WaitWithDefaultTimeout() 423 Expect(check.ExitCode()).To(Equal(0)) 424 data := check.InspectPodToJSON() 425 426 check1 := podmanTest.Podman([]string{"container", "inspect", "--format", "{{.Config.Entrypoint}}", data.Containers[0].ID}) 427 check1.WaitWithDefaultTimeout() 428 Expect(check1.ExitCode()).To(Equal(0)) 429 Expect(check1.OutputToString()).To(Equal("/fromcommand")) 430 431 // check the Path and Args 432 check2 := podmanTest.Podman([]string{"container", "inspect", "--format", "{{.Path}}:{{.Args}}", data.Containers[0].ID}) 433 check2.WaitWithDefaultTimeout() 434 Expect(check2.ExitCode()).To(Equal(0)) 435 Expect(check2.OutputToString()).To(Equal("/fromcommand:[/fromcommand]")) 436 }) 437 438 It("podman create pod with slirp network option", func() { 439 name := "test" 440 session := podmanTest.Podman([]string{"pod", "create", "--name", name, "--network", "slirp4netns:port_handler=slirp4netns", "-p", "8082:8000"}) 441 session.WaitWithDefaultTimeout() 442 Expect(session.ExitCode()).To(Equal(0)) 443 444 check := podmanTest.Podman([]string{"pod", "inspect", "--format", "{{.InfraConfig.NetworkOptions.slirp4netns}}", name}) 445 check.WaitWithDefaultTimeout() 446 Expect(check.ExitCode()).To(Equal(0)) 447 Expect(check.OutputToString()).To(Equal("[port_handler=slirp4netns]")) 448 }) 449 450 It("podman pod status test", func() { 451 podName := "testpod" 452 create := podmanTest.Podman([]string{"pod", "create", "--name", podName}) 453 create.WaitWithDefaultTimeout() 454 Expect(create.ExitCode()).To(Equal(0)) 455 456 status1 := podmanTest.Podman([]string{"pod", "inspect", "--format", "{{ .State }}", podName}) 457 status1.WaitWithDefaultTimeout() 458 Expect(status1.ExitCode()).To(Equal(0)) 459 Expect(strings.Contains(status1.OutputToString(), "Created")).To(BeTrue()) 460 461 ctr1 := podmanTest.Podman([]string{"run", "--pod", podName, "-d", ALPINE, "top"}) 462 ctr1.WaitWithDefaultTimeout() 463 Expect(ctr1.ExitCode()).To(Equal(0)) 464 465 status2 := podmanTest.Podman([]string{"pod", "inspect", "--format", "{{ .State }}", podName}) 466 status2.WaitWithDefaultTimeout() 467 Expect(status2.ExitCode()).To(Equal(0)) 468 Expect(strings.Contains(status2.OutputToString(), "Running")).To(BeTrue()) 469 470 ctr2 := podmanTest.Podman([]string{"create", "--pod", podName, ALPINE, "top"}) 471 ctr2.WaitWithDefaultTimeout() 472 Expect(ctr2.ExitCode()).To(Equal(0)) 473 474 status3 := podmanTest.Podman([]string{"pod", "inspect", "--format", "{{ .State }}", podName}) 475 status3.WaitWithDefaultTimeout() 476 Expect(status3.ExitCode()).To(Equal(0)) 477 Expect(strings.Contains(status3.OutputToString(), "Degraded")).To(BeTrue()) 478 }) 479 })