Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tardo committed Mar 26, 2024
1 parent 5a7b11c commit 2737b7c
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ jobs:
- run: pip install poetry
- name: Patch $PATH
run: echo "$HOME/.local/bin" >> $GITHUB_PATH
- run: poetry install --no-root
- run: poetry install
# Precreate shared networks to avoid race conditions
- run: docker network create inverseproxy_shared
- run: docker network create globalwhitelist_shared
Expand Down
2 changes: 1 addition & 1 deletion copier.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ _migrations:
- --collection=migrations
- remove-odoo-auto-folder
- version: v2.0.0a0
before:
after:
- - invoke
- --search-root={{ _copier_conf.src_path }}
- --collection=migrations
Expand Down
51 changes: 32 additions & 19 deletions tasks_downstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""
import json
import os
import re
import shutil
import stat
import tempfile
Expand Down Expand Up @@ -783,13 +784,13 @@ def test(
# Limit tests to explicit list
# Filter spec format (comma-separated)
# [-][tag][/module][:class][.method]
odoo_command.extend(["--test-tags", "/" + ",/".join(modules_list)])
odoo_command.extend(["--test-tags", f"/{',/'.join(modules_list)}"])
if debugpy:
_test_in_debug_mode(c, odoo_command)
else:
cmd = [DOCKER_COMPOSE_CMD, "run", "--rm"]
if db_filter:
cmd.extend(["-e", "DB_FILTER='%s'" % db_filter])
cmd.extend(["-e", f"DB_FILTER='{db_filter}'"])
cmd.append("odoo")
cmd.extend(odoo_command)
with c.cd(str(PROJECT_ROOT)):
Expand All @@ -805,7 +806,7 @@ def test(
)
def stop(c, purge=False):
"""Stop and (optionally) purge environment."""
cmd = DOCKER_COMPOSE_CMD + " down --remove-orphans"
cmd = f"{DOCKER_COMPOSE_CMD} down --remove-orphans"
if purge:
cmd += " --rmi local --volumes"
with c.cd(str(PROJECT_ROOT)):
Expand Down Expand Up @@ -849,8 +850,8 @@ def resetdb(
else:
modules = modules or "base"
with c.cd(str(PROJECT_ROOT)):
c.run(DOCKER_COMPOSE_CMD + " stop odoo", pty=True)
_run = DOCKER_COMPOSE_CMD + " run --rm -l traefik.enable=false odoo"
c.run(f"{DOCKER_COMPOSE_CMD} stop odoo", pty=True)
_run = f"{DOCKER_COMPOSE_CMD} run --rm -l traefik.enable=false odoo"
c.run(
f"{_run} click-odoo-dropdb {dbname}",
env=UID_ENV,
Expand All @@ -864,7 +865,7 @@ def resetdb(
)
if populate and ODOO_VERSION < 11:
_logger.warn(
"Skipping populate task as it is not available in v%s" % ODOO_VERSION
f"Skipping populate task as it is not available in v{ODOO_VERSION}"
)
populate = False
if populate:
Expand All @@ -883,7 +884,7 @@ def preparedb(c):
)
with c.cd(str(PROJECT_ROOT)):
c.run(
DOCKER_COMPOSE_CMD + " run --rm -l traefik.enable=false odoo preparedb",
f"{DOCKER_COMPOSE_CMD} run --rm -l traefik.enable=false odoo preparedb",
env=UID_ENV,
pty=True,
)
Expand All @@ -892,7 +893,7 @@ def preparedb(c):
@task()
def restart(c, quick=True):
"""Restart odoo container(s)."""
cmd = DOCKER_COMPOSE_CMD + " restart"
cmd = f"{DOCKER_COMPOSE_CMD} restart"
if quick:
cmd = f"{cmd} -t0"
cmd = f"{cmd} odoo odoo_proxy"
Expand All @@ -909,7 +910,7 @@ def restart(c, quick=True):
)
def logs(c, tail=10, follow=True, container=None):
"""Obtain last logs of current environment."""
cmd = DOCKER_COMPOSE_CMD + " logs"
cmd = f"{DOCKER_COMPOSE_CMD} logs"
if follow:
cmd += " -f"
if tail:
Expand Down Expand Up @@ -971,17 +972,23 @@ def snapshot(
if not destination_db:
destination_db = f"{source_db}-{datetime.now().strftime('%Y_%m_%d-%H_%M')}"
with c.cd(str(PROJECT_ROOT)):
cur_state = c.run(DOCKER_COMPOSE_CMD + " stop odoo db", pty=True).stdout
cur_state = c.run(f"{DOCKER_COMPOSE_CMD} stop odoo db", pty=True).stdout
_logger.info("Snapshoting current %s DB to %s", (source_db, destination_db))
_run = DOCKER_COMPOSE_CMD + " run --rm -l traefik.enable=false odoo"
_run = f"{DOCKER_COMPOSE_CMD} run --rm -l traefik.enable=false odoo"
c.run(
f"{_run} click-odoo-copydb {source_db} {destination_db}",
env=UID_ENV,
pty=True,
)
if "Stopping" in cur_state:
# Restart services if they were previously active
c.run(DOCKER_COMPOSE_CMD + " start odoo db", pty=True)
down_services = []
if re.match(r"odoo-\d+\s+Stopped", cur_state):
down_services.append("odoo")
if re.match(r"db-\d+\s+Stopped", cur_state):
down_services.append("db")
if len(down_services) != 0:
# Restart services if they were previously active
c.run(f"{DOCKER_COMPOSE_CMD} start {' '.join(down_services)}", pty=True)


@task(
Expand All @@ -1002,11 +1009,11 @@ def restore_snapshot(
Uses click-odoo-copydb behind the scenes to restore a DB snapshot.
"""
with c.cd(str(PROJECT_ROOT)):
cur_state = c.run(DOCKER_COMPOSE_CMD + " stop odoo db", pty=True).stdout
cur_state = c.run(f"{DOCKER_COMPOSE_CMD} stop odoo db", pty=True).stdout
if not snapshot_name:
# List DBs
res = c.run(
DOCKER_COMPOSE_CMD + " run --rm -e LOG_LEVEL=WARNING odoo psql -tc"
f"{DOCKER_COMPOSE_CMD} run --rm -e LOG_LEVEL=WARNING odoo psql -tc"
" 'SELECT datname FROM pg_database;'",
env=UID_ENV,
hide="stdout",
Expand All @@ -1019,7 +1026,7 @@ def restore_snapshot(
db_name = db.lstrip()
try:
db_date = datetime.strptime(
db_name.lstrip(destination_db + "-"), "%Y_%m_%d-%H_%M"
db_name.lstrip(f"{destination_db}-"), "%Y_%m_%d-%H_%M"
)
db_list.append((db_name, db_date))
except ValueError:
Expand All @@ -1030,7 +1037,7 @@ def restore_snapshot(
"No snapshot found for destination_db %s" % destination_db
)
_logger.info("Restoring snapshot %s to %s", (snapshot_name, destination_db))
_run = DOCKER_COMPOSE_CMD + " run --rm -l traefik.enable=false odoo"
_run = f"{DOCKER_COMPOSE_CMD} run --rm -l traefik.enable=false odoo"
c.run(
f"{_run} click-odoo-dropdb {destination_db}",
env=UID_ENV,
Expand All @@ -1043,5 +1050,11 @@ def restore_snapshot(
pty=True,
)
if "Stopping" in cur_state:
# Restart services if they were previously active
c.run(DOCKER_COMPOSE_CMD + " start odoo db", pty=True)
down_services = []
if re.match(r"odoo-\d+\s+Stopped", cur_state):
down_services.append("odoo")
if re.match(r"db-\d+\s+Stopped", cur_state):
down_services.append("db")
if len(down_services) != 0:
# Restart services if they were previously active
c.run(f"{DOCKER_COMPOSE_CMD} start {' '.join(down_services)}", pty=True)
16 changes: 10 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,13 +339,17 @@ def safe_stop_env(exec_path, purge=True):
invoke.run(args)
except ProcessExecutionError as e:
if (
"has active endpoints" not in e.stderr
and "has active endpoints" not in e.stdout
"is already in progress" not in e.stderr
and "is already in progress" not in e.stdout
):
raise e
assert not _containers_running(
exec_path
), "Containers running or not removed. 'stop [--purge]' command did not work."
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."


@contextmanager
Expand Down
4 changes: 1 addition & 3 deletions tests/test_tasks_downstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,7 @@ def test_resetdb(
invoke("restore-snapshot", "--snapshot-name", "db_with_sale")
assert _install_status("sale") == "installed"
finally:
safe_stop_env(
tmp_path / "odoo" / "custom" / "src" / "odoo",
)
safe_stop_env(tmp_path / "odoo" / "custom" / "src" / "odoo")


@pytest.mark.sequential
Expand Down

0 comments on commit 2737b7c

Please sign in to comment.