Skip to content

Commit f2f5483

Browse files
committed
Extract compose_run args parsing and add unit tests
Signed-off-by: Ari Pollak <ajp@aripollak.com>
1 parent c4fa8f7 commit f2f5483

File tree

2 files changed

+72
-8
lines changed

2 files changed

+72
-8
lines changed

podman_compose.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2455,6 +2455,18 @@ async def compose_run(compose, args):
24552455
)
24562456
await compose.commands["build"](compose, build_args)
24572457

2458+
compose_run_update_container_from_args(compose, cnt, args)
2459+
# run podman
2460+
podman_args = await container_to_args(compose, cnt, args.detach)
2461+
if not args.detach:
2462+
podman_args.insert(1, "-i")
2463+
if args.rm:
2464+
podman_args.insert(1, "--rm")
2465+
p = await compose.podman.run([], "run", podman_args)
2466+
sys.exit(p)
2467+
2468+
2469+
def compose_run_update_container_from_args(compose, cnt, args):
24582470
# adjust one-off container options
24592471
name0 = "{}_{}_tmp{}".format(compose.project_name, args.service, random.randrange(0, 65536))
24602472
cnt["name"] = args.name or name0
@@ -2486,14 +2498,6 @@ async def compose_run(compose, args):
24862498
# can't restart and --rm
24872499
if args.rm and "restart" in cnt:
24882500
del cnt["restart"]
2489-
# run podman
2490-
podman_args = await container_to_args(compose, cnt, args.detach)
2491-
if not args.detach:
2492-
podman_args.insert(1, "-i")
2493-
if args.rm:
2494-
podman_args.insert(1, "--rm")
2495-
p = await compose.podman.run([], "run", podman_args)
2496-
sys.exit(p)
24972501

24982502

24992503
@cmd_run(podman_compose, "exec", "execute a command in a running container")
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
3+
import argparse
4+
import unittest
5+
6+
from podman_compose import PodmanCompose
7+
from podman_compose import compose_run_update_container_from_args
8+
9+
10+
class TestRunArgs(unittest.TestCase):
11+
def test_minimal(self):
12+
cnt = get_minimal_container()
13+
compose = get_minimal_compose()
14+
args = get_minimal_args()
15+
16+
compose_run_update_container_from_args(compose, cnt, args)
17+
18+
expected_cnt = {"name": "default_name", "tty": True}
19+
self.assertEqual(cnt, expected_cnt)
20+
21+
def test_additional_env_value_equals(self):
22+
cnt = get_minimal_container()
23+
compose = get_minimal_compose()
24+
args = get_minimal_args()
25+
args.env = ["key=valuepart1=valuepart2"]
26+
27+
compose_run_update_container_from_args(compose, cnt, args)
28+
29+
expected_cnt = {
30+
"environment": {
31+
"key": "valuepart1=valuepart2",
32+
},
33+
"name": "default_name",
34+
"tty": True,
35+
}
36+
self.assertEqual(cnt, expected_cnt)
37+
38+
39+
def get_minimal_container():
40+
return {}
41+
42+
43+
def get_minimal_compose():
44+
return PodmanCompose()
45+
46+
47+
def get_minimal_args():
48+
return argparse.Namespace(
49+
T=None,
50+
cnt_command=None,
51+
entrypoint=None,
52+
env=None,
53+
name="default_name",
54+
rm=None,
55+
service=None,
56+
service_ports=None,
57+
user=None,
58+
volume=None,
59+
workdir=None,
60+
)

0 commit comments

Comments
 (0)