github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/test/e2e/systemd_test.go (about) 1 package integration 2 3 import ( 4 "io/ioutil" 5 "os" 6 "strings" 7 "time" 8 9 . "github.com/containers/podman/v2/test/utils" 10 . "github.com/onsi/ginkgo" 11 . "github.com/onsi/gomega" 12 ) 13 14 var _ = Describe("Podman systemd", func() { 15 var ( 16 tempdir string 17 err error 18 podmanTest *PodmanTestIntegration 19 systemd_unit_file 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 systemd_unit_file = `[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=multi-user.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(systemd_unit_file), 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.ExitCode()).To(Equal(0)) 60 }() 61 62 create := podmanTest.Podman([]string{"create", "--name", "redis", redis}) 63 create.WaitWithDefaultTimeout() 64 Expect(create.ExitCode()).To(Equal(0)) 65 66 enable := SystemExec("bash", []string{"-c", "systemctl daemon-reload"}) 67 Expect(enable.ExitCode()).To(Equal(0)) 68 69 start := SystemExec("bash", []string{"-c", "systemctl start redis"}) 70 Expect(start.ExitCode()).To(Equal(0)) 71 72 logs := SystemExec("bash", []string{"-c", "journalctl -n 20 -u redis"}) 73 Expect(logs.ExitCode()).To(Equal(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.ExitCode()).To(Equal(0)) 84 ctrID := run.OutputToString() 85 86 logs := podmanTest.Podman([]string{"logs", ctrName}) 87 logs.WaitWithDefaultTimeout() 88 Expect(logs.ExitCode()).To(Equal(0)) 89 90 // Give container 10 seconds to start 91 started := false 92 for i := 0; i < 10; i++ { 93 runningCtrs := podmanTest.Podman([]string{"ps", "-q", "--no-trunc"}) 94 runningCtrs.WaitWithDefaultTimeout() 95 Expect(runningCtrs.ExitCode()).To(Equal(0)) 96 97 if strings.Contains(runningCtrs.OutputToString(), ctrID) { 98 started = true 99 break 100 } 101 102 time.Sleep(1 * time.Second) 103 } 104 105 Expect(started).To(BeTrue()) 106 107 systemctl := podmanTest.Podman([]string{"exec", "-t", "-i", ctrName, "systemctl", "status", "--no-pager"}) 108 systemctl.WaitWithDefaultTimeout() 109 Expect(systemctl.ExitCode()).To(Equal(0)) 110 Expect(strings.Contains(systemctl.OutputToString(), "State:")).To(BeTrue()) 111 112 result := podmanTest.Podman([]string{"inspect", ctrName}) 113 result.WaitWithDefaultTimeout() 114 Expect(result.ExitCode()).To(Equal(0)) 115 conData := result.InspectContainerToJSON() 116 Expect(len(conData)).To(Equal(1)) 117 Expect(conData[0].Config.SystemdMode).To(BeTrue()) 118 }) 119 120 It("podman create container with systemd entrypoint triggers systemd mode", func() { 121 ctrName := "testCtr" 122 run := podmanTest.Podman([]string{"create", "--name", ctrName, "--entrypoint", "/sbin/init", ubi_init}) 123 run.WaitWithDefaultTimeout() 124 Expect(run.ExitCode()).To(Equal(0)) 125 126 result := podmanTest.Podman([]string{"inspect", ctrName}) 127 result.WaitWithDefaultTimeout() 128 Expect(result.ExitCode()).To(Equal(0)) 129 conData := result.InspectContainerToJSON() 130 Expect(len(conData)).To(Equal(1)) 131 Expect(conData[0].Config.SystemdMode).To(BeTrue()) 132 }) 133 134 It("podman create container with systemd=always triggers systemd mode", func() { 135 ctrName := "testCtr" 136 run := podmanTest.Podman([]string{"create", "--name", ctrName, "--systemd", "always", ALPINE}) 137 run.WaitWithDefaultTimeout() 138 Expect(run.ExitCode()).To(Equal(0)) 139 140 result := podmanTest.Podman([]string{"inspect", ctrName}) 141 result.WaitWithDefaultTimeout() 142 Expect(result.ExitCode()).To(Equal(0)) 143 conData := result.InspectContainerToJSON() 144 Expect(len(conData)).To(Equal(1)) 145 Expect(conData[0].Config.SystemdMode).To(BeTrue()) 146 }) 147 148 It("podman run --systemd container should NOT mount /run noexec", func() { 149 session := podmanTest.Podman([]string{"run", "--systemd", "always", ALPINE, "sh", "-c", "mount | grep \"/run \""}) 150 session.WaitWithDefaultTimeout() 151 Expect(session.ExitCode()).To(Equal(0)) 152 153 Expect(session.OutputToString()).To(Not(ContainSubstring("noexec"))) 154 }) 155 })