mirror of
https://pagure.io/fm-orchestrator.git
synced 2026-11-06 07:09:21 +08:00
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:
@@ -19,47 +19,3 @@
|
||||
# SOFTWARE.
|
||||
#
|
||||
# Written by Ralph Bean <rbean@redhat.com>
|
||||
|
||||
import os
|
||||
from datetime import datetime
|
||||
|
||||
import module_build_service
|
||||
from module_build_service.utils import load_mmd
|
||||
|
||||
from tests import db, clean_database
|
||||
from module_build_service.config import init_config
|
||||
from module_build_service.models import ModuleBuild, BUILD_STATES
|
||||
|
||||
app = module_build_service.app
|
||||
|
||||
conf = init_config(app)
|
||||
|
||||
datadir = os.path.dirname(__file__) + "/data/"
|
||||
|
||||
|
||||
def module_build_from_modulemd(yaml):
|
||||
mmd = load_mmd(yaml)
|
||||
build = ModuleBuild()
|
||||
build.name = mmd.get_module_name()
|
||||
build.stream = mmd.get_stream_name()
|
||||
build.version = mmd.get_version()
|
||||
build.state = BUILD_STATES["ready"]
|
||||
build.modulemd = yaml
|
||||
build.koji_tag = None
|
||||
build.batch = 0
|
||||
build.owner = "some_other_user"
|
||||
build.time_submitted = datetime(2016, 9, 3, 12, 28, 33)
|
||||
build.time_modified = datetime(2016, 9, 3, 12, 28, 40)
|
||||
build.time_completed = None
|
||||
build.rebuild_strategy = "changed-and-after"
|
||||
return build
|
||||
|
||||
|
||||
def init_data():
|
||||
clean_database()
|
||||
for filename in os.listdir(datadir):
|
||||
with open(datadir + filename, "r") as f:
|
||||
yaml = f.read()
|
||||
build = module_build_from_modulemd(yaml)
|
||||
db.session.add(build)
|
||||
db.session.commit()
|
||||
|
||||
@@ -24,47 +24,48 @@ import pytest
|
||||
|
||||
from mock import patch
|
||||
from module_build_service import conf
|
||||
from module_build_service.models import ComponentBuild, ModuleBuild, make_session
|
||||
from module_build_service.models import ComponentBuild, ComponentBuildTrace, ModuleBuild
|
||||
from module_build_service.utils.general import mmd_to_str, load_mmd
|
||||
from tests import init_data as init_data_contexts, clean_database, make_module, read_staged_data
|
||||
from tests.test_models import init_data, module_build_from_modulemd
|
||||
from tests import module_build_from_modulemd
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("model_tests_init_data")
|
||||
class TestModels:
|
||||
def setup_method(self, test_method):
|
||||
init_data()
|
||||
|
||||
def test_app_sqlalchemy_events(self):
|
||||
with make_session(conf) as session:
|
||||
component_build = ComponentBuild()
|
||||
component_build.package = "before_models_committed"
|
||||
component_build.scmurl = (
|
||||
"git://pkgs.domain.local/rpms/before_models_committed?"
|
||||
"#9999999999999999999999999999999999999999"
|
||||
)
|
||||
component_build.format = "rpms"
|
||||
component_build.task_id = 999999999
|
||||
component_build.state = 1
|
||||
component_build.nvr = \
|
||||
"before_models_committed-0.0.0-0.module_before_models_committed_0_0"
|
||||
component_build.batch = 1
|
||||
component_build.module_id = 1
|
||||
def test_app_sqlalchemy_events(self, db_session):
|
||||
component_build = ComponentBuild(
|
||||
package="before_models_committed",
|
||||
scmurl="git://pkgs.domain.local/rpms/before_models_committed?"
|
||||
"#9999999999999999999999999999999999999999",
|
||||
format="rpms",
|
||||
task_id=999999999,
|
||||
state=1,
|
||||
nvr="before_models_committed-0.0.0-0.module_before_models_committed_0_0",
|
||||
batch=1,
|
||||
module_id=1,
|
||||
)
|
||||
|
||||
session.add(component_build)
|
||||
session.commit()
|
||||
db_session.add(component_build)
|
||||
db_session.commit()
|
||||
|
||||
with make_session(conf) as session:
|
||||
c = session.query(ComponentBuild).filter(ComponentBuild.id == 1).one()
|
||||
assert c.component_builds_trace[0].id == 1
|
||||
assert c.component_builds_trace[0].component_id == 1
|
||||
assert c.component_builds_trace[0].state == 1
|
||||
assert c.component_builds_trace[0].state_reason is None
|
||||
assert c.component_builds_trace[0].task_id == 999999999
|
||||
component_builds_trace = db_session.query(ComponentBuildTrace).filter(
|
||||
ComponentBuildTrace.component_id == component_build.id).one()
|
||||
db_session.commit()
|
||||
|
||||
def test_context_functions(self):
|
||||
assert component_builds_trace.id == 1
|
||||
assert component_builds_trace.component_id == 1
|
||||
assert component_builds_trace.state == 1
|
||||
assert component_builds_trace.state_reason is None
|
||||
assert component_builds_trace.task_id == 999999999
|
||||
|
||||
def test_context_functions(self, db_session):
|
||||
""" Test that the build_context, runtime_context, and context hashes are correctly
|
||||
determined"""
|
||||
build = ModuleBuild.query.filter_by(id=1).one()
|
||||
db_session.commit()
|
||||
|
||||
build = ModuleBuild.get_by_id(db_session, 1)
|
||||
db_session.commit()
|
||||
build.modulemd = read_staged_data("testmodule_dependencies")
|
||||
(
|
||||
build.ref_build_context,
|
||||
@@ -89,8 +90,12 @@ class TestModels:
|
||||
build.runtime_context = "bbc84c7b817ab3dd54916c0bcd6c6bdf512f7f9c" + str(i)
|
||||
db_session.add(build)
|
||||
db_session.commit()
|
||||
|
||||
build_one = ModuleBuild.get_by_id(db_session, 2)
|
||||
assert build_one.siblings == [3, 4]
|
||||
sibling_ids = build_one.siblings(db_session)
|
||||
db_session.commit()
|
||||
|
||||
assert sibling_ids == [3, 4]
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"stream,right_pad,expected",
|
||||
@@ -111,100 +116,100 @@ class TestModels:
|
||||
|
||||
|
||||
class TestModelsGetStreamsContexts:
|
||||
def test_get_last_build_in_all_streams(self):
|
||||
def test_get_last_build_in_all_streams(self, db_session):
|
||||
init_data_contexts(contexts=True)
|
||||
with make_session(conf) as session:
|
||||
builds = ModuleBuild.get_last_build_in_all_streams(session, "nginx")
|
||||
builds = sorted([
|
||||
"%s:%s:%s" % (build.name, build.stream, str(build.version)) for build in builds
|
||||
])
|
||||
assert builds == ["nginx:%d:%d" % (i, i + 2) for i in range(10)]
|
||||
builds = ModuleBuild.get_last_build_in_all_streams(db_session, "nginx")
|
||||
builds = sorted([
|
||||
"%s:%s:%s" % (build.name, build.stream, str(build.version)) for build in builds
|
||||
])
|
||||
db_session.commit()
|
||||
assert builds == ["nginx:%d:%d" % (i, i + 2) for i in range(10)]
|
||||
|
||||
def test_get_last_build_in_all_stream_last_version(self):
|
||||
def test_get_last_build_in_all_stream_last_version(self, db_session):
|
||||
init_data_contexts(contexts=False)
|
||||
with make_session(conf) as session:
|
||||
builds = ModuleBuild.get_last_build_in_all_streams(session, "nginx")
|
||||
builds = [
|
||||
"%s:%s:%s" % (build.name, build.stream, str(build.version)) for build in builds
|
||||
]
|
||||
assert builds == ["nginx:1:11"]
|
||||
builds = ModuleBuild.get_last_build_in_all_streams(db_session, "nginx")
|
||||
builds = [
|
||||
"%s:%s:%s" % (build.name, build.stream, str(build.version)) for build in builds
|
||||
]
|
||||
db_session.commit()
|
||||
assert builds == ["nginx:1:11"]
|
||||
|
||||
def test_get_last_builds_in_stream(self):
|
||||
def test_get_last_builds_in_stream(self, db_session):
|
||||
init_data_contexts(contexts=True)
|
||||
with make_session(conf) as session:
|
||||
builds = ModuleBuild.get_last_builds_in_stream(session, "nginx", "1")
|
||||
builds = [
|
||||
"%s:%s:%s:%s" % (build.name, build.stream, str(build.version), build.context)
|
||||
for build in builds
|
||||
]
|
||||
assert builds == ["nginx:1:3:d5a6c0fa", "nginx:1:3:795e97c1"]
|
||||
builds = ModuleBuild.get_last_builds_in_stream(db_session, "nginx", "1")
|
||||
builds = [
|
||||
"%s:%s:%s:%s" % (build.name, build.stream, str(build.version), build.context)
|
||||
for build in builds
|
||||
]
|
||||
db_session.commit()
|
||||
assert builds == ["nginx:1:3:d5a6c0fa", "nginx:1:3:795e97c1"]
|
||||
|
||||
def test_get_last_builds_in_stream_version_lte(self):
|
||||
def test_get_last_builds_in_stream_version_lte(self, db_session):
|
||||
init_data_contexts(1, multiple_stream_versions=True)
|
||||
with make_session(conf) as session:
|
||||
builds = ModuleBuild.get_last_builds_in_stream_version_lte(session, "platform", 290100)
|
||||
builds = set([
|
||||
"%s:%s:%s:%s" % (build.name, build.stream, str(build.version), build.context)
|
||||
for build in builds
|
||||
])
|
||||
assert builds == set(["platform:f29.0.0:3:00000000", "platform:f29.1.0:3:00000000"])
|
||||
builds = ModuleBuild.get_last_builds_in_stream_version_lte(db_session, "platform", 290100)
|
||||
builds = set([
|
||||
"%s:%s:%s:%s" % (build.name, build.stream, str(build.version), build.context)
|
||||
for build in builds
|
||||
])
|
||||
db_session.commit()
|
||||
assert builds == set(["platform:f29.0.0:3:00000000", "platform:f29.1.0:3:00000000"])
|
||||
|
||||
def test_get_last_builds_in_stream_version_lte_different_versions(self):
|
||||
def test_get_last_builds_in_stream_version_lte_different_versions(self, db_session):
|
||||
"""
|
||||
Tests that get_last_builds_in_stream_version_lte works in case the
|
||||
name:stream_ver modules have different versions.
|
||||
"""
|
||||
clean_database(False)
|
||||
|
||||
with make_session(conf) as db_session:
|
||||
make_module(
|
||||
db_session, "platform:f29.1.0:10:old_version", {}, {}, virtual_streams=["f29"])
|
||||
make_module(
|
||||
db_session, "platform:f29.1.0:15:c11.another", {}, {}, virtual_streams=["f29"])
|
||||
make_module(
|
||||
db_session, "platform:f29.1.0:15:c11", {}, {}, virtual_streams=["f29"])
|
||||
make_module(
|
||||
db_session, "platform:f29.2.0:0:old_version", {}, {}, virtual_streams=["f29"])
|
||||
make_module(
|
||||
db_session, "platform:f29.2.0:1:c11", {}, {}, virtual_streams=["f29"])
|
||||
make_module(
|
||||
db_session, "platform:f29.3.0:15:old_version", {}, {}, virtual_streams=["f29"])
|
||||
make_module(
|
||||
db_session, "platform:f29.3.0:20:c11", {}, {}, virtual_streams=["f29"])
|
||||
make_module(
|
||||
db_session, "platform:f29.1.0:10:old_version", {}, {}, virtual_streams=["f29"])
|
||||
make_module(
|
||||
db_session, "platform:f29.1.0:15:c11.another", {}, {}, virtual_streams=["f29"])
|
||||
make_module(
|
||||
db_session, "platform:f29.1.0:15:c11", {}, {}, virtual_streams=["f29"])
|
||||
make_module(
|
||||
db_session, "platform:f29.2.0:0:old_version", {}, {}, virtual_streams=["f29"])
|
||||
make_module(
|
||||
db_session, "platform:f29.2.0:1:c11", {}, {}, virtual_streams=["f29"])
|
||||
make_module(
|
||||
db_session, "platform:f29.3.0:15:old_version", {}, {}, virtual_streams=["f29"])
|
||||
make_module(
|
||||
db_session, "platform:f29.3.0:20:c11", {}, {}, virtual_streams=["f29"])
|
||||
|
||||
builds = ModuleBuild.get_last_builds_in_stream_version_lte(
|
||||
db_session, "platform", 290200)
|
||||
builds = set([
|
||||
"%s:%s:%s:%s" % (build.name, build.stream, str(build.version), build.context)
|
||||
for build in builds
|
||||
])
|
||||
assert builds == set([
|
||||
"platform:f29.1.0:15:c11",
|
||||
"platform:f29.1.0:15:c11.another",
|
||||
"platform:f29.2.0:1:c11",
|
||||
])
|
||||
builds = ModuleBuild.get_last_builds_in_stream_version_lte(
|
||||
db_session, "platform", 290200)
|
||||
builds = set([
|
||||
"%s:%s:%s:%s" % (build.name, build.stream, str(build.version), build.context)
|
||||
for build in builds
|
||||
])
|
||||
db_session.commit()
|
||||
assert builds == set([
|
||||
"platform:f29.1.0:15:c11",
|
||||
"platform:f29.1.0:15:c11.another",
|
||||
"platform:f29.2.0:1:c11",
|
||||
])
|
||||
|
||||
def test_get_module_count(self):
|
||||
def test_get_module_count(self, db_session):
|
||||
clean_database(False)
|
||||
with make_session(conf) as db_session:
|
||||
make_module(db_session, "platform:f29.1.0:10:c11", {}, {})
|
||||
make_module(db_session, "platform:f29.1.0:10:c12", {}, {})
|
||||
make_module(db_session, "platform:f29.1.0:10:c11", {}, {})
|
||||
make_module(db_session, "platform:f29.1.0:10:c12", {}, {})
|
||||
|
||||
count = ModuleBuild.get_module_count(db_session, name="platform")
|
||||
assert count == 2
|
||||
count = ModuleBuild.get_module_count(db_session, name="platform")
|
||||
db_session.commit()
|
||||
assert count == 2
|
||||
|
||||
def test_add_virtual_streams_filter(self):
|
||||
def test_add_virtual_streams_filter(self, db_session):
|
||||
clean_database(False)
|
||||
|
||||
with make_session(conf) as db_session:
|
||||
make_module(db_session, "platform:f29.1.0:10:c1", {}, {}, virtual_streams=["f29"])
|
||||
make_module(db_session, "platform:f29.1.0:15:c1", {}, {}, virtual_streams=["f29"])
|
||||
make_module(
|
||||
db_session, "platform:f29.3.0:15:old_version", {}, {},
|
||||
virtual_streams=["f28", "f29"])
|
||||
make_module(db_session, "platform:f29.3.0:20:c11", {}, {}, virtual_streams=["f30"])
|
||||
make_module(db_session, "platform:f29.1.0:10:c1", {}, {}, virtual_streams=["f29"])
|
||||
make_module(db_session, "platform:f29.1.0:15:c1", {}, {}, virtual_streams=["f29"])
|
||||
make_module(
|
||||
db_session, "platform:f29.3.0:15:old_version", {}, {},
|
||||
virtual_streams=["f28", "f29"])
|
||||
make_module(db_session, "platform:f29.3.0:20:c11", {}, {}, virtual_streams=["f30"])
|
||||
|
||||
query = db_session.query(ModuleBuild).filter_by(name="platform")
|
||||
query = ModuleBuild._add_virtual_streams_filter(db_session, query, ["f28", "f29"])
|
||||
count = query.count()
|
||||
assert count == 3
|
||||
query = db_session.query(ModuleBuild).filter_by(name="platform")
|
||||
query = ModuleBuild._add_virtual_streams_filter(db_session, query, ["f28", "f29"])
|
||||
count = query.count()
|
||||
db_session.commit()
|
||||
assert count == 3
|
||||
|
||||
Reference in New Issue
Block a user