| 1 |
|
| 2 |
|
| 3 |
""" |
| 4 |
test_podman_compose_up_down.py |
| 5 |
|
| 6 |
Tests the podman compose up and down commands used to create and remove services. |
| 7 |
""" |
| 8 |
|
| 9 |
|
| 10 |
import os |
| 11 |
import unittest |
| 12 |
from typing import List |
| 13 |
|
| 14 |
from parameterized import parameterized |
| 15 |
|
| 16 |
from tests.integration.test_utils import RunSubprocessMixin |
| 17 |
from tests.integration.test_utils import podman_compose_path |
| 18 |
from tests.integration.test_utils import test_path |
| 19 |
|
| 20 |
|
| 21 |
def profile_compose_file() -> str: |
| 22 |
""" "Returns the path to the `profile` compose file used for this test module""" |
| 23 |
return os.path.join(test_path(), "profile", "docker-compose.yml") |
| 24 |
|
| 25 |
|
| 26 |
class TestUpDown(unittest.TestCase, RunSubprocessMixin): |
| 27 |
def tearDown(self) -> None: |
| 28 |
""" |
| 29 |
Ensures that the services within the "profile compose file" are removed between each test |
| 30 |
case. |
| 31 |
""" |
| 32 |
|
| 33 |
|
| 34 |
down_cmd = [ |
| 35 |
"coverage", |
| 36 |
"run", |
| 37 |
podman_compose_path(), |
| 38 |
"--profile", |
| 39 |
"profile-1", |
| 40 |
"--profile", |
| 41 |
"profile-2", |
| 42 |
"-f", |
| 43 |
profile_compose_file(), |
| 44 |
"down", |
| 45 |
] |
| 46 |
self.run_subprocess(down_cmd) |
| 47 |
|
| 48 |
@parameterized.expand( |
| 49 |
[ |
| 50 |
( |
| 51 |
["--profile", "profile-1", "up", "-d"], |
| 52 |
{"default-service": True, "service-1": True, "service-2": False}, |
| 53 |
), |
| 54 |
( |
| 55 |
["--profile", "profile-2", "up", "-d"], |
| 56 |
{"default-service": True, "service-1": False, "service-2": True}, |
| 57 |
), |
| 58 |
( |
| 59 |
["--profile", "profile-1", "--profile", "profile-2", "up", "-d"], |
| 60 |
{"default-service": True, "service-1": True, "service-2": True}, |
| 61 |
), |
| 62 |
], |
| 63 |
) |
| 64 |
def test_up(self, profiles: List[str], expected_services: dict) -> None: |
| 65 |
up_cmd = [ |
| 66 |
"coverage", |
| 67 |
"run", |
| 68 |
podman_compose_path(), |
| 69 |
"-f", |
| 70 |
profile_compose_file(), |
| 71 |
] |
| 72 |
up_cmd.extend(profiles) |
| 73 |
|
| 74 |
self.run_subprocess_assert_returncode(up_cmd) |
| 75 |
|
| 76 |
check_cmd = [ |
| 77 |
"podman", |
| 78 |
"container", |
| 79 |
"ps", |
| 80 |
"--format", |
| 81 |
'"{{.Names}}"', |
| 82 |
] |
| 83 |
out, _ = self.run_subprocess_assert_returncode(check_cmd) |
| 84 |
|
| 85 |
self.assertEqual(len(expected_services), 3) |
| 86 |
actual_output = out.decode("utf-8") |
| 87 |
|
| 88 |
actual_services = {} |
| 89 |
for service, _ in expected_services.items(): |
| 90 |
actual_services[service] = service in actual_output |
| 91 |
|
| 92 |
self.assertEqual(expected_services, actual_services) |
| 93 |
|
| 94 |
@parameterized.expand( |
| 95 |
[ |
| 96 |
( |
| 97 |
["--profile", "profile-1"], |
| 98 |
"profile-2", |
| 99 |
{"default-service": True, "service-1": True, "service-2": True}, |
| 100 |
), |
| 101 |
( |
| 102 |
[], |
| 103 |
"profile-1,profile-2", |
| 104 |
{"default-service": True, "service-1": True, "service-2": True}, |
| 105 |
), |
| 106 |
( |
| 107 |
[], |
| 108 |
"profile-1, profile-2", |
| 109 |
{"default-service": True, "service-1": True, "service-2": True}, |
| 110 |
), |
| 111 |
( |
| 112 |
[], |
| 113 |
"", |
| 114 |
{"default-service": True, "service-1": False, "service-2": False}, |
| 115 |
), |
| 116 |
( |
| 117 |
[], |
| 118 |
",", |
| 119 |
{"default-service": True, "service-1": False, "service-2": False}, |
| 120 |
), |
| 121 |
], |
| 122 |
) |
| 123 |
def test_up_with_compose_profiles_env( |
| 124 |
self, profiles: List[str], compose_profiles: str, expected_services: dict |
| 125 |
) -> None: |
| 126 |
""" |
| 127 |
Tests the `up` command when the `COMPOSE_PROFILES` environment variable is set. |
| 128 |
""" |
| 129 |
up_cmd = [ |
| 130 |
"coverage", |
| 131 |
"run", |
| 132 |
podman_compose_path(), |
| 133 |
"-f", |
| 134 |
profile_compose_file(), |
| 135 |
] |
| 136 |
up_cmd.extend(profiles) |
| 137 |
up_cmd.extend(["up", "-d"]) |
| 138 |
|
| 139 |
env = os.environ.copy() |
| 140 |
env["COMPOSE_PROFILES"] = compose_profiles |
| 141 |
|
| 142 |
self.run_subprocess_assert_returncode(up_cmd, env=env) |
| 143 |
|
| 144 |
check_cmd = [ |
| 145 |
"podman", |
| 146 |
"container", |
| 147 |
"ps", |
| 148 |
"--format", |
| 149 |
'"{{.Names}}"', |
| 150 |
] |
| 151 |
out, _ = self.run_subprocess_assert_returncode(check_cmd) |
| 152 |
|
| 153 |
self.assertEqual(len(expected_services), 3) |
| 154 |
actual_output = out.decode("utf-8") |
| 155 |
|
| 156 |
actual_services = {} |
| 157 |
for service, _ in expected_services.items(): |
| 158 |
actual_services[service] = service in actual_output |
| 159 |
|
| 160 |
self.assertEqual(expected_services, actual_services) |