Skip to content

Commit a8a6ac9

Browse files
committed
Fixing container destroy for unit tests
1 parent 6974018 commit a8a6ac9

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

app/container_manager/docker_shell_commands.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
Useful for debugging and understanding what Docker operations are being performed.
1919
"""
2020
from pathlib import Path
21-
from typing import Dict, List, Union
21+
from typing import Dict, List, Optional, Union
2222

2323
# Log message constants
2424
SHELL_CMD_LOG_PREFIX = "Docker API call equivalent shell command:\n"
@@ -42,18 +42,25 @@
4242
]
4343

4444

45-
def escape_shell_arg(arg: str) -> str:
45+
def escape_shell_arg(arg: Optional[str]) -> str:
4646
"""
4747
Escape shell argument if it contains spaces or special characters.
4848
4949
Uses single-quote wrapping for safety. Any single quotes in the argument
5050
are escaped using the pattern: ' becomes '\''
5151
(close quote, escaped quote, open quote).
5252
53+
Args:
54+
arg: The argument to escape. Docker container/image names can be None
55+
(e.g. a Container whose attrs have no "Name" key), so this is
56+
tolerated and rendered as an empty string rather than raising.
57+
5358
Returns:
5459
The argument wrapped in single quotes if it contains special characters,
5560
otherwise returns the argument unchanged.
5661
"""
62+
if arg is None:
63+
return ""
5764
if any(c in arg for c in SHELL_SPECIAL_CHARS):
5865
# Escape any single quotes: ' becomes '\''
5966
escaped_arg = arg.replace("'", "'\\''")
@@ -193,7 +200,7 @@ def docker_exec_command(
193200
return " ".join(cmd_parts)
194201

195202

196-
def docker_kill_command(container_name: str) -> str:
203+
def docker_kill_command(container_name: Optional[str]) -> str:
197204
"""
198205
Generate docker kill command.
199206
@@ -206,7 +213,7 @@ def docker_kill_command(container_name: str) -> str:
206213
return f"docker kill {escape_shell_arg(container_name)}"
207214

208215

209-
def docker_stop_command(container_name: str) -> str:
216+
def docker_stop_command(container_name: Optional[str]) -> str:
210217
"""
211218
Generate docker stop command.
212219
@@ -219,7 +226,7 @@ def docker_stop_command(container_name: str) -> str:
219226
return f"docker stop {escape_shell_arg(container_name)}"
220227

221228

222-
def docker_rm_command(container_name: str, force: bool = False) -> str:
229+
def docker_rm_command(container_name: Optional[str], force: bool = False) -> str:
223230
"""
224231
Generate docker rm command.
225232

0 commit comments

Comments
 (0)