Skip to content

Commit

Permalink
Merge branch 'master' into add_filter_with_count
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangce authored Dec 26, 2024
2 parents 7fe878c + 747e52d commit c33d64e
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 5 deletions.
14 changes: 9 additions & 5 deletions insights/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
109 changes: 109 additions & 0 deletions insights/tests/client/test_log_messages.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit c33d64e

Please sign in to comment.