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

132 lines
5.3 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.
#
# Written by Jan Kaluza <jkaluza@redhat.com>
import os
from os import path
import shutil
import tempfile
from module_build_service import log, models
from module_build_service.logger import ModuleBuildLogs
from module_build_service.scheduler.consumer import MBSConsumer
from tests import init_data
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, db_session):
"""
Tests that ModuleBuildLogs is logging properly to build log file.
"""
build = models.ModuleBuild.get_by_id(db_session, 2)
# Initialize logging, get the build log path and remove it to
# ensure we are not using some garbage from previous failed test.
self.build_log.start(db_session, build)
path = self.build_log.path(db_session, build)
assert path[len(self.base):] == "/build-2.log"
if os.path.exists(path):
os.unlink(path)
# Try logging without the MBSConsumer.current_module_build_id set.
# No log file should be created.
log.debug("ignore this test msg")
log.info("ignore this test msg")
log.warning("ignore this test msg")
log.error("ignore this test msg")
self.build_log.stop(build)
assert not os.path.exists(path)
# Try logging with current_module_build_id set to 2 and then to 2.
# Only messages with current_module_build_id set to 2 should appear in
# the log.
self.build_log.start(db_session, build)
MBSConsumer.current_module_build_id = 1
log.debug("ignore this test msg1")
log.info("ignore this test msg1")
log.warning("ignore this test msg1")
log.error("ignore this test msg1")
MBSConsumer.current_module_build_id = 2
log.debug("ignore this test msg2")
log.info("ignore this test msg2")
log.warning("ignore this test msg2")
log.error("ignore this test msg2")
self.build_log.stop(build)
assert os.path.exists(path)
with open(path, "r") as f:
data = f.read()
# Note that DEBUG is not present unless configured server-wide.
for level in ["INFO", "WARNING", "ERROR"]:
assert data.find("MBS - {0} - ignore this test msg2".format(level)) != -1
# Try to log more messages when build_log for module 1 is stopped.
# New messages should not appear in a log.
MBSConsumer.current_module_build_id = 2
log.debug("ignore this test msg3")
log.info("ignore this test msg3")
log.warning("ignore this test msg3")
log.error("ignore this test msg3")
self.build_log.stop(build)
with open(path, "r") as f:
data = f.read()
assert data.find("ignore this test msg3") == -1
def test_module_build_logs_name_format(self, db_session):
build = models.ModuleBuild.get_by_id(db_session, 2)
log1 = ModuleBuildLogs("/some/path", "build-{id}.log")
assert log1.name(db_session, build) == "build-2.log"
assert log1.path(db_session, build) == "/some/path/build-2.log"
log2 = ModuleBuildLogs("/some/path", "build-{name}-{stream}-{version}.log")
assert log2.name(db_session, build) == "build-nginx-1-2.log"
assert log2.path(db_session, build) == "/some/path/build-nginx-1-2.log"