# Copyright (C) 2010-2024 RhodeCode GmbH # # 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 import tempfile from rhodecode.lib.hook_daemon import celery_hooks_deamon from rhodecode.lib.hook_daemon import utils as hooks_utils from rhodecode.lib.hook_daemon import base as hook_base from rhodecode.tests.utils import assert_message_in_log class TestHooks(object): def test_hooks_can_be_used_as_a_context_processor(self): hooks = hook_base.Hooks() with hooks as return_value: pass assert hooks == return_value class TestPrepareHooksDaemon(object): @pytest.mark.parametrize('protocol', ('celery',)) def test_returns_celery_hooks_callback_daemon_when_celery_protocol_specified(self, protocol): with tempfile.NamedTemporaryFile(mode='w') as temp_file: temp_file.write( "[app:main]\n" "celery.broker_url = redis://redis/0\n" "celery.result_backend = redis://redis/0\n" ) temp_file.flush() expected_extras = {'config': temp_file.name} callback, extras = hooks_utils.prepare_callback_daemon(expected_extras, protocol=protocol) assert isinstance(callback, celery_hooks_deamon.CeleryHooksCallbackDaemon) @pytest.mark.parametrize('protocol, expected_class', ( ('celery', celery_hooks_deamon.CeleryHooksCallbackDaemon), )) 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 @pytest.mark.parametrize('protocol', ( 'invalid', 'Http', 'HTTP', 'celerY' )) def test_raises_on_invalid_protocol(self, protocol): expected_extras = { 'extra1': 'value1', 'hooks_protocol': protocol.lower() } with pytest.raises(Exception): callback, extras = hooks_utils.prepare_callback_daemon(expected_extras.copy(), protocol=protocol)