Skip to content
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

Add support for multi-line environment files #949

Merged
merged 3 commits into from
May 28, 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
6 changes: 5 additions & 1 deletion podman_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -1072,7 +1072,11 @@ async def container_to_args(compose, cnt, detached=True):
if not required:
continue
raise ValueError("Env file at {} does not exist".format(i))
podman_args.extend(["--env-file", i])
dotenv_dict = {}
dotenv_dict = dotenv_to_dict(i)
env = norm_as_list(dotenv_dict)
for e in env:
podman_args.extend(["-e", e])
env = norm_as_list(cnt.get("environment", {}))
for e in env:
podman_args.extend(["-e", e])
Expand Down
65 changes: 58 additions & 7 deletions pytests/test_container_to_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,12 @@ async def test_env_file_str(self):
[
"--name=project_name_service_name1",
"-d",
"--env-file",
env_file,
"-e",
"ZZVAR1=podman-rocks-123",
"-e",
"ZZVAR2=podman-rocks-124",
"-e",
"ZZVAR3=podman-rocks-125",
"--network=bridge",
"--network-alias=service_name",
"busybox",
Expand All @@ -268,7 +272,7 @@ async def test_env_file_str_not_exists(self):
with self.assertRaises(ValueError):
await container_to_args(c, cnt)

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

cnt = get_minimal_container()
Expand All @@ -281,8 +285,42 @@ async def test_env_file_str_arr(self):
[
"--name=project_name_service_name1",
"-d",
"--env-file",
env_file,
"-e",
"ZZVAR1=podman-rocks-123",
"-e",
"ZZVAR2=podman-rocks-124",
"-e",
"ZZVAR3=podman-rocks-125",
"--network=bridge",
"--network-alias=service_name",
"busybox",
],
)

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

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

args = await container_to_args(c, cnt)
self.assertEqual(
args,
[
"--name=project_name_service_name1",
"-d",
"-e",
"ZZVAR1=podman-rocks-123",
"-e",
"ZZVAR2=podman-rocks-124",
"-e",
"ZZVAR3=podman-rocks-125",
"-e",
"ZZVAR1=podman-rocks-223",
"-e",
"ZZVAR2=podman-rocks-224",
"--network=bridge",
"--network-alias=service_name",
"busybox",
Expand All @@ -302,14 +340,27 @@ async def test_env_file_obj_required(self):
[
"--name=project_name_service_name1",
"-d",
"--env-file",
env_file,
"-e",
"ZZVAR1=podman-rocks-123",
"-e",
"ZZVAR2=podman-rocks-124",
"-e",
"ZZVAR3=podman-rocks-125",
"--network=bridge",
"--network-alias=service_name",
"busybox",
],
)

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

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

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

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

Expand Down
2 changes: 2 additions & 0 deletions tests/env-file-tests/env-files/project-1.env
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
ZZVAR1=podman-rocks-123
ZZVAR2=podman-rocks-124
ZZVAR3=podman-rocks-125
2 changes: 2 additions & 0 deletions tests/env-file-tests/env-files/project-2.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ZZVAR1=podman-rocks-223
ZZVAR2=podman-rocks-224