mirror of
https://pagure.io/fm-orchestrator.git
synced 2026-04-14 08:49:45 +08:00
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>
170 lines
6.8 KiB
Python
170 lines
6.8 KiB
Python
# Copyright (c) 2019 Red Hat, Inc.
|
|
#
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
# in the Software without restriction, including without limitation the rights
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
# furnished to do so, subject to the following conditions:
|
|
#
|
|
# The above copyright notice and this permission notice shall be included in all
|
|
# copies or substantial portions of the Software.
|
|
#
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
# SOFTWARE.
|
|
#
|
|
# Written by Chenxiong Qi <cqi@redhat.com>
|
|
|
|
import pytest
|
|
|
|
from mock import call, patch, Mock
|
|
from sqlalchemy import func
|
|
|
|
from module_build_service import conf
|
|
from module_build_service.models import BUILD_STATES, ModuleBuild
|
|
from module_build_service.scheduler.consumer import MBSConsumer
|
|
from module_build_service.scheduler.handlers.greenwave import get_corresponding_module_build
|
|
from module_build_service.scheduler.handlers.greenwave import decision_update
|
|
from tests import clean_database, make_module
|
|
|
|
|
|
class TestGetCorrespondingModuleBuild:
|
|
"""Test get_corresponding_module_build"""
|
|
|
|
def setup_method(self, method):
|
|
clean_database()
|
|
|
|
@patch("module_build_service.builder.KojiModuleBuilder.KojiClientSession")
|
|
def test_module_build_nvr_does_not_exist_in_koji(self, ClientSession, db_session):
|
|
ClientSession.return_value.getBuild.return_value = None
|
|
|
|
assert get_corresponding_module_build(db_session, "n-v-r") is None
|
|
|
|
@pytest.mark.parametrize(
|
|
"build_info",
|
|
[
|
|
# Build info does not have key extra
|
|
{"id": 1000, "name": "ed"},
|
|
# Build info contains key extra, but it is not for the module build
|
|
{"extra": {"submitter": "osbs", "image": {}}},
|
|
# Key module_build_service_id is missing
|
|
{"extra": {"typeinfo": {"module": {}}}},
|
|
],
|
|
)
|
|
@patch("module_build_service.builder.KojiModuleBuilder.KojiClientSession")
|
|
def test_cannot_find_module_build_id_from_build_info(
|
|
self, ClientSession, build_info, db_session
|
|
):
|
|
ClientSession.return_value.getBuild.return_value = build_info
|
|
|
|
assert get_corresponding_module_build(db_session, "n-v-r") is None
|
|
|
|
@patch("module_build_service.builder.KojiModuleBuilder.KojiClientSession")
|
|
def test_corresponding_module_build_id_does_not_exist_in_db(self, ClientSession, db_session):
|
|
fake_module_build_id, = db_session.query(func.max(ModuleBuild.id)).first()
|
|
|
|
ClientSession.return_value.getBuild.return_value = {
|
|
"extra": {"typeinfo": {"module": {"module_build_service_id": fake_module_build_id + 1}}}
|
|
}
|
|
|
|
assert get_corresponding_module_build(db_session, "n-v-r") is None
|
|
|
|
@patch("module_build_service.builder.KojiModuleBuilder.KojiClientSession")
|
|
def test_find_the_module_build(self, ClientSession, db_session):
|
|
expected_module_build = (
|
|
db_session.query(ModuleBuild).filter(ModuleBuild.name == "platform").first()
|
|
)
|
|
|
|
ClientSession.return_value.getBuild.return_value = {
|
|
"extra": {"typeinfo": {"module": {"module_build_service_id": expected_module_build.id}}}
|
|
}
|
|
|
|
build = get_corresponding_module_build(db_session, "n-v-r")
|
|
|
|
assert expected_module_build.id == build.id
|
|
assert expected_module_build.name == build.name
|
|
|
|
|
|
class TestDecisionUpdateHandler:
|
|
"""Test handler decision_update"""
|
|
|
|
@patch("module_build_service.scheduler.handlers.greenwave.log")
|
|
def test_decision_context_is_not_match(self, log, db_session):
|
|
msg = Mock(msg_id="msg-id-1", decision_context="bodhi_update_push_testing")
|
|
decision_update(conf, db_session, msg)
|
|
log.debug.assert_called_once_with(
|
|
'Skip Greenwave message %s as MBS only handles messages with the decision context "%s"',
|
|
"msg-id-1",
|
|
"test_dec_context"
|
|
)
|
|
|
|
@patch("module_build_service.scheduler.handlers.greenwave.log")
|
|
def test_not_satisfy_policies(self, log, db_session):
|
|
msg = Mock(
|
|
msg_id="msg-id-1",
|
|
decision_context="test_dec_context",
|
|
policies_satisfied=False,
|
|
subject_identifier="pkg-0.1-1.c1",
|
|
)
|
|
decision_update(conf, db_session, msg)
|
|
log.debug.assert_called_once_with(
|
|
"Skip to handle module build %s because it has not satisfied Greenwave policies.",
|
|
msg.subject_identifier,
|
|
)
|
|
|
|
@patch("module_build_service.messaging.publish")
|
|
@patch("module_build_service.builder.KojiModuleBuilder.KojiClientSession")
|
|
def test_transform_from_done_to_ready(self, ClientSession, publish, db_session):
|
|
clean_database()
|
|
|
|
# This build should be queried and transformed to ready state
|
|
module_build = make_module(db_session, "pkg:0.1:1:c1", requires_list={"platform": "el8"})
|
|
module_build.transition(
|
|
db_session, conf, BUILD_STATES["done"], "Move to done directly for running test."
|
|
)
|
|
db_session.commit()
|
|
|
|
# Assert this call below
|
|
first_publish_call = call(
|
|
service="mbs",
|
|
topic="module.state.change",
|
|
msg=module_build.json(db_session, show_tasks=False),
|
|
conf=conf,
|
|
)
|
|
|
|
ClientSession.return_value.getBuild.return_value = {
|
|
"extra": {"typeinfo": {"module": {"module_build_service_id": module_build.id}}}
|
|
}
|
|
|
|
msg = {
|
|
"msg_id": "msg-id-1",
|
|
"topic": "org.fedoraproject.prod.greenwave.decision.update",
|
|
"msg": {
|
|
"decision_context": "test_dec_context",
|
|
"policies_satisfied": True,
|
|
"subject_identifier": "pkg-0.1-1.c1",
|
|
},
|
|
}
|
|
hub = Mock(config={"validate_signatures": False})
|
|
consumer = MBSConsumer(hub)
|
|
consumer.consume(msg)
|
|
|
|
# Load module build again to check its state is moved correctly
|
|
db_session.refresh(module_build)
|
|
assert BUILD_STATES["ready"] == module_build.state
|
|
|
|
publish.assert_has_calls([
|
|
first_publish_call,
|
|
call(
|
|
service="mbs",
|
|
topic="module.state.change",
|
|
msg=module_build.json(db_session, show_tasks=False),
|
|
conf=conf,
|
|
),
|
|
])
|