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

     1  package integration
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"strings"
     7  
     8  	. "github.com/containers/podman/v3/test/utils"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	. "github.com/onsi/gomega/gexec"
    12  )
    13  
    14  var _ = Describe("Podman systemd", func() {
    15  	var (
    16  		tempdir         string
    17  		err             error
    18  		podmanTest      *PodmanTestIntegration
    19  		systemdUnitFile string
    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  		systemdUnitFile = `[Unit]
    31  Description=redis container
    32  [Service]
    33  Restart=always
    34  ExecStart=/usr/bin/podman start -a redis
    35  ExecStop=/usr/bin/podman stop -t 10 redis
    36  KillMode=process
    37  [Install]
    38  WantedBy=default.target
    39  `
    40  	})
    41  
    42  	AfterEach(func() {
    43  		podmanTest.Cleanup()
    44  		f := CurrentGinkgoTestDescription()
    45  		processTestResult(f)
    46  
    47  	})
    48  
    49  	It("podman start container by systemd", func() {
    50  		SkipIfRootless("rootless can not write to /etc")
    51  		SkipIfContainerized("test does not have systemd as pid 1")
    52  
    53  		sys_file := ioutil.WriteFile("/etc/systemd/system/redis.service", []byte(systemdUnitFile), 0644)
    54  		Expect(sys_file).To(BeNil())
    55  		defer func() {
    56  			stop := SystemExec("bash", []string{"-c", "systemctl stop redis"})
    57  			os.Remove("/etc/systemd/system/redis.service")
    58  			SystemExec("bash", []string{"-c", "systemctl daemon-reload"})
    59  			Expect(stop).Should(Exit(0))
    60  		}()
    61  
    62  		create := podmanTest.Podman([]string{"create", "--name", "redis", redis})
    63  		create.WaitWithDefaultTimeout()
    64  		Expect(create).Should(Exit(0))
    65  
    66  		enable := SystemExec("bash", []string{"-c", "systemctl daemon-reload"})
    67  		Expect(enable).Should(Exit(0))
    68  
    69  		start := SystemExec("bash", []string{"-c", "systemctl start redis"})
    70  		Expect(start).Should(Exit(0))
    71  
    72  		logs := SystemExec("bash", []string{"-c", "journalctl -n 20 -u redis"})
    73  		Expect(logs).Should(Exit(0))
    74  
    75  		status := SystemExec("bash", []string{"-c", "systemctl status redis"})
    76  		Expect(status.OutputToString()).To(ContainSubstring("active (running)"))
    77  	})
    78  
    79  	It("podman run container with systemd PID1", func() {
    80  		ctrName := "testSystemd"
    81  		run := podmanTest.Podman([]string{"run", "--name", ctrName, "-t", "-i", "-d", ubi_init, "/sbin/init"})
    82  		run.WaitWithDefaultTimeout()
    83  		Expect(run).Should(Exit(0))
    84  
    85  		logs := podmanTest.Podman([]string{"logs", ctrName})
    86  		logs.WaitWithDefaultTimeout()
    87  		Expect(logs).Should(Exit(0))
    88  
    89  		// Give container 10 seconds to start
    90  		started := podmanTest.WaitContainerReady(ctrName, "Reached target Multi-User System.", 30, 1)
    91  		Expect(started).To(BeTrue())
    92  
    93  		systemctl := podmanTest.Podman([]string{"exec", "-t", "-i", ctrName, "systemctl", "status", "--no-pager"})
    94  		systemctl.WaitWithDefaultTimeout()
    95  		Expect(systemctl).Should(Exit(0))
    96  		Expect(strings.Contains(systemctl.OutputToString(), "State:")).To(BeTrue())
    97  
    98  		result := podmanTest.Podman([]string{"inspect", ctrName})
    99  		result.WaitWithDefaultTimeout()
   100  		Expect(result).Should(Exit(0))
   101  		conData := result.InspectContainerToJSON()
   102  		Expect(len(conData)).To(Equal(1))
   103  		Expect(conData[0].Config.SystemdMode).To(BeTrue())
   104  
   105  		// stats not supported w/ CGv1 rootless or containerized
   106  		if isCgroupsV1() && (isRootless() || isContainerized()) {
   107  			return
   108  		}
   109  		stats := podmanTest.Podman([]string{"stats", "--no-stream", ctrName})
   110  		stats.WaitWithDefaultTimeout()
   111  		Expect(stats).Should(Exit(0))
   112  
   113  		cgroupPath := podmanTest.Podman([]string{"inspect", "--format='{{.State.CgroupPath}}'", ctrName})
   114  		cgroupPath.WaitWithDefaultTimeout()
   115  		Expect(cgroupPath).Should(Exit(0))
   116  		Expect(result.OutputToString()).To(Not(ContainSubstring("init.scope")))
   117  	})
   118  
   119  	It("podman create container with systemd entrypoint triggers systemd mode", func() {
   120  		ctrName := "testCtr"
   121  		run := podmanTest.Podman([]string{"create", "--name", ctrName, "--entrypoint", "/sbin/init", ubi_init})
   122  		run.WaitWithDefaultTimeout()
   123  		Expect(run).Should(Exit(0))
   124  
   125  		result := podmanTest.Podman([]string{"inspect", ctrName})
   126  		result.WaitWithDefaultTimeout()
   127  		Expect(result).Should(Exit(0))
   128  		conData := result.InspectContainerToJSON()
   129  		Expect(len(conData)).To(Equal(1))
   130  		Expect(conData[0].Config.SystemdMode).To(BeTrue())
   131  	})
   132  
   133  	It("podman create container with --uidmap and conmon PidFile accessible", func() {
   134  		ctrName := "testCtrUidMap"
   135  		run := podmanTest.Podman([]string{"run", "-d", "--uidmap=0:1:1000", "--name", ctrName, ALPINE, "top"})
   136  		run.WaitWithDefaultTimeout()
   137  		Expect(run).Should(Exit(0))
   138  
   139  		session := podmanTest.Podman([]string{"inspect", "--format", "{{.ConmonPidFile}}", ctrName})
   140  		session.WaitWithDefaultTimeout()
   141  		Expect(session).Should(Exit(0))
   142  
   143  		pidFile := strings.TrimSuffix(session.OutputToString(), "\n")
   144  		_, err := ioutil.ReadFile(pidFile)
   145  		Expect(err).To(BeNil())
   146  	})
   147  
   148  	It("podman create container with systemd=always triggers systemd mode", func() {
   149  		ctrName := "testCtr"
   150  		run := podmanTest.Podman([]string{"create", "--name", ctrName, "--systemd", "always", ALPINE})
   151  		run.WaitWithDefaultTimeout()
   152  		Expect(run).Should(Exit(0))
   153  
   154  		result := podmanTest.Podman([]string{"inspect", ctrName})
   155  		result.WaitWithDefaultTimeout()
   156  		Expect(result).Should(Exit(0))
   157  		conData := result.InspectContainerToJSON()
   158  		Expect(len(conData)).To(Equal(1))
   159  		Expect(conData[0].Config.SystemdMode).To(BeTrue())
   160  	})
   161  
   162  	It("podman run --systemd container should NOT mount /run noexec", func() {
   163  		session := podmanTest.Podman([]string{"run", "--systemd", "always", ALPINE, "sh", "-c", "mount  | grep \"/run \""})
   164  		session.WaitWithDefaultTimeout()
   165  		Expect(session).Should(Exit(0))
   166  
   167  		Expect(session.OutputToString()).To(Not(ContainSubstring("noexec")))
   168  	})
   169  
   170  	It("podman run --systemd arg is case insensitive", func() {
   171  		session := podmanTest.Podman([]string{"run", "--rm", "--systemd", "Always", ALPINE, "echo", "test"})
   172  		session.WaitWithDefaultTimeout()
   173  		Expect(session).Should(Exit(0))
   174  		Expect(session.OutputToString()).Should(Equal("test"))
   175  
   176  		session = podmanTest.Podman([]string{"run", "--rm", "--systemd", "True", ALPINE, "echo", "test"})
   177  		session.WaitWithDefaultTimeout()
   178  		Expect(session).Should(Exit(0))
   179  		Expect(session.OutputToString()).Should(Equal("test"))
   180  
   181  		session = podmanTest.Podman([]string{"run", "--rm", "--systemd", "False", ALPINE, "echo", "test"})
   182  		session.WaitWithDefaultTimeout()
   183  		Expect(session).Should(Exit(0))
   184  		Expect(session.OutputToString()).Should(Equal("test"))
   185  	})
   186  })