Skip to content

Commit

Permalink
Split exec args parsing into new function and add unit tests for it
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 4c270b9 commit c4fa8f7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
9 changes: 7 additions & 2 deletions podman_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -2502,6 +2502,12 @@ async def compose_exec(compose, args):
container_names = compose.container_names_by_service[args.service]
container_name = container_names[args.index - 1]
cnt = compose.container_by_name[container_name]
podman_args = compose_exec_args(cnt, container_name, args)
p = await compose.podman.run([], "exec", podman_args)
sys.exit(p)


def compose_exec_args(cnt, container_name, args):
podman_args = ["--interactive"]
if args.privileged:
podman_args += ["--privileged"]
Expand All @@ -2522,8 +2528,7 @@ async def compose_exec(compose, args):
podman_args += [container_name]
if args.cnt_command is not None and len(args.cnt_command) > 0:
podman_args += args.cnt_command
p = await compose.podman.run([], "exec", podman_args)
sys.exit(p)
return podman_args


async def transfer_service_status(compose, args, action):
Expand Down
46 changes: 46 additions & 0 deletions pytests/test_compose_exec_args.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# SPDX-License-Identifier: GPL-2.0

import argparse
import unittest

from podman_compose import compose_exec_args


class TestExecArgs(unittest.TestCase):
def test_minimal(self):
cnt = get_minimal_container()
args = get_minimal_args()

result = compose_exec_args(cnt, "container_name", args)
expected = ["--interactive", "--tty", "container_name"]
self.assertEqual(result, expected)

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

result = compose_exec_args(cnt, "container_name", args)
expected = [
"--interactive",
"--tty",
"--env",
"key=valuepart1=valuepart2",
"container_name",
]
self.assertEqual(result, expected)


def get_minimal_container():
return {}


def get_minimal_args():
return argparse.Namespace(
T=None,
cnt_command=None,
env=None,
privileged=None,
user=None,
workdir=None,
)

0 comments on commit c4fa8f7

Please sign in to comment.