Skip to content

Fixes #897: support env_file as objects #925

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions podman_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -993,10 +993,18 @@ async def container_to_args(compose, cnt, detached=True):
for item in norm_as_list(cnt.get("dns_search", None)):
podman_args.extend(["--dns-search", item])
env_file = cnt.get("env_file", [])
if is_str(env_file):
if is_str(env_file) or is_dict(env_file):
env_file = [env_file]
for i in env_file:
i = os.path.realpath(os.path.join(dirname, i))
if is_str(i):
i = {"path": i}
path = i["path"]
required = i.get("required", True)
i = os.path.realpath(os.path.join(dirname, path))
if not os.path.exists(i):
if not required:
continue
raise ValueError("Env file at {} does not exist".format(i))
podman_args.extend(["--env-file", i])
env = norm_as_list(cnt.get("environment", {}))
for e in env:
Expand Down
91 changes: 91 additions & 0 deletions pytests/test_container_to_args.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: GPL-2.0

import unittest
from os import path
from unittest import mock

from podman_compose import container_to_args
Expand Down Expand Up @@ -234,3 +235,93 @@ async def test_rootfs_extension(self):
"/path/to/rootfs",
],
)

async def test_env_file_str(self):
c = create_compose_mock()

cnt = get_minimal_container()
env_file = path.realpath('tests/env-file-tests/env-files/project-1.env')
cnt['env_file'] = env_file

args = await container_to_args(c, cnt)
self.assertEqual(
args,
[
"--name=project_name_service_name1",
"-d",
"--env-file",
env_file,
"--network=bridge",
"--network-alias=service_name",
"busybox",
],
)

async def test_env_file_str_not_exists(self):
c = create_compose_mock()

cnt = get_minimal_container()
cnt['env_file'] = 'notexists'

with self.assertRaises(ValueError):
await container_to_args(c, cnt)

async def test_env_file_str_arr(self):
c = create_compose_mock()

cnt = get_minimal_container()
env_file = path.realpath('tests/env-file-tests/env-files/project-1.env')
cnt['env_file'] = [env_file]

args = await container_to_args(c, cnt)
self.assertEqual(
args,
[
"--name=project_name_service_name1",
"-d",
"--env-file",
env_file,
"--network=bridge",
"--network-alias=service_name",
"busybox",
],
)

async def test_env_file_obj_required(self):
c = create_compose_mock()

cnt = get_minimal_container()
env_file = path.realpath('tests/env-file-tests/env-files/project-1.env')
cnt['env_file'] = {'path': env_file, 'required': True}

args = await container_to_args(c, cnt)
self.assertEqual(
args,
[
"--name=project_name_service_name1",
"-d",
"--env-file",
env_file,
"--network=bridge",
"--network-alias=service_name",
"busybox",
],
)

async def test_env_file_obj_optional(self):
c = create_compose_mock()

cnt = get_minimal_container()
cnt['env_file'] = {'path': 'not-exists', 'required': False}

args = await container_to_args(c, cnt)
self.assertEqual(
args,
[
"--name=project_name_service_name1",
"-d",
"--network=bridge",
"--network-alias=service_name",
"busybox",
],
)
12 changes: 12 additions & 0 deletions tests/env-file-tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,15 @@ podman-compose -f project/container-compose.yaml --env-file env-files/project-1.
```
podman-compose -f $(pwd)/project/container-compose.yaml --env-file $(pwd)/env-files/project-1.env up
```

```
podman-compose -f $(pwd)/project/container-compose.env-file-flat.yaml up
```

```
podman-compose -f $(pwd)/project/container-compose.env-file-obj.yaml up
```

```
podman-compose -f $(pwd)/project/container-compose.env-file-obj-optional.yaml up
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:
app:
image: busybox
command: ["/bin/busybox", "sh", "-c", "env | grep ZZ"]
tmpfs:
- /run
- /tmp
env_file:
- ../env-files/project-1.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
services:
app:
image: busybox
command: ["/bin/busybox", "sh", "-c", "env | grep ZZ"]
tmpfs:
- /run
- /tmp
env_file:
- path: ../env-files/project-1.env
- path: ../env-files/project-2.env
required: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:
app:
image: busybox
command: ["/bin/busybox", "sh", "-c", "env | grep ZZ"]
tmpfs:
- /run
- /tmp
env_file:
- path: ../env-files/project-1.env