From d91b4f688a4ee0ec491a5ec16e7d607e0cb9d520 Mon Sep 17 00:00:00 2001 From: Xiaoxue Wang Date: Wed, 18 Dec 2024 17:55:12 +0800 Subject: [PATCH 1/3] fix: json ParseException in FwupdagentSecurity Signed-off-by: Xiaoxue Wang --- insights/parsers/fwupdagent.py | 22 ++++++++++++++++--- insights/tests/parsers/test_fwupdagent.py | 26 ++++++++++++++++++++++- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/insights/parsers/fwupdagent.py b/insights/parsers/fwupdagent.py index 7a6695b52d..8bd4416a72 100644 --- a/insights/parsers/fwupdagent.py +++ b/insights/parsers/fwupdagent.py @@ -123,13 +123,21 @@ class FwupdagentDevices(CommandParser, JSONParser): >>> len(devices["Devices"]) 2 """ - pass + def _handle_content(self, context): + # Find the actual json start line + # To skip the "INFO:", "WARNING:" lines at the top of the command output + index = 0 + for idx, line in enumerate(context.content): + if line and line.strip().startswith('{'): + index = idx + break + self.parse_content(context.content[index:]) @parser(Specs.fw_security) class FwupdagentSecurity(CommandParser, JSONParser): """ - Class ``FwupdagentSecurity`` parses the output of the ``/bin/fwupdagent get-devices`` command. + Class ``FwupdagentSecurity`` parses the output of the ``/bin/fwupdagent security --force`` command. Attributes: data (dict): The parsed output of the command. @@ -164,4 +172,12 @@ class FwupdagentSecurity(CommandParser, JSONParser): >>> len(security["HostSecurityAttributes"]) 2 """ - pass + def _handle_content(self, context): + # Find the actual json start line + # To skip the "INFO:", "WARNING:" lines at the top of the command output + index = 0 + for idx, line in enumerate(context.content): + if line and line.strip().startswith('{'): + index = idx + break + self.parse_content(context.content[index:]) diff --git a/insights/tests/parsers/test_fwupdagent.py b/insights/tests/parsers/test_fwupdagent.py index 984dbf6568..285fcc2f82 100644 --- a/insights/tests/parsers/test_fwupdagent.py +++ b/insights/tests/parsers/test_fwupdagent.py @@ -2,12 +2,15 @@ import doctest import pytest -from insights.core.exceptions import ParseException +from insights.core.exceptions import ParseException, SkipComponent from insights.parsers import fwupdagent from insights.parsers.fwupdagent import FwupdagentDevices, FwupdagentSecurity 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" : [ { @@ -103,7 +106,16 @@ } """ +DEVICES_ERROR_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. +""".strip() + 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" : [ { @@ -150,6 +162,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)) @@ -160,6 +178,9 @@ def test_devices(): assert devices["Devices"][1]["Name"] == "USB3.0 Hub" assert devices["Devices"][1]["Version"] == "3.114" + with pytest.raises(SkipComponent): + FwupdagentDevices(context_wrap(DEVICES_ERROR_1)) + def test_security(): security = FwupdagentSecurity(context_wrap(SECURITY)) @@ -175,6 +196,9 @@ def test_security(): with pytest.raises(ParseException): FwupdagentSecurity(context_wrap(SECURITY_ERROR_2)) + with pytest.raises(SkipComponent): + FwupdagentSecurity(context_wrap(SECURITY_ERROR_3)) + def test_doc_examples(): env = { From 175ad3655539e31a0ce7b426f4908b1633f51dd7 Mon Sep 17 00:00:00 2001 From: Xiaoxue Wang Date: Wed, 18 Dec 2024 17:58:26 +0800 Subject: [PATCH 2/3] fix: remove the collection of unsed spec fw_devices Signed-off-by: Xiaoxue Wang --- insights/collect.py | 2 +- insights/specs/default.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/insights/collect.py b/insights/collect.py index b99d4a01ba..d9418b239b 100755 --- a/insights/collect.py +++ b/insights/collect.py @@ -184,7 +184,7 @@ - name: insights.combiners.sap.Sap enabled: true - # needed for fw_devices and fw_security specs + # needed for fw_security specs - name: insights.parsers.virt_what.VirtWhat enabled: true - name: insights.combiners.virt_what.VirtWhat diff --git a/insights/specs/default.py b/insights/specs/default.py index 9e842311ab..21204cb4fa 100644 --- a/insights/specs/default.py +++ b/insights/specs/default.py @@ -356,7 +356,6 @@ class DefaultSpecs(Specs): firewalld_conf = simple_file("/etc/firewalld/firewalld.conf") foreman_production_log = simple_file("/var/log/foreman/production.log") fstab = simple_file("/etc/fstab") - fw_devices = simple_command("/bin/fwupdagent get-devices", deps=[IsBareMetal]) fw_security = simple_command("/bin/fwupdagent security --force", deps=[IsBareMetal]) galera_cnf = first_file( [ From 198093ca14fdcaa5325ea4345c43c7378758b26d Mon Sep 17 00:00:00 2001 From: Xiaoxue Wang Date: Wed, 18 Dec 2024 19:54:04 +0800 Subject: [PATCH 3/3] pytest error fix Signed-off-by: Xiaoxue Wang --- insights/tests/parsers/test_fwupdagent.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/insights/tests/parsers/test_fwupdagent.py b/insights/tests/parsers/test_fwupdagent.py index 285fcc2f82..422a4ba7d4 100644 --- a/insights/tests/parsers/test_fwupdagent.py +++ b/insights/tests/parsers/test_fwupdagent.py @@ -2,7 +2,7 @@ import doctest import pytest -from insights.core.exceptions import ParseException, SkipComponent +from insights.core.exceptions import ParseException from insights.parsers import fwupdagent from insights.parsers.fwupdagent import FwupdagentDevices, FwupdagentSecurity from insights.tests import context_wrap @@ -178,7 +178,7 @@ def test_devices(): assert devices["Devices"][1]["Name"] == "USB3.0 Hub" assert devices["Devices"][1]["Version"] == "3.114" - with pytest.raises(SkipComponent): + with pytest.raises(ParseException): FwupdagentDevices(context_wrap(DEVICES_ERROR_1)) @@ -196,7 +196,7 @@ def test_security(): with pytest.raises(ParseException): FwupdagentSecurity(context_wrap(SECURITY_ERROR_2)) - with pytest.raises(SkipComponent): + with pytest.raises(ParseException): FwupdagentSecurity(context_wrap(SECURITY_ERROR_3))