From 06e0546aae2c43ef0d82d89e1e0543fd278ab793 Mon Sep 17 00:00:00 2001 From: Pino Toscano Date: Tue, 16 Jul 2024 10:03:52 +0200 Subject: [PATCH] feat: add internal Podman.cp() helper Add a simple helper function to the Podman class to wrap the "podman cp" behaviour. There are a couple of things added on top of it: - to ease its usage, in addition to the actual container ID, it is possible to specify "$C" (as literal string) as ID, which will be replaced dynamically by the right ID - any other ID is rejected, as the helper is supposed to copy only for the container represented by the specific class instance Signed-off-by: Pino Toscano --- pytest_client_tools/podman.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/pytest_client_tools/podman.py b/pytest_client_tools/podman.py index 1bcd47c..574e9a4 100644 --- a/pytest_client_tools/podman.py +++ b/pytest_client_tools/podman.py @@ -40,6 +40,15 @@ def __init__( self._hostname = hostname self._port_mappings = port_mappings + def _replace_container_name(self, path): + if path.startswith("$C:"): + return self._running_id + path[2:] + elif path.startswith(f"{self._running_id}:"): + return path + elif ":" in path: + raise ValueError("referring to a different podman container") + return path + @property def running_id(self): return self._running_id @@ -90,3 +99,21 @@ def stop(self): stderr=subprocess.PIPE, ) self._running_id = None + + @requires_running + def cp(self, src, dest): + actual_src = self._replace_container_name(src) + actual_dest = self._replace_container_name(dest) + + args = [ + "podman", + "cp", + actual_src, + actual_dest, + ] + subprocess.run( + args, + check=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + )