Skip to content

Commit a3b9a20

Browse files
Merge pull request #72 from amd/alex_package_fix
PackagePlugin update: fix for incomplete err message
2 parents 5a8964b + 09668cf commit a3b9a20

File tree

2 files changed

+76
-10
lines changed

2 files changed

+76
-10
lines changed

nodescraper/plugins/inband/package/package_analyzer.py

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def regex_version_data(
4444
package_data: dict[str, str],
4545
key_search: re.Pattern[str],
4646
value_search: Optional[Pattern[str]],
47-
) -> bool:
47+
) -> tuple[bool, list[tuple[str, str, str]]]:
4848
"""Searches the package values for the key and value search patterns
4949
5050
Args:
@@ -53,10 +53,12 @@ def regex_version_data(
5353
value_search (Optional[Pattern[str]]): a compiled regex pattern to search for the package version, if None then any version is accepted
5454
5555
Returns:
56-
bool: A boolean indicating if the value was found
56+
tuple: (value_found, version_mismatches) where value_found is a bool and
57+
version_mismatches is a list of (package_name, expected_pattern, found_version) tuples
5758
"""
5859

5960
value_found = False
61+
version_mismatches = []
6062
for name, version in package_data.items():
6163
self.logger.debug("Package data: %s, %s", name, version)
6264
key_search_res = key_search.search(name)
@@ -66,6 +68,7 @@ def regex_version_data(
6668
continue
6769
value_search_res = value_search.search(version)
6870
if not value_search_res:
71+
version_mismatches.append((name, value_search.pattern, version))
6972
self._log_event(
7073
EventCategory.APPLICATION,
7174
f"Package {key_search.pattern} Version Mismatch, Expected {value_search.pattern} but found {version}",
@@ -77,7 +80,7 @@ def regex_version_data(
7780
"found_version": version,
7881
},
7982
)
80-
return value_found
83+
return value_found, version_mismatches
8184

8285
def package_regex_search(
8386
self, package_data: dict[str, str], exp_package_data: dict[str, Optional[str]]
@@ -87,16 +90,23 @@ def package_regex_search(
8790
Args:
8891
package_data (dict[str, str]): a dictionary of package names and versions
8992
exp_package_data (dict[str, Optional[str]]): a dictionary of expected package names and versions
93+
94+
Returns:
95+
tuple: (not_found_keys, regex_errors, version_mismatches) containing lists of errors
9096
"""
9197
not_found_keys = []
98+
regex_errors = []
99+
version_mismatches = []
100+
92101
for exp_key, exp_value in exp_package_data.items():
93102
try:
94103
if exp_value is not None:
95104
value_search = re.compile(exp_value)
96105
else:
97106
value_search = None
98107
key_search = re.compile(exp_key)
99-
except re.error:
108+
except re.error as e:
109+
regex_errors.append((exp_key, exp_value, str(e)))
100110
self._log_event(
101111
EventCategory.RUNTIME,
102112
f"Regex Compile Error either {exp_key} {exp_value}",
@@ -108,10 +118,13 @@ def package_regex_search(
108118
)
109119
continue
110120

111-
key_found = self.regex_version_data(package_data, key_search, value_search)
121+
key_found, mismatches = self.regex_version_data(package_data, key_search, value_search)
122+
123+
# Collect version mismatches
124+
version_mismatches.extend(mismatches)
112125

113126
if not key_found:
114-
not_found_keys.append(exp_key)
127+
not_found_keys.append((exp_key, exp_value))
115128
self._log_event(
116129
EventCategory.APPLICATION,
117130
f"Package {exp_key} not found in the package list",
@@ -123,7 +136,8 @@ def package_regex_search(
123136
"found_version": None,
124137
},
125138
)
126-
return not_found_keys
139+
140+
return not_found_keys, regex_errors, version_mismatches
127141

128142
def package_exact_match(
129143
self, package_data: dict[str, str], exp_package_data: dict[str, Optional[str]]
@@ -190,9 +204,43 @@ def analyze_data(
190204
return self.result
191205

192206
if args.regex_match:
193-
not_found_keys = self.package_regex_search(data.version_info, args.exp_package_ver)
194-
self.result.message = f"Packages not found: {not_found_keys}"
195-
self.result.status = ExecutionStatus.ERROR
207+
not_found_keys, regex_errors, version_mismatches = self.package_regex_search(
208+
data.version_info, args.exp_package_ver
209+
)
210+
211+
# Adding details for err message
212+
error_parts = []
213+
if not_found_keys:
214+
packages_detail = ", ".join(
215+
[
216+
f"'{pkg}' (expected version: {ver if ver else 'any'})"
217+
for pkg, ver in not_found_keys
218+
]
219+
)
220+
error_parts.append(f"Packages not found: {packages_detail}")
221+
222+
if regex_errors:
223+
regex_detail = ", ".join(
224+
[f"'{pkg}' pattern (version: {ver})" for pkg, ver, _ in regex_errors]
225+
)
226+
error_parts.append(f"Regex compile errors: {regex_detail}")
227+
228+
if version_mismatches:
229+
version_detail = ", ".join(
230+
[
231+
f"'{pkg}' (expected: {exp}, found: {found})"
232+
for pkg, exp, found in version_mismatches
233+
]
234+
)
235+
error_parts.append(f"Version mismatches: {version_detail}")
236+
237+
total_errors = len(not_found_keys) + len(regex_errors) + len(version_mismatches)
238+
if total_errors > 0:
239+
self.result.message = f"{'; '.join(error_parts)}"
240+
self.result.status = ExecutionStatus.ERROR
241+
else:
242+
self.result.message = "All packages found and versions matched"
243+
self.result.status = ExecutionStatus.OK
196244
else:
197245
self.logger.info("Expected packages: %s", list(args.exp_package_ver.keys()))
198246
not_found_match, not_found_version = self.package_exact_match(

test/unit/plugin/test_package_analyzer.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,22 @@ def test_data_version_regex(package_analyzer, default_data_lib):
8989
regex_match=True,
9090
)
9191
res = package_analyzer.analyze_data(default_data_lib, args=args)
92+
assert res.status == ExecutionStatus.OK
93+
assert res.message == "All packages found and versions matched"
94+
95+
96+
def test_data_multiple_errors_regex(package_analyzer, default_data_lib):
97+
"""Test that detailed error messages are shown for multiple package errors"""
98+
args = PackageAnalyzerArgs(
99+
exp_package_ver={
100+
"missing-package": None,
101+
"test-ubuntu-package\\.x86_64": "2\\.\\d+",
102+
"another-missing": "1\\.0",
103+
},
104+
regex_match=True,
105+
)
106+
res = package_analyzer.analyze_data(default_data_lib, args=args)
92107
assert res.status == ExecutionStatus.ERROR
108+
assert "missing-package" in res.message
109+
assert "another-missing" in res.message
110+
assert len(res.events) == 3

0 commit comments

Comments
 (0)