Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure filestore is shared when ran in a distributed way #78

Merged
merged 3 commits into from
Oct 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions pytest_odoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import ast
import os
import signal
import subprocess
import threading
from contextlib import contextmanager
from unittest import mock
from pathlib import Path
from typing import Optional

Expand Down Expand Up @@ -110,6 +112,21 @@ def load_http(request):
odoo.service.server.start(stop=True)
signal.signal(signal.SIGINT, signal.default_int_handler)

@contextmanager
def _shared_filestore(original_db_name, db_name):
# This method ensure that if tests are ran in a distributed way
# we share the filestore between the original database and the
# copy of the database. This is useful to avoid copying the
# filestore for each worker.
# This is done by patching the filestore method of the odoo
# configuration to point to the original filestore.
if original_db_name == db_name:
yield
return
with mock.patch.object(odoo.tools.config, "filestore") as filestore:
fs_path = os.path.join(odoo.tools.config['data_dir'], 'filestore', original_db_name)
filestore.return_value = fs_path
yield

@contextmanager
def _worker_db_name():
Expand All @@ -122,15 +139,18 @@ def _worker_db_name():
try:
if xdist_worker:
db_name = f"{original_db_name}-{xdist_worker}"
os.system(f"dropdb {db_name} --if-exists")
os.system(f"createdb -T {original_db_name} {db_name}")
subprocess.run(["dropdb", db_name, "--if-exists"], check=True)
subprocess.run(["createdb", "-T", original_db_name, db_name], check=True)
odoo.tools.config["db_name"] = db_name
yield db_name
odoo.tools.config["dbfilter"] = f"^{db_name}$"
with _shared_filestore(original_db_name, db_name):
yield db_name
finally:
if db_name != original_db_name:
odoo.sql_db.close_db(db_name)
os.system(f"dropdb {db_name}")
subprocess.run(["dropdb", db_name, "--if-exists"], check=True)
odoo.tools.config["db_name"] = original_db_name
odoo.tools.config["dbfilter"] = f"^{original_db_name}$"


@pytest.fixture(scope='session', autouse=True)
Expand Down
Loading