Files
fm-orchestrator/tests/test_builder/test_mock.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

230 lines
8.7 KiB
Python

import os
import mock
import koji
import tempfile
import shutil
from textwrap import dedent
import kobo.rpmlib
from module_build_service import conf
from module_build_service.models import ModuleBuild, ComponentBuild
from module_build_service.builder.MockModuleBuilder import MockModuleBuilder
from module_build_service.utils import import_fake_base_module, mmd_to_str, load_mmd
from tests import clean_database, make_module, read_staged_data
class TestMockModuleBuilder:
def setup_method(self, test_method):
clean_database()
self.resultdir = tempfile.mkdtemp()
def teardown_method(self, test_method):
clean_database()
shutil.rmtree(self.resultdir)
def _create_module_with_filters(self, db_session, batch, state):
mmd = load_mmd(read_staged_data("testmodule-with-filters"))
# Set the name and stream
mmd = mmd.copy("mbs-testmodule", "test")
mmd.set_xmd({
"mbs": {
"rpms": {
"ed": {"ref": "01bf8330812fea798671925cc537f2f29b0bd216"},
"mksh": {"ref": "f70fd11ddf96bce0e2c64309706c29156b39141d"},
},
"buildrequires": {
"host": {
"version": "20171024133034",
"filtered_rpms": [],
"stream": "master",
"ref": "6df253bb3c53e84706c01b8ab2d5cac24f0b6d45",
"context": "00000000",
},
"platform": {
"version": "20171028112959",
"filtered_rpms": [],
"stream": "master",
"ref": "4f7787370a931d57421f9f9555fc41c3e31ff1fa",
"context": "00000000",
},
},
"scmurl": "file:///testdir",
"commit": "5566bc792ec7a03bb0e28edd1b104a96ba342bd8",
"requires": {
"platform": {
"version": "20171028112959",
"filtered_rpms": [],
"stream": "master",
"ref": "4f7787370a931d57421f9f9555fc41c3e31ff1fa",
"context": "00000000",
}
},
}
})
module = ModuleBuild.create(
db_session,
conf,
name="mbs-testmodule",
stream="test",
version="20171027111452",
modulemd=mmd_to_str(mmd),
scmurl="file:///testdir",
username="test",
)
module.koji_tag = "module-mbs-testmodule-test-20171027111452"
module.batch = batch
db_session.add(module)
db_session.commit()
comp_builds = [
{
"module_id": module.id,
"state": state,
"package": "ed",
"format": "rpms",
"scmurl": (
"https://src.fedoraproject.org/rpms/ed"
"?#01bf8330812fea798671925cc537f2f29b0bd216"
),
"batch": 2,
"ref": "01bf8330812fea798671925cc537f2f29b0bd216",
},
{
"module_id": module.id,
"state": state,
"package": "mksh",
"format": "rpms",
"scmurl": (
"https://src.fedoraproject.org/rpms/mksh"
"?#f70fd11ddf96bce0e2c64309706c29156b39141d"
),
"batch": 3,
"ref": "f70fd11ddf96bce0e2c64309706c29156b39141d",
},
]
for build in comp_builds:
db_session.add(ComponentBuild(**build))
db_session.commit()
return module
@mock.patch("module_build_service.conf.system", new="mock")
def test_createrepo_filter_last_batch(self, db_session):
module = self._create_module_with_filters(db_session, 3, koji.BUILD_STATES["COMPLETE"])
builder = MockModuleBuilder(
db_session, "mcurlej", module, conf, module.koji_tag, module.component_builds
)
builder.resultsdir = self.resultdir
rpms = [
"ed-1.14.1-4.module+24957a32.x86_64.rpm",
"mksh-56b-1.module+24957a32.x86_64.rpm",
"module-build-macros-0.1-1.module+24957a32.noarch.rpm",
]
rpm_qf_output = dedent("""\
ed 0 1.14.1 4.module+24957a32 x86_64
mksh 0 56b-1 module+24957a32 x86_64
module-build-macros 0 0.1 1.module+24957a32 noarch
""")
with mock.patch("os.listdir", return_value=rpms):
with mock.patch("subprocess.check_output", return_value=rpm_qf_output):
builder._createrepo()
with open(os.path.join(self.resultdir, "pkglist"), "r") as fd:
pkglist = fd.read().strip()
rpm_names = [kobo.rpmlib.parse_nvr(rpm)["name"] for rpm in pkglist.split("\n")]
assert "ed" not in rpm_names
@mock.patch("module_build_service.conf.system", new="mock")
def test_createrepo_not_last_batch(self, db_session):
module = self._create_module_with_filters(db_session, 2, koji.BUILD_STATES["COMPLETE"])
builder = MockModuleBuilder(
db_session, "mcurlej", module, conf, module.koji_tag, module.component_builds
)
builder.resultsdir = self.resultdir
rpms = [
"ed-1.14.1-4.module+24957a32.x86_64.rpm",
"mksh-56b-1.module+24957a32.x86_64.rpm",
]
rpm_qf_output = dedent("""\
ed 0 1.14.1 4.module+24957a32 x86_64
mksh 0 56b-1 module+24957a32 x86_64
""")
with mock.patch("os.listdir", return_value=rpms):
with mock.patch("subprocess.check_output", return_value=rpm_qf_output):
builder._createrepo()
with open(os.path.join(self.resultdir, "pkglist"), "r") as fd:
pkglist = fd.read().strip()
rpm_names = [kobo.rpmlib.parse_nvr(rpm)["name"] for rpm in pkglist.split("\n")]
assert "ed" in rpm_names
@mock.patch("module_build_service.conf.system", new="mock")
def test_createrepo_empty_rmp_list(self, db_session):
module = self._create_module_with_filters(db_session, 3, koji.BUILD_STATES["COMPLETE"])
builder = MockModuleBuilder(
db_session, "mcurlej", module, conf, module.koji_tag, module.component_builds)
builder.resultsdir = self.resultdir
rpms = []
with mock.patch("os.listdir", return_value=rpms):
builder._createrepo()
with open(os.path.join(self.resultdir, "pkglist"), "r") as fd:
pkglist = fd.read().strip()
assert not pkglist
class TestMockModuleBuilderAddRepos:
def setup_method(self, test_method):
clean_database(add_platform_module=False)
@mock.patch("module_build_service.conf.system", new="mock")
@mock.patch(
"module_build_service.config.Config.base_module_repofiles",
new_callable=mock.PropertyMock,
return_value=["/etc/yum.repos.d/bar.repo", "/etc/yum.repos.d/bar-updates.repo"],
create=True,
)
@mock.patch("module_build_service.builder.MockModuleBuilder.open", create=True)
@mock.patch(
"module_build_service.builder.MockModuleBuilder.MockModuleBuilder._load_mock_config"
)
@mock.patch(
"module_build_service.builder.MockModuleBuilder.MockModuleBuilder._write_mock_config"
)
def test_buildroot_add_repos(
self, write_config, load_config, patched_open, base_module_repofiles, db_session
):
import_fake_base_module(db_session, "platform:f29:1:000000")
platform = ModuleBuild.get_last_build_in_stream(db_session, "platform", "f29")
foo = make_module(
db_session, "foo:1:1:1", {"platform": ["f29"]}, {"platform": ["f29"]})
app = make_module(
db_session, "app:1:1:1", {"platform": ["f29"]}, {"platform": ["f29"]})
patched_open.side_effect = [
mock.mock_open(read_data="[fake]\nrepofile 1\n").return_value,
mock.mock_open(read_data="[fake]\nrepofile 2\n").return_value,
mock.mock_open(read_data="[fake]\nrepofile 3\n").return_value,
]
builder = MockModuleBuilder(db_session, "user", app, conf, "module-app", [])
dependencies = {
"repofile://": [platform.mmd()],
"repofile:///etc/yum.repos.d/foo.repo": [foo.mmd(), app.mmd()],
}
builder.buildroot_add_repos(dependencies)
assert "repofile 1" in builder.yum_conf
assert "repofile 2" in builder.yum_conf
assert "repofile 3" in builder.yum_conf
assert set(builder.enabled_modules) == set(["foo:1", "app:1"])