|
| 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