github.com/containers/podman/v4@v4.9.4/test/e2e/pod_initcontainers_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  
     7  	"github.com/containers/podman/v4/libpod/define"
     8  	. "github.com/containers/podman/v4/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  		stopPod := podmanTest.Podman([]string{"pod", "stop", "foobar"})
    98  		stopPod.WaitWithDefaultTimeout()
    99  		Expect(stopPod).Should(ExitCleanly())
   100  		startPod := podmanTest.Podman([]string{"pod", "start", "foobar"})
   101  		startPod.WaitWithDefaultTimeout()
   102  		Expect(startPod).Should(ExitCleanly())
   103  
   104  		// Because no init was run, the file should not even exist
   105  		doubleCheck := podmanTest.Podman([]string{"exec", verify.OutputToString(), "cat", filename})
   106  		doubleCheck.WaitWithDefaultTimeout()
   107  		Expect(doubleCheck).Should(Exit(1))
   108  
   109  	})
   110  
   111  	It("podman ensure always init containers always run", func() {
   112  		filename := filepath.Join("/dev/shm", RandomString(12))
   113  
   114  		// Write the date to a file
   115  		session := podmanTest.Podman([]string{"create", "--init-ctr", "always", "--pod", "new:foobar", fedoraMinimal, "/bin/sh", "-c", "date +%T.%N > " + filename})
   116  		session.WaitWithDefaultTimeout()
   117  		Expect(session).Should(ExitCleanly())
   118  		verify := podmanTest.Podman([]string{"create", "--pod", "foobar", "-t", ALPINE, "top"})
   119  		verify.WaitWithDefaultTimeout()
   120  		Expect(verify).Should(ExitCleanly())
   121  		start := podmanTest.Podman([]string{"pod", "start", "foobar"})
   122  		start.WaitWithDefaultTimeout()
   123  		Expect(start).Should(ExitCleanly())
   124  
   125  		// capture the date written
   126  		checkLog := podmanTest.Podman([]string{"exec", verify.OutputToString(), "cat", filename})
   127  		checkLog.WaitWithDefaultTimeout()
   128  		firstResult := checkLog.OutputToString()
   129  		Expect(checkLog).Should(ExitCleanly())
   130  
   131  		// Stop and start the pod
   132  		stopPod := podmanTest.Podman([]string{"pod", "stop", "foobar"})
   133  		stopPod.WaitWithDefaultTimeout()
   134  		Expect(stopPod).Should(ExitCleanly())
   135  		startPod := podmanTest.Podman([]string{"pod", "start", "foobar"})
   136  		startPod.WaitWithDefaultTimeout()
   137  		Expect(startPod).Should(ExitCleanly())
   138  
   139  		// Check the file again with exec
   140  		secondCheckLog := podmanTest.Podman([]string{"exec", verify.OutputToString(), "cat", filename})
   141  		secondCheckLog.WaitWithDefaultTimeout()
   142  		Expect(secondCheckLog).Should(ExitCleanly())
   143  
   144  		// Dates should not match
   145  		Expect(firstResult).ToNot(Equal(secondCheckLog.OutputToString()))
   146  	})
   147  
   148  })