github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/test/e2e/systemd_activate_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"io/fs"
     7  	"os"
     8  	"os/exec"
     9  	"path/filepath"
    10  	"syscall"
    11  	"time"
    12  
    13  	testUtils "github.com/hanks177/podman/v4/test/utils"
    14  	podmanUtils "github.com/hanks177/podman/v4/utils"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  	. "github.com/onsi/gomega/gexec"
    18  )
    19  
    20  var _ = Describe("Systemd activate", func() {
    21  	var tempDir string
    22  	var err error
    23  	var podmanTest *PodmanTestIntegration
    24  
    25  	BeforeEach(func() {
    26  		tempDir, err = testUtils.CreateTempDirInTempDir()
    27  		if err != nil {
    28  			fmt.Fprintf(os.Stderr, "%v\n", err)
    29  			os.Exit(1)
    30  		}
    31  
    32  		podmanTest = PodmanTestCreate(tempDir)
    33  		podmanTest.Setup()
    34  	})
    35  
    36  	AfterEach(func() {
    37  		podmanTest.Cleanup()
    38  		processTestResult(CurrentGinkgoTestDescription())
    39  	})
    40  
    41  	It("stop podman.service", func() {
    42  		SkipIfRemote("Testing stopped service requires both podman and podman-remote binaries")
    43  
    44  		activate, err := exec.LookPath("systemd-socket-activate")
    45  		if err != nil {
    46  			activate = "/usr/bin/systemd-socket-activate"
    47  		}
    48  		stat, err := os.Stat(activate)
    49  		switch {
    50  		case errors.Is(err, fs.ErrNotExist):
    51  			Skip(activate + " required for systemd activation tests")
    52  		case stat.Mode()&0111 == 0:
    53  			Skip("Unable to execute " + activate)
    54  		case err != nil:
    55  			Skip(err.Error())
    56  		}
    57  
    58  		// systemd-socket-activate does not support DNS lookups
    59  		host := "127.0.0.1"
    60  		port, err := podmanUtils.GetRandomPort()
    61  		Expect(err).ToNot(HaveOccurred())
    62  
    63  		activateSession := testUtils.StartSystemExec(activate, []string{
    64  			fmt.Sprintf("--listen=%s:%d", host, port),
    65  			podmanTest.PodmanBinary,
    66  			"--root=" + filepath.Join(tempDir, "server_root"),
    67  			"system", "service",
    68  			"--time=0",
    69  		})
    70  		Expect(activateSession.Exited).ShouldNot(Receive(), "Failed to start podman service")
    71  
    72  		// Curried functions for specialized podman calls
    73  		podmanRemote := func(args ...string) *testUtils.PodmanSession {
    74  			args = append([]string{"--url", fmt.Sprintf("tcp://%s:%d", host, port)}, args...)
    75  			return testUtils.SystemExec(podmanTest.RemotePodmanBinary, args)
    76  		}
    77  
    78  		podman := func(args ...string) *testUtils.PodmanSession {
    79  			args = append([]string{"--root", filepath.Join(tempDir, "server_root")}, args...)
    80  			return testUtils.SystemExec(podmanTest.PodmanBinary, args)
    81  		}
    82  
    83  		containerName := "top_" + testUtils.RandomString(8)
    84  		apiSession := podmanRemote(
    85  			"create", "--tty", "--name", containerName, "--entrypoint", "top",
    86  			"quay.io/libpod/alpine_labels:latest",
    87  		)
    88  		Expect(apiSession).Should(Exit(0))
    89  
    90  		apiSession = podmanRemote("start", containerName)
    91  		Expect(apiSession).Should(Exit(0))
    92  
    93  		apiSession = podmanRemote("inspect", "--format={{.State.Running}}", containerName)
    94  		Expect(apiSession).Should(Exit(0))
    95  		Expect(apiSession.OutputToString()).To(Equal("true"))
    96  
    97  		// Emulate 'systemd stop podman.service'
    98  		activateSession.Signal(syscall.SIGTERM)
    99  		time.Sleep(100 * time.Millisecond)
   100  		Eventually(activateSession).Should(Exit(0))
   101  
   102  		abiSession := podman("inspect", "--format={{.State.Running}}", containerName)
   103  		Expect(abiSession).To(Exit(0))
   104  		Expect(abiSession.OutputToString()).To(Equal("true"))
   105  	})
   106  })