Skip to content

Commit

Permalink
Extract compose_run args parsing and add unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Ari Pollak <[email protected]>
  • Loading branch information
aripollak committed Mar 9, 2024
1 parent c4fa8f7 commit f2f5483
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 8 deletions.
20 changes: 12 additions & 8 deletions podman_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -2455,6 +2455,18 @@ async def compose_run(compose, args):
)
await compose.commands["build"](compose, build_args)

compose_run_update_container_from_args(compose, cnt, args)
# run podman
podman_args = await container_to_args(compose, cnt, args.detach)
if not args.detach:
podman_args.insert(1, "-i")
if args.rm:
podman_args.insert(1, "--rm")
p = await compose.podman.run([], "run", podman_args)
sys.exit(p)


def compose_run_update_container_from_args(compose, cnt, args):
# adjust one-off container options
name0 = "{}_{}_tmp{}".format(compose.project_name, args.service, random.randrange(0, 65536))
cnt["name"] = args.name or name0
Expand Down Expand Up @@ -2486,14 +2498,6 @@ async def compose_run(compose, args):
# can't restart and --rm
if args.rm and "restart" in cnt:
del cnt["restart"]
# run podman
podman_args = await container_to_args(compose, cnt, args.detach)
if not args.detach:
podman_args.insert(1, "-i")
if args.rm:
podman_args.insert(1, "--rm")
p = await compose.podman.run([], "run", podman_args)
sys.exit(p)


@cmd_run(podman_compose, "exec", "execute a command in a running container")
Expand Down
60 changes: 60 additions & 0 deletions pytests/test_compose_run_update_container_from_args.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# SPDX-License-Identifier: GPL-2.0

import argparse
import unittest

from podman_compose import PodmanCompose
from podman_compose import compose_run_update_container_from_args


class TestRunArgs(unittest.TestCase):
def test_minimal(self):
cnt = get_minimal_container()
compose = get_minimal_compose()
args = get_minimal_args()

compose_run_update_container_from_args(compose, cnt, args)

expected_cnt = {"name": "default_name", "tty": True}
self.assertEqual(cnt, expected_cnt)

def test_additional_env_value_equals(self):
cnt = get_minimal_container()
compose = get_minimal_compose()
args = get_minimal_args()
args.env = ["key=valuepart1=valuepart2"]

compose_run_update_container_from_args(compose, cnt, args)

expected_cnt = {
"environment": {
"key": "valuepart1=valuepart2",
},
"name": "default_name",
"tty": True,
}
self.assertEqual(cnt, expected_cnt)


def get_minimal_container():
return {}


def get_minimal_compose():
return PodmanCompose()


def get_minimal_args():
return argparse.Namespace(
T=None,
cnt_command=None,
entrypoint=None,
env=None,
name="default_name",
rm=None,
service=None,
service_ports=None,
user=None,
volume=None,
workdir=None,
)

0 comments on commit f2f5483

Please sign in to comment.