| 1 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 49 |
|
| 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 |
|
| 68 |
|
| 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 |
|
| 83 |
|
| 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 |
|
| 102 |
|
| 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 |
|
| 117 |
|
| 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 |
|
| 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 |
|
| 152 |
|
| 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 |
|
| 173 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 221 |
|
| 222 |
|
| 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 |
|
| 229 |
|
| 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 |
|
| 239 |
|
| 240 |
|
| 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 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 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 |
|
| 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 |
]) |