Skip to content

Commit da16f25

Browse files
committed
Adopt suggestion from Fernando
1 parent 178de6e commit da16f25

File tree

2 files changed

+71
-69
lines changed

2 files changed

+71
-69
lines changed

providers/base/bin/ishtp.py

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,17 @@
11
#!/usr/bin/env python3
22
import subprocess
3-
import sys
43
import os
54
import argparse
5+
from checkbox_support.helpers.release_info import get_release_info
66

77

8-
def get_release_version():
9-
try:
10-
output = subprocess.check_output(
11-
["lsb_release", "-rs"], text=True
12-
).strip()
13-
return int(float(output))
14-
except (subprocess.CalledProcessError, ValueError):
15-
print("Error: Unable to determine release version.")
16-
sys.exit(1)
17-
18-
19-
def is_module_loaded(module_name):
20-
try:
21-
output = subprocess.check_output(["lsmod"], text=True)
22-
return module_name in output
23-
except subprocess.CalledProcessError:
24-
return False
8+
def get_module_list():
9+
output = subprocess.check_output(["lsmod"], text=True)
10+
return [line.split()[0] for line in output.splitlines() if line]
2511

2612

2713
def check_modules():
28-
release = get_release_version()
14+
release = int(get_release_info()["release"].split(".")[0])
2915

3016
if release >= 24:
3117
expected_modules = ["intel_ishtp_hid", "intel_ish_ipc", "intel_ishtp"]
@@ -37,32 +23,33 @@ def check_modules():
3723
"intel_ishtp",
3824
]
3925

40-
exit_code = 0
26+
module_list = get_module_list()
4127
for module in expected_modules:
4228
print("Checking module: {}".format(module))
43-
if not is_module_loaded(module):
44-
print("FAIL: The '{}' module is not loaded!".format(module))
45-
exit_code = 1
29+
if module not in module_list:
30+
raise SystemExit(
31+
"FAIL: The '{}' module is not loaded!".format(module)
32+
)
4633
else:
4734
print("PASS: It's loaded")
4835
print()
4936

50-
return exit_code
37+
return 0
5138

5239

5340
def check_devices():
5441
ishtp_dir = "/sys/bus/ishtp/devices/"
5542

5643
if not os.path.isdir(ishtp_dir):
57-
print("ISHTP devices directory does not exist!")
58-
print("The ISHTP folder: {}".format(ishtp_dir))
59-
return 1
44+
raise SystemExit(
45+
"The ISHTP folder does not exist: {}".format(ishtp_dir)
46+
)
6047

6148
devices = os.listdir(ishtp_dir)
6249
if not devices:
63-
print("ISHTP devices directory empty - no devices found!")
64-
print("The ISHTP folder: {}".format(ishtp_dir))
65-
return 1
50+
raise SystemExit(
51+
"No devices found on the ISHTP folder: {}".format(ishtp_dir)
52+
)
6653

6754
print("Found ishtp devices under {}:".format(ishtp_dir))
6855
for device in devices:
@@ -84,12 +71,8 @@ def check_devices():
8471
)
8572
args = parser.parse_args()
8673

87-
exit_code = 0
88-
8974
if args.module:
90-
exit_code |= check_modules()
75+
check_modules()
9176

9277
if args.device:
93-
exit_code |= check_devices()
94-
95-
sys.exit(exit_code)
78+
check_devices()

providers/base/tests/test_ishtp.py

Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,19 @@
44
import os
55
import subprocess
66
from ishtp import (
7-
get_release_version,
8-
is_module_loaded,
7+
get_module_list,
98
check_modules,
109
check_devices,
1110
)
1211

1312

1413
class TestISHTP(unittest.TestCase):
1514

16-
@patch("subprocess.check_output", return_value="24.04\n")
17-
def test_get_release_version(self, mock_subproc):
18-
self.assertEqual(get_release_version(), 24)
19-
20-
@patch("subprocess.check_output", return_value="somethingwrong\n")
21-
def test_get_release_version_value_error(self, mock_subproc):
22-
with self.assertRaises(SystemExit) as cm:
23-
get_release_version()
24-
self.assertEqual(cm.exception.code, 1)
25-
26-
@patch("subprocess.check_output", return_value="intel_ishtp 123 0\n")
27-
def test_is_module_loaded(self, mock_subproc):
28-
self.assertTrue(is_module_loaded("intel_ishtp"))
29-
30-
@patch("subprocess.check_output", return_value="")
31-
def test_is_module_not_loaded(self, mock_subproc):
32-
self.assertFalse(is_module_loaded("intel_ishtp"))
33-
3415
@patch(
35-
"subprocess.check_output",
36-
side_effect=subprocess.CalledProcessError(1, "lsmod"),
16+
"subprocess.check_output", return_value="module1\nnodule2\nmodule3\n"
3717
)
38-
def test_is_module_loaded_error(self, mock_subproc):
39-
self.assertFalse(is_module_loaded("intel_ishtp"))
18+
def test_get_module_list(self, mock_subproc):
19+
self.assertEqual(get_module_list(), ["module1", "nodule2", "module3"])
4020

4121
@patch("os.path.isdir", return_value=True)
4222
@patch("os.listdir", return_value=["device1", "device2"])
@@ -45,22 +25,61 @@ def test_check_devices_success(self, mock_listdir, mock_isdir):
4525

4626
@patch("os.path.isdir", return_value=False)
4727
def test_check_devices_no_directory(self, mock_isdir):
48-
self.assertEqual(check_devices(), 1)
28+
self.assertRaises(SystemExit)
4929

5030
@patch("os.path.isdir", return_value=True)
5131
@patch("os.listdir", return_value=[])
5232
def test_check_devices_empty_directory(self, mock_listdir, mock_isdir):
53-
self.assertEqual(check_devices(), 1)
33+
self.assertRaises(SystemExit)
5434

55-
@patch("ishtp.is_module_loaded", return_value=True)
56-
@patch("ishtp.get_release_version", return_value=24)
57-
def test_check_modules_success(self, mock_release, mock_module):
35+
@patch(
36+
"ishtp.get_module_list",
37+
return_value=["intel_ishtp_hid", "intel_ish_ipc", "intel_ishtp"],
38+
)
39+
@patch(
40+
"checkbox_support.helpers.release_info.get_release_info",
41+
return_value="24.04",
42+
)
43+
def test_check_modules_success_24(self, mock_release, mock_module):
5844
self.assertEqual(check_modules(), 0)
5945

60-
@patch("ishtp.is_module_loaded", return_value=False)
61-
@patch("ishtp.get_release_version", return_value=24)
62-
def test_check_modules_fail(self, mock_release, mock_module):
63-
self.assertEqual(check_modules(), 1)
46+
@patch(
47+
"ishtp.get_module_list",
48+
return_value=["intel_ishtp_hid", "intel_ish_ipc"],
49+
)
50+
@patch(
51+
"checkbox_support.helpers.release_info.get_release_info",
52+
return_value="24.04",
53+
)
54+
def test_check_modules_fail_24(self, mock_release, mock_module):
55+
self.assertRaises(SystemExit)
56+
57+
@patch(
58+
"ishtp.get_module_list",
59+
return_value=[
60+
"intel_ishtp_loader",
61+
"intel_ishtp_hid",
62+
"intel_ish_ipc",
63+
"intel_ishtp",
64+
],
65+
)
66+
@patch(
67+
"checkbox_support.helpers.release_info.get_release_info",
68+
return_value="22.04",
69+
)
70+
def test_check_modules_success_22(self, mock_release, mock_module):
71+
self.assertEqual(check_modules(), 0)
72+
73+
@patch(
74+
"ishtp.get_module_list",
75+
return_value=["intel_ishtp_hid", "intel_ish_ipc"],
76+
)
77+
@patch(
78+
"checkbox_support.helpers.release_info.get_release_info",
79+
return_value="22.04",
80+
)
81+
def test_check_modules_fail_22(self, mock_release, mock_module):
82+
self.assertRaises(SystemExit)
6483

6584

6685
if __name__ == "__main__":

0 commit comments

Comments
 (0)