-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
retrieve firmware by debian and snap fwupd
implement a script to be able to retrieve frimware information by debian fwupd and snap fwupd
- Loading branch information
1 parent
8faa9be
commit 5c875ed
Showing
3 changed files
with
128 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#!/usr/bin/env python3 | ||
# This file is part of Checkbox. | ||
# | ||
# Copyright 2024 Canonical Ltd. | ||
# Written by: | ||
# Stanley Huang <[email protected]> | ||
# | ||
# Checkbox is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License version 3, | ||
# as published by the Free Software Foundation. | ||
# | ||
# Checkbox is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with Checkbox. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
import os | ||
import json | ||
import shlex | ||
import subprocess | ||
from checkbox_support.snap_utils.snapd import Snapd | ||
|
||
|
||
def get_firmware_info_fwupd(): | ||
|
||
fwupd_snap = Snapd().list("fwupd") | ||
if fwupd_snap: | ||
# 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("."))) | ||
# 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): | ||
del os.environ["SNAP"] | ||
|
||
subprocess.run(shlex.split("fwupdmgr get-devices --json")) | ||
|
||
|
||
if __name__ == "__main__": | ||
try: | ||
get_firmware_info_fwupd() | ||
except Exception as err: | ||
print(err) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import os | ||
import unittest | ||
from unittest.mock import patch | ||
from get_firmware_info_fwupd import get_firmware_info_fwupd | ||
|
||
|
||
class TestGetFirmwareInfo(unittest.TestCase): | ||
|
||
@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 = True | ||
get_firmware_info_fwupd() | ||
mock_snapd.assert_called_with("fwupd") | ||
mock_subporcess.assert_called_with( | ||
['fwupd.fwupdmgr', 'get-devices', '--json']) | ||
|
||
@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): | ||
|
||
mock_snapd.return_value = False | ||
mock_json.return_value = { | ||
"Versions": [ | ||
{ | ||
"Type": "runtime", | ||
"AppstreamId": "org.freedesktop.fwupd", | ||
"Version": "1.9.14" | ||
} | ||
] | ||
} | ||
get_firmware_info_fwupd() | ||
mock_snapd.assert_called_with("fwupd") | ||
mock_subporcess.assert_called_with( | ||
['fwupdmgr', 'get-devices', '--json']) | ||
self.assertEqual( | ||
os.environ.get("SNAP"), "checkbox-snap") | ||
|
||
@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_fwupd_deb_on_checkbox_snap( | ||
self, mock_snapd, mock_subporcess, mock_json): | ||
|
||
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_snapd.assert_called_with("fwupd") | ||
mock_subporcess.assert_called_with( | ||
['fwupdmgr', 'get-devices', '--json']) | ||
self.assertIsNone(os.environ.get("SNAP")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters