Debian Codegraph @

tests/integration/env_file_tests/test_podman_compose_env_file.py

raw symbols SCIP
1 # SPDX-License-Identifier: GPL-2.0
2
3 import os
4 import unittest
5
6 from tests.integration.test_utils import RunSubprocessMixin
7 from tests.integration.test_utils import podman_compose_path
8 from tests.integration.test_utils import test_path
9
10
11 def compose_base_path() -> str:
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) -> None:
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 "--no-log-prefix",
35 "--no-color",
36 ])
37 # takes only value ZZVAR1 as container-compose.yaml file requires
38 self.assertEqual(output, b"ZZVAR1=podman-rocks-123\n")
39 finally:
40 self.run_subprocess_assert_returncode([
41 podman_compose_path(),
42 "-f",
43 path_compose_file,
44 "down",
45 ])
46
47 def test_path_env_file_flat_in_compose_file(self) -> None:
48 # Test taking env variable value from env-file/project-1.env which was declared in
49 # compose file's env_file
50 base_path = compose_base_path()
51 path_compose_file = os.path.join(base_path, "project/container-compose.env-file-flat.yaml")
52 try:
53 self.run_subprocess_assert_returncode([
54 podman_compose_path(),
55 "-f",
56 path_compose_file,
57 "up",
58 ])
59 output, _ = self.run_subprocess_assert_returncode([
60 podman_compose_path(),
61 "-f",
62 path_compose_file,
63 "logs",
64 "--no-log-prefix",
65 "--no-color",
66 ])
67 # takes all values with a substring ZZ as container-compose.env-file-flat.yaml
68 # file requires
69 self.assertEqual(
70 output,
71 b"ZZVAR1=podman-rocks-123\nZZVAR2=podman-rocks-124\nZZVAR3=podman-rocks-125\n",
72 )
73 finally:
74 self.run_subprocess_assert_returncode([
75 podman_compose_path(),
76 "-f",
77 path_compose_file,
78 "down",
79 ])
80
81 def test_path_env_file_obj_in_compose_file(self) -> None:
82 # take variable value from env-file project-1.env which was declared in compose
83 # file's env_file by -path: ...
84 base_path = compose_base_path()
85 path_compose_file = os.path.join(base_path, "project/container-compose.env-file-obj.yaml")
86 try:
87 self.run_subprocess_assert_returncode([
88 podman_compose_path(),
89 "-f",
90 path_compose_file,
91 "up",
92 ])
93 output, _ = self.run_subprocess_assert_returncode([
94 podman_compose_path(),
95 "-f",
96 path_compose_file,
97 "logs",
98 "--no-log-prefix",
99 "--no-color",
100 ])
101 # takes all values with a substring ZZ as container-compose.env-file-obj.yaml
102 # file requires
103 self.assertEqual(
104 output,
105 b"ZZVAR1=podman-rocks-123\nZZVAR2=podman-rocks-124\nZZVAR3=podman-rocks-125\n",
106 )
107 finally:
108 self.run_subprocess_assert_returncode([
109 podman_compose_path(),
110 "-f",
111 path_compose_file,
112 "down",
113 ])
114
115 def test_exists_optional_env_file_path_in_compose_file(self) -> None:
116 # test taking env variable values from several env-files when one of them is optional
117 # and exists
118 base_path = compose_base_path()
119 path_compose_file = os.path.join(
120 base_path, "project/container-compose.env-file-obj-optional-exists.yaml"
121 )
122 try:
123 self.run_subprocess_assert_returncode([
124 podman_compose_path(),
125 "-f",
126 path_compose_file,
127 "up",
128 ])
129 output, _ = self.run_subprocess_assert_returncode([
130 podman_compose_path(),
131 "-f",
132 path_compose_file,
133 "logs",
134 "--no-log-prefix",
135 "--no-color",
136 ])
137 # FIXME: gives a weird output, needs to be double checked
138 self.assertEqual(
139 output,
140 b"ZZVAR1=podman-rocks-223\nZZVAR2=podman-rocks-224\nZZVAR3=podman-rocks-125\n",
141 )
142 finally:
143 self.run_subprocess_assert_returncode([
144 podman_compose_path(),
145 "-f",
146 path_compose_file,
147 "down",
148 ])
149
150 def test_missing_optional_env_file_path_in_compose_file(self) -> None:
151 # test taking env variable values from several env-files when one of them is optional and
152 # is missing (silently skip it)
153 base_path = compose_base_path()
154 path_compose_file = os.path.join(
155 base_path, "project/container-compose.env-file-obj-optional-missing.yaml"
156 )
157 try:
158 self.run_subprocess_assert_returncode([
159 podman_compose_path(),
160 "-f",
161 path_compose_file,
162 "up",
163 ])
164 output, _ = self.run_subprocess_assert_returncode([
165 podman_compose_path(),
166 "-f",
167 path_compose_file,
168 "logs",
169 "--no-log-prefix",
170 "--no-color",
171 ])
172 # takes all values with a substring ZZ as container-compose.env-file-obj-optional.yaml
173 # file requires
174 self.assertEqual(
175 output,
176 b"ZZVAR1=podman-rocks-123\nZZVAR2=podman-rocks-124\nZZVAR3=podman-rocks-125\n",
177 )
178 finally:
179 self.run_subprocess_assert_returncode([
180 podman_compose_path(),
181 "-f",
182 path_compose_file,
183 "down",
184 ])
185
186 def test_var_value_inline_overrides_env_file_path_inline(self) -> None:
187 # Test overriding env value when value is declared in inline command
188 base_path = compose_base_path()
189 path_compose_file = os.path.join(base_path, "project/container-compose.yaml")
190 try:
191 self.run_subprocess_assert_returncode([
192 "env",
193 "ZZVAR1=podman-rocks-321",
194 podman_compose_path(),
195 "-f",
196 path_compose_file,
197 "--env-file",
198 os.path.join(base_path, "env-files/project-1.env"),
199 "up",
200 ])
201 output, _ = self.run_subprocess_assert_returncode([
202 podman_compose_path(),
203 "-f",
204 path_compose_file,
205 "logs",
206 "--no-log-prefix",
207 "--no-color",
208 ])
209 # takes only value ZZVAR1 as container-compose.yaml file requires
210 self.assertEqual(output, b"ZZVAR1=podman-rocks-321\n")
211 finally:
212 self.run_subprocess_assert_returncode([
213 podman_compose_path(),
214 "-f",
215 path_compose_file,
216 "down",
217 ])
218
219 def test_taking_env_variables_from_env_files_from_different_directories(self) -> None:
220 # FIXME: It is not clear what this test actually tests, but from README.md it looks like:
221 # Test overriding env values by directory env-files-tests/.env file values
222 # and only take value from project/.env, when it does not exist in env-files-tests/.env
223 base_path = compose_base_path()
224 path_compose_file = os.path.join(
225 base_path, "project/container-compose.load-.env-in-project.yaml"
226 )
227 try:
228 # looks like 'run' command does not actually create a container, so output_logs can not
229 # be used for test comparison
230 output, _ = self.run_subprocess_assert_returncode([
231 podman_compose_path(),
232 "-f",
233 path_compose_file,
234 "run",
235 "--rm",
236 "app",
237 ])
238 # takes all values with a substring ZZ as container-compose.load-.env-in-project.yaml
239 # file requires
240 # first line is random ID so is ignored in asserting
241 lines = output.decode('utf-8').split('\n')[1:]
242
243 self.assertEqual(
244 lines,
245 [
246 'ZZVAR1=This value is loaded but should be overwritten\r',
247 'ZZVAR2=This value is loaded from .env in project/ directory\r',
248 'ZZVAR3=TEST\r',
249 '',
250 ],
251 )
252 finally:
253 self.run_subprocess_assert_returncode([
254 podman_compose_path(),
255 "-f",
256 path_compose_file,
257 "down",
258 ])
259
260 def test_env_var_value_accessed_in_compose_file_short_syntax(self) -> None:
261 # Test that compose file can access the environment variable set in .env file using
262 # short syntax, that is: only the name of environment variable is used in "environment:" in
263 # compose.yml file and its value is picked up directly from .env file
264 # long syntax of environment variables interpolation is tested in
265 # tests/integration/interpolation
266
267 base_path = compose_base_path()
268 compose_file_path = os.path.join(base_path, "project/container-compose.short_syntax.yaml")
269 try:
270 self.run_subprocess_assert_returncode([
271 podman_compose_path(),
272 "-f",
273 compose_file_path,
274 "up",
275 "-d",
276 ])
277 output, _ = self.run_subprocess_assert_returncode([
278 podman_compose_path(),
279 "-f",
280 compose_file_path,
281 "logs",
282 "--no-log-prefix",
283 "--no-color",
284 ])
285 # ZZVAR3 was set in .env file
286 self.assertEqual(output, b"ZZVAR3=TEST\n")
287 finally:
288 self.run_subprocess_assert_returncode([
289 podman_compose_path(),
290 "-f",
291 compose_file_path,
292 "down",
293 ])