Skip to content

Commit

Permalink
journald: add unit parameter to is_match and count
Browse files Browse the repository at this point in the history
Usually, we want to only match logs for our program.
  • Loading branch information
pbrezina committed Sep 10, 2024
1 parent 5a81eab commit f935c8a
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions pytest_mh/utils/journald.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,25 +126,29 @@ def journalctl(

return self.host.conn.exec(["journalctl"] + cli.args(builder) + args, raise_on_error=False)

def is_match(self, pattern: str) -> bool:
def is_match(self, pattern: str, unit: str | None = None) -> bool:
"""
Search the logs for a pattern.
:param pattern: Pattern to be searched for
:type pattern: str
:param unit: Search only messages for given systemd unit, defaults to None
:type unit: str | None, optional
:return: True, if pattern found
:rtype: bool
"""
return self.journalctl(grep=pattern).rc == 0
return self.journalctl(grep=pattern, unit=unit).rc == 0

def count(self, pattern: str) -> int:
def count(self, pattern: str, unit: str | None = None) -> int:
"""
Search the logs for a pattern and return number of occurrences.
:param pattern: Pattern to be searched for
:type pattern: str
:param unit: Search only messages for given systemd unit, defaults to None
:type unit: str | None, optional
:return: Number of occurrences of the pattern
:rtype: int
"""
process = self.journalctl(grep=pattern)
process = self.journalctl(grep=pattern, unit=unit)
return len(process.stdout_lines) if process.rc == 0 else 0

0 comments on commit f935c8a

Please sign in to comment.