test_hooks_daemon.py
98 lines
| 3.9 KiB
| text/x-python
|
PythonLexer
r5607 | # Copyright (C) 2010-2024 RhodeCode GmbH | |||
r1 | # | |||
# This program is free software: you can redistribute it and/or modify | ||||
# it under the terms of the GNU Affero General Public License, version 3 | ||||
# (only), as published by the Free Software Foundation. | ||||
# | ||||
# This program is distributed in the hope that it will be useful, | ||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
# GNU General Public License for more details. | ||||
# | ||||
# You should have received a copy of the GNU Affero General Public License | ||||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
# | ||||
# This program is dual-licensed. If you wish to learn more about the | ||||
# RhodeCode Enterprise Edition, including its added features, Support services, | ||||
# and proprietary license terms, please see https://rhodecode.com/licenses/ | ||||
import logging | ||||
import mock | ||||
import pytest | ||||
r5298 | import tempfile | |||
r1 | ||||
r5325 | from rhodecode.lib.hook_daemon import celery_hooks_deamon | |||
r5607 | from rhodecode.lib.hook_daemon import utils as hooks_utils | |||
r5325 | from rhodecode.lib.hook_daemon import base as hook_base | |||
r5607 | ||||
r1 | from rhodecode.tests.utils import assert_message_in_log | |||
class TestHooks(object): | ||||
def test_hooks_can_be_used_as_a_context_processor(self): | ||||
r5607 | hooks = hook_base.Hooks() | |||
r1 | with hooks as return_value: | |||
pass | ||||
assert hooks == return_value | ||||
class TestPrepareHooksDaemon(object): | ||||
r5325 | ||||
r5298 | @pytest.mark.parametrize('protocol', ('celery',)) | |||
r5607 | def test_returns_celery_hooks_callback_daemon_when_celery_protocol_specified(self, protocol): | |||
r5298 | with tempfile.NamedTemporaryFile(mode='w') as temp_file: | |||
r5607 | temp_file.write( | |||
"[app:main]\n" | ||||
"celery.broker_url = redis://redis/0\n" | ||||
"celery.result_backend = redis://redis/0\n" | ||||
) | ||||
r5298 | temp_file.flush() | |||
expected_extras = {'config': temp_file.name} | ||||
r5607 | callback, extras = hooks_utils.prepare_callback_daemon(expected_extras, protocol=protocol) | |||
r5325 | assert isinstance(callback, celery_hooks_deamon.CeleryHooksCallbackDaemon) | |||
r1 | ||||
@pytest.mark.parametrize('protocol, expected_class', ( | ||||
r5607 | ('celery', celery_hooks_deamon.CeleryHooksCallbackDaemon), | |||
r1 | )) | |||
r5607 | def test_returns_real_hooks_callback_daemon_when_protocol_is_specified(self, protocol, expected_class): | |||
with tempfile.NamedTemporaryFile(mode='w') as temp_file: | ||||
temp_file.write( | ||||
"[app:main]\n" | ||||
"celery.broker_url = redis://redis:6379/0\n" | ||||
"celery.result_backend = redis://redis:6379/0\n" | ||||
) | ||||
temp_file.flush() | ||||
expected_extras = { | ||||
'extra1': 'value1', | ||||
'txn_id': 'txnid2', | ||||
'hooks_protocol': protocol.lower(), | ||||
'hooks_config': { | ||||
'broker_url': 'redis://redis:6379/0', | ||||
'result_backend': 'redis://redis:6379/0', | ||||
}, | ||||
'repo_store': '/var/opt/rhodecode_repo_store', | ||||
'repository': 'rhodecode', | ||||
'config': temp_file.name | ||||
} | ||||
from rhodecode import CONFIG | ||||
CONFIG['vcs.svn.redis_conn'] = 'redis://redis:6379/0' | ||||
callback, extras = hooks_utils.prepare_callback_daemon(expected_extras.copy(), protocol=protocol,txn_id='txnid2') | ||||
assert isinstance(callback, expected_class) | ||||
expected_extras['time'] = extras['time'] | ||||
assert extras == expected_extras | ||||
r1 | ||||
Martin Bornhold
|
r968 | @pytest.mark.parametrize('protocol', ( | ||
'invalid', | ||||
'Http', | ||||
'HTTP', | ||||
r5589 | 'celerY' | |||
Martin Bornhold
|
r968 | )) | ||
def test_raises_on_invalid_protocol(self, protocol): | ||||
expected_extras = { | ||||
'extra1': 'value1', | ||||
'hooks_protocol': protocol.lower() | ||||
} | ||||
with pytest.raises(Exception): | ||||
r5607 | callback, extras = hooks_utils.prepare_callback_daemon(expected_extras.copy(), protocol=protocol) | |||