Skip to content

Commit

Permalink
Merge pull request #19034 from edsantiago/enable_rootless_systemd_test
Browse files Browse the repository at this point in the history
e2e: systemd test: major fixes
  • Loading branch information
openshift-merge-robot authored Jun 28, 2023
2 parents 2ac2ba9 + 8fb719a commit b2ce082
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions test/e2e/systemd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var _ = Describe("Podman systemd", func() {
var systemdUnitFile string

BeforeEach(func() {
podmanCmd := fmt.Sprintf("%s %s", podmanTest.PodmanBinary, strings.Join(podmanTest.MakeOptions(nil, false, false), " "))
systemdUnitFile = fmt.Sprintf(`[Unit]
Description=redis container
[Service]
Expand All @@ -26,36 +27,47 @@ ExecStop=%s stop -t 10 redis
KillMode=process
[Install]
WantedBy=default.target
`, podmanTest.PodmanBinary, podmanTest.PodmanBinary)
`, podmanCmd, podmanCmd)
})

It("podman start container by systemd", func() {
SkipIfRootless("rootless can not write to /etc")
SkipIfRemote("cannot create unit file on remote host")
SkipIfContainerized("test does not have systemd as pid 1")

sysFile := os.WriteFile("/etc/systemd/system/redis.service", []byte(systemdUnitFile), 0644)
dashWhat := "--system"
unitDir := "/run/systemd/system"
if isRootless() {
dashWhat = "--user"
unitDir = fmt.Sprintf("%s/systemd/user", os.Getenv("XDG_RUNTIME_DIR"))
}
err := os.MkdirAll(unitDir, 0700)
Expect(err).ToNot(HaveOccurred())

serviceName := "redis-" + RandomString(10)
sysFilePath := filepath.Join(unitDir, serviceName+".service")
sysFile := os.WriteFile(sysFilePath, []byte(systemdUnitFile), 0644)
Expect(sysFile).ToNot(HaveOccurred())
defer func() {
stop := SystemExec("bash", []string{"-c", "systemctl stop redis"})
os.Remove("/etc/systemd/system/redis.service")
SystemExec("bash", []string{"-c", "systemctl daemon-reload"})
stop := SystemExec("systemctl", []string{dashWhat, "stop", serviceName})
os.Remove(sysFilePath)
SystemExec("systemctl", []string{dashWhat, "daemon-reload"})
Expect(stop).Should(Exit(0))
}()

create := podmanTest.Podman([]string{"create", "--name", "redis", REDIS_IMAGE})
create.WaitWithDefaultTimeout()
Expect(create).Should(Exit(0))

enable := SystemExec("bash", []string{"-c", "systemctl daemon-reload"})
enable := SystemExec("systemctl", []string{dashWhat, "daemon-reload"})
Expect(enable).Should(Exit(0))

start := SystemExec("bash", []string{"-c", "systemctl start redis"})
start := SystemExec("systemctl", []string{dashWhat, "start", serviceName})
Expect(start).Should(Exit(0))

logs := SystemExec("bash", []string{"-c", "journalctl -n 20 -u redis"})
logs := SystemExec("journalctl", []string{dashWhat, "-n", "20", "-u", serviceName})
Expect(logs).Should(Exit(0))

status := SystemExec("bash", []string{"-c", "systemctl status redis"})
status := SystemExec("systemctl", []string{dashWhat, "status", serviceName})
Expect(status.OutputToString()).To(ContainSubstring("active (running)"))
})

Expand Down

0 comments on commit b2ce082

Please sign in to comment.