Files
fm-orchestrator/tests/test_scheduler/test_module_init.py
Chenxiong Qi 3878affa41 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>
2019-07-18 21:26:50 +08:00

208 lines
8.6 KiB
Python

# Copyright (c) 2017 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.
#
import os
from mock import patch, PropertyMock
from tests import conf, clean_database, read_staged_data
from tests.test_views.test_views import FakeSCM
import module_build_service.messaging
import module_build_service.scheduler.handlers.modules
from module_build_service import build_logs
from module_build_service.models import make_db_session, ModuleBuild
from module_build_service.utils.general import mmd_to_str, load_mmd
class TestModuleInit:
def setup_method(self, test_method):
self.fn = module_build_service.scheduler.handlers.modules.init
testmodule_yml_path = read_staged_data("testmodule_init")
mmd = load_mmd(testmodule_yml_path)
# Set the name and stream
mmd = mmd.copy("testmodule", "1")
scmurl = "git://pkgs.domain.local/modules/testmodule?#620ec77"
clean_database()
with make_db_session(conf) as session:
ModuleBuild.create(
session, conf, "testmodule", "1", 3, mmd_to_str(mmd), scmurl, "mprahl")
def teardown_method(self, test_method):
try:
with make_db_session(conf) as db_session:
path = build_logs.path(db_session, 1)
os.remove(path)
except Exception:
pass
@patch(
"module_build_service.builder.KojiModuleBuilder.KojiModuleBuilder."
"get_built_rpms_in_module_build"
)
@patch("module_build_service.scm.SCM")
@patch("module_build_service.scheduler.handlers.modules.handle_stream_collision_modules")
@patch("module_build_service.utils.submit.get_build_arches", return_value=["x86_64"])
def init_basic(self, db_session, get_build_arches, rscm, mocked_scm, built_rpms):
FakeSCM(
mocked_scm,
"testmodule",
"testmodule_init.yaml",
"620ec77321b2ea7b0d67d82992dda3e1d67055b4",
)
built_rpms.return_value = [
"foo-0:2.4.48-3.el8+1308+551bfa71",
"foo-debuginfo-0:2.4.48-3.el8+1308+551bfa71",
"bar-0:2.5.48-3.el8+1308+551bfa71",
"bar-debuginfo-0:2.5.48-3.el8+1308+551bfa71",
"x-0:2.5.48-3.el8+1308+551bfa71",
"x-debuginfo-0:2.5.48-3.el8+1308+551bfa71",
]
platform_build = ModuleBuild.get_by_id(db_session, 1)
mmd = platform_build.mmd()
for rpm in mmd.get_rpm_filters():
mmd.remove_rpm_filter(rpm)
mmd.add_rpm_filter("foo")
mmd.add_rpm_filter("bar")
platform_build.modulemd = mmd_to_str(mmd)
db_session.commit()
msg = module_build_service.messaging.MBSModule(
msg_id=None, module_build_id=2, module_build_state="init"
)
self.fn(config=conf, db_session=db_session, msg=msg)
build = ModuleBuild.get_by_id(db_session, 2)
# Make sure the module entered the wait state
assert build.state == 1, build.state
# Make sure format_mmd was run properly
xmd_mbs = build.mmd().get_xmd()["mbs"]
assert xmd_mbs["buildrequires"]["platform"]["filtered_rpms"] == [
"foo-0:2.4.48-3.el8+1308+551bfa71",
"bar-0:2.5.48-3.el8+1308+551bfa71",
]
return build
def test_init_called_twice(self, db_session):
build = self.init_basic(db_session)
old_component_builds = len(build.component_builds)
old_mmd = load_mmd(build.modulemd)
build.state = 4
db_session.commit()
build = self.init_basic(db_session)
db_session.refresh(build)
assert build.state == 1
assert old_component_builds == len(build.component_builds)
new_mmd = load_mmd(build.modulemd)
# Compare only lengths, because `mmd_to_str` can shuffle the fields randomly.
assert len(mmd_to_str(old_mmd)) == len(mmd_to_str(new_mmd))
@patch("module_build_service.scm.SCM")
@patch("module_build_service.utils.submit.get_build_arches", return_value=["x86_64"])
def test_init_scm_not_available(self, get_build_arches, mocked_scm, db_session):
FakeSCM(
mocked_scm, "testmodule", "testmodule.yaml", "620ec77321b2ea7b0d67d82992dda3e1d67055b4",
get_latest_raise=True,
get_latest_error=RuntimeError("Failed in mocked_scm_get_latest")
)
msg = module_build_service.messaging.MBSModule(
msg_id=None, module_build_id=2, module_build_state="init")
self.fn(config=conf, db_session=db_session, msg=msg)
build = ModuleBuild.get_by_id(db_session, 2)
# Make sure the module entered the failed state
# since the git server is not available
assert build.state == 4, build.state
@patch(
"module_build_service.config.Config.modules_allow_repository",
new_callable=PropertyMock,
return_value=True,
)
@patch("module_build_service.scm.SCM")
@patch("module_build_service.utils.submit.get_build_arches", return_value=["x86_64"])
def test_init_includedmodule(
self, get_build_arches, mocked_scm, mocked_mod_allow_repo, db_session
):
FakeSCM(mocked_scm, "includedmodules", ["testmodule_init.yaml"])
includedmodules_yml_path = read_staged_data("includedmodules")
mmd = load_mmd(includedmodules_yml_path)
# Set the name and stream
mmd = mmd.copy("includedmodules", "1")
scmurl = "git://pkgs.domain.local/modules/includedmodule?#da95886"
ModuleBuild.create(
db_session, conf, "includemodule", "1", 3, mmd_to_str(mmd), scmurl, "mprahl")
msg = module_build_service.messaging.MBSModule(
msg_id=None, module_build_id=3, module_build_state="init")
self.fn(config=conf, db_session=db_session, msg=msg)
build = ModuleBuild.get_by_id(db_session, 3)
assert build.state == 1
assert build.name == "includemodule"
batches = {}
for comp_build in build.component_builds:
batches[comp_build.package] = comp_build.batch
assert batches["perl-List-Compare"] == 2
assert batches["perl-Tangerine"] == 2
assert batches["foo"] == 2
assert batches["tangerine"] == 3
assert batches["file"] == 4
# Test that the RPMs are properly merged in xmd
xmd_rpms = {
"perl-List-Compare": {"ref": "4f26aeafdb"},
"perl-Tangerine": {"ref": "4f26aeafdb"},
"tangerine": {"ref": "4f26aeafdb"},
"foo": {"ref": "93dea37599"},
"file": {"ref": "a2740663f8"},
}
assert build.mmd().get_xmd()["mbs"]["rpms"] == xmd_rpms
@patch("module_build_service.models.ModuleBuild.from_module_event")
@patch("module_build_service.scm.SCM")
@patch("module_build_service.utils.submit.get_build_arches", return_value=["x86_64"])
def test_init_when_get_latest_raises(
self, get_build_arches, mocked_scm, mocked_from_module_event, db_session):
FakeSCM(
mocked_scm,
"testmodule",
"testmodule.yaml",
"7035bd33614972ac66559ac1fdd019ff6027ad22",
get_latest_raise=True,
)
msg = module_build_service.messaging.MBSModule(
msg_id=None, module_build_id=2, module_build_state="init")
build = ModuleBuild.get_by_id(db_session, 2)
mocked_from_module_event.return_value = build
self.fn(config=conf, db_session=db_session, msg=msg)
# Query the database again to make sure the build object is updated
db_session.refresh(build)
# Make sure the module entered the failed state
assert build.state == 4, build.state
assert "Failed to get the latest commit for" in build.state_reason