Skip to content

Commit

Permalink
Fix tests to handle deadlocks with docker networks
Browse files Browse the repository at this point in the history
Avoid errors like https://github.com/Tecnativa/doodba-copier-template/runs/1917653981?check_suite_focus=true#step:13:214

See moby/moby#17217

Avoiding false errors where the containers where shutdown correctly but the command returned an error
  • Loading branch information
joao-p-marques authored and yajo committed Mar 8, 2021
1 parent 3aac8fd commit 34ffc06
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
31 changes: 29 additions & 2 deletions tests/conftest.py
@@ -1,4 +1,5 @@
import json
import logging
import os
import socket
import textwrap
Expand All @@ -8,10 +9,13 @@
import pytest
import yaml
from packaging import version
from plumbum import FG, local
from plumbum.cmd import git
from plumbum import FG, ProcessExecutionError, local
from plumbum.cmd import docker_compose, git, invoke
from plumbum.machines.local import LocalCommand

_logger = logging.getLogger(__name__)


with open("copier.yml") as copier_fd:
COPIER_SETTINGS = yaml.safe_load(copier_fd)

Expand Down Expand Up @@ -262,3 +266,26 @@ def some_method(self, test):
}
)
build_file_tree(file_tree)


def _containers_running(exec_path):
with local.cwd(exec_path):
if len(docker_compose("ps", "-aq").splitlines()) > 0:
_logger.error(docker_compose("ps", "-a"))
return True
return False


def safe_stop_env(exec_path):
with local.cwd(exec_path):
try:
invoke("stop", "--purge")
except ProcessExecutionError as e:
if (
"has active endpoints" not in e.stderr
and "has active endpoints" not in e.stdout
):
raise e
assert not _containers_running(
exec_path
), "Containers running or not removed. 'stop --purge' command did not work."
24 changes: 14 additions & 10 deletions tests/test_tasks_downstream.py
@@ -1,3 +1,4 @@
import logging
import re
import time
from pathlib import Path
Expand All @@ -8,7 +9,9 @@
from plumbum.cmd import docker_compose, invoke
from plumbum.machines.local import LocalCommand

from .conftest import socket_is_open
from .conftest import safe_stop_env, socket_is_open

_logger = logging.getLogger(__name__)


def _install_status(module, dbname="devel"):
Expand Down Expand Up @@ -52,6 +55,7 @@ def _tests_ran(stdout, odoo_version, addon_name):
assert not re.search(fr"{main_pkg}\.addons\.base\.tests\.\w+{suffix}", stdout)


@pytest.mark.sequential
def test_resetdb(
cloned_template: Path,
docker: LocalCommand,
Expand Down Expand Up @@ -115,9 +119,9 @@ def test_resetdb(
assert _install_status("purchase") == "uninstalled"
assert _install_status("sale") == "installed"
finally:
# Imagine the user is in the odoo subrepo for this command
with local.cwd(tmp_path / "odoo" / "custom" / "src" / "odoo"):
invoke("stop", "--purge")
safe_stop_env(
tmp_path / "odoo" / "custom" / "src" / "odoo",
)


@pytest.mark.sequential
Expand Down Expand Up @@ -160,9 +164,9 @@ def test_start(
container_logs = docker_compose("logs", "odoo")
assert "dev=reload" not in container_logs
finally:
# Imagine the user is in the odoo subrepo for this command
with local.cwd(tmp_path / "odoo" / "custom" / "src" / "odoo"):
invoke("stop", "--purge")
safe_stop_env(
tmp_path,
)


@pytest.mark.sequential
Expand Down Expand Up @@ -231,6 +235,6 @@ def test_install_test(
stdout = _wait_for_test_to_start()
assert "python -m debugpy" in stdout
finally:
# Imagine the user is in the odoo subrepo for this command
with local.cwd(tmp_path / "odoo" / "custom" / "src" / "odoo"):
invoke("stop", "--purge")
safe_stop_env(
tmp_path,
)

0 comments on commit 34ffc06

Please sign in to comment.