Skip to content

Commit

Permalink
fix: Soscleaner fix (#3502)
Browse files Browse the repository at this point in the history
* Move results object to ReportItem

Signed-off-by: Štěpán Tomsa <[email protected]>

* Go classes

Signed-off-by: Štěpán Tomsa <[email protected]>

* Start the Glob File test

Signed-off-by: Štěpán Tomsa <[email protected]>

* Make tests saner

Signed-off-by: Štěpán Tomsa <[email protected]>

* Fix SOSCleaner for glob files

Signed-off-by: Štěpán Tomsa <[email protected]>

* Revert an unnecessary whitespace change

Signed-off-by: Štěpán Tomsa <[email protected]>

* Remove an unused import

This fixes flake8 violations.

Signed-off-by: Štěpán Tomsa <[email protected]>
  • Loading branch information
Glutexo authored Sep 19, 2022
1 parent a316c02 commit eb852e1
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 21 deletions.
7 changes: 6 additions & 1 deletion insights/contrib/soscleaner.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,12 @@ def _excluded_files(self):
with open(file_path) as file:
meta_data = json.load(file)
if meta_data["name"] in self.excluded_specs:
excluded_files.append(meta_data["results"]["object"]["relative_path"])
if isinstance(meta_data["results"], list):
results = meta_data["results"]
else:
results = [meta_data["results"]]
relative_paths = [result["object"]["relative_path"] for result in results]
excluded_files.extend(relative_paths)
return excluded_files

def clean_report(self, options, sosreport): # pragma: no cover
Expand Down
86 changes: 66 additions & 20 deletions insights/tests/test_soscleaner.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,45 @@
from insights.client.data_collector import CleanOptions
from insights.contrib.soscleaner import SOSCleaner

ReportItem = namedtuple("ReportItem", ("name", "relative_path"))

class ReportFile:
def __init__(self, report_item):
self.report_item = report_item

@staticmethod
def result(relative_path):
return {
"object": {
"relative_path": relative_path
}
}

@property
def file_name(self):
return "%s.json" % self.report_item.name

@property
def contents(self):
return {
"name": self.report_item.name,
"results": self.report_item.results,
}


class ReportItemSimpleFile(namedtuple("ReportItemSimpleFile", ("name", "relative_path"))):
@property
def results(self):
return ReportFile.result(self.relative_path)

@property
def relative_paths(self):
return [self.relative_path]


class ReportItemGlobFile(namedtuple("ReportItemGlobFile", ("name", "relative_paths"))):
@property
def results(self):
return [ReportFile.result(relative_path) for relative_path in self.relative_paths]


def _soscleaner():
Expand All @@ -38,18 +76,10 @@ def _mock_report_dir(report_items):
mkdir(path)

for report_item in report_items:
json_path = join(
meta_data_path, "%s.json" % report_item.name
)
report_file = ReportFile(report_item)
json_path = join(meta_data_path, report_file.file_name)
with open(json_path, "w") as json_file:
meta_data_obj = {
"name": report_item.name,
"results": {
"object": {
"relative_path": report_item.relative_path
}
}
}
meta_data_obj = report_file.contents
dump(meta_data_obj, json_file)

yield tmpdir
Expand Down Expand Up @@ -171,16 +201,16 @@ def test_excluded_specs():
assert spec.find("insights.specs.Specs.") == 0


def test_excluded_files():
def test_excluded_files_simple_file():
report_items = [
ReportItem(
ReportItemSimpleFile(
"insights.specs.Specs.installed_rpms",
"insights_commands/rpm_-qa_--qf_name_NAME_epoch_EPOCH_"
"version_VERSION_release_RELEASE_arch_ARCH_"
"installtime_INSTALLTIME_date_buildtime_BUILDTIME_"
"vendor_VENDOR_buildhost_BUILDHOST_sigpgp_SIGPGP_pgpsig"
),
ReportItem(
ReportItemSimpleFile(
"insights.specs.Specs.ip_addr",
"insights_commands/ip_addr"
)
Expand All @@ -191,14 +221,30 @@ def test_excluded_files():
soscleaner.dir_path = tmpdir
actual = soscleaner._excluded_files()

expected = [
report_item.relative_path
for report_item in report_items
if report_item.name in soscleaner.excluded_specs
]
expected = report_items[0].relative_paths
assert actual == expected


def test_excluded_files_glob_file():
report_items = [
ReportItemGlobFile(
"insights.specs.Specs.dnf_modules",
["etc/dnf/modules.d/ruby.module"]
),
ReportItemGlobFile(
"insights.specs.Specs.dnsmasq_config",
["etc/dnsmasq.conf"]
)
]

soscleaner = _soscleaner()
with _mock_report_dir(report_items) as tmpdir:
soscleaner.dir_path = tmpdir
actual = soscleaner._excluded_files()

assert actual == report_items[0].relative_paths


@patch("insights.contrib.soscleaner.SOSCleaner._clean_file")
@patch("insights.contrib.soscleaner.SOSCleaner._excluded_files")
@patch("insights.contrib.soscleaner.SOSCleaner._extract_sosreport")
Expand Down

0 comments on commit eb852e1

Please sign in to comment.