Skip to content

Commit c4fa8f7

Browse files
committed
Split exec args parsing into new function and add unit tests for it
Signed-off-by: Ari Pollak <ajp@aripollak.com>
1 parent 4c270b9 commit c4fa8f7

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

podman_compose.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2502,6 +2502,12 @@ async def compose_exec(compose, args):
25022502
container_names = compose.container_names_by_service[args.service]
25032503
container_name = container_names[args.index - 1]
25042504
cnt = compose.container_by_name[container_name]
2505+
podman_args = compose_exec_args(cnt, container_name, args)
2506+
p = await compose.podman.run([], "exec", podman_args)
2507+
sys.exit(p)
2508+
2509+
2510+
def compose_exec_args(cnt, container_name, args):
25052511
podman_args = ["--interactive"]
25062512
if args.privileged:
25072513
podman_args += ["--privileged"]
@@ -2522,8 +2528,7 @@ async def compose_exec(compose, args):
25222528
podman_args += [container_name]
25232529
if args.cnt_command is not None and len(args.cnt_command) > 0:
25242530
podman_args += args.cnt_command
2525-
p = await compose.podman.run([], "exec", podman_args)
2526-
sys.exit(p)
2531+
return podman_args
25272532

25282533

25292534
async def transfer_service_status(compose, args, action):

pytests/test_compose_exec_args.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
3+
import argparse
4+
import unittest
5+
6+
from podman_compose import compose_exec_args
7+
8+
9+
class TestExecArgs(unittest.TestCase):
10+
def test_minimal(self):
11+
cnt = get_minimal_container()
12+
args = get_minimal_args()
13+
14+
result = compose_exec_args(cnt, "container_name", args)
15+
expected = ["--interactive", "--tty", "container_name"]
16+
self.assertEqual(result, expected)
17+
18+
def test_additional_env_value_equals(self):
19+
cnt = get_minimal_container()
20+
args = get_minimal_args()
21+
args.env = ["key=valuepart1=valuepart2"]
22+
23+
result = compose_exec_args(cnt, "container_name", args)
24+
expected = [
25+
"--interactive",
26+
"--tty",
27+
"--env",
28+
"key=valuepart1=valuepart2",
29+
"container_name",
30+
]
31+
self.assertEqual(result, expected)
32+
33+
34+
def get_minimal_container():
35+
return {}
36+
37+
38+
def get_minimal_args():
39+
return argparse.Namespace(
40+
T=None,
41+
cnt_command=None,
42+
env=None,
43+
privileged=None,
44+
user=None,
45+
workdir=None,
46+
)

0 commit comments

Comments
 (0)