Skip to content

Commit 348461c

Browse files
authored
Merge pull request #1007 from mokibit/automate-env-tests
tests/integration: Automate manual `env` tests
2 parents a1e9a82 + 92dbd36 commit 348461c

6 files changed

+296
-43
lines changed

tests/integration/env-file-tests/README.md

Lines changed: 0 additions & 37 deletions
This file was deleted.

tests/integration/env-file-tests/project/container-compose.env-file-obj-optional.yaml renamed to tests/integration/env-file-tests/project/container-compose.env-file-obj-optional-exists.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ services:
77
- /tmp
88
env_file:
99
- path: ../env-files/project-1.env
10-
- path: ../env-files/project-2.env
10+
- path: ../env-files/project-2.env # this file exists
1111
required: false
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-3.env # this file is missing
11+
required: false

tests/integration/env-tests/README.md

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
3+
import os
4+
import unittest
5+
6+
from tests.integration.test_podman_compose import podman_compose_path
7+
from tests.integration.test_podman_compose import test_path
8+
from tests.integration.test_utils import RunSubprocessMixin
9+
10+
11+
def compose_yaml_path():
12+
return os.path.join(os.path.join(test_path(), "env-tests"), "container-compose.yml")
13+
14+
15+
class TestComposeEnv(unittest.TestCase, RunSubprocessMixin):
16+
"""Test that inline environment variable overrides environment variable from compose file."""
17+
18+
def test_env(self):
19+
try:
20+
output, _ = self.run_subprocess_assert_returncode([
21+
podman_compose_path(),
22+
"-f",
23+
compose_yaml_path(),
24+
"run",
25+
"-l",
26+
"monkey",
27+
"-e",
28+
"ZZVAR1=myval2",
29+
"env-test",
30+
])
31+
self.assertIn("ZZVAR1='myval2'", str(output))
32+
finally:
33+
self.run_subprocess_assert_returncode([
34+
podman_compose_path(),
35+
"-f",
36+
compose_yaml_path(),
37+
"down",
38+
])
Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
3+
import os
4+
import unittest
5+
6+
from tests.integration.test_podman_compose import podman_compose_path
7+
from tests.integration.test_podman_compose import test_path
8+
from tests.integration.test_utils import RunSubprocessMixin
9+
10+
11+
def compose_base_path():
12+
return os.path.join(test_path(), "env-file-tests")
13+
14+
15+
class TestComposeEnvFile(unittest.TestCase, RunSubprocessMixin):
16+
def test_path_env_file_inline(self):
17+
# Test taking env variable value directly from env-file when its path is inline path
18+
base_path = compose_base_path()
19+
path_compose_file = os.path.join(base_path, "project/container-compose.yaml")
20+
try:
21+
self.run_subprocess_assert_returncode([
22+
podman_compose_path(),
23+
"-f",
24+
path_compose_file,
25+
"--env-file",
26+
os.path.join(base_path, "env-files/project-1.env"),
27+
"up",
28+
])
29+
output, _ = self.run_subprocess_assert_returncode([
30+
podman_compose_path(),
31+
"-f",
32+
path_compose_file,
33+
"logs",
34+
])
35+
# takes only value ZZVAR1 as container-compose.yaml file requires
36+
self.assertEqual(output, b"ZZVAR1=podman-rocks-123\n")
37+
finally:
38+
self.run_subprocess_assert_returncode([
39+
podman_compose_path(),
40+
"-f",
41+
path_compose_file,
42+
"down",
43+
])
44+
45+
def test_path_env_file_flat_in_compose_file(self):
46+
# Test taking env variable value from env-file/project-1.env which was declared in
47+
# compose file's env_file
48+
base_path = compose_base_path()
49+
path_compose_file = os.path.join(base_path, "project/container-compose.env-file-flat.yaml")
50+
try:
51+
self.run_subprocess_assert_returncode([
52+
podman_compose_path(),
53+
"-f",
54+
path_compose_file,
55+
"up",
56+
])
57+
output, _ = self.run_subprocess_assert_returncode([
58+
podman_compose_path(),
59+
"-f",
60+
path_compose_file,
61+
"logs",
62+
])
63+
# takes all values with a substring ZZ as container-compose.env-file-flat.yaml
64+
# file requires
65+
self.assertEqual(
66+
output,
67+
b"ZZVAR1=podman-rocks-123\nZZVAR2=podman-rocks-124\nZZVAR3=podman-rocks-125\n",
68+
)
69+
finally:
70+
self.run_subprocess_assert_returncode([
71+
podman_compose_path(),
72+
"-f",
73+
path_compose_file,
74+
"down",
75+
])
76+
77+
def test_path_env_file_obj_in_compose_file(self):
78+
# take variable value from env-file project-1.env which was declared in compose
79+
# file's env_file by -path: ...
80+
base_path = compose_base_path()
81+
path_compose_file = os.path.join(base_path, "project/container-compose.env-file-obj.yaml")
82+
try:
83+
self.run_subprocess_assert_returncode([
84+
podman_compose_path(),
85+
"-f",
86+
path_compose_file,
87+
"up",
88+
])
89+
output, _ = self.run_subprocess_assert_returncode([
90+
podman_compose_path(),
91+
"-f",
92+
path_compose_file,
93+
"logs",
94+
])
95+
# takes all values with a substring ZZ as container-compose.env-file-obj.yaml
96+
# file requires
97+
self.assertEqual(
98+
output,
99+
b"ZZVAR1=podman-rocks-123\nZZVAR2=podman-rocks-124\nZZVAR3=podman-rocks-125\n",
100+
)
101+
finally:
102+
self.run_subprocess_assert_returncode([
103+
podman_compose_path(),
104+
"-f",
105+
path_compose_file,
106+
"down",
107+
])
108+
109+
def test_exists_optional_env_file_path_in_compose_file(self):
110+
# test taking env variable values from several env-files when one of them is optional
111+
# and exists
112+
base_path = compose_base_path()
113+
path_compose_file = os.path.join(
114+
base_path, "project/container-compose.env-file-obj-optional-exists.yaml"
115+
)
116+
try:
117+
self.run_subprocess_assert_returncode([
118+
podman_compose_path(),
119+
"-f",
120+
path_compose_file,
121+
"up",
122+
])
123+
output, _ = self.run_subprocess_assert_returncode([
124+
podman_compose_path(),
125+
"-f",
126+
path_compose_file,
127+
"logs",
128+
])
129+
# FIXME: gives a weird output, needs to be double checked
130+
self.assertEqual(
131+
output,
132+
b"ZZVAR1=podman-rocks-223\nZZVAR2=podman-rocks-224\nZZVAR3=podman-rocks-125\n",
133+
)
134+
finally:
135+
self.run_subprocess_assert_returncode([
136+
podman_compose_path(),
137+
"-f",
138+
path_compose_file,
139+
"down",
140+
])
141+
142+
def test_missing_optional_env_file_path_in_compose_file(self):
143+
# test taking env variable values from several env-files when one of them is optional and
144+
# is missing (silently skip it)
145+
base_path = compose_base_path()
146+
path_compose_file = os.path.join(
147+
base_path, "project/container-compose.env-file-obj-optional-missing.yaml"
148+
)
149+
try:
150+
self.run_subprocess_assert_returncode([
151+
podman_compose_path(),
152+
"-f",
153+
path_compose_file,
154+
"up",
155+
])
156+
output, _ = self.run_subprocess_assert_returncode([
157+
podman_compose_path(),
158+
"-f",
159+
path_compose_file,
160+
"logs",
161+
])
162+
# takes all values with a substring ZZ as container-compose.env-file-obj-optional.yaml
163+
# file requires
164+
self.assertEqual(
165+
output,
166+
b"ZZVAR1=podman-rocks-123\nZZVAR2=podman-rocks-124\nZZVAR3=podman-rocks-125\n",
167+
)
168+
finally:
169+
self.run_subprocess_assert_returncode([
170+
podman_compose_path(),
171+
"-f",
172+
path_compose_file,
173+
"down",
174+
])
175+
176+
def test_var_value_inline_overrides_env_file_path_inline(self):
177+
# Test overriding env value when value is declared in inline command
178+
base_path = compose_base_path()
179+
path_compose_file = os.path.join(base_path, "project/container-compose.yaml")
180+
try:
181+
self.run_subprocess_assert_returncode([
182+
"env",
183+
"ZZVAR1=podman-rocks-321",
184+
podman_compose_path(),
185+
"-f",
186+
path_compose_file,
187+
"--env-file",
188+
os.path.join(base_path, "env-files/project-1.env"),
189+
"up",
190+
])
191+
output, _ = self.run_subprocess_assert_returncode([
192+
podman_compose_path(),
193+
"-f",
194+
path_compose_file,
195+
"logs",
196+
])
197+
# takes only value ZZVAR1 as container-compose.yaml file requires
198+
self.assertEqual(output, b"ZZVAR1=podman-rocks-321\n")
199+
finally:
200+
self.run_subprocess_assert_returncode([
201+
podman_compose_path(),
202+
"-f",
203+
path_compose_file,
204+
"down",
205+
])
206+
207+
def test_taking_env_variables_from_env_files_from_different_directories(self):
208+
# FIXME: It is not clear what this test actually tests, but from README.md it looks like:
209+
# Test overriding env values by directory env-files-tests/.env file values
210+
# and only take value from project/.env, when it does not exist in env-files-tests/.env
211+
base_path = compose_base_path()
212+
path_compose_file = os.path.join(
213+
base_path, "project/container-compose.load-.env-in-project.yaml"
214+
)
215+
try:
216+
# looks like 'run' command does not actually create a container, so output_logs can not
217+
# be used for test comparison
218+
output, _ = self.run_subprocess_assert_returncode([
219+
podman_compose_path(),
220+
"-f",
221+
path_compose_file,
222+
"run",
223+
"--rm",
224+
"app",
225+
])
226+
# takes all values with a substring ZZ as container-compose.load-.env-in-project.yaml
227+
# file requires
228+
# first line is random ID so is ignored in asserting
229+
lines = output.decode('utf-8').split('\n')[1:]
230+
231+
self.assertEqual(
232+
lines,
233+
[
234+
'ZZVAR1=This value is loaded but should be overwritten\r',
235+
'ZZVAR2=This value is loaded from .env in project/ directory\r',
236+
'ZZVAR3=$ZZVAR3\r',
237+
'',
238+
],
239+
)
240+
finally:
241+
self.run_subprocess_assert_returncode([
242+
podman_compose_path(),
243+
"-f",
244+
path_compose_file,
245+
"down",
246+
])

0 commit comments

Comments
 (0)