Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Introduce PodmanTestIntegration.PodmanCleanly #24977

Merged
merged 2 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use ExpectWithOffset(1,...), the last line in the stack trace in the log will otherwise all point to this line here which is pretty pointless as I like to get linked to the actual command that failed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying GinkgoHelper — that should also affect the failures in WaitWithDefaultTimeout.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes that seems much better than having to deal with correct offset numbers.

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
Loading