From 3305b4fa7cb14a3745e4c1e467125a442278d986 Mon Sep 17 00:00:00 2001
From: Urvashi Mohnani <umohnani@redhat.com>
Date: Thu, 22 Feb 2024 09:53:55 -0500
Subject: [PATCH] User volumes param for container rm

Use the volumes param instead of v to ensure that
the libpod endpoint actually removes anonymous volumes
when a container is deleted.

Signed-off-by: Urvashi Mohnani <umohnani@redhat.com>
---
 podman/domain/containers_manager.py         |  6 ++--
 podman/tests/integration/test_containers.py | 31 +++++++++++++++++++++
 2 files changed, 33 insertions(+), 4 deletions(-)

diff --git a/podman/domain/containers_manager.py b/podman/domain/containers_manager.py
index 9a82717d..0d3aa7c1 100644
--- a/podman/domain/containers_manager.py
+++ b/podman/domain/containers_manager.py
@@ -139,10 +139,8 @@ def remove(self, container_id: Union[Container, str], **kwargs):
         if isinstance(container_id, Container):
             container_id = container_id.id
 
-        params = {
-            "v": kwargs.get("v"),
-            "force": kwargs.get("force"),
-        }
+        # v is used for the compat endpoint while volumes is used for the libpod endpoint
+        params = {"v": kwargs.get("v"), "force": kwargs.get("force"), "volumes": kwargs.get("v")}
 
         response = self.client.delete(f"/containers/{container_id}", params=params)
         response.raise_for_status()
diff --git a/podman/tests/integration/test_containers.py b/podman/tests/integration/test_containers.py
index b63e0a47..0fe0363f 100644
--- a/podman/tests/integration/test_containers.py
+++ b/podman/tests/integration/test_containers.py
@@ -2,6 +2,7 @@
 import random
 import tarfile
 import unittest
+import tempfile
 
 try:
     # Python >= 3.10
@@ -158,6 +159,36 @@ def test_container_commit(self):
         self.assertIn("localhost/busybox.local:unittest", image.attrs["RepoTags"])
         busybox.remove(force=True)
 
+    def test_container_rm_anonymous_volume(self):
+        with self.subTest("Check anonymous volume is removed"):
+            container_file = """
+FROM alpine
+VOLUME myvol
+ENV foo=bar
+"""
+            tmp_file = tempfile.mktemp()
+            file = open(tmp_file, 'w')
+            file.write(container_file)
+            file.close()
+            self.client.images.build(dockerfile=tmp_file, tag="test-img", path=".")
+
+            # get existing number of containers and volumes
+            existing_containers = self.client.containers.list(all=True)
+            existing_volumes = self.client.volumes.list()
+
+            container = self.client.containers.create("test-img")
+            container_list = self.client.containers.list(all=True)
+            self.assertEqual(len(container_list), len(existing_containers) + 1)
+            volume_list = self.client.volumes.list()
+            self.assertEqual(len(volume_list), len(existing_volumes) + 1)
+
+            # remove the container with v=True
+            container.remove(v=True)
+            container_list = self.client.containers.list(all=True)
+            self.assertEqual(len(container_list), len(existing_containers))
+            volume_list = self.client.volumes.list()
+            self.assertEqual(len(volume_list), len(existing_volumes))
+
 
 if __name__ == '__main__':
     unittest.main()