Separate use of database sessions

This patch separates the use of database session in different MBS components
and do not mix them together.

In general, MBS components could be separated as the REST API (implemented
based on Flask) and non-REST API including the backend build workflow
(implemented as a fedmsg consumer on top of fedmsg-hub and running
independently) and library shared by them. As a result, there are two kind of
database session used in MBS, one is created and managed by Flask-SQLAlchemy,
and another one is created from SQLAclhemy Session API directly. The goal of
this patch is to make ensure session object is used properly in the right
place.

All the changes follow these rules:

* REST API related code uses the session object db.session created and
  managed by Flask-SQLAlchemy.
* Non-REST API related code uses the session object created with SQLAlchemy
  Session API. Function make_db_session does that.
* Shared code does not created a new session object as much as possible.
  Instead, it accepts an argument db_session.

The first two rules are applicable to tests as well.

Major changes:

* Switch tests back to run with a file-based SQLite database.
* make_session is renamed to make_db_session and SQLAlchemy connection pool
  options are applied for PostgreSQL backend.
* Frontend Flask related code uses db.session
* Shared code by REST API and backend build workflow accepts SQLAlchemy session
  object as an argument. For example, resolver class is constructed with a
  database session, and some functions accepts an argument for database session.
* Build workflow related code use session object returned from make_db_session
  and ensure db.session is not used.
* Only tests for views use db.session, and other tests use db_session fixture
  to access database.
* All argument name session, that is for database access, are renamed to
  db_session.
* Functions model_tests_init_data, reuse_component_init_data and
  reuse_shared_userspace_init_data, which creates fixture data for
  tests, are converted into pytest fixtures from original function
  called inside setup_method or a test method. The reason of this
  conversion is to use fixture ``db_session`` rather than create a
  new one. That would also benefit the whole test suite to reduce the
  number of SQLAlchemy session objects.

Signed-off-by: Chenxiong Qi <cqi@redhat.com>
This commit is contained in:
Chenxiong Qi
2019-07-12 23:43:17 +08:00
parent 64698fbde8
commit 3878affa41
54 changed files with 2692 additions and 2454 deletions

View File

@@ -27,7 +27,7 @@ import module_build_service.scheduler.handlers.modules
import os
import koji
import pytest
from tests import conf, db, scheduler_init_data, read_staged_data
from tests import conf, scheduler_init_data, read_staged_data
import module_build_service.resolver
from module_build_service import build_logs, Modulemd
from module_build_service.utils.general import load_mmd
@@ -44,7 +44,8 @@ class TestModuleWait:
def teardown_method(self, test_method):
try:
path = build_logs.path(1)
with module_build_service.models.make_db_session(conf) as db_session:
path = build_logs.path(db_session, 1)
os.remove(path)
except Exception:
pass
@@ -79,8 +80,8 @@ class TestModuleWait:
msg = module_build_service.messaging.MBSModule(
msg_id=None, module_build_id=1, module_build_state="some state")
with patch.object(module_build_service.resolver, "system_resolver"):
self.fn(config=self.config, session=self.session, msg=msg)
with patch("module_build_service.resolver.GenericResolver.create"):
self.fn(config=self.config, db_session=self.session, msg=msg)
@patch(
"module_build_service.builder.GenericBuilder.default_buildroot_groups",
@@ -115,20 +116,19 @@ class TestModuleWait:
resolver.backend = "db"
resolver.get_module_tag.return_value = "module-testmodule-master-20170109091357"
with patch.object(module_build_service.resolver, "system_resolver", new=resolver):
msg = module_build_service.messaging.MBSModule(
msg_id=None, module_build_id=2, module_build_state="some state")
module_build_service.scheduler.handlers.modules.wait(
config=conf, session=db_session, msg=msg)
koji_session.newRepo.assert_called_once_with("module-123-build")
generic_resolver.create.return_value = resolver
msg = module_build_service.messaging.MBSModule(
msg_id=None, module_build_id=2, module_build_state="some state")
module_build_service.scheduler.handlers.modules.wait(
config=conf, db_session=db_session, msg=msg)
koji_session.newRepo.assert_called_once_with("module-123-build")
# When module-build-macros is reused, it still has to appear only
# once in database.
builds_count = (
db.session.query(ComponentBuild)
.filter_by(package="module-build-macros", module_id=2)
.count()
)
builds_count = db_session.query(ComponentBuild).filter_by(
package="module-build-macros", module_id=2).count()
assert builds_count == 1
@patch(
@@ -164,12 +164,14 @@ class TestModuleWait:
resolver.backend = "db"
resolver.get_module_tag.return_value = "module-testmodule-master-20170109091357"
with patch.object(module_build_service.resolver, "system_resolver", new=resolver):
msg = module_build_service.messaging.MBSModule(
msg_id=None, module_build_id=2, module_build_state="some state")
module_build_service.scheduler.handlers.modules.wait(
config=conf, session=db_session, msg=msg)
assert koji_session.newRepo.called
generic_resolver.create.return_value = resolver
msg = module_build_service.messaging.MBSModule(
msg_id=None, module_build_id=2, module_build_state="some state")
module_build_service.scheduler.handlers.modules.wait(
config=conf, db_session=db_session, msg=msg)
assert koji_session.newRepo.called
@patch(
"module_build_service.builder.GenericBuilder.default_buildroot_groups",
@@ -209,13 +211,15 @@ class TestModuleWait:
"module-bootstrap-tag": [base_mmd]
}
with patch.object(module_build_service.resolver, "system_resolver", new=resolver):
msg = module_build_service.messaging.MBSModule(
msg_id=None, module_build_id=2, module_build_state="some state")
module_build_service.scheduler.handlers.modules.wait(
config=conf, session=db_session, msg=msg)
module_build = ModuleBuild.query.filter_by(id=2).one()
assert module_build.cg_build_koji_tag == "modular-updates-candidate"
generic_resolver.create.return_value = resolver
msg = module_build_service.messaging.MBSModule(
msg_id=None, module_build_id=2, module_build_state="some state")
module_build_service.scheduler.handlers.modules.wait(
config=conf, db_session=db_session, msg=msg)
module_build = ModuleBuild.get_by_id(db_session, 2)
assert module_build.cg_build_koji_tag == "modular-updates-candidate"
@pytest.mark.parametrize(
"koji_cg_tag_build,expected_cg_koji_build_tag",
@@ -280,12 +284,12 @@ class TestModuleWait:
"koji_cg_tag_build",
new=koji_cg_tag_build,
):
with patch.object(module_build_service.resolver, "system_resolver", new=resolver):
msg = module_build_service.messaging.MBSModule(
msg_id=None, module_build_id=2, module_build_state="some state"
)
module_build_service.scheduler.handlers.modules.wait(
config=conf, session=db_session, msg=msg
)
module_build = ModuleBuild.query.filter_by(id=2).one()
assert module_build.cg_build_koji_tag == expected_cg_koji_build_tag
generic_resolver.create.return_value = resolver
msg = module_build_service.messaging.MBSModule(
msg_id=None, module_build_id=2, module_build_state="some state"
)
module_build_service.scheduler.handlers.modules.wait(
config=conf, db_session=db_session, msg=msg
)
module_build = ModuleBuild.get_by_id(db_session, 2)
assert module_build.cg_build_koji_tag == expected_cg_koji_build_tag