Debian Codegraph @

tests/test_stress_menu.py

raw symbols SCIP
1 """Tests for StressMenu: command building, validation helpers, and defaults."""
2
3 from unittest.mock import MagicMock
4
5 import pytest
6
7 from s_tui.stress_menu import StressMenu
8
9
10 @pytest.fixture
11 def menu(mocker):
12 """Create a StressMenu with psutil.cpu_count mocked to 4."""
13 mocker.patch("psutil.cpu_count", return_value=4)
14 return_fn = MagicMock()
15 return StressMenu(return_fn, "stress")
16
17
18 # =====================================================================
19 # Initialization
20 # =====================================================================
21
22
23 class TestStressMenuInit:
24 def test_defaults(self, menu):
25 """Default values after construction."""
26 assert menu.stress_exe == "stress"
27 assert menu.time_out == "none"
28 assert menu.sqrt_workers == "4" # cpu_count mocked to 4
29 assert menu.sync_workers == "0"
30 assert menu.memory_workers == "0"
31 assert menu.malloc_byte == "256M"
32 assert menu.byte_touch_cnt == "4096"
33 assert menu.malloc_delay == "none"
34 assert menu.no_malloc is False
35 assert menu.write_workers == "0"
36 assert menu.write_bytes == "1G"
37
38 def test_cpu_count_fallback_on_error(self, mocker):
39 """If psutil.cpu_count raises, sqrt_workers defaults to 1."""
40 mocker.patch("psutil.cpu_count", side_effect=OSError("no cpus"))
41 m = StressMenu(MagicMock(), "stress")
42 assert m.sqrt_workers == "1"
43
44
45 # =====================================================================
46 # get_stress_cmd
47 # =====================================================================
48
49
50 class TestGetStressCmd:
51 def test_default_cmd(self, menu):
52 """Default command includes -c with cpu_count."""
53 cmd = menu.get_stress_cmd()
54 assert cmd[0] == "stress"
55 assert "-c" in cmd
56 idx = cmd.index("-c")
57 assert cmd[idx + 1] == "4"
58
59 def test_no_sqrt_workers(self, menu):
60 """When sqrt_workers is 0, -c is omitted."""
61 menu.sqrt_workers = "0"
62 cmd = menu.get_stress_cmd()
63 assert "-c" not in cmd
64
65 def test_sync_workers(self, menu):
66 """sync_workers > 0 adds -i flag."""
67 menu.sync_workers = "2"
68 cmd = menu.get_stress_cmd()
69 assert "-i" in cmd
70 assert cmd[cmd.index("-i") + 1] == "2"
71
72 def test_memory_workers(self, menu):
73 """memory_workers > 0 adds --vm, --vm-bytes, --vm-stride."""
74 menu.memory_workers = "3"
75 cmd = menu.get_stress_cmd()
76 assert "--vm" in cmd
77 assert cmd[cmd.index("--vm") + 1] == "3"
78 assert "--vm-bytes" in cmd
79 assert "--vm-stride" in cmd
80
81 def test_no_malloc_adds_vm_keep(self, menu):
82 """When no_malloc is True, --vm-keep is added."""
83 menu.no_malloc = True
84 cmd = menu.get_stress_cmd()
85 assert "--vm-keep" in cmd
86
87 def test_no_malloc_false(self, menu):
88 """When no_malloc is False, --vm-keep not present."""
89 menu.no_malloc = False
90 cmd = menu.get_stress_cmd()
91 assert "--vm-keep" not in cmd
92
93 def test_write_workers(self, menu):
94 """write_workers > 0 adds --hdd and --hdd-bytes."""
95 menu.write_workers = "1"
96 cmd = menu.get_stress_cmd()
97 assert "--hdd" in cmd
98 assert cmd[cmd.index("--hdd") + 1] == "1"
99 assert "--hdd-bytes" in cmd
100
101 def test_timeout(self, menu):
102 """Non-"none" timeout adds -t flag."""
103 menu.time_out = "60"
104 cmd = menu.get_stress_cmd()
105 assert "-t" in cmd
106 assert cmd[cmd.index("-t") + 1] == "60"
107
108 def test_timeout_none(self, menu):
109 """When timeout is "none", -t is omitted."""
110 menu.time_out = "none"
111 cmd = menu.get_stress_cmd()
112 assert "-t" not in cmd
113
114 def test_full_cmd(self, menu):
115 """A fully configured command has all flags."""
116 menu.sqrt_workers = "2"
117 menu.sync_workers = "1"
118 menu.memory_workers = "1"
119 menu.no_malloc = True
120 menu.write_workers = "1"
121 menu.time_out = "120"
122 cmd = menu.get_stress_cmd()
123 assert "-c" in cmd
124 assert "-i" in cmd
125 assert "--vm" in cmd
126 assert "--vm-keep" in cmd
127 assert "--hdd" in cmd
128 assert "-t" in cmd
129
130
131 # =====================================================================
132 # Validation helpers
133 # =====================================================================
134
135
136 class TestStressMenuValidation:
137 def test_get_pos_num_valid(self):
138 assert StressMenu.get_pos_num("42", "0") == "42"
139
140 def test_get_pos_num_zero(self):
141 assert StressMenu.get_pos_num("0", "1") == "0"
142
143 def test_get_pos_num_none_with_none_default(self):
144 assert StressMenu.get_pos_num("none", "none") == "none"
145
146 def test_get_pos_num_none_with_numeric_default(self):
147 assert StressMenu.get_pos_num("none", "4") == "4"
148
149 def test_get_pos_num_invalid(self):
150 assert StressMenu.get_pos_num("abc", "5") == "5"
151
152 def test_get_pos_num_negative(self):
153 assert StressMenu.get_pos_num("-1", "0") == "0"
154
155 def test_get_pos_num_float(self):
156 assert StressMenu.get_pos_num("3.5", "0") == "0"
157
158 def test_get_valid_byte_plain_number(self):
159 assert StressMenu.get_valid_byte("256", "0") == "256"
160
161 def test_get_valid_byte_megabytes(self):
162 assert StressMenu.get_valid_byte("256M", "0") == "256M"
163
164 def test_get_valid_byte_gigabytes(self):
165 assert StressMenu.get_valid_byte("1G", "0") == "1G"
166
167 def test_get_valid_byte_with_b_suffix(self):
168 assert StressMenu.get_valid_byte("256MB", "0") == "256MB"
169
170 def test_get_valid_byte_invalid(self):
171 assert StressMenu.get_valid_byte("abc", "1G") == "1G"
172
173 def test_get_valid_byte_empty(self):
174 assert StressMenu.get_valid_byte("", "1G") == "1G"
175
176
177 # =====================================================================
178 # on_default resets to defaults
179 # =====================================================================
180
181
182 class TestStressMenuOnDefault:
183 def test_on_default_resets(self, menu):
184 """on_default() resets all fields and calls return_fn."""
185 menu.sqrt_workers = "8"
186 menu.time_out = "60"
187 menu.sync_workers = "5"
188 menu.on_default(None)
189 assert menu.sqrt_workers == "1"
190 assert menu.time_out == "none"
191 assert menu.sync_workers == "0"
192 menu.return_fn.assert_called_once()
193
194
195 # =====================================================================
196 # get_size
197 # =====================================================================
198
199
200 class TestStressMenuGetSize:
201 def test_get_size(self, menu):
202 """get_size() returns tuple of (height, width)."""
203 size = menu.get_size()
204 assert isinstance(size, tuple)
205 assert len(size) == 2
206 assert size[1] == StressMenu.MAX_TITLE_LEN