From 1fe57e0589373b24615073b49f97ba805b77085d Mon Sep 17 00:00:00 2001 From: Lukas Holecek Date: Mar 29 2019 17:56:50 +0000 Subject: Tests: Fix using correct configuration for tests Avoids overriding configuration file path in current pytest process (fixes invocation of `pytest -x`). Signed-off-by: Lukas Holecek --- diff --git a/functional-tests/conftest.py b/functional-tests/conftest.py index 90597d6..ded6fd1 100644 --- a/functional-tests/conftest.py +++ b/functional-tests/conftest.py @@ -92,11 +92,6 @@ def server_subprocess( config_env_var = env_var_prefix + '_CONFIG' env[config_env_var] = settings_file.strpath - # We also update the config file for *this* process, as well as the server subprocess, - # because the fedmsg consumer tests actually invoke the handler code in-process. - # This way they will see the same config as the server. - os.environ[config_env_var] = settings_file.strpath - subprocess_arguments = dict(env=env, cwd=source_path) # Create and populate the database @@ -189,17 +184,22 @@ def distgit_server(tmpdir_factory): @pytest.yield_fixture(scope='session') -def greenwave_server(tmpdir_factory, resultsdb_server, waiverdb_server): +def cache_config(tmpdir_factory): cache_file = tmpdir_factory.mktemp('greenwave').join('cache.dbm') + return { + 'backend': 'dogpile.cache.dbm', + 'expiration_time': 300, + 'arguments': {'filename': cache_file.strpath}, + } + + +@pytest.yield_fixture(scope='session') +def greenwave_server(tmpdir_factory, cache_config, resultsdb_server, waiverdb_server): settings_content = """ - CACHE = { - 'backend': 'dogpile.cache.dbm', - 'expiration_time': 300, - 'arguments': {'filename': %r}, - } + CACHE = %s RESULTSDB_API_URL = '%sapi/v2.0' WAIVERDB_API_URL = '%sapi/v1.0' - """ % (cache_file.strpath, resultsdb_server, waiverdb_server) + """ % (json.dumps(cache_config), resultsdb_server, waiverdb_server) start_server_arguments = [ 'gunicorn-3', diff --git a/functional-tests/consumers/test_resultsdb.py b/functional-tests/consumers/test_resultsdb.py index 676c869..c6ad37a 100644 --- a/functional-tests/consumers/test_resultsdb.py +++ b/functional-tests/consumers/test_resultsdb.py @@ -4,9 +4,29 @@ import json import mock import pprint +from greenwave.config import TestingConfig from greenwave.consumers import resultsdb +def create_resultdb_handler(cache_config=None): + hub = mock.MagicMock() + hub.config = { + 'environment': 'environment', + 'topic_prefix': 'topic_prefix', + } + + class Config(TestingConfig): + CACHE = cache_config or TestingConfig.CACHE + + handler = resultsdb.ResultsDBHandler(hub, Config()) + assert handler.topic == [ + 'topic_prefix.environment.taskotron.result.new', + # Not ready to handle waiverdb yet. + #'topic_prefix.environment.waiver.new', + ] + return handler + + @mock.patch('greenwave.consumers.resultsdb.fedmsg.config.load_config') @mock.patch('greenwave.consumers.resultsdb.fedmsg.publish') def test_consume_new_result( @@ -33,13 +53,7 @@ def test_consume_new_result( } } } - hub = mock.MagicMock() - hub.config = { - 'environment': 'environment', - 'topic_prefix': 'topic_prefix', - } - handler = resultsdb.ResultsDBHandler(hub) - assert handler.topic == ['topic_prefix.environment.taskotron.result.new'] + handler = create_resultdb_handler() handler.consume(message) assert len(mock_fedmsg.mock_calls) == 2 @@ -183,13 +197,7 @@ def test_consume_unchanged_result( } } } - hub = mock.MagicMock() - hub.config = { - 'environment': 'environment', - 'topic_prefix': 'topic_prefix', - } - handler = resultsdb.ResultsDBHandler(hub) - assert handler.topic == ['topic_prefix.environment.taskotron.result.new'] + handler = create_resultdb_handler() handler.consume(message) assert len(mock_fedmsg.mock_calls) == 0 @@ -221,18 +229,8 @@ def test_invalidate_new_result_with_mocked_cache( } } } - hub = mock.MagicMock() - hub.config = { - 'environment': 'environment', - 'topic_prefix': 'topic_prefix', - } - handler = resultsdb.ResultsDBHandler(hub) + handler = create_resultdb_handler() handler.cache = mock.MagicMock() - assert handler.topic == [ - 'topic_prefix.environment.taskotron.result.new', - # Not ready to handle waiverdb yet. - #'topic_prefix.environment.waiver.new', - ] handler.consume(message) cache_key1 = 'greenwave.resources:CachedResults|koji_build {} dist.rpmdeplint'.format(nvr) cache_key2 = 'greenwave.resources:CachedResults|koji_build {} None'.format(nvr) @@ -247,7 +245,7 @@ def test_invalidate_new_result_with_mocked_cache( @mock.patch('greenwave.consumers.resultsdb.fedmsg.publish') def test_invalidate_new_result_with_real_cache( mock_fedmsg, load_config, requests_session, greenwave_server, - testdatabuilder): + testdatabuilder, cache_config): load_config.return_value = {'greenwave_api_url': greenwave_server + 'api/v1.0'} nvr = testdatabuilder.unique_nvr() for testcase_name in ['dist.rpmdeplint', 'dist.upgradepath', 'dist.abicheck']: @@ -297,17 +295,7 @@ def test_invalidate_new_result_with_real_cache( } } } - hub = mock.MagicMock() - hub.config = { - 'environment': 'environment', - 'topic_prefix': 'topic_prefix', - } - handler = resultsdb.ResultsDBHandler(hub) - assert handler.topic == [ - 'topic_prefix.environment.taskotron.result.new', - # Not ready to handle waiverdb yet. - #'topic_prefix.environment.waiver.new', - ] + handler = create_resultdb_handler(cache_config) handler.consume(message) # At this point, the invalidator should have invalidated the cache. If we @@ -348,18 +336,8 @@ def test_invalidate_new_result_with_no_preexisting_cache( } } } - hub = mock.MagicMock() - hub.config = { - 'environment': 'environment', - 'topic_prefix': 'topic_prefix', - } - handler = resultsdb.ResultsDBHandler(hub) + handler = create_resultdb_handler() handler.cache.delete = mock.MagicMock() - assert handler.topic == [ - 'topic_prefix.environment.taskotron.result.new', - # Not ready to handle waiverdb yet. - #'topic_prefix.environment.waiver.new', - ] handler.consume(message) cache_key1 = 'greenwave.resources:CachedResults|koji_build {} dist.rpmdeplint'.format(nvr) cache_key2 = 'greenwave.resources:CachedResults|koji_build {} None'.format(nvr) @@ -397,13 +375,7 @@ def test_consume_compose_id_result( } } } - hub = mock.MagicMock() - hub.config = { - 'environment': 'environment', - 'topic_prefix': 'topic_prefix', - } - handler = resultsdb.ResultsDBHandler(hub) - assert handler.topic == ['topic_prefix.environment.taskotron.result.new'] + handler = create_resultdb_handler() handler.consume(message) # get old decision @@ -478,13 +450,7 @@ def test_consume_legacy_result( } } } - hub = mock.MagicMock() - hub.config = { - 'environment': 'environment', - 'topic_prefix': 'topic_prefix', - } - handler = resultsdb.ResultsDBHandler(hub) - assert handler.topic == ['topic_prefix.environment.taskotron.result.new'] + handler = create_resultdb_handler() handler.consume(message) # get old decision @@ -617,13 +583,7 @@ def test_no_message_for_nonapplicable_policies( } } } - hub = mock.MagicMock() - hub.config = { - 'environment': 'environment', - 'topic_prefix': 'topic_prefix', - } - handler = resultsdb.ResultsDBHandler(hub) - assert handler.topic == ['topic_prefix.environment.taskotron.result.new'] + handler = create_resultdb_handler() handler.consume(message) # No message should be published as the decision is unchanged since we # are still missing the required tests. @@ -749,13 +709,7 @@ def test_consume_new_result_container_image( } } } - hub = mock.MagicMock() - hub.config = { - 'environment': 'environment', - 'topic_prefix': 'topic_prefix', - } - handler = resultsdb.ResultsDBHandler(hub) - assert handler.topic == ['topic_prefix.environment.taskotron.result.new'] + handler = create_resultdb_handler() handler.consume(message) # get old decision diff --git a/greenwave/consumers/resultsdb.py b/greenwave/consumers/resultsdb.py index 3bca365..56201ec 100644 --- a/greenwave/consumers/resultsdb.py +++ b/greenwave/consumers/resultsdb.py @@ -154,7 +154,7 @@ class ResultsDBHandler(fedmsg.consumers.FedmsgConsumer): config_key = 'resultsdb_handler' - def __init__(self, hub, *args, **kwargs): + def __init__(self, hub, config_obj=None, *args, **kwargs): """ Initialize the ResultsDBHandler, subscribing it to the appropriate topics. @@ -171,7 +171,7 @@ class ResultsDBHandler(fedmsg.consumers.FedmsgConsumer): super(ResultsDBHandler, self).__init__(hub, *args, **kwargs) - self.flask_app = greenwave.app_factory.create_app() + self.flask_app = greenwave.app_factory.create_app(config_obj) self.cache = self.flask_app.cache log.info('Greenwave resultsdb handler listening on: %s', self.topic)