Skip to content

Commit

Permalink
Restructure scripts and added unittest cases
Browse files Browse the repository at this point in the history
restructure scripts and added unittest cases
  • Loading branch information
stanley31huang committed Apr 9, 2024
1 parent 5c875ed commit 34588f1
Show file tree
Hide file tree
Showing 2 changed files with 205 additions and 45 deletions.
77 changes: 62 additions & 15 deletions providers/base/bin/get_firmware_info_fwupd.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,91 @@
# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.

import os
import sys
import json
import shlex
import logging
import subprocess
from checkbox_support.snap_utils.snapd import Snapd


def get_firmware_info_fwupd():
def get_fwupdmgr_services_versions():
"""Show fwupd client and daemon versions
Returns:
list: fwupd client and daemon versions
"""
fwupd_vers = subprocess.run(
shlex.split("fwupdmgr --version --json"),
capture_output=True)
fwupd_vers = json.loads(fwupd_vers.stdout).get("Versions", [])

return fwupd_vers


def get_fwupd_runtime_version():
"""Get fwupd runtime version
Returns:
tuple: fwupd runtime version
"""
runtime_ver = ()

for ver in get_fwupdmgr_services_versions():
if (ver.get("Type") == "runtime" and
ver.get("AppstreamId") == "org.freedesktop.fwupd"):
runtime_ver = tuple(map(int, ver.get("Version").split(".")))

return runtime_ver


fwupd_snap = Snapd().list("fwupd")
if fwupd_snap:
def get_firmware_info_fwupd():
"""
Dump firmware information for all devices detected by fwupd
"""
if Snapd().list("fwupd"):
# Dump firmware info by fwupd snap
subprocess.run(shlex.split("fwupd.fwupdmgr get-devices --json"))
else:
# Dump firmware info by fwupd debian package
fwupd_vers = subprocess.run(
shlex.split("fwupdmgr --version --json"),
capture_output=True)
fwupd_vers = json.loads(fwupd_vers.stdout)

runtime_ver = ()
for ver in fwupd_vers.get("Versions", []):
if (ver.get("Type") == "runtime" and
ver.get("AppstreamId") == "org.freedesktop.fwupd"):
runtime_ver = tuple(map(int, ver.get("Version").split(".")))
runtime_ver = get_fwupd_runtime_version()
# Apply workaround to unset the SNAP for the fwupd issue
# See details from following PR
# https://github.com/canonical/checkbox/pull/1089

# SNAP environ is avaialble, so it's running on checkbox snap
# Unset the environ variable if debian fwupd lower than 1.9.14
if os.environ["SNAP"] and runtime_ver < (1, 9, 14):
if os.environ.get("SNAP") and runtime_ver < (1, 9, 14):
del os.environ["SNAP"]

subprocess.run(shlex.split("fwupdmgr get-devices --json"))


if __name__ == "__main__":

root_logger = logging.getLogger()
root_logger.setLevel(logging.INFO)
logger_format = "%(asctime)s %(levelname)-8s %(message)s"
date_format = "%Y-%m-%d %H:%M:%S"

# Log DEBUG and INFO to stdout, others to stderr
stdout_handler = logging.StreamHandler(sys.stdout)
stdout_handler.setFormatter(logging.Formatter(logger_format, date_format))

stderr_handler = logging.StreamHandler(sys.stderr)
stderr_handler.setFormatter(logging.Formatter(logger_format, date_format))

stdout_handler.setLevel(logging.DEBUG)
stderr_handler.setLevel(logging.WARNING)

# Add a filter to the stdout handler to limit log records to
# INFO level and below
stdout_handler.addFilter(lambda record: record.levelno <= logging.INFO)

root_logger.addHandler(stderr_handler)
root_logger.addHandler(stdout_handler)

try:
get_firmware_info_fwupd()
except Exception as err:
print(err)
logging.error(err)
173 changes: 143 additions & 30 deletions providers/base/tests/test_get_firmware_info_fwupd.py
Original file line number Diff line number Diff line change
@@ -1,65 +1,178 @@
import os
import json
import unittest
import subprocess
from unittest.mock import patch
from get_firmware_info_fwupd import get_firmware_info_fwupd
import get_firmware_info_fwupd


class TestGetFirmwareInfo(unittest.TestCase):

@patch("json.loads")
@patch("subprocess.run")
@patch("checkbox_support.snap_utils.snapd.Snapd.list")
def test_get_firmware_data_by_fwupd_snap(
self, mock_snapd, mock_subporcess):
def test_get_deb_fwupd_version_success(self, mock_subporcess, mock_json):

mock_snapd.return_value = True
get_firmware_info_fwupd()
mock_snapd.assert_called_with("fwupd")
dict_resp = {
"Versions": [
{
"Type": "runtime",
"AppstreamId": "org.freedesktop.fwupd",
"Version": "1.9.14"
},
{
"Type": "compile",
"AppstreamId": "org.freedesktop.fwupd",
"Version": "1.7.9"
}
]
}
json_resp = json.dumps(dict_resp)
mock_subporcess.return_value = subprocess.CompletedProcess(
returncode=0,
stdout=json_resp,
args=["fwupdmgr", "--version", "--json"]
)
mock_json.return_value = dict_resp

fwupd_vers = get_firmware_info_fwupd.get_fwupdmgr_services_versions()
mock_subporcess.assert_called_with(
['fwupd.fwupdmgr', 'get-devices', '--json'])
['fwupdmgr', '--version', '--json'],
capture_output=True)
mock_json.assert_called_with(json_resp)
self.assertListEqual(dict_resp["Versions"], fwupd_vers)

@patch.dict(os.environ, {"SNAP": "checkbox-snap"})
@patch("json.loads")
@patch("subprocess.run")
@patch("checkbox_support.snap_utils.snapd.Snapd.list")
def test_get_firmware_data_by_fwupd1914_deb_on_checkbox_snap(
self, mock_snapd, mock_subporcess, mock_json):
def test_get_deb_fwupd_version_key_not_match(
self, mock_subporcess, mock_json):

mock_snapd.return_value = False
mock_json.return_value = {
"Versions": [
dict_resp = {
"Services": [
{
"Type": "runtime",
"AppstreamId": "org.freedesktop.fwupd",
"Version": "1.9.14"
},
{
"Type": "compile",
"AppstreamId": "org.freedesktop.fwupd",
"Version": "1.7.9"
}
]
}
get_firmware_info_fwupd()
json_resp = json.dumps(dict_resp)
mock_subporcess.return_value = subprocess.CompletedProcess(
returncode=0,
stdout=json_resp,
args=["fwupdmgr", "--version", "--json"]
)
mock_json.return_value = dict_resp

fwupd_vers = get_firmware_info_fwupd.get_fwupdmgr_services_versions()
mock_subporcess.assert_called_with(
['fwupdmgr', '--version', '--json'], capture_output=True)
mock_json.assert_called_with(json_resp)
self.assertListEqual([], fwupd_vers)

@patch("get_firmware_info_fwupd.get_fwupdmgr_services_versions")
def test_get_deb_fwupd_runtime_version_success(self, mock_fwupd_vers):

expected_fwupd_ver = (1, 7, 9)
fwupd_vers_resp = [
{
"Type": "runtime",
"AppstreamId": "org.freedesktop.fwupd",
"Version": "1.7.9"
},
{
"Type": "compile",
"AppstreamId": "org.freedesktop.fwupd",
"Version": "1.7.9"
}
]

mock_fwupd_vers.return_value = fwupd_vers_resp
runtime_ver = get_firmware_info_fwupd.get_fwupd_runtime_version()
self.assertEqual(expected_fwupd_ver, runtime_ver)

@patch("get_firmware_info_fwupd.get_fwupdmgr_services_versions")
def test_get_deb_fwupd_runtime_version_failed(self, mock_fwupd_vers):

fwupd_vers_resp = [
{
"Type": "compile",
"AppstreamId": "org.freedesktop.fwupd",
"Version": "1.7.9"
}
]

mock_fwupd_vers.return_value = fwupd_vers_resp
runtime_ver = get_firmware_info_fwupd.get_fwupd_runtime_version()
self.assertEqual((), runtime_ver)

@patch("subprocess.run")
@patch("checkbox_support.snap_utils.snapd.Snapd.list")
def test_get_firmware_data_by_fwupd_snap(
self, mock_snapd, mock_subporcess):

mock_snapd.return_value = {
"id": "HpOj37PuyuaMUZY0NQhtwnp7oS5P8u5R",
"title": "fwupd",
"summary": "Firmware updates for Linux"
}
get_firmware_info_fwupd.get_firmware_info_fwupd()
mock_snapd.assert_called_with("fwupd")
mock_subporcess.assert_called_with(
['fwupdmgr', 'get-devices', '--json'])
['fwupd.fwupdmgr', 'get-devices', '--json'])

@patch.dict(os.environ, {"SNAP": "checkbox-snap"})
@patch("subprocess.run")
@patch("get_firmware_info_fwupd.get_fwupd_runtime_version")
@patch("checkbox_support.snap_utils.snapd.Snapd.list")
def test_get_firmware_data_by_fwupd1914_deb_on_checkbox_snap(
self, mock_snapd, mock_fwupd_ver, mock_subporcess):

mock_snapd.return_value = None
mock_fwupd_ver.return_value = (1, 9, 14)

get_firmware_info_fwupd.get_firmware_info_fwupd()
mock_snapd.assert_called_with("fwupd")
self.assertEqual(
os.environ.get("SNAP"), "checkbox-snap")
mock_subporcess.assert_called_with(
['fwupdmgr', 'get-devices', '--json'])

@patch.dict(os.environ, {"SNAP": "checkbox-snap"})
@patch("json.loads")
@patch("subprocess.run")
@patch("get_firmware_info_fwupd.get_fwupd_runtime_version")
@patch("checkbox_support.snap_utils.snapd.Snapd.list")
def test_get_firmware_data_by_fwupd_deb_on_checkbox_snap(
self, mock_snapd, mock_subporcess, mock_json):
def test_get_firmware_data_by_fwupd_deb179_on_checkbox_snap(
self, mock_snapd, mock_fwupd_ver, mock_subporcess):

mock_snapd.return_value = False
mock_json.return_value = {
"Versions": [
{
"Type": "runtime",
"AppstreamId": "org.freedesktop.fwupd",
"Version": "1.7.9"
}
]
}
get_firmware_info_fwupd()
mock_fwupd_ver.return_value = (1, 7, 9)

# SNAP env is available before get_firmware_info_fwupd been called
self.assertEqual(os.environ.get("SNAP"), "checkbox-snap")
get_firmware_info_fwupd.get_firmware_info_fwupd()
mock_snapd.assert_called_with("fwupd")
# SNAP env is empty after get_firmware_info_fwupd been called
self.assertIsNone(os.environ.get("SNAP"))
mock_subporcess.assert_called_with(
['fwupdmgr', 'get-devices', '--json'])

@patch("subprocess.run")
@patch("get_firmware_info_fwupd.get_fwupd_runtime_version")
@patch("checkbox_support.snap_utils.snapd.Snapd.list")
def test_get_firmware_data_by_fwupd_deb_on_checkbox_deb(
self, mock_snapd, mock_fwupd_ver, mock_subporcess):

mock_snapd.return_value = False
mock_fwupd_ver.return_value = (1, 7, 9)

# SNAP env is empty
self.assertIsNone(os.environ.get("SNAP"))
get_firmware_info_fwupd.get_firmware_info_fwupd()
mock_snapd.assert_called_with("fwupd")
mock_subporcess.assert_called_with(
['fwupdmgr', 'get-devices', '--json'])

0 comments on commit 34588f1

Please sign in to comment.