Skip to content

Commit

Permalink
fix: handler extra header line in JSONParser
Browse files Browse the repository at this point in the history
 - fix json ParseException in FwupdagentSecurity

Signed-off-by: Xiaoxue Wang <[email protected]>
  • Loading branch information
JoySnow committed Dec 20, 2024
1 parent 952f219 commit d5c9c33
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
11 changes: 10 additions & 1 deletion insights/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,16 @@ class JSONParser(Parser, LegacyItemAccess):
def parse_content(self, content):
try:
if isinstance(content, list):
self.data = json.loads('\n'.join(content))
# Find the actual json start line with '{' and '[' as identifier
# To skip any extra lines before the actual json start line
actual_start_index = 0
for idx, _line in enumerate(content):
line = _line.strip()
if line and line.startswith('{') or line.startswith('['):
actual_start_index = idx
break
print("content[actual_start_index:]: ", content[actual_start_index:])
self.data = json.loads('\n'.join(content[actual_start_index:]))
else:
self.data = json.loads(content)
except:
Expand Down
15 changes: 15 additions & 0 deletions insights/tests/parsers/test_fwupdagent.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
from insights.tests import context_wrap

DEVICES = """
INFO: The fwupdagent command is deprecated, use `fwupdmgr --json` instead
WARNING: UEFI firmware can not be updated in legacy BIOS mode
See https://github.com/fwupd/fwupd/wiki/PluginFlag:legacy-bios for more information.
{
"Devices" : [
{
Expand Down Expand Up @@ -104,6 +107,9 @@
"""

SECURITY = """
INFO: The fwupdagent command is deprecated, use `fwupdmgr --json` instead
WARNING: UEFI firmware can not be updated in legacy BIOS mode
See https://github.com/fwupd/fwupd/wiki/PluginFlag:legacy-bios for more information.
{
"HostSecurityAttributes" : [
{
Expand Down Expand Up @@ -150,6 +156,12 @@
This tool can be used from other tools and from shell scripts.
"""

SECURITY_ERROR_3 = """
INFO: The fwupdagent command is deprecated, use `fwupdmgr --json` instead
WARNING: UEFI firmware can not be updated in legacy BIOS mode
See https://github.com/fwupd/fwupd/wiki/PluginFlag:legacy-bios for more information.
""".strip()


def test_devices():
devices = FwupdagentDevices(context_wrap(DEVICES))
Expand All @@ -175,6 +187,9 @@ def test_security():
with pytest.raises(ParseException):
FwupdagentSecurity(context_wrap(SECURITY_ERROR_2))

with pytest.raises(ParseException):
FwupdagentSecurity(context_wrap(SECURITY_ERROR_3))


def test_doc_examples():
env = {
Expand Down
34 changes: 34 additions & 0 deletions insights/tests/test_json_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,24 @@ class MyJsonParser(JSONParser):
[{'a': '1', 'b': '2'}, {'a': '3', 'b': '4'}]
}

JSON_CONTENT_WITH_EXTRA_HEADER_LINES_1 = """
INFO: The fwupdagent command is deprecated, use `fwupdmgr --json` instead
WARNING: UEFI firmware can not be updated in legacy BIOS mode
See https://github.com/fwupd/fwupd/wiki/PluginFlag:legacy-bios for more information.
{"a": [{"b": "x"}, {"b": "y"}]}
""".strip()
JSON_CONTENT_WITH_EXTRA_HEADER_LINES_2 = """
INFO: The fwupdagent command is deprecated, use `fwupdmgr --json` instead
WARNING: UEFI firmware can not be updated in legacy BIOS mode
See https://github.com/fwupd/fwupd/wiki/PluginFlag:legacy-bios for more information.
{}
""".strip()
JSON_CONTENT_WITH_EXTRA_HEADER_LINES_3 = """
INFO: Some info line from a command spec output
WARNING: Some warning line-1 from a command spec output
Some warning line-2 from a command spec output.
""".strip()


def test_json_parser_success():
for jsonstr in json_test_strings:
Expand All @@ -36,3 +54,19 @@ def test_json_parser_null_value():
MyJsonParser(ctx)

assert "Empty input" == ex.value.args[0]


def test_json_parser_with_extra_header_lines():
ctx = context_wrap(JSON_CONTENT_WITH_EXTRA_HEADER_LINES_1)
assert MyJsonParser(ctx).data == {'a': [{'b': 'x'}, {'b': 'y'}]}

ctx = context_wrap(JSON_CONTENT_WITH_EXTRA_HEADER_LINES_2)
assert MyJsonParser(ctx).data == {}
# with pytest.raises(SkipComponent) as ex:
# MyJsonParser(ctx)

ctx = context_wrap(JSON_CONTENT_WITH_EXTRA_HEADER_LINES_3)
with pytest.raises(ParseException) as ex:
MyJsonParser(ctx)

assert "couldn't parse json" in ex.value.args[0]

0 comments on commit d5c9c33

Please sign in to comment.