mirror of
https://github.com/containers/podman-compose.git
synced 2025-01-10 07:58:50 +01:00
Merge pull request #925 from GerkinDev/feat/env-file-object
Fixes #897: support `env_file` as objects
This commit is contained in:
commit
36a3d3c207
@ -993,10 +993,18 @@ async def container_to_args(compose, cnt, detached=True):
|
|||||||
for item in norm_as_list(cnt.get("dns_search", None)):
|
for item in norm_as_list(cnt.get("dns_search", None)):
|
||||||
podman_args.extend(["--dns-search", item])
|
podman_args.extend(["--dns-search", item])
|
||||||
env_file = cnt.get("env_file", [])
|
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]
|
env_file = [env_file]
|
||||||
for i in 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])
|
podman_args.extend(["--env-file", i])
|
||||||
env = norm_as_list(cnt.get("environment", {}))
|
env = norm_as_list(cnt.get("environment", {}))
|
||||||
for e in env:
|
for e in env:
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
# SPDX-License-Identifier: GPL-2.0
|
# SPDX-License-Identifier: GPL-2.0
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
|
from os import path
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
|
|
||||||
from podman_compose import container_to_args
|
from podman_compose import container_to_args
|
||||||
@ -234,3 +235,93 @@ class TestContainerToArgs(unittest.IsolatedAsyncioTestCase):
|
|||||||
"/path/to/rootfs",
|
"/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",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
@ -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.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
|
||||||
|
```
|
||||||
|
@ -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
|
@ -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
|
@ -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
|
Loading…
Reference in New Issue
Block a user