<commit_before># Licensed to the StackStorm, Inc ('StackStorm') under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import tempfile

from st2common import service_setup

from st2tests.base import CleanFilesTestCase
from st2tests import config

__all__ = [
    'ServiceSetupTestCase'
]

MOCK_LOGGING_CONFIG_INVALID_LOG_LEVEL = """
[loggers]
keys=root

[handlers]
keys=consoleHandler

[formatters]
keys=simpleConsoleFormatter

[logger_root]
level=invalid_log_level
handlers=consoleHandler

[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleConsoleFormatter
args=(sys.stdout,)

[formatter_simpleConsoleFormatter]
class=st2common.logging.formatters.ConsoleLogFormatter
format=%(asctime)s %(levelname)s [-] %(message)s
datefmt=
""".strip()


class ServiceSetupTestCase(CleanFilesTestCase):
    def test_no_logging_config_found(self):
        def mock_get_logging_config_path():
            return ''

        config.get_logging_config_path = mock_get_logging_config_path

        expected_msg = "No section: .*"
        self.assertRaisesRegexp(Exception, expected_msg,
                                service_setup.setup, service='api',
                                config=config,
                                setup_db=False, register_mq_exchanges=False,
                                register_signal_handlers=False,
                                register_internal_trigger_types=False,
                                run_migrations=False)

    def test_invalid_log_level_friendly_error_message(self):
        _, mock_logging_config_path = tempfile.mkstemp()
        self.to_delete_files.append(mock_logging_config_path)

        with open(mock_logging_config_path, 'w') as fp:
            fp.write(MOCK_LOGGING_CONFIG_INVALID_LOG_LEVEL)

        def mock_get_logging_config_path():
            return mock_logging_config_path

        config.get_logging_config_path = mock_get_logging_config_path

        expected_msg = 'Invalid log level selected. Log level names need to be all uppercase'
        self.assertRaisesRegexp(KeyError, expected_msg,
                                service_setup.setup, service='api',
                                config=config,
                                setup_db=False, register_mq_exchanges=False,
                                register_signal_handlers=False,
                                register_internal_trigger_types=False,
                                run_migrations=False)
<commit_msg>Add tests for queue predeclaration.<commit_after># Licensed to the StackStorm, Inc ('StackStorm') under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import tempfile

import mock
from oslo_config import cfg

from st2common import service_setup
from st2common.transport.bootstrap_utils import register_exchanges
from st2common.transport.bootstrap_utils import QUEUES

from st2tests.base import CleanFilesTestCase
from st2tests import config

__all__ = [
    'ServiceSetupTestCase'
]

MOCK_LOGGING_CONFIG_INVALID_LOG_LEVEL = """
[loggers]
keys=root

[handlers]
keys=consoleHandler

[formatters]
keys=simpleConsoleFormatter

[logger_root]
level=invalid_log_level
handlers=consoleHandler

[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleConsoleFormatter
args=(sys.stdout,)

[formatter_simpleConsoleFormatter]
class=st2common.logging.formatters.ConsoleLogFormatter
format=%(asctime)s %(levelname)s [-] %(message)s
datefmt=
""".strip()


class ServiceSetupTestCase(CleanFilesTestCase):
    def test_no_logging_config_found(self):
        def mock_get_logging_config_path():
            return ''

        config.get_logging_config_path = mock_get_logging_config_path

        expected_msg = "No section: .*"
        self.assertRaisesRegexp(Exception, expected_msg,
                                service_setup.setup, service='api',
                                config=config,
                                setup_db=False, register_mq_exchanges=False,
                                register_signal_handlers=False,
                                register_internal_trigger_types=False,
                                run_migrations=False)

    def test_invalid_log_level_friendly_error_message(self):
        _, mock_logging_config_path = tempfile.mkstemp()
        self.to_delete_files.append(mock_logging_config_path)

        with open(mock_logging_config_path, 'w') as fp:
            fp.write(MOCK_LOGGING_CONFIG_INVALID_LOG_LEVEL)

        def mock_get_logging_config_path():
            return mock_logging_config_path

        config.get_logging_config_path = mock_get_logging_config_path

        expected_msg = 'Invalid log level selected. Log level names need to be all uppercase'
        self.assertRaisesRegexp(KeyError, expected_msg,
                                service_setup.setup, service='api',
                                config=config,
                                setup_db=False, register_mq_exchanges=False,
                                register_signal_handlers=False,
                                register_internal_trigger_types=False,
                                run_migrations=False)

    @mock.patch('kombu.Queue.declare')
    def test_register_exchanges_predeclare_queues(self, mock_declare):
        # Verify that queues are correctly pre-declared if the corresponding config option is set

        # Pre-declaration is disabled
        self.assertEqual(mock_declare.call_count, 0)
        cfg.CONF.set_override(group='messaging', name='predeclare_queues', override=False)

        register_exchanges()
        self.assertEqual(mock_declare.call_count, 0)

        # Pre-declaration is enabled
        self.assertEqual(mock_declare.call_count, 0)
        cfg.CONF.set_override(group='messaging', name='predeclare_queues', override=True)

        register_exchanges()
        self.assertEqual(mock_declare.call_count, len(QUEUES))

{'fraction_non_alphanumeric': 0.06568030973451328, 'fraction_numerical': 0.002903761061946903, 'mean_word_length': 2.9741758241758243, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4141727', 'n_tokens_mistral': 1951, 'n_tokens_neox': 1890, 'n_words': 481}