Skip to content

Commit

Permalink
Add resetdb task
Browse files Browse the repository at this point in the history
- Test some other downstream tasks on the way.
- Fix permission problems detected with those tests.
- Fix race condition when running several docker tasks.
  • Loading branch information
Jairo Llopis authored and github-actions[bot] committed Jun 8, 2020
1 parent d1234a2 commit 4132ea2
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,5 @@ jobs:
# Run all tests
- run: invoke test --verbose
env:
QA_TEST: 1
DOCKER_TEST: 1
SELECTED_ODOO_VERSIONS: ${{ matrix.odoo-version }}
3 changes: 3 additions & 0 deletions devel.yaml.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ services:
args:
# To aggregate in development, use `setup-devel.yaml`
AGGREGATE: "false"
# Export these variables to own files created by odoo in your filesystem
UID: "${UID:-1000}"
GID: "${GID:-1000}"
# No need for this in development
PIP_INSTALL_ODOO: "false"
CLEAN: "false"
Expand Down
2 changes: 2 additions & 0 deletions setup-devel.yaml.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ services:
PIP_INSTALL_ODOO: "false"
CLEAN: "false"
COMPILE: "false"
UID: "${UID:-1000}"
GID: "${GID:-1000}"
networks:
- public
volumes:
Expand Down
31 changes: 29 additions & 2 deletions tasks_downstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def start(c, detach=True, ptvsd=False):
if detach:
cmd += " --detach"
with c.cd(str(PROJECT_ROOT)):
c.run(cmd, env={"DOODBA_PTVSD_ENABLE": str(int(ptvsd))})
c.run(cmd, env=dict(UID_ENV, DOODBA_PTVSD_ENABLE=str(int(ptvsd))))


@task(
Expand All @@ -164,6 +164,33 @@ def stop(c, purge=False):
c.run(cmd)


@task(
develop,
help={
"dbname": "The DB that will be DESTROYED and recreated. Default: 'devel'.",
"modules": "Comma-separated list of modules to install. Default: 'base'.",
},
)
def resetdb(c, modules="base", dbname="devel"):
"""Reset the specified database with the specified modules.
Uses click-odoo-initdb behind the scenes, which has a caching system that
makes DB resets quicker. See its docs for more info.
"""
c.run("docker-compose stop odoo", pty=True)
c.run(
f"docker-compose run --rm odoo click-odoo-dropdb {dbname}",
env=UID_ENV,
warn=True,
pty=True,
)
c.run(
f"docker-compose run --rm odoo click-odoo-initdb -n {dbname} -m {modules}",
env=UID_ENV,
pty=True,
)


@task(develop)
def restart(c, quick=True):
"""Restart odoo container(s)."""
Expand All @@ -172,7 +199,7 @@ def restart(c, quick=True):
cmd = f"{cmd} -t0"
cmd = f"{cmd} odoo odoo_proxy"
with c.cd(str(PROJECT_ROOT)):
c.run(cmd)
c.run(cmd, env=UID_ENV)


@task(develop)
Expand Down
5 changes: 3 additions & 2 deletions tests/test_doodba_qa.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pytest
from copier.main import copy
from plumbum import FG
from plumbum.cmd import invoke

try:
from plumbum.cmd import docker
Expand All @@ -13,7 +14,7 @@

@pytest.mark.skipif(docker is None, reason="Need docker CLI to test doodba-qa")
@pytest.mark.skipif(
os.environ.get("QA_TEST") != "1", reason="Missing QA_TEST=1 env variable"
os.environ.get("DOCKER_TEST") != "1", reason="Missing DOCKER_TEST=1 env variable"
)
def test_doodba_qa(tmp_path: Path, supported_odoo_version: float):
"""Test Doodba QA works fine with a scaffolding copy."""
Expand Down Expand Up @@ -49,4 +50,4 @@ def test_doodba_qa(tmp_path: Path, supported_odoo_version: float):
qa_run["coverage"] & FG
finally:
qa_run["shutdown"] & FG
docker["system", "prune", "--all", "--force", "--volumes"]
invoke["-r", tmp_path, "stop", "--purge"] & FG
82 changes: 82 additions & 0 deletions tests/test_tasks_downstream.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import os
from pathlib import Path

import pytest
from copier import copy
from plumbum import ProcessExecutionError, local
from plumbum.cmd import docker_compose, invoke


def _install_status(module, dbname="devel"):
return docker_compose(
"run",
"--rm",
"-e",
"LOG_LEVEL=WARNING",
"-e",
f"PGDATABASE={dbname}",
"odoo",
"psql",
"-tc",
f"select state from ir_module_module where name='{module}'",
).strip()


@pytest.mark.skipif(
os.environ.get("DOCKER_TEST") != "1", reason="Missing DOCKER_TEST=1 env variable"
)
def test_resetdb(tmp_path: Path, cloned_template: Path, supported_odoo_version: float):
"""Test the dropdb task.
On this test flow, other downsream tasks are also tested:
- img-build
- git-aggregate
- stop --purge
"""
with local.cwd(tmp_path):
copy(
src_path=str(cloned_template),
vcs_ref="HEAD",
force=True,
data={"odoo_version": supported_odoo_version},
)
try:
invoke("img-build")
invoke("git-aggregate")
# No ir_module_module table exists yet
with pytest.raises(ProcessExecutionError):
_install_status("base")
# This should install just "base" addon
stdout = invoke("resetdb")
assert "Creating database cache" in stdout
assert "from template devel" in stdout
assert _install_status("base") == "installed"
assert _install_status("purchase") == "uninstalled"
assert _install_status("sale") == "uninstalled"
# Install "purchase"
stdout = invoke("resetdb", "-m", "purchase")
assert "Creating database cache" in stdout
assert "from template devel" in stdout
assert _install_status("base") == "installed"
assert _install_status("purchase") == "installed"
assert _install_status("sale") == "uninstalled"
# Install "sale" in a separate database
stdout = invoke("resetdb", "-m", "sale", "-d", "sale_only")
assert "Creating database cache" in stdout
assert "from template sale_only" in stdout
assert _install_status("base") == "installed"
assert _install_status("purchase") == "installed"
assert _install_status("sale") == "uninstalled"
assert _install_status("base", "sale_only") == "installed"
assert _install_status("purchase", "sale_only") == "uninstalled"
assert _install_status("sale", "sale_only") == "installed"
# Install "sale" in main database
stdout = invoke("resetdb", "-m", "sale")
assert "Creating database devel from template cache" in stdout
assert "Found matching database template" in stdout
assert _install_status("base") == "installed"
assert _install_status("purchase") == "uninstalled"
assert _install_status("sale") == "installed"
finally:
invoke("stop", "--purge")

0 comments on commit 4132ea2

Please sign in to comment.