Skip to content

Commit

Permalink
Merge pull request #24977 from mtrmac/exitcleanly
Browse files Browse the repository at this point in the history
RFC: Introduce PodmanTestIntegration.PodmanCleanly
  • Loading branch information
openshift-merge-bot[bot] authored Jan 10, 2025
2 parents 1a00c92 + de1aa44 commit 1194557
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 29 deletions.
26 changes: 6 additions & 20 deletions test/e2e/attach_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,15 @@ var _ = Describe("Podman attach", func() {
})

It("podman attach to non-running container", func() {
session := podmanTest.Podman([]string{"create", "--name", "test1", "-i", CITEST_IMAGE, "ls"})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
podmanTest.PodmanExitCleanly("create", "--name", "test1", "-i", CITEST_IMAGE, "ls")

results := podmanTest.Podman([]string{"attach", "test1"})
results.WaitWithDefaultTimeout()
Expect(results).Should(ExitWithError(125, "you can only attach to running containers"))
})

It("podman container attach to non-running container", func() {
session := podmanTest.Podman([]string{"container", "create", "--name", "test1", "-i", CITEST_IMAGE, "ls"})

session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
podmanTest.PodmanExitCleanly("container", "create", "--name", "test1", "-i", CITEST_IMAGE, "ls")

results := podmanTest.Podman([]string{"container", "attach", "test1"})
results.WaitWithDefaultTimeout()
Expand All @@ -55,9 +50,7 @@ var _ = Describe("Podman attach", func() {
})

It("podman attach to a running container", func() {
session := podmanTest.Podman([]string{"run", "-d", "--name", "test", CITEST_IMAGE, "/bin/sh", "-c", "while true; do echo test; sleep 1; done"})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
podmanTest.PodmanExitCleanly("run", "-d", "--name", "test", CITEST_IMAGE, "/bin/sh", "-c", "while true; do echo test; sleep 1; done")

results := podmanTest.Podman([]string{"attach", "test"})
time.Sleep(2 * time.Second)
Expand All @@ -67,13 +60,8 @@ var _ = Describe("Podman attach", func() {
})

It("podman attach to the latest container", func() {
session := podmanTest.Podman([]string{"run", "-d", "--name", "test1", CITEST_IMAGE, "/bin/sh", "-c", "while true; do echo test1; sleep 1; done"})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())

session = podmanTest.Podman([]string{"run", "-d", "--name", "test2", CITEST_IMAGE, "/bin/sh", "-c", "while true; do echo test2; sleep 1; done"})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
podmanTest.PodmanExitCleanly("run", "-d", "--name", "test1", CITEST_IMAGE, "/bin/sh", "-c", "while true; do echo test1; sleep 1; done")
podmanTest.PodmanExitCleanly("run", "-d", "--name", "test2", CITEST_IMAGE, "/bin/sh", "-c", "while true; do echo test2; sleep 1; done")

cid := "-l"
if IsRemote() {
Expand All @@ -87,9 +75,7 @@ var _ = Describe("Podman attach", func() {
})

It("podman attach to a container with --sig-proxy set to false", func() {
session := podmanTest.Podman([]string{"run", "-d", "--name", "test", CITEST_IMAGE, "/bin/sh", "-c", "while true; do echo test; sleep 1; done"})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
podmanTest.PodmanExitCleanly("run", "-d", "--name", "test", CITEST_IMAGE, "/bin/sh", "-c", "while true; do echo test; sleep 1; done")

results := podmanTest.Podman([]string{"attach", "--sig-proxy=false", "test"})
time.Sleep(2 * time.Second)
Expand Down
22 changes: 13 additions & 9 deletions test/e2e/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,16 @@ func (s *PodmanSessionIntegration) InspectImageJSON() []inspect.ImageData {
return i
}

// PodmanExitCleanly runs a podman command with args, and expects it to ExitCleanly within the default timeout.
// It returns the session (to allow consuming output if desired).
func (p *PodmanTestIntegration) PodmanExitCleanly(args ...string) *PodmanSessionIntegration {
GinkgoHelper()
session := p.Podman(args)
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
return session
}

// InspectContainer returns a container's inspect data in JSON format
func (p *PodmanTestIntegration) InspectContainer(name string) []define.InspectContainerData {
cmd := []string{"inspect", name}
Expand Down Expand Up @@ -520,15 +530,11 @@ func (p *PodmanTestIntegration) CheckFileInContainerSubstring(name, filepath, ex

// StopContainer stops a container with no timeout, ensuring a fast test.
func (p *PodmanTestIntegration) StopContainer(nameOrID string) {
stop := p.Podman([]string{"stop", "-t0", nameOrID})
stop.WaitWithDefaultTimeout()
Expect(stop).Should(ExitCleanly())
p.PodmanExitCleanly("stop", "-t0", nameOrID)
}

func (p *PodmanTestIntegration) StopPod(nameOrID string) {
stop := p.Podman([]string{"pod", "stop", "-t0", nameOrID})
stop.WaitWithDefaultTimeout()
Expect(stop).Should(ExitCleanly())
p.PodmanExitCleanly("pod", "stop", "-t0", nameOrID)
}

func processTestResult(r SpecReport) {
Expand Down Expand Up @@ -586,9 +592,7 @@ func (p *PodmanTestIntegration) RunTopContainerWithArgs(name string, args []stri
}
podmanArgs = append(podmanArgs, args...)
podmanArgs = append(podmanArgs, "-d", ALPINE, "top", "-b")
session := p.Podman(podmanArgs)
session.WaitWithDefaultTimeout()
Expect(session).To(ExitCleanly())
session := p.PodmanExitCleanly(podmanArgs...)
cid := session.OutputToString()
// Output indicates that top is running, which means it's safe
// for our caller to invoke `podman stop`
Expand Down
1 change: 1 addition & 0 deletions test/utils/matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func (matcher *ExitMatcher) MatchMayChangeInTheFuture(actual interface{}) bool {
}

// ExitCleanly asserts that a PodmanSession exits 0 and with no stderr
// Consider using PodmanTestIntegration.PodmanExitCleanly instead of directly using this matcher.
func ExitCleanly() types.GomegaMatcher {
return &exitCleanlyMatcher{}
}
Expand Down

0 comments on commit 1194557

Please sign in to comment.