Skip to content

Commit 107ccd5

Browse files
committed
fix: update get_messages_additional method to correctly filter number of lines
Signed-off-by: Lasota, Adrian <adrian.lasota@intel.com>
1 parent 490527c commit 107ccd5

2 files changed

Lines changed: 52 additions & 6 deletions

File tree

mfd_dmesg/base.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,13 @@ def get_os_package_info(self) -> Union[OSPackageInfo, None]:
178178

179179
def get_messages_additional(
180180
self,
181-
service_name: str = None,
181+
service_name: str | None = None,
182182
lines: int = 1000,
183-
expected_return_codes: Iterable = frozenset({0}),
184-
additional_greps: Optional[List[str]] = None,
183+
expected_return_codes: Iterable[int] = frozenset({0}),
184+
additional_greps: list[str] | None = None,
185185
) -> str:
186-
"""Read the last lines of message buffer of the kernel (dmesg).
186+
"""
187+
Read the last lines of message buffer of the kernel (dmesg).
187188
188189
:param service_name: limits dmesg messages only to provided service
189190
:param lines: limit number of lines
@@ -195,7 +196,7 @@ def get_messages_additional(
195196
if not additional_greps:
196197
additional_greps = []
197198

198-
command = self._tool_exec
199+
command = f"{self._tool_exec} | tail -n {lines}"
199200
if service_name is not None:
200201
command += f" | grep '{service_name}'"
201202

@@ -204,7 +205,6 @@ def get_messages_additional(
204205
for additional_grep in additional_greps:
205206
grep_content = grep_content + f"\\|{additional_grep}" if grep_content else f"{additional_grep}"
206207
command += f" | grep -i '{grep_content}'"
207-
command += f" | tail -n {lines}"
208208
out = self._connection.execute_command(command, shell=True, expected_return_codes=expected_return_codes).stdout
209209
return out.strip()
210210

tests/unit/test_mfd_dmesg/test_base.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,30 @@ def test_get_messages_imc_acc_command_level_error_service_name(self, dmesg):
223223
'dmesg | grep -v "Step" | grep -iE "error|fail" | grep \'ix1\'', shell=True, expected_return_codes={0, 1}
224224
)
225225

226+
def test_get_messages_imc_acc_command_level_warning(self, dmesg):
227+
dmesg._connection.execute_command.side_effect = [
228+
ConnectionCalledProcessError(returncode=0, cmd=""),
229+
ConnectionCompletedProcess(return_code=0, args="command", stdout="", stderr="stderr"),
230+
]
231+
dmesg.get_messages(level=DmesgLevelOptions.WARNINGS)
232+
dmesg._connection.execute_command.assert_called_with(
233+
'dmesg | grep -v "Step" | grep -iE "warning" ',
234+
shell=True,
235+
expected_return_codes={0, 1},
236+
)
237+
238+
def test_get_messages_imc_acc_command_level_critical(self, dmesg):
239+
dmesg._connection.execute_command.side_effect = [
240+
ConnectionCalledProcessError(returncode=0, cmd=""),
241+
ConnectionCompletedProcess(return_code=0, args="command", stdout="", stderr="stderr"),
242+
]
243+
dmesg.get_messages(level=DmesgLevelOptions.CRITICAL)
244+
dmesg._connection.execute_command.assert_called_with(
245+
"dmesg | grep -v \"Step\" | grep crit ",
246+
shell=True,
247+
expected_return_codes={0, 1},
248+
)
249+
226250
def test_get_messages_without_level(self, dmesg):
227251
output = dedent(
228252
"""
@@ -267,6 +291,28 @@ def test_get_messages_additional(self, dmesg):
267291
)
268292
assert expected == dmesg.get_messages_additional()
269293

294+
def test_get_messages_additional_command_check(self, dmesg):
295+
"""
296+
Test to verify that correct command is executed.
297+
298+
when getting additional messages with service name and without level filtering.
299+
"""
300+
output = dedent(
301+
"""
302+
[15197896.276724] IPv6: ens785: IPv6 duplicate address 24:1:1::1
303+
used by aa:bb:cc:dd:ee:ff detected!
304+
[15222488.275756] ice 0000:4e:00.0 ens786: NIC Link is Down
305+
[15222488.308184] ice 0000:4b:00.0 ens785: NIC Link is Down
306+
[15222506.769174] ice 0000:4e:00.0 ens786: A parallel fault was detected."""
307+
)
308+
dmesg._connection.execute_command.return_value = ConnectionCompletedProcess(
309+
return_code=0, args="command", stdout=output, stderr="stderr"
310+
)
311+
dmesg.get_messages_additional(lines=5, service_name="ice")
312+
dmesg._connection.execute_command.assert_called_with(
313+
f"{dmesg._tool_exec} | tail -n 5 | grep 'ice'", shell=True, expected_return_codes={0}
314+
)
315+
270316
def test_verify_messages(self, dmesg):
271317
output = dedent(
272318
"""

0 commit comments

Comments
 (0)