Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hookutils: skip checking stat for skipped DMI files and add unit test for apport.hookutils.attach_dmi #227

Merged
merged 2 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions apport/hookutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,13 @@ def attach_dmi(report):
dmi_dir = "/sys/class/dmi/id"
if os.path.isdir(dmi_dir):
for f in os.listdir(dmi_dir):
if f in ("subsystem", "uevent"):
continue
p = os.path.realpath(f"{dmi_dir}/{f}")
st = os.stat(p)
# ignore the root-only ones, since they have serial numbers
if not stat.S_ISREG(st.st_mode) or (st.st_mode & 4 == 0):
continue
if f in ("subsystem", "uevent"):
continue

try:
value = read_file(p)
Expand Down
73 changes: 73 additions & 0 deletions tests/unit/test_hookutils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Unit tests for the apport.hookutils module."""

import os
import re
import subprocess
import time
Expand All @@ -13,6 +14,78 @@ class TestHookutils(unittest.TestCase):
# pylint: disable=missing-function-docstring
"""Test apport.hookutils module."""

maxDiff = None

@unittest.mock.patch("os.path.isdir", unittest.mock.MagicMock(return_value=True))
@unittest.mock.patch("os.listdir")
@unittest.mock.patch("os.stat")
@unittest.mock.patch("apport.hookutils.read_file")
def test_attach_dmi(
self,
read_file_mock: unittest.mock.MagicMock,
stat_mock: unittest.mock.MagicMock,
listdir_mock: unittest.mock.MagicMock,
) -> None:
"""attach_dmi()"""

def stat(
content: str = "private\n",
st_mode: int = 0o100444,
st_nlink: int = 1,
st_size: int = 4096,
) -> tuple[str, os.stat_result]:
st_time = 1698056204.0
stat = os.stat_result(
(st_mode, 1337, 22, st_nlink, 0, 0, st_size, st_time, st_time, st_time)
)
return (content, stat)

dmi_files = {
"bios_date": stat("07/20/2022\n"),
"board_serial": stat(st_mode=0o100400),
"uevent": stat("MODALIAS=dmi:[...]\n", st_mode=0o100644),
"product_serial": stat(st_mode=0o100400),
"product_name": stat("B550I AORUS PRO AX\n"),
"sys_vendor": stat("Gigabyte Technology Co., Ltd.\n"),
"power": stat(st_mode=0o40755, st_nlink=2, st_size=0),
"bios_version": stat("F16e\n"),
"bios_release": stat("5.17\n"),
"board_vendor": stat("Gigabyte Technology Co., Ltd.\n"),
"subsystem": stat(st_mode=0o40755, st_nlink=2, st_size=0),
"product_family": stat("B550 MB\n"),
"product_uuid": stat(
"2cf4a3c2-8f76-4f38-95b5-77c8a9f6ec4f\n", st_mode=0o100400
),
"bios_vendor": stat("American Megatrends International, LLC.\n"),
"board_name": stat("B550I AORUS PRO AX\n"),
}

def mock_os_stat(path: str) -> os.stat_result:
return dmi_files[os.path.basename(path)][1]

def mock_read_file(path: str) -> str:
return dmi_files[os.path.basename(path)][0].strip()

listdir_mock.return_value = list(dmi_files.keys())
stat_mock.side_effect = mock_os_stat
read_file_mock.side_effect = mock_read_file
report: dict[str, str] = {}
apport.hookutils.attach_dmi(report)
self.assertEqual(
report,
{
"dmi.bios.date": "07/20/2022",
"dmi.bios.release": "5.17",
"dmi.bios.vendor": "American Megatrends International, LLC.",
"dmi.bios.version": "F16e",
"dmi.board.name": "B550I AORUS PRO AX",
"dmi.board.vendor": "Gigabyte Technology Co., Ltd.",
"dmi.product.family": "B550 MB",
"dmi.product.name": "B550I AORUS PRO AX",
"dmi.sys.vendor": "Gigabyte Technology Co., Ltd.",
},
)

@unittest.mock.patch("apport.hookutils.root_command_output")
def test_attach_dmesg(self, root_command_output_mock):
"""attach_dmesg()"""
Expand Down