Skip to content

Commit 03ad86b

Browse files
author
Alexandre Germain
committed
test: add test
Signed-off-by: Alexandre Germain <[email protected]>
1 parent a5e55dc commit 03ad86b

File tree

2 files changed

+94
-3
lines changed

2 files changed

+94
-3
lines changed

podman_compose.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -993,13 +993,13 @@ async def container_to_args(compose, cnt, detached=True):
993993
for item in norm_as_list(cnt.get("dns_search", None)):
994994
podman_args.extend(["--dns-search", item])
995995
env_file = cnt.get("env_file", [])
996-
if is_str(env_file):
996+
if is_str(env_file) or is_dict(env_file):
997997
env_file = [env_file]
998998
for i in env_file:
999999
if is_str(i):
10001000
i = {"path": i}
1001-
path = i['path']
1002-
required = i.get('required', True)
1001+
path = i["path"]
1002+
required = i.get("required", True)
10031003
i = os.path.realpath(os.path.join(dirname, path))
10041004
if not os.path.exists(i):
10051005
if not required:

pytests/test_container_to_args.py

+91
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# SPDX-License-Identifier: GPL-2.0
22

3+
from os import path
34
import unittest
45
from unittest import mock
56

@@ -234,3 +235,93 @@ async def test_rootfs_extension(self):
234235
"/path/to/rootfs",
235236
],
236237
)
238+
239+
async def test_env_file_str(self):
240+
c = create_compose_mock()
241+
242+
cnt = get_minimal_container()
243+
env_file = path.realpath('tests/env-file-tests/env-files/project-1.env')
244+
cnt['env_file'] = env_file
245+
246+
args = await container_to_args(c, cnt)
247+
self.assertEqual(
248+
args,
249+
[
250+
"--name=project_name_service_name1",
251+
"-d",
252+
"--env-file",
253+
env_file,
254+
"--network=bridge",
255+
"--network-alias=service_name",
256+
"busybox",
257+
],
258+
)
259+
260+
async def test_env_file_str_not_exists(self):
261+
c = create_compose_mock()
262+
263+
cnt = get_minimal_container()
264+
cnt['env_file'] = 'notexists'
265+
266+
with self.assertRaises(ValueError):
267+
await container_to_args(c, cnt)
268+
269+
async def test_env_file_str_arr(self):
270+
c = create_compose_mock()
271+
272+
cnt = get_minimal_container()
273+
env_file = path.realpath('tests/env-file-tests/env-files/project-1.env')
274+
cnt['env_file'] = [env_file]
275+
276+
args = await container_to_args(c, cnt)
277+
self.assertEqual(
278+
args,
279+
[
280+
"--name=project_name_service_name1",
281+
"-d",
282+
"--env-file",
283+
env_file,
284+
"--network=bridge",
285+
"--network-alias=service_name",
286+
"busybox",
287+
],
288+
)
289+
290+
async def test_env_file_obj_required(self):
291+
c = create_compose_mock()
292+
293+
cnt = get_minimal_container()
294+
env_file = path.realpath('tests/env-file-tests/env-files/project-1.env')
295+
cnt['env_file'] = {'path': env_file, 'required': True}
296+
297+
args = await container_to_args(c, cnt)
298+
self.assertEqual(
299+
args,
300+
[
301+
"--name=project_name_service_name1",
302+
"-d",
303+
"--env-file",
304+
env_file,
305+
"--network=bridge",
306+
"--network-alias=service_name",
307+
"busybox",
308+
],
309+
)
310+
311+
async def test_env_file_obj_optional(self):
312+
c = create_compose_mock()
313+
314+
cnt = get_minimal_container()
315+
cnt['env_file'] = {'path': 'not-exists', 'required': False}
316+
317+
args = await container_to_args(c, cnt)
318+
self.assertEqual(
319+
args,
320+
[
321+
"--name=project_name_service_name1",
322+
"-d",
323+
"--network=bridge",
324+
"--network-alias=service_name",
325+
"busybox",
326+
],
327+
)

0 commit comments

Comments
 (0)