Skip to content

Commit 88e4055

Browse files
Alexandre Germainp12tic
Alexandre Germain
authored andcommitted
Add support for env_file as objects
Signed-off-by: Alexandre Germain <[email protected]>
1 parent 35cbc49 commit 88e4055

6 files changed

+142
-2
lines changed

podman_compose.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -993,10 +993,18 @@ 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:
999-
i = os.path.realpath(os.path.join(dirname, i))
999+
if is_str(i):
1000+
i = {"path": i}
1001+
path = i["path"]
1002+
required = i.get("required", True)
1003+
i = os.path.realpath(os.path.join(dirname, path))
1004+
if not os.path.exists(i):
1005+
if not required:
1006+
continue
1007+
raise ValueError("Env file at {} does not exist".format(i))
10001008
podman_args.extend(["--env-file", i])
10011009
env = norm_as_list(cnt.get("environment", {}))
10021010
for e in env:

pytests/test_container_to_args.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# SPDX-License-Identifier: GPL-2.0
22

33
import unittest
4+
from os import path
45
from unittest import mock
56

67
from podman_compose import container_to_args
@@ -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+
)

tests/env-file-tests/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,15 @@ podman-compose -f project/container-compose.yaml --env-file env-files/project-1.
77
```
88
podman-compose -f $(pwd)/project/container-compose.yaml --env-file $(pwd)/env-files/project-1.env up
99
```
10+
11+
```
12+
podman-compose -f $(pwd)/project/container-compose.env-file-flat.yaml up
13+
```
14+
15+
```
16+
podman-compose -f $(pwd)/project/container-compose.env-file-obj.yaml up
17+
```
18+
19+
```
20+
podman-compose -f $(pwd)/project/container-compose.env-file-obj-optional.yaml up
21+
```
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
services:
2+
app:
3+
image: busybox
4+
command: ["/bin/busybox", "sh", "-c", "env | grep ZZ"]
5+
tmpfs:
6+
- /run
7+
- /tmp
8+
env_file:
9+
- ../env-files/project-1.env
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
services:
2+
app:
3+
image: busybox
4+
command: ["/bin/busybox", "sh", "-c", "env | grep ZZ"]
5+
tmpfs:
6+
- /run
7+
- /tmp
8+
env_file:
9+
- path: ../env-files/project-1.env
10+
- path: ../env-files/project-2.env
11+
required: false
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
services:
2+
app:
3+
image: busybox
4+
command: ["/bin/busybox", "sh", "-c", "env | grep ZZ"]
5+
tmpfs:
6+
- /run
7+
- /tmp
8+
env_file:
9+
- path: ../env-files/project-1.env

0 commit comments

Comments
 (0)