Skip to content

Commit 2ed50b9

Browse files
authored
Merge pull request #1258 from mokibit/add-env-var-interpolation-to-keys
Implement environment variable interpolation to YAML dictionary keys
2 parents 9fe6e7f + e97d446 commit 2ed50b9

File tree

6 files changed

+53
-1
lines changed

6 files changed

+53
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add support for environment variable interpolation for YAML keys.

podman_compose.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ def rec_subs(value: dict | str | Iterable, subs_dict: dict[str, Any]) -> dict |
290290
svc_envs = rec_subs(svc_envs, subs_dict)
291291
subs_dict.update(svc_envs)
292292

293-
value = {k: rec_subs(v, subs_dict) for k, v in value.items()}
293+
value = {rec_subs(k, subs_dict): rec_subs(v, subs_dict) for k, v in value.items()}
294294
elif isinstance(value, str):
295295

296296
def convert(m: re.Match) -> str:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
DOT_ENV_VARIABLE=This value is from the .env file
2+
TEST_LABELS=TEST

tests/integration/interpolation/docker-compose.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,10 @@ services:
1111
EXAMPLE_DOT_ENV: $DOT_ENV_VARIABLE
1212
EXAMPLE_LITERAL: This is a $$literal
1313
EXAMPLE_EMPTY: $NOT_A_VARIABLE
14+
labels_test:
15+
image: busybox
16+
labels:
17+
- "$TEST_LABELS=test_labels"
18+
- test.${TEST_LABELS}=${TEST_LABELS}
19+
- "${TEST_LABELS}.test2=test2(`${TEST_LABELS}`)"
1420

tests/integration/interpolation/test_podman_compose_interpolation.py

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

3+
import json
34
import os
45
import unittest
56

@@ -36,6 +37,18 @@ def test_interpolation(self) -> None:
3637
self.assertIn("EXAMPLE_DOT_ENV='This value is from the .env file'", str(output))
3738
self.assertIn("EXAMPLE_EMPTY=''", str(output))
3839
self.assertIn("EXAMPLE_LITERAL='This is a $literal'", str(output))
40+
41+
output, _ = self.run_subprocess_assert_returncode([
42+
"podman",
43+
"inspect",
44+
"interpolation_labels_test_1",
45+
])
46+
inspect_output = json.loads(output)
47+
labels_dict = inspect_output[0].get("Config", {}).get("Labels", {})
48+
self.assertIn(('TEST', 'test_labels'), labels_dict.items())
49+
self.assertIn(('TEST.test2', 'test2(`TEST`)'), labels_dict.items())
50+
self.assertIn(('test.TEST', 'TEST'), labels_dict.items())
51+
3952
finally:
4053
self.run_subprocess_assert_returncode([
4154
podman_compose_path(),

tests/unit/test_rec_subs.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,34 @@ def test_rec_subs(self, desc: str, input: Any, expected: Any) -> None:
7171
sub_dict = {"v1": "high priority", "empty": ""}
7272
result = rec_subs(input, sub_dict)
7373
self.assertEqual(result, expected, msg=desc)
74+
75+
def test_env_var_substitution_in_dictionary_keys(self) -> None:
76+
sub_dict = {"NAME": "TEST1", "NAME2": "TEST2"}
77+
input = {
78+
'services': {
79+
'test': {
80+
'image': 'busybox',
81+
'labels': {
82+
'$NAME and ${NAME2}': '${NAME2} and $NAME',
83+
'test1.${NAME}': 'test1',
84+
'$NAME': '${NAME2}',
85+
'${NAME}.test2': 'Host(`${NAME2}`)',
86+
},
87+
}
88+
}
89+
}
90+
result = rec_subs(input, sub_dict)
91+
expected = {
92+
'services': {
93+
'test': {
94+
'image': 'busybox',
95+
'labels': {
96+
'TEST1 and TEST2': 'TEST2 and TEST1',
97+
'test1.TEST1': 'test1',
98+
'TEST1': 'TEST2',
99+
'TEST1.test2': 'Host(`TEST2`)',
100+
},
101+
}
102+
}
103+
}
104+
self.assertEqual(result, expected)

0 commit comments

Comments
 (0)