Skip to content

Commit 8a833d3

Browse files
committed
ttm: Use relaunch_sudo()
Running in a wheel is more awkward than necessary when using is_root() Signed-off-by: Mario Limonciello <superm1@kernel.org>
1 parent e355b71 commit 8a833d3

File tree

2 files changed

+14
-33
lines changed

2 files changed

+14
-33
lines changed

src/amd_debug/ttm.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
bytes_to_gb,
1010
gb_to_pages,
1111
get_system_mem,
12-
is_root,
12+
relaunch_sudo,
1313
print_color,
1414
reboot,
1515
version,
@@ -57,9 +57,7 @@ def get(self) -> bool:
5757

5858
def set(self, gb_value) -> bool:
5959
"""Set a new page limit"""
60-
if not is_root():
61-
print_color("Root privileges required", "❌")
62-
return False
60+
relaunch_sudo()
6361

6462
# Check against system memory
6563
total = get_system_mem()
@@ -108,9 +106,7 @@ def clear(self) -> bool:
108106
print_color(f"{MODPROBE_CONF_PATH} doesn't exist", "❌")
109107
return False
110108

111-
if not is_root():
112-
print_color("Root privileges required", "❌")
113-
return False
109+
relaunch_sudo()
114110

115111
os.remove(MODPROBE_CONF_PATH)
116112
print_color(f"Configuration {MODPROBE_CONF_PATH} removed", "🐧")
Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -199,38 +199,32 @@ def test_get_file_not_found(self, mock_print, _mock_open):
199199
)
200200
self.assertFalse(result)
201201

202-
@mock.patch("amd_debug.ttm.is_root", return_value=False)
203-
@mock.patch("amd_debug.ttm.print_color")
204-
def test_set_not_root(self, mock_print, _mock_is_root):
205-
"""Test set() when not root"""
206-
result = self.tool.set(2)
207-
mock_print.assert_called_with("Root privileges required", "❌")
208-
self.assertFalse(result)
209-
210-
@mock.patch("amd_debug.ttm.is_root", return_value=True)
202+
@mock.patch("amd_debug.ttm.relaunch_sudo", return_value=True)
211203
@mock.patch("amd_debug.ttm.get_system_mem", return_value=8.0)
212204
@mock.patch("amd_debug.ttm.print_color")
213-
def test_set_gb_greater_than_total(self, mock_print, _mock_mem, _mock_is_root):
205+
def test_set_gb_greater_than_total(
206+
self, mock_print, _mock_mem, _mock_relaunch_sudo
207+
):
214208
"""Test set() when gb_value > total system memory"""
215209
result = self.tool.set(16)
216210
mock_print.assert_any_call(
217211
"16.00 GB is greater than total system memory (8.00 GB)", "❌"
218212
)
219213
self.assertFalse(result)
220214

221-
@mock.patch("amd_debug.ttm.is_root", return_value=True)
215+
@mock.patch("amd_debug.ttm.relaunch_sudo", return_value=True)
222216
@mock.patch("amd_debug.ttm.get_system_mem", return_value=10.0)
223217
@mock.patch("amd_debug.ttm.print_color")
224218
@mock.patch("builtins.input", return_value="n")
225219
def test_set_gb_exceeds_max_percentage_cancel(
226-
self, _mock_input, mock_print, _mock_mem, mock_is_root
220+
self, _mock_input, mock_print, _mock_mem, mock_relaunch_sudo
227221
):
228222
"""Test set() when gb_value exceeds max percentage and user cancels"""
229223
result = self.tool.set(9.5)
230224
self.assertFalse(result)
231225
mock_print.assert_any_call("Operation cancelled.", "🚦")
232226

233-
@mock.patch("amd_debug.ttm.is_root", return_value=True)
227+
@mock.patch("amd_debug.ttm.relaunch_sudo", return_value=True)
234228
@mock.patch("amd_debug.ttm.get_system_mem", return_value=10.0)
235229
@mock.patch("amd_debug.ttm.gb_to_pages", return_value=20480)
236230
@mock.patch("amd_debug.ttm.print_color")
@@ -244,8 +238,8 @@ def test_set_success(
244238
mock_open,
245239
mock_print,
246240
_mock_gb_to_pages,
247-
mock_mem,
248-
mock_is_root,
241+
_mock_mem,
242+
_relaunch_sudo,
249243
):
250244
"""Test set() success path"""
251245
result = self.tool.set(5)
@@ -266,21 +260,12 @@ def test_clear_file_not_exists(self, mock_print, _mock_exists):
266260
self.assertFalse(result)
267261

268262
@mock.patch("os.path.exists", return_value=True)
269-
@mock.patch("amd_debug.ttm.is_root", return_value=False)
270-
@mock.patch("amd_debug.ttm.print_color")
271-
def test_clear_not_root(self, mock_print, _mock_is_root, _mock_exists):
272-
"""Test clear() when not root"""
273-
result = self.tool.clear()
274-
mock_print.assert_called_with("Root privileges required", "❌")
275-
self.assertFalse(result)
276-
277-
@mock.patch("os.path.exists", return_value=True)
278-
@mock.patch("amd_debug.ttm.is_root", return_value=True)
263+
@mock.patch("amd_debug.ttm.relaunch_sudo", return_value=True)
279264
@mock.patch("os.remove")
280265
@mock.patch("amd_debug.ttm.print_color")
281266
@mock.patch("amd_debug.ttm.maybe_reboot", return_value=True)
282267
def test_clear_success(
283-
self, _mock_reboot, mock_print, mock_remove, _mock_is_root, mock_exists
268+
self, _mock_reboot, mock_print, mock_remove, _mock_relaunch_sudo, _mock_exists
284269
):
285270
"""Test clear() success path"""
286271
result = self.tool.clear()

0 commit comments

Comments
 (0)