From 8fca626e33670d49b4e8f06e09f0a87df3dca1fe Mon Sep 17 00:00:00 2001
From: Aditya Rajan <arajan@redhat.com>
Date: Mon, 27 Sep 2021 15:17:38 +0530
Subject: [PATCH] stop: Do nothing if container was never created in runtime

Following commit ensures we silently return container id on `stop` if
container was never created in OCI runtime.

Following behaviour ensures that we are in parity with docker.

Signed-off-by: Aditya Rajan <arajan@redhat.com>
---
 pkg/domain/infra/abi/containers.go |  4 ++++
 test/e2e/stop_test.go              | 11 +++++++++++
 2 files changed, 15 insertions(+)

diff --git a/pkg/domain/infra/abi/containers.go b/pkg/domain/infra/abi/containers.go
index 02af214a64..8e7e2d4113 100644
--- a/pkg/domain/infra/abi/containers.go
+++ b/pkg/domain/infra/abi/containers.go
@@ -169,6 +169,10 @@ func (ic *ContainerEngine) ContainerStop(ctx context.Context, namesOrIds []strin
 				logrus.Debugf("Container %s is already stopped", c.ID())
 			case options.All && errors.Cause(err) == define.ErrCtrStateInvalid:
 				logrus.Debugf("Container %s is not running, could not stop", c.ID())
+			// container never created in OCI runtime
+			// docker parity: do nothing just return container id
+			case errors.Cause(err) == define.ErrCtrStateInvalid:
+				logrus.Debugf("Container %s is either not created on runtime or is in a invalid state", c.ID())
 			default:
 				return err
 			}
diff --git a/test/e2e/stop_test.go b/test/e2e/stop_test.go
index a984bf6d0d..7f178d7190 100644
--- a/test/e2e/stop_test.go
+++ b/test/e2e/stop_test.go
@@ -234,6 +234,17 @@ var _ = Describe("Podman stop", func() {
 		Expect(strings.TrimSpace(finalCtrs.OutputToString())).To(Equal(""))
 	})
 
+	It("podman stop should return silent success on stopping configured containers", func() {
+		// following container is not created on OCI runtime
+		// so we return success and assume that is is stopped
+		session2 := podmanTest.Podman([]string{"create", "--name", "stopctr", ALPINE, "/bin/sh"})
+		session2.WaitWithDefaultTimeout()
+		Expect(session2).Should(Exit(0))
+		session3 := podmanTest.Podman([]string{"stop", "stopctr"})
+		session3.WaitWithDefaultTimeout()
+		Expect(session3).Should(Exit(0))
+	})
+
 	It("podman stop --cidfile", func() {
 
 		tmpDir, err := ioutil.TempDir("", "")