diff --git a/insights/client/client.py b/insights/client/client.py index 4c7a102e2c..74d623e94d 100644 --- a/insights/client/client.py +++ b/insights/client/client.py @@ -326,11 +326,15 @@ def collect(config): pc = InsightsUploadConf(config) dc = CoreCollector(config) - logger.info('Starting to collect Insights data for %s' % determine_hostname(config.display_name)) - - dc.run_collection(pc.get_rm_conf(), - get_branch_info(config), - pc.create_report()) + rm_conf = {} + # Do not print collection relevant messages for compliance apiv2 options + if not (config.compliance_policies or config.compliance_assign or config.compliance_unassign): + rm_conf = pc.get_rm_conf() + logger.info( + 'Starting to collect Insights data for %s' % determine_hostname(config.display_name) + ) + + dc.run_collection(rm_conf, get_branch_info(config), pc.create_report()) return dc.done() diff --git a/insights/tests/client/test_log_messages.py b/insights/tests/client/test_log_messages.py new file mode 100644 index 0000000000..fe76e34590 --- /dev/null +++ b/insights/tests/client/test_log_messages.py @@ -0,0 +1,109 @@ +# -*- coding: UTF-8 -*- +try: + from unittest.mock import patch +except Exception: + from mock import patch + +from insights.client.client import collect +from insights.client.config import InsightsConfig +from insights.client.utilities import determine_hostname + + +def patch_get_branch_info(): + """ + Sets a static response to get_branch_info method. + """ + + def decorator(old_function): + patcher = patch("insights.client.client.get_branch_info") + return patcher(old_function) + + return decorator + + +def patch_get_rm_conf(): + """ + Mocks InsightsUploadConf.get_rm_conf so it returns a fixed configuration. + """ + + def decorator(old_function): + patcher = patch("insights.client.client.InsightsUploadConf.get_rm_conf") + return patcher(old_function) + + return decorator + + +def patch_core_collector(): + """ + Replaces CoreCollector with a dummy mock. + """ + + def decorator(old_function): + patcher = patch("insights.client.client.CoreCollector") + return patcher(old_function) + + return decorator + + +@patch_core_collector() +@patch_get_rm_conf() +@patch_get_branch_info() +@patch('insights.client.client.logger') +def test_log_msgs_general(log, get_branch_info, get_rm_conf, core_collector): + config = InsightsConfig() + collect(config) + get_branch_info.assert_called_once_with(config) + get_rm_conf.assert_called_once_with() + log.info.assert_called_once_with( + 'Starting to collect Insights data for %s' % determine_hostname(config.display_name) + ) + + +@patch_core_collector() +@patch_get_rm_conf() +@patch_get_branch_info() +@patch('insights.client.client.logger') +def test_log_msgs_compliance(log, get_branch_info, get_rm_conf, core_collector): + config = InsightsConfig(compliance=True) + collect(config) + get_branch_info.assert_called_once_with(config) + get_rm_conf.assert_called_once_with() + log.info.assert_called_once_with( + 'Starting to collect Insights data for %s' % determine_hostname(config.display_name) + ) + + +@patch_core_collector() +@patch_get_rm_conf() +@patch_get_branch_info() +@patch('insights.client.client.logger') +def test_log_msgs_compliance_policies(log, get_branch_info, get_rm_conf, core_collector): + config = InsightsConfig(compliance_policies=True) + collect(config) + get_branch_info.assert_called_once_with(config) + get_rm_conf.assert_not_called() + log.info.assert_not_called() + + +@patch_core_collector() +@patch_get_rm_conf() +@patch_get_branch_info() +@patch('insights.client.client.logger') +def test_log_msgs_compliance_assign(log, get_branch_info, get_rm_conf, core_collector): + config = InsightsConfig(compliance_assign=['abc']) + collect(config) + get_branch_info.assert_called_once_with(config) + get_rm_conf.assert_not_called() + log.info.assert_not_called() + + +@patch_core_collector() +@patch_get_rm_conf() +@patch_get_branch_info() +@patch('insights.client.client.logger') +def test_log_msgs_compliance_unassign(log, get_branch_info, get_rm_conf, core_collector): + config = InsightsConfig(compliance_unassign=['abc']) + collect(config) + get_branch_info.assert_called_once_with(config) + get_rm_conf.assert_not_called() + log.info.assert_not_called()