Skip to content

Commit

Permalink
chore: Skip empty spec only when collecting (#4183)
Browse files Browse the repository at this point in the history
- When running plugins, for empty specs, print debug
  information only, but do not raise Exceptions
- The same as before, the empty spec will be collected,
  and the reason will be recorded as an ContentException
  into its meta_data JSON
- JIRA: RHINENG-11843
- And change to show the full real path instead of relative path
  in the debug message
- Fix the relevant tests of compliance ds

Signed-off-by: Xiangce Liu <[email protected]>
  • Loading branch information
xiangce authored Aug 9, 2024
1 parent 66c6081 commit d4b092a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
7 changes: 5 additions & 2 deletions insights/core/spec_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def _clean_content(self):
obf_funcs=self.cleaner.get_obfuscate_functions(self.relative_path, no_obf),
no_redact=no_red)
if len(self._content) == 0:
log.debug("Skipping %s due to empty after cleaning", "/" + self.relative_path)
log.debug("Skipping %s due to empty after cleaning", self.path)
raise ContentException("Empty after cleaning: %s" % self.path)
else:
log.debug("Skipping cleaning %s", "/" + self.relative_path)
Expand All @@ -117,7 +117,10 @@ def content(self):
raise

if len(self._content) == 0:
raise ContentException("Empty (after filtering): %s" % self.path)
log.debug("File is empty (after filtering): %s", self.path)
if isinstance(self.ctx, HostContext):
# Do not collect empty spec
raise ContentException("Empty (after filtering): %s" % self.path)

return self._content

Expand Down
24 changes: 16 additions & 8 deletions insights/tests/datasources/compliance/test_compliance_ds.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from pytest import raises
from tempfile import NamedTemporaryFile

from insights.core.exceptions import SkipComponent, ContentException
from insights.core.exceptions import SkipComponent
from insights.parsers.installed_rpms import InstalledRpms
from insights.parsers.os_release import OsRelease
from insights.parsers.redhat_release import RedhatRelease
Expand Down Expand Up @@ -84,23 +84,25 @@ def test_package_check():
assert "openscap" in msg


@patch("insights.core.spec_factory.log")
@patch("insights.specs.datasources.compliance.InsightsConnection")
@patch("insights.specs.datasources.compliance.ComplianceClient.get_initial_profiles",
return_value=[{'attributes': {'ref_id': 'foo', 'tailored': False}}])
@patch("insights.specs.datasources.compliance.ComplianceClient.get_profiles_matching_os",
return_value=[])
@patch("insights.specs.datasources.compliance.ComplianceClient.run_scan",
return_value=None)
@patch("insights.specs.datasources.compliance.ComplianceClient.find_scap_policy",
return_value='test.xml')
@patch("insights.client.config.InsightsConfig", base_url='localhost/app', systemid='', proxy=None, compressor='gz', obfuscate=False)
def test_compliance_ds(config, run_scan, pm_os, init_p, conn):
def test_compliance_ds(config, _xml, run_scan, pm_os, init_p, conn, log):
conn.session.get = Mock(return_value=Mock(status_code=200, json=Mock(return_value={'data': [{'attributes': 'data'}]})))
broker = {os_version: ['8', '10'], package_check: '0.1.73', 'client_config': config}
ret = compliance(broker)
assert len(ret) == 1
assert ret[0].relative_path == 'oscap_results-foo.xml'
with raises(ContentException) as ce:
ret[0].content
assert "Empty content" in str(ce)
assert ret[0].content == []
log.debug.assert_called_once_with('File is empty (after filtering): %s', '/' + ret[0].relative_path)


@patch("insights.specs.datasources.compliance.compliance_ds.NamedTemporaryFile")
Expand All @@ -111,8 +113,10 @@ def test_compliance_ds(config, run_scan, pm_os, init_p, conn):
return_value=[])
@patch("insights.specs.datasources.compliance.ComplianceClient.run_scan",
return_value=None)
@patch("insights.specs.datasources.compliance.ComplianceClient.find_scap_policy",
return_value='test.xml')
@patch("insights.client.config.InsightsConfig", base_url='localhost/app', systemid='', proxy=None, compressor='gz', obfuscate=True)
def test_compliance_ds_with_obfuscation(config, run_scan, pm_os, init_p, conn, ntmpf):
def test_compliance_ds_with_obfuscation(config, _xml, run_scan, pm_os, init_p, conn, ntmpf):
tmp_file = NamedTemporaryFile(mode='w', delete=False)
tmp_file.write("""
<xml>
Expand Down Expand Up @@ -181,8 +185,10 @@ def test_compliance_ds_with_obfuscation(config, run_scan, pm_os, init_p, conn, n
return_value=[])
@patch("insights.specs.datasources.compliance.ComplianceClient.run_scan",
return_value=None)
@patch("insights.specs.datasources.compliance.ComplianceClient.find_scap_policy",
return_value='test.xml')
@patch("insights.client.config.InsightsConfig", base_url='localhost/app', systemid='', proxy=None, compressor='gz', obfuscate=True, obfuscate_hostname=True)
def test_compliance_ds_with_hostname_obfuscation(config, run_scan, pm_os, init_p, conn, ntmpf):
def test_compliance_ds_with_hostname_obfuscation(config, _xml, run_scan, pm_os, init_p, conn, ntmpf):
tmp_file = NamedTemporaryFile(mode='w', delete=False)
tmp_file.write("""
<xml>
Expand Down Expand Up @@ -266,8 +272,10 @@ def test_compliance_ds_with_hostname_obfuscation(config, run_scan, pm_os, init_p
return_value=[])
@patch("insights.specs.datasources.compliance.ComplianceClient.run_scan",
return_value=None)
@patch("insights.specs.datasources.compliance.ComplianceClient.find_scap_policy",
return_value='test.xml')
@patch("insights.client.config.InsightsConfig", base_url='localhost/app', systemid='', proxy=None, compressor='gz')
def test_compliance_ds_with_results_repaired(config, run_scan, pm_os, init_p, conn, ntmpf):
def test_compliance_ds_with_results_repaired(config, _xml, run_scan, pm_os, init_p, conn, ntmpf):
tmp_file = NamedTemporaryFile(mode='w', delete=False)
tmp_file.write("""
<xml>
Expand Down

0 comments on commit d4b092a

Please sign in to comment.