Skip to content

Commit f602202

Browse files
authored
Merge pull request #78 from acsone/master-fix-distributed-filestore
Ensure filestore is shared when ran in a distributed way
2 parents 6e925f3 + f9bd2d8 commit f602202

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

pytest_odoo.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
import ast
88
import os
99
import signal
10+
import subprocess
1011
import threading
1112
from contextlib import contextmanager
13+
from unittest import mock
1214
from pathlib import Path
1315
from typing import Optional
1416

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

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

114131
@contextmanager
115132
def _worker_db_name():
@@ -122,15 +139,18 @@ def _worker_db_name():
122139
try:
123140
if xdist_worker:
124141
db_name = f"{original_db_name}-{xdist_worker}"
125-
os.system(f"dropdb {db_name} --if-exists")
126-
os.system(f"createdb -T {original_db_name} {db_name}")
142+
subprocess.run(["dropdb", db_name, "--if-exists"], check=True)
143+
subprocess.run(["createdb", "-T", original_db_name, db_name], check=True)
127144
odoo.tools.config["db_name"] = db_name
128-
yield db_name
145+
odoo.tools.config["dbfilter"] = f"^{db_name}$"
146+
with _shared_filestore(original_db_name, db_name):
147+
yield db_name
129148
finally:
130149
if db_name != original_db_name:
131150
odoo.sql_db.close_db(db_name)
132-
os.system(f"dropdb {db_name}")
151+
subprocess.run(["dropdb", db_name, "--if-exists"], check=True)
133152
odoo.tools.config["db_name"] = original_db_name
153+
odoo.tools.config["dbfilter"] = f"^{original_db_name}$"
134154

135155

136156
@pytest.fixture(scope='session', autouse=True)

0 commit comments

Comments
 (0)