github.com/containers/podman/v5@v5.1.0-rc1/test/e2e/pod_initcontainers_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  
     7  	"github.com/containers/podman/v5/libpod/define"
     8  	. "github.com/containers/podman/v5/test/utils"
     9  	. "github.com/onsi/ginkgo/v2"
    10  	. "github.com/onsi/gomega"
    11  	. "github.com/onsi/gomega/gexec"
    12  )
    13  
    14  var _ = Describe("Podman init containers", func() {
    15  
    16  	It("podman create init container without --pod should fail", func() {
    17  		session := podmanTest.Podman([]string{"create", "--init-ctr", "always", ALPINE, "top"})
    18  		session.WaitWithDefaultTimeout()
    19  		Expect(session).Should(Exit(125))
    20  	})
    21  
    22  	It("podman create init container with bad init type should fail", func() {
    23  		session := podmanTest.Podman([]string{"create", "--init-ctr", "unknown", "--pod", "new:foobar", ALPINE, "top"})
    24  		session.WaitWithDefaultTimeout()
    25  		Expect(session).Should(Exit(125))
    26  	})
    27  
    28  	It("podman init containers should not degrade pod status", func() {
    29  		// create a pod
    30  		topPod := podmanTest.Podman([]string{"create", "-t", "--pod", "new:foobar", ALPINE, "top"})
    31  		topPod.WaitWithDefaultTimeout()
    32  		Expect(topPod).Should(ExitCleanly())
    33  		// add an init container
    34  		session := podmanTest.Podman([]string{"create", "--init-ctr", "always", "--pod", "foobar", ALPINE, "date"})
    35  		session.WaitWithDefaultTimeout()
    36  		Expect(session).Should(ExitCleanly())
    37  		// start a pod
    38  		start := podmanTest.Podman([]string{"pod", "start", "foobar"})
    39  		start.WaitWithDefaultTimeout()
    40  		Expect(start).Should(ExitCleanly())
    41  
    42  		inspect := podmanTest.Podman([]string{"pod", "inspect", "foobar"})
    43  		inspect.WaitWithDefaultTimeout()
    44  		Expect(inspect).Should(ExitCleanly())
    45  		data := inspect.InspectPodToJSON()
    46  		Expect(data).To(HaveField("State", define.PodStateRunning))
    47  	})
    48  
    49  	It("podman create init container should fail in running pod", func() {
    50  		// create a running pod
    51  		topPod := podmanTest.Podman([]string{"run", "-dt", "--pod", "new:foobar", ALPINE, "top"})
    52  		topPod.WaitWithDefaultTimeout()
    53  		Expect(topPod).Should(ExitCleanly())
    54  		// adding init-ctr to running pod should fail
    55  		session := podmanTest.Podman([]string{"create", "--init-ctr", "always", "--pod", "foobar", ALPINE, "date"})
    56  		session.WaitWithDefaultTimeout()
    57  		Expect(session).Should(Exit(125))
    58  	})
    59  
    60  	It("podman make sure init container runs before pod containers", func() {
    61  		filename := filepath.Join("/dev/shm", RandomString(12))
    62  		content := RandomString(16)
    63  		session := podmanTest.Podman([]string{"create", "--init-ctr", "always", "--pod", "new:foobar", ALPINE, "bin/sh", "-c", fmt.Sprintf("echo %s > %s", content, filename)})
    64  		session.WaitWithDefaultTimeout()
    65  		Expect(session).Should(ExitCleanly())
    66  		verify := podmanTest.Podman([]string{"create", "--pod", "foobar", "-t", ALPINE, "top"})
    67  		verify.WaitWithDefaultTimeout()
    68  		Expect(verify).Should(ExitCleanly())
    69  		start := podmanTest.Podman([]string{"pod", "start", "foobar"})
    70  		start.WaitWithDefaultTimeout()
    71  		Expect(start).Should(ExitCleanly())
    72  		checkLog := podmanTest.Podman([]string{"exec", verify.OutputToString(), "cat", filename})
    73  		checkLog.WaitWithDefaultTimeout()
    74  		Expect(checkLog).Should(ExitCleanly())
    75  		Expect(checkLog.OutputToString()).To(Equal(content))
    76  	})
    77  
    78  	It("podman make sure once container is removed", func() {
    79  		filename := filepath.Join("/dev/shm", RandomString(12))
    80  		content := RandomString(16)
    81  		session := podmanTest.Podman([]string{"create", "--init-ctr", "once", "--pod", "new:foobar", ALPINE, "bin/sh", "-c", fmt.Sprintf("echo %s > %s", content, filename)})
    82  		session.WaitWithDefaultTimeout()
    83  		initContainerID := session.OutputToString()
    84  		Expect(session).Should(ExitCleanly())
    85  		verify := podmanTest.Podman([]string{"create", "--pod", "foobar", "-t", ALPINE, "top"})
    86  		verify.WaitWithDefaultTimeout()
    87  		Expect(verify).Should(ExitCleanly())
    88  		start := podmanTest.Podman([]string{"pod", "start", "foobar"})
    89  		start.WaitWithDefaultTimeout()
    90  		Expect(start).Should(ExitCleanly())
    91  		check := podmanTest.Podman([]string{"container", "exists", initContainerID})
    92  		check.WaitWithDefaultTimeout()
    93  		// Container was rm'd
    94  		// Expect(check).Should(Exit(1))
    95  		Expect(check.ExitCode()).To(Equal(1), "I dont understand why the other way does not work")
    96  		// Let's double check with a stop and start
    97  		podmanTest.StopPod("foobar")
    98  		startPod := podmanTest.Podman([]string{"pod", "start", "foobar"})
    99  		startPod.WaitWithDefaultTimeout()
   100  		Expect(startPod).Should(ExitCleanly())
   101  
   102  		// Because no init was run, the file should not even exist
   103  		doubleCheck := podmanTest.Podman([]string{"exec", verify.OutputToString(), "cat", filename})
   104  		doubleCheck.WaitWithDefaultTimeout()
   105  		Expect(doubleCheck).Should(Exit(1))
   106  
   107  	})
   108  
   109  	It("podman ensure always init containers always run", func() {
   110  		filename := filepath.Join("/dev/shm", RandomString(12))
   111  
   112  		// Write the date to a file
   113  		session := podmanTest.Podman([]string{"create", "--init-ctr", "always", "--pod", "new:foobar", fedoraMinimal, "/bin/sh", "-c", "date +%T.%N > " + filename})
   114  		session.WaitWithDefaultTimeout()
   115  		Expect(session).Should(ExitCleanly())
   116  		verify := podmanTest.Podman([]string{"create", "--pod", "foobar", "-t", ALPINE, "top"})
   117  		verify.WaitWithDefaultTimeout()
   118  		Expect(verify).Should(ExitCleanly())
   119  		start := podmanTest.Podman([]string{"pod", "start", "foobar"})
   120  		start.WaitWithDefaultTimeout()
   121  		Expect(start).Should(ExitCleanly())
   122  
   123  		// capture the date written
   124  		checkLog := podmanTest.Podman([]string{"exec", verify.OutputToString(), "cat", filename})
   125  		checkLog.WaitWithDefaultTimeout()
   126  		firstResult := checkLog.OutputToString()
   127  		Expect(checkLog).Should(ExitCleanly())
   128  
   129  		// Stop and start the pod
   130  		podmanTest.StopPod("foobar")
   131  		startPod := podmanTest.Podman([]string{"pod", "start", "foobar"})
   132  		startPod.WaitWithDefaultTimeout()
   133  		Expect(startPod).Should(ExitCleanly())
   134  
   135  		// Check the file again with exec
   136  		secondCheckLog := podmanTest.Podman([]string{"exec", verify.OutputToString(), "cat", filename})
   137  		secondCheckLog.WaitWithDefaultTimeout()
   138  		Expect(secondCheckLog).Should(ExitCleanly())
   139  
   140  		// Dates should not match
   141  		Expect(firstResult).ToNot(Equal(secondCheckLog.OutputToString()))
   142  	})
   143  
   144  })