Skip to content

Commit

Permalink
fix: error msg for compliance apiv2 assign/unassign (#4328)
Browse files Browse the repository at this point in the history
- RHINENG-14834
- N/A to "rhel_6" branch

Signed-off-by: Xiangce Liu <[email protected]>
(cherry picked from commit 340d7a6)
  • Loading branch information
xiangce committed Jan 9, 2025
1 parent 799c95d commit 8e07eb1
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 12 deletions.
20 changes: 11 additions & 9 deletions insights/specs/datasources/compliance/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import os
import six
import tempfile

from glob import glob
from insights.client.connection import InsightsConnection
from insights.client.constants import InsightsConstants as constants
from logging import getLogger
from re import findall
from sys import exit
import tempfile

from insights.client.connection import InsightsConnection
from insights.client.constants import InsightsConstants as constants
from insights.util.subproc import call
import os
import six


NONCOMPLIANT_STATUS = 2
Expand Down Expand Up @@ -211,21 +213,21 @@ def assignable_policies(self):
print("%-12s %-40s %s" % (is_assigned, policy['id'], policy['title']))
return 0
else:
logger.error("An error has occured while communicating with the API.\n")
logger.error("An error has occurred while communicating with the API.\n")
return constants.sig_kill_bad

def policy_link(self, policy_id, dir):
def policy_link(self, policy_id, opt):
url = "https://{0}/compliance/v2/policies/{1}/systems/{2}"
full_url = url.format(self.config.base_url, policy_id, self.inventory_id)
logger.debug("Fetching: {0}".format(full_url))
response = getattr(self.conn.session, dir)(full_url)
response = getattr(self.conn.session, opt)(full_url)
logger.debug("Content of the response {0} - {1}".format(response, response.content))

if response.status_code == 202:
logger.info("Operation completed successfully.\n")
return 0
else:
logger.error("An error has occured while communicating with the API.\n")
logger.error("Policy ID {0} does not exist.".format(policy_id))
return constants.sig_kill_bad

def get_system_policies(self):
Expand Down
93 changes: 90 additions & 3 deletions insights/tests/datasources/compliance/test_compliance.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from pytest import raises

from insights.client.constants import InsightsConstants as constants
from insights.specs.datasources.compliance import ComplianceClient

ENV_TZ = None
Expand Down Expand Up @@ -292,8 +293,9 @@ def test__inventory_id(config):
assert compliance_client.inventory_id == '12345'


@patch('insights.specs.datasources.compliance.logger')
@patch("insights.client.config.InsightsConfig", base_url='localhost/app', systemid='', proxy=None)
def test_assignable_policies(config):
def test_assignable_policies(config, log):
compliance_client = ComplianceClient(config=config)
compliance_client.conn.session.get = Mock(
return_value=Mock(status_code=200, json=Mock(return_value={'data': ['test']}))
Expand All @@ -310,10 +312,58 @@ def test_assignable_policies(config):
assert compliance_client.assignable_policies() == 0
url = "https://localhost/app/compliance/v2/policies?filter=(os_major_version=9 and os_minor_version=3)"
compliance_client.conn.session.get.assert_called_with(url)
log.warning.assert_not_called()
log.error.assert_not_called()


@patch('insights.specs.datasources.compliance.logger')
@patch("insights.client.config.InsightsConfig", base_url='localhost/app', systemid='', proxy=None)
def test_policy_link_assign(config):
def test_assignable_policies_failed_code(config, log):
compliance_client = ComplianceClient(config=config)
compliance_client.conn.session.get = Mock(
return_value=Mock(status_code=200, json=Mock(return_value={'data': ['test']}))
)
compliance_client._inventory_id = '068040f1-08c8-43e4-949f-7d6470e9111c'
compliance_client.get_system_policies = lambda: []
compliance_client.os_major = 9
compliance_client.os_minor = 3
compliance_client.conn.session.get = Mock(
return_value=Mock(
status_code=422, json=Mock(return_value={'data': [{"id": 123456, "title": "foo"}]})
)
)
assert compliance_client.assignable_policies() == constants.sig_kill_bad
url = "https://localhost/app/compliance/v2/policies?filter=(os_major_version=9 and os_minor_version=3)"
compliance_client.conn.session.get.assert_called_with(url)
log.error.assert_called_with("An error has occurred while communicating with the API.\n")


@patch('insights.specs.datasources.compliance.logger')
@patch("insights.client.config.InsightsConfig", base_url='localhost/app', systemid='', proxy=None)
def test_assignable_policies_failed_empty(config, log):
compliance_client = ComplianceClient(config=config)
compliance_client.conn.session.get = Mock(
return_value=Mock(status_code=200, json=Mock(return_value={'data': ['test']}))
)
compliance_client._inventory_id = '068040f1-08c8-43e4-949f-7d6470e9111c'
compliance_client.get_system_policies = lambda: []
compliance_client.os_major = 9
compliance_client.os_minor = 3
compliance_client.conn.session.get = Mock(
return_value=Mock(
status_code=200, json=Mock(return_value={'data': []})
)
)
assert compliance_client.assignable_policies() == constants.sig_kill_bad
url = "https://localhost/app/compliance/v2/policies?filter=(os_major_version=9 and os_minor_version=3)"
compliance_client.conn.session.get.assert_called_with(url)
log.warning.assert_called_with("System is not assignable to any policy. Create supported policy using the Compliance web UI.\n")
log.error.assert_not_called()


@patch('insights.specs.datasources.compliance.logger')
@patch("insights.client.config.InsightsConfig", base_url='localhost/app', systemid='', proxy=None)
def test_policy_link_assign(config, log):
compliance_client = ComplianceClient(config=config)
compliance_client._inventory_id = '068040f1-08c8-43e4-949f-7d6470e9111c'
policy_id = "d83ddbac-ab56-420b-9e71-878795375af5"
Expand All @@ -325,10 +375,29 @@ def test_policy_link_assign(config):
policy_id, compliance_client.inventory_id
)
compliance_client.conn.session.patch.assert_called_with(url)
log.info.assert_called_with("Operation completed successfully.\n")


@patch('insights.specs.datasources.compliance.logger')
@patch("insights.client.config.InsightsConfig", base_url='localhost/app', systemid='', proxy=None)
def test_policy_link_assign_invalid_policy_id(config, log):
compliance_client = ComplianceClient(config=config)
compliance_client._inventory_id = '068040f1-08c8-43e4-949f-7d6470e9111c'
policy_id = "________-ab56-420b-9e71-878795375af5"
compliance_client.conn.session.patch = Mock(
return_value=Mock(status_code=404, json=Mock(return_value={}))
)
assert compliance_client.policy_link(policy_id, 'patch') == constants.sig_kill_bad
url = "https://localhost/app/compliance/v2/policies/{0}/systems/{1}".format(
policy_id, compliance_client.inventory_id
)
compliance_client.conn.session.patch.assert_called_with(url)
log.error.assert_called_with("Policy ID {0} does not exist.".format(policy_id))


@patch('insights.specs.datasources.compliance.logger')
@patch("insights.client.config.InsightsConfig", base_url='localhost/app', systemid='', proxy=None)
def test_policy_link_unassign(config):
def test_policy_link_unassign(config, log):
compliance_client = ComplianceClient(config=config)
compliance_client.conn.session.delete = Mock(
return_value=Mock(status_code=202, json=Mock(return_value={}))
Expand All @@ -340,3 +409,21 @@ def test_policy_link_unassign(config):
policy_id, compliance_client.inventory_id
)
compliance_client.conn.session.delete.assert_called_with(url)
log.info.assert_called_with("Operation completed successfully.\n")


@patch('insights.specs.datasources.compliance.logger')
@patch("insights.client.config.InsightsConfig", base_url='localhost/app', systemid='', proxy=None)
def test_policy_link_unassign_invalid_policy_id(config, log):
compliance_client = ComplianceClient(config=config)
compliance_client.conn.session.delete = Mock(
return_value=Mock(status_code=404, json=Mock(return_value={}))
)
compliance_client._inventory_id = '068040f1-08c8-43e4-949f-7d6470e9111c'
policy_id = "________-ab56-420b-9e71-878795375af5"
assert compliance_client.policy_link(policy_id, 'delete') == constants.sig_kill_bad
url = "https://localhost/app/compliance/v2/policies/{0}/systems/{1}".format(
policy_id, compliance_client.inventory_id
)
compliance_client.conn.session.delete.assert_called_with(url)
log.error.assert_called_with("Policy ID {0} does not exist.".format(policy_id))

0 comments on commit 8e07eb1

Please sign in to comment.