Skip to content

Commit 6b1a26e

Browse files
authored
After stack rm, waits until all services are down (#835)
* After stack rm, waits until all services are down asserts published ports are not -1 * Waits until container stops Cleanup and added comments
1 parent 7be9827 commit 6b1a26e

File tree

3 files changed

+51
-8
lines changed

3 files changed

+51
-8
lines changed

services/web/server/tests/integration/conftest.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,13 @@ def webserver_environ(request, devel_environ, services_docker_compose, docker_st
6262
for name in core_services:
6363
if 'ports' not in services_docker_compose['services'][name]:
6464
continue
65-
65+
6666
# published port is sometimes dynamically defined by the swarm
67+
published_port = get_service_published_port(name)
68+
assert published_port != "-1"
6769

6870
environ['%s_HOST' % name.upper()] = '127.0.0.1'
69-
environ['%s_PORT' % name.upper()] = get_service_published_port(name)
71+
environ['%s_PORT' % name.upper()] = published_port
7072
# to swarm boundary since webserver is installed in the host and therefore outside the swarm's network
7173
from pprint import pprint
7274
pprint(environ)
@@ -121,9 +123,9 @@ def get_service_published_port(service_name: str) -> str:
121123
if not services:
122124
return published_port
123125
service_endpoint = services[0].attrs["Endpoint"]
124-
126+
125127
if "Ports" not in service_endpoint or not service_endpoint["Ports"]:
126128
return published_port
127-
129+
128130
published_port = service_endpoint["Ports"][0]["PublishedPort"]
129-
return str(published_port)
131+
return str(published_port)

services/web/server/tests/integration/fixtures/docker_registry.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import docker
88
import pytest
99
import tenacity
10-
10+
import time
1111

1212
@pytest.fixture(scope="session")
1313
def docker_registry():
@@ -46,6 +46,9 @@ def docker_registry():
4646

4747
container.stop()
4848

49+
while docker_client.containers.list(filters={"name": container.name}):
50+
time.sleep(1)
51+
4952
@tenacity.retry(wait=tenacity.wait_fixed(1), stop=tenacity.stop_after_delay(60))
5053
def _wait_till_registry_is_responsive(url):
5154
docker_client = docker.from_env()

services/web/server/tests/integration/fixtures/docker_swarm.py

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# pylint:disable=redefined-outer-name
66

77
import subprocess
8+
import time
89
from pathlib import Path
910

1011
import docker
@@ -24,6 +25,7 @@ def docker_swarm(docker_client):
2425
# teardown
2526
assert docker_client.swarm.leave(force=True)
2627

28+
2729
@pytest.fixture(scope='module')
2830
def docker_stack(docker_swarm, docker_client, docker_compose_file: Path, tools_docker_compose_file: Path):
2931
docker_compose_ignore_file = docker_compose_file.parent / "docker-compose.ignore.yml"
@@ -44,10 +46,46 @@ def docker_stack(docker_swarm, docker_client, docker_compose_file: Path, tools_d
4446
)
4547
assert process.returncode == 0, "Error in '{}'".format(cmd)
4648

49+
4750
with docker_compose_ignore_file.open() as fp:
4851
docker_stack_cfg = yaml.safe_load(fp)
49-
yield docker_stack_cfg
5052

51-
# clean up
53+
def _print_services(msg):
54+
from pprint import pprint
55+
print("{:*^100}".format("docker services list " + msg))
56+
for service in docker_client.services.list():
57+
pprint(service.attrs)
58+
print("-"*100)
59+
60+
61+
_print_services("[BEFORE TEST]")
62+
63+
yield docker_stack_cfg
64+
65+
_print_services("[AFTER TEST]")
66+
67+
# clean up. Guarantees that all services are down before creating a new stack!
68+
#
69+
# WORKAROUND https://github.com/moby/moby/issues/30942#issue-207070098
70+
#
71+
# docker stack rm services
72+
73+
# until [ -z "$(docker service ls --filter label=com.docker.stack.namespace=services -q)" ] || [ "$limit" -lt 0 ]; do
74+
# sleep 1;
75+
# done
76+
77+
# until [ -z "$(docker network ls --filter label=com.docker.stack.namespace=services -q)" ] || [ "$limit" -lt 0 ]; do
78+
# sleep 1;
79+
# done
80+
5281
assert subprocess.run("docker stack rm services", shell=True).returncode == 0
82+
83+
while docker_client.services.list(filters={"label":"com.docker.stack.namespace=services"}):
84+
time.sleep(1)
85+
86+
while docker_client.networks.list(filters={"label":"com.docker.stack.namespace=services"}):
87+
time.sleep(1)
88+
5389
docker_compose_ignore_file.unlink()
90+
91+
_print_services("[AFTER REMOVED]")

0 commit comments

Comments
 (0)