Skip to content

Commit 312d321

Browse files
committed
Adding integration tests for service level change detect
Signed-off-by: Songmin Li <[email protected]>
1 parent c3d62c5 commit 312d321

File tree

5 files changed

+175
-0
lines changed

5 files changed

+175
-0
lines changed

tests/integration/compose_up_behavior/__init__.py

Whitespace-only changes.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
services:
2+
app:
3+
image: nopush/podman-compose-test
4+
command: ["dumb-init", "/bin/busybox", "httpd", "-f", "-p", "8080"]
5+
environment:
6+
- SERVICE_CHANGE=true
7+
depends_on:
8+
- db
9+
db:
10+
image: nopush/podman-compose-test
11+
command: ["dumb-init", "/bin/busybox", "httpd", "-f", "-p", "8080"]
12+
no_deps:
13+
image: nopush/podman-compose-test
14+
command: ["dumb-init", "/bin/busybox", "httpd", "-f", "-p", "8080"]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
services:
2+
app:
3+
image: nopush/podman-compose-test
4+
command: ["dumb-init", "/bin/busybox", "httpd", "-f", "-p", "8080"]
5+
depends_on:
6+
- db
7+
db:
8+
image: nopush/podman-compose-test
9+
command: ["dumb-init", "/bin/busybox", "httpd", "-f", "-p", "8080"]
10+
no_deps:
11+
image: nopush/podman-compose-test
12+
command: ["dumb-init", "/bin/busybox", "httpd", "-f", "-p", "8080"]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
services:
2+
app:
3+
image: nopush/podman-compose-test
4+
command: ["dumb-init", "/bin/busybox", "httpd", "-f", "-p", "8080"]
5+
depends_on:
6+
- db
7+
db:
8+
image: nopush/podman-compose-test
9+
command: ["dumb-init", "/bin/busybox", "httpd", "-f", "-p", "8080"]
10+
environment:
11+
- SERVICE_CHANGE=true
12+
no_deps:
13+
image: nopush/podman-compose-test
14+
command: ["dumb-init", "/bin/busybox", "httpd", "-f", "-p", "8080"]
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
3+
import json
4+
import os
5+
import unittest
6+
from typing import Any
7+
8+
from parameterized import parameterized
9+
10+
from tests.integration.test_utils import RunSubprocessMixin
11+
from tests.integration.test_utils import podman_compose_path
12+
from tests.integration.test_utils import test_path
13+
14+
15+
def compose_yaml_path(scenario: str) -> str:
16+
return os.path.join(
17+
os.path.join(test_path(), "compose_up_behavior"), f"docker-compose_{scenario}.yaml"
18+
)
19+
20+
21+
class TestComposeDownBehavior(unittest.TestCase, RunSubprocessMixin):
22+
def get_existing_containers(self, scenario: str) -> dict[str, Any]:
23+
out, _ = self.run_subprocess_assert_returncode(
24+
[
25+
podman_compose_path(),
26+
"-f",
27+
compose_yaml_path(scenario),
28+
"ps",
29+
"--format",
30+
'json',
31+
],
32+
)
33+
containers = json.loads(out)
34+
return {
35+
c.get("Names")[0]: {
36+
"name": c.get("Names")[0],
37+
"id": c.get("Id"),
38+
"service_name": c.get("Labels", {}).get("io.podman.compose.service", ""),
39+
"config_hash": c.get("Labels", {}).get("io.podman.compose.config-hash", ""),
40+
"exited": c.get("Exited"),
41+
}
42+
for c in containers
43+
}
44+
45+
@parameterized.expand([
46+
(
47+
"service_change_app",
48+
"service_change_base",
49+
["up"],
50+
{"app"},
51+
),
52+
(
53+
"service_change_app",
54+
"service_change_base",
55+
["up", "app"],
56+
{"app"},
57+
),
58+
(
59+
"service_change_app",
60+
"service_change_base",
61+
["up", "db"],
62+
set(),
63+
),
64+
(
65+
"service_change_db",
66+
"service_change_base",
67+
["up"],
68+
{"db", "app"},
69+
),
70+
(
71+
"service_change_db",
72+
"service_change_base",
73+
["up", "app"],
74+
{"db", "app"},
75+
),
76+
(
77+
"service_change_db",
78+
"service_change_base",
79+
["up", "db"],
80+
{"db", "app"},
81+
),
82+
])
83+
def test_recreate_on_config_changed(
84+
self,
85+
change_to: str,
86+
running_scenario: str,
87+
command_args: list[str],
88+
expect_recreated_services: set[str],
89+
) -> None:
90+
try:
91+
self.run_subprocess_assert_returncode(
92+
[podman_compose_path(), "-f", compose_yaml_path(running_scenario), "up", "-d"],
93+
)
94+
95+
original_containers = self.get_existing_containers(running_scenario)
96+
97+
out, err = self.run_subprocess_assert_returncode(
98+
[
99+
podman_compose_path(),
100+
"--verbose",
101+
"-f",
102+
compose_yaml_path(change_to),
103+
*command_args,
104+
"-d",
105+
],
106+
)
107+
108+
new_containers = self.get_existing_containers(change_to)
109+
recreated_services = {
110+
c.get("service_name")
111+
for c in original_containers.values()
112+
if new_containers.get(c.get("name"), {}).get("id") != c.get("id")
113+
}
114+
115+
self.assertEqual(
116+
recreated_services,
117+
expect_recreated_services,
118+
msg=f"Expected services to be recreated: {expect_recreated_services}, "
119+
f"but got: {recreated_services}, containers: "
120+
f"[{original_containers}, {new_containers}]",
121+
)
122+
self.assertTrue(
123+
all([c.get("exited") is False for c in new_containers.values()]),
124+
msg="Not all containers are running after up command",
125+
)
126+
127+
finally:
128+
self.run_subprocess_assert_returncode([
129+
podman_compose_path(),
130+
"-f",
131+
compose_yaml_path(change_to),
132+
"down",
133+
"-t",
134+
"0",
135+
])

0 commit comments

Comments
 (0)