| 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_yaml_path() -> str: |
| 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) -> None: |
| 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 |
]) |
| 39 |
|
| 40 |
""" |
| 41 |
Tests interpolation of COMPOSE_PROJECT_NAME in the podman-compose config, |
| 42 |
which is different from external environment variables because COMPOSE_PROJECT_NAME |
| 43 |
is a predefined environment variable generated from the `name` value in the top-level |
| 44 |
of the compose.yaml. |
| 45 |
|
| 46 |
See also |
| 47 |
- https://docs.docker.com/reference/compose-file/interpolation/ |
| 48 |
- https://docs.docker.com/reference/compose-file/version-and-name/#name-top-level-element |
| 49 |
- https://docs.docker.com/compose/how-tos/environment-variables/envvars/ |
| 50 |
- https://github.com/compose-spec/compose-spec/blob/main/04-version-and-name.md |
| 51 |
""" |
| 52 |
|
| 53 |
def test_project_name(self) -> None: |
| 54 |
try: |
| 55 |
output, _ = self.run_subprocess_assert_returncode([ |
| 56 |
podman_compose_path(), |
| 57 |
"-f", |
| 58 |
compose_yaml_path(), |
| 59 |
"run", |
| 60 |
"project-name-test", |
| 61 |
]) |
| 62 |
self.assertIn("my-project-name", str(output)) |
| 63 |
finally: |
| 64 |
self.run_subprocess_assert_returncode([ |
| 65 |
podman_compose_path(), |
| 66 |
"-f", |
| 67 |
compose_yaml_path(), |
| 68 |
"down", |
| 69 |
]) |
| 70 |
|
| 71 |
def test_project_name_override(self) -> None: |
| 72 |
try: |
| 73 |
output, _ = self.run_subprocess_assert_returncode([ |
| 74 |
podman_compose_path(), |
| 75 |
"-f", |
| 76 |
compose_yaml_path(), |
| 77 |
"run", |
| 78 |
"-e", |
| 79 |
"COMPOSE_PROJECT_NAME=project-name-override", |
| 80 |
"project-name-test", |
| 81 |
]) |
| 82 |
self.assertIn("project-name-override", str(output)) |
| 83 |
finally: |
| 84 |
self.run_subprocess_assert_returncode([ |
| 85 |
podman_compose_path(), |
| 86 |
"-f", |
| 87 |
compose_yaml_path(), |
| 88 |
"down", |
| 89 |
]) |
| 90 |
|
| 91 |
def test_project_name_interpolation(self) -> None: |
| 92 |
try: |
| 93 |
output, _ = self.run_subprocess_assert_returncode([ |
| 94 |
podman_compose_path(), |
| 95 |
"-f", |
| 96 |
compose_yaml_path(), |
| 97 |
"run", |
| 98 |
"-e", |
| 99 |
"ZZVAR1=-myval2", |
| 100 |
"project-name-test", |
| 101 |
]) |
| 102 |
self.assertIn("my-project-name-myval2", str(output)) |
| 103 |
finally: |
| 104 |
self.run_subprocess_assert_returncode([ |
| 105 |
podman_compose_path(), |
| 106 |
"-f", |
| 107 |
compose_yaml_path(), |
| 108 |
"down", |
| 109 |
]) |