github.com/AbhinandanKurakure/podman/v3@v3.4.10/test/e2e/pod_initcontainers_test.go (about)

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