JIRA: RHELBLD-257,RHELBLD-310 - refactor clean_database

Replace clean_database calls with cheaper truncate operation.
Remove duplicate calls of clean_database.
Extract clean_database/init_data into fixtures.
This commit is contained in:
jobrauer
2020-05-15 15:38:05 +02:00
parent 53351670e3
commit c584d84b76
27 changed files with 730 additions and 693 deletions

View File

@@ -3,6 +3,7 @@
from __future__ import absolute_import
import os
from os import path
import pytest
import shutil
import tempfile
@@ -10,36 +11,38 @@ from module_build_service.common import log, models
from module_build_service.common.logger import ModuleBuildLogs
from module_build_service.scheduler.consumer import MBSConsumer
from module_build_service.scheduler.db_session import db_session
from tests import init_data
@pytest.fixture()
def test_logger_fixture(request, provide_test_data):
log.debug(request.function.__module__)
try:
# py2
test_id = ".".join([
path.splitext(path.basename(__file__))[0],
request.function.im_class.__name__,
request.function.im_func.__name__,
])
except AttributeError:
# py3
test_id = ".".join([
path.splitext(path.basename(__file__))[0],
request.function.__self__.__class__.__name__,
request.function.__self__.__class__.__name__,
])
base = tempfile.mkdtemp(prefix="mbs-", suffix="-%s" % test_id)
name_format = "build-{id}.log"
print("Storing build logs in %r" % base)
request.cls.build_log = ModuleBuildLogs(base, name_format)
request.cls.base = base
yield
MBSConsumer.current_module_build_id = None
shutil.rmtree(base)
@pytest.mark.usefixtures("test_logger_fixture")
class TestLogger:
def setup_method(self, test_method):
init_data(1)
log.debug(test_method.__module__)
try:
# py2
test_id = ".".join([
path.splitext(path.basename(__file__))[0],
test_method.im_class.__name__,
test_method.im_func.__name__,
])
except AttributeError:
# py3
test_id = ".".join([
path.splitext(path.basename(__file__))[0],
test_method.__self__.__class__.__name__,
test_method.__self__.__class__.__name__,
])
self.base = tempfile.mkdtemp(prefix="mbs-", suffix="-%s" % test_id)
self.name_format = "build-{id}.log"
print("Storing build logs in %r" % self.base)
self.build_log = ModuleBuildLogs(self.base, self.name_format)
def teardown_method(self, test_method):
MBSConsumer.current_module_build_id = None
shutil.rmtree(self.base)
def test_module_build_logs(self):
"""

View File

@@ -10,7 +10,6 @@ from module_build_service.common.models import ComponentBuild, ComponentBuildTra
from module_build_service.common.utils import load_mmd, mmd_to_str
from module_build_service.scheduler.db_session import db_session
from tests import (
clean_database,
init_data as init_data_contexts,
make_module_in_db,
module_build_from_modulemd,
@@ -18,7 +17,6 @@ from tests import (
)
@pytest.mark.usefixtures("model_tests_init_data")
class TestModels:
def test_app_sqlalchemy_events(self):
@@ -63,11 +61,10 @@ class TestModels:
assert build.context == "3ee22b28"
assert build.build_context_no_bms == "089df24993c037e10174f3fa7342ab4dc191a4d4"
def test_siblings_property(self):
def test_siblings_property(self, require_empty_database):
""" Tests that the siblings property returns the ID of all modules with
the same name:stream:version
"""
clean_database()
mmd = load_mmd(read_staged_data("formatted_testmodule"))
for i in range(3):
build = module_build_from_modulemd(mmd_to_str(mmd))
@@ -77,11 +74,11 @@ class TestModels:
db_session.add(build)
db_session.commit()
build_one = ModuleBuild.get_by_id(db_session, 2)
build_one = ModuleBuild.get_by_id(db_session, 1)
sibling_ids = build_one.siblings(db_session)
db_session.commit()
assert sorted(sibling_ids) == [3, 4]
assert sorted(sibling_ids) == [2, 3]
@pytest.mark.parametrize(
"stream,right_pad,expected",
@@ -101,6 +98,7 @@ class TestModels:
assert expected == ModuleBuild.get_stream_version(stream, right_pad)
@pytest.mark.usefixtures("require_platform_and_default_arch")
class TestModelsGetStreamsContexts:
def test_get_last_build_in_all_streams(self):
init_data_contexts(contexts=True)
@@ -145,7 +143,6 @@ class TestModelsGetStreamsContexts:
Tests that get_last_builds_in_stream_version_lte works in case the
name:stream_ver modules have different versions.
"""
clean_database(False)
make_module_in_db(
"platform:f29.1.0:10:old_version", virtual_streams=["f29"])
@@ -175,18 +172,7 @@ class TestModelsGetStreamsContexts:
"platform:f29.2.0:1:c11",
}
def test_get_module_count(self):
clean_database(False)
make_module_in_db("platform:f29.1.0:10:c11")
make_module_in_db("platform:f29.1.0:10:c12")
count = ModuleBuild.get_module_count(db_session, name="platform")
db_session.commit()
assert count == 2
def test_add_virtual_streams_filter(self):
clean_database(False)
make_module_in_db(
"platform:f29.1.0:10:c1", virtual_streams=["f29"])
make_module_in_db(
@@ -201,3 +187,12 @@ class TestModelsGetStreamsContexts:
count = query.count()
db_session.commit()
assert count == 3
def test_get_module_count(require_empty_database):
make_module_in_db("platform:f29.1.0:10:c11")
make_module_in_db("platform:f29.1.0:10:c12")
count = ModuleBuild.get_module_count(db_session, name="platform")
db_session.commit()
assert count == 2

View File

@@ -8,26 +8,23 @@ import pytest
import requests
from six.moves import reload_module
from module_build_service import app
from module_build_service.common import conf, models
import module_build_service.common.monitor
from module_build_service.scheduler.db_session import db_session
from tests import clean_database, init_data, make_module_in_db
from tests import make_module_in_db
num_of_metrics = 18
@pytest.mark.usefixtures("provide_test_client")
class TestViews:
def setup_method(self, test_method):
self.client = app.test_client()
init_data(2)
def test_metrics(self):
def test_metrics(self, provide_test_data):
rv = self.client.get("/module-build-service/1/monitor/metrics")
count = len([
l for l in rv.get_data(as_text=True).splitlines()
if (l.startswith("# TYPE") and "_created " not in l)
line for line in rv.get_data(as_text=True).splitlines()
if (line.startswith("# TYPE") and "_created " not in line)
])
assert count == num_of_metrics
@@ -43,16 +40,15 @@ def test_standalone_metrics_server():
r = requests.get("http://127.0.0.1:10040/metrics")
count = len([
l for l in r.text.splitlines()
if (l.startswith("# TYPE") and "_created " not in l)
line for line in r.text.splitlines()
if (line.startswith("# TYPE") and "_created " not in line)
])
assert count == num_of_metrics
@mock.patch("module_build_service.common.monitor.builder_failed_counter.labels")
@mock.patch("module_build_service.common.monitor.builder_success_counter.inc")
def test_monitor_state_changing_success(succ_cnt, failed_cnt):
clean_database(add_platform_module=False, add_default_arches=False)
def test_monitor_state_changing_success(succ_cnt, failed_cnt, require_empty_database):
b = make_module_in_db(
"pkg:0.1:1:c1",
[
@@ -72,8 +68,7 @@ def test_monitor_state_changing_success(succ_cnt, failed_cnt):
@mock.patch("module_build_service.common.monitor.builder_failed_counter.labels")
@mock.patch("module_build_service.common.monitor.builder_success_counter.inc")
def test_monitor_state_changing_failure(succ_cnt, failed_cnt):
clean_database(add_platform_module=False, add_default_arches=False)
def test_monitor_state_changing_failure(succ_cnt, failed_cnt, require_empty_database):
failure_type = "user"
b = make_module_in_db(
"pkg:0.1:1:c1",

View File

@@ -10,15 +10,11 @@ from module_build_service.common.modulemd import Modulemd
from module_build_service.common.utils import load_mmd
from module_build_service.common.resolve import get_base_module_mmds
from module_build_service.scheduler.db_session import db_session
from tests import clean_database, make_module_in_db, init_data, read_staged_data
from tests import make_module_in_db, init_data, read_staged_data
@pytest.mark.usefixtures("require_platform_and_default_arch")
class TestResolve:
def setup_method(self, test_method):
clean_database(False)
def teardown_method(self, test_method):
clean_database()
def test__get_base_module_mmds(self):
"""Ensure the correct results are returned without duplicates."""

View File

@@ -8,7 +8,7 @@ from module_build_service.common import models
from module_build_service.common.errors import UnprocessableEntity
from module_build_service.common.utils import import_mmd, load_mmd
from module_build_service.scheduler.db_session import db_session
from tests import clean_database, read_staged_data
from tests import read_staged_data
@pytest.mark.parametrize("context", ["c1", None])
@@ -80,8 +80,7 @@ def test_import_mmd_minimal_xmd_from_local_repository():
("f-28", "fedora-28", "The disttag_marking cannot contain a dash"),
),
)
def test_import_mmd_base_module(stream, disttag_marking, error_msg):
clean_database(add_platform_module=False)
def test_import_mmd_base_module(stream, disttag_marking, error_msg, require_empty_database):
mmd = load_mmd(read_staged_data("platform"))
mmd = mmd.copy(mmd.get_module_name(), stream)