Skip to content

Commit 00c8187

Browse files
authored
Merge pull request #88 from d9pouces/81-tox3-docker-cleanup
remove remaining Docker containers when tests are interrupted
2 parents ebcb738 + 284101a commit 00c8187

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

tox_docker/__init__.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from docker.errors import ImageNotFound
88
from docker.types import Mount
9-
from tox import hookimpl
9+
from tox import hookimpl, hookspecs
1010
from tox.config import SectionReader
1111
import docker as docker_module
1212
import py
@@ -364,6 +364,15 @@ def tox_runtest_post(venv):
364364
stop_containers(venv)
365365

366366

367+
def tox_cleanup(session): # noqa: F841
368+
for venv in session.existing_venvs.values():
369+
stop_containers(venv)
370+
371+
372+
if hasattr(hookspecs, "tox_cleanup"):
373+
tox_cleanup = hookimpl(tox_cleanup)
374+
375+
367376
def stop_containers(venv):
368377
envconfig = venv.envconfig
369378
if not envconfig.docker:
@@ -387,6 +396,7 @@ def stop_containers(venv):
387396
)
388397
with action:
389398
pass
399+
envconfig._docker_containers.clear()
390400

391401

392402
@hookimpl

tox_docker/tests/test_tox_cleanup.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
from tox import hookspecs
2+
from tox.action import Action
3+
4+
from tox_docker import tox_cleanup, tox_runtest_post
5+
6+
7+
class Session:
8+
def __init__(self, config):
9+
self.config = config
10+
self.existing_venvs = {}
11+
12+
13+
class Config:
14+
def __init__(self):
15+
self._docker_container_configs = {}
16+
17+
18+
class Container:
19+
def __init__(self, short_id, virtual_env):
20+
self.running = True
21+
self.short_id = short_id
22+
env_config = virtual_env.envconfig
23+
env_config._docker_containers[short_id] = self
24+
env_config.config._docker_container_configs[short_id] = {"stop": True}
25+
26+
# noinspection PyUnusedLocal
27+
def remove(self, v=False, force=False):
28+
if not self.running:
29+
raise ValueError("Container is not running")
30+
self.running = False
31+
32+
33+
class EnvConfig:
34+
def __init__(self, config):
35+
self._docker_containers = {}
36+
self.docker = True
37+
self.config = config
38+
39+
40+
class VEnv:
41+
def __init__(self, config):
42+
self.envconfig = EnvConfig(config)
43+
44+
@classmethod
45+
def new_action(cls, message):
46+
return Action(
47+
"docker", message, [], ".", False, False, None, None, 100, 100, 100
48+
)
49+
50+
51+
def test_tox_emergency_cleanup():
52+
"""check if the container is stopped even if tox_runtest_post has not been called"""
53+
config = Config()
54+
session = Session(config)
55+
virtual_env = VEnv(config)
56+
session.existing_venvs["test_venv"] = virtual_env
57+
container = Container("test_container", virtual_env)
58+
assert container.running
59+
tox_cleanup(session)
60+
if hasattr(hookspecs, "tox_cleanup"):
61+
assert not container.running
62+
else:
63+
assert container.running
64+
65+
66+
def test_tox_normal_cleanup():
67+
"""normal situation: tox_runtest_post has been called before tox_cleanup"""
68+
config = Config()
69+
session = Session(config)
70+
virtual_env = VEnv(config)
71+
session.existing_venvs["test_venv"] = virtual_env
72+
container = Container("test_container", virtual_env)
73+
assert container.running
74+
tox_runtest_post(virtual_env)
75+
assert not container.running
76+
tox_cleanup(session)
77+
assert not container.running

0 commit comments

Comments
 (0)