github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/test/e2e/systemd_test.go (about) 1 // +build !remoteclient 2 3 package integration 4 5 import ( 6 "io/ioutil" 7 "os" 8 "strings" 9 "time" 10 11 "github.com/containers/libpod/pkg/cgroups" 12 . "github.com/containers/libpod/test/utils" 13 . "github.com/onsi/ginkgo" 14 . "github.com/onsi/gomega" 15 ) 16 17 var _ = Describe("Podman systemd", func() { 18 var ( 19 tempdir string 20 err error 21 podmanTest *PodmanTestIntegration 22 systemd_unit_file string 23 ) 24 25 BeforeEach(func() { 26 SkipIfRootless() 27 tempdir, err = CreateTempDirInTempDir() 28 if err != nil { 29 os.Exit(1) 30 } 31 podmanTest = PodmanTestCreate(tempdir) 32 podmanTest.Setup() 33 podmanTest.SeedImages() 34 systemd_unit_file = `[Unit] 35 Description=redis container 36 [Service] 37 Restart=always 38 ExecStart=/usr/bin/podman start -a redis 39 ExecStop=/usr/bin/podman stop -t 10 redis 40 KillMode=process 41 [Install] 42 WantedBy=multi-user.target 43 ` 44 }) 45 46 AfterEach(func() { 47 podmanTest.Cleanup() 48 f := CurrentGinkgoTestDescription() 49 processTestResult(f) 50 51 }) 52 53 It("podman start container by systemd", func() { 54 if os.Getenv("SKIP_USERNS") != "" { 55 Skip("Skip userns tests.") 56 } 57 58 sys_file := ioutil.WriteFile("/etc/systemd/system/redis.service", []byte(systemd_unit_file), 0644) 59 Expect(sys_file).To(BeNil()) 60 defer func() { 61 stop := SystemExec("bash", []string{"-c", "systemctl stop redis"}) 62 os.Remove("/etc/systemd/system/redis.service") 63 SystemExec("bash", []string{"-c", "systemctl daemon-reload"}) 64 Expect(stop.ExitCode()).To(Equal(0)) 65 }() 66 67 create := podmanTest.Podman([]string{"create", "-d", "--name", "redis", "redis"}) 68 create.WaitWithDefaultTimeout() 69 Expect(create.ExitCode()).To(Equal(0)) 70 71 enable := SystemExec("bash", []string{"-c", "systemctl daemon-reload"}) 72 Expect(enable.ExitCode()).To(Equal(0)) 73 74 start := SystemExec("bash", []string{"-c", "systemctl start redis"}) 75 Expect(start.ExitCode()).To(Equal(0)) 76 77 logs := SystemExec("bash", []string{"-c", "journalctl -n 20 -u redis"}) 78 Expect(logs.ExitCode()).To(Equal(0)) 79 80 status := SystemExec("bash", []string{"-c", "systemctl status redis"}) 81 Expect(status.OutputToString()).To(ContainSubstring("active (running)")) 82 }) 83 84 It("podman run container with systemd PID1", func() { 85 cgroupsv2, err := cgroups.IsCgroup2UnifiedMode() 86 Expect(err).To(BeNil()) 87 if cgroupsv2 { 88 Skip("systemd test does not work in cgroups V2 mode yet") 89 } 90 91 systemdImage := "fedora" 92 pull := podmanTest.Podman([]string{"pull", systemdImage}) 93 pull.WaitWithDefaultTimeout() 94 Expect(pull.ExitCode()).To(Equal(0)) 95 96 ctrName := "testSystemd" 97 run := podmanTest.Podman([]string{"run", "--name", ctrName, "-t", "-i", "-d", systemdImage, "/usr/sbin/init"}) 98 run.WaitWithDefaultTimeout() 99 Expect(run.ExitCode()).To(Equal(0)) 100 ctrID := run.OutputToString() 101 102 logs := podmanTest.Podman([]string{"logs", ctrName}) 103 logs.WaitWithDefaultTimeout() 104 Expect(logs.ExitCode()).To(Equal(0)) 105 106 // Give container 10 seconds to start 107 started := false 108 for i := 0; i < 10; i++ { 109 runningCtrs := podmanTest.Podman([]string{"ps", "-q", "--no-trunc"}) 110 runningCtrs.WaitWithDefaultTimeout() 111 Expect(runningCtrs.ExitCode()).To(Equal(0)) 112 113 if strings.Contains(runningCtrs.OutputToString(), ctrID) { 114 started = true 115 break 116 } 117 118 time.Sleep(1 * time.Second) 119 } 120 121 Expect(started).To(BeTrue()) 122 123 systemctl := podmanTest.Podman([]string{"exec", "-t", "-i", ctrName, "systemctl", "status", "--no-pager"}) 124 systemctl.WaitWithDefaultTimeout() 125 Expect(systemctl.ExitCode()).To(Equal(0)) 126 Expect(strings.Contains(systemctl.OutputToString(), "State:")).To(BeTrue()) 127 }) 128 })