Skip to content

Commit

Permalink
Use regular expression to specify interfaces to check
Browse files Browse the repository at this point in the history
Signed-off-by: mulhern <[email protected]>
  • Loading branch information
mulkieran committed Mar 6, 2024
1 parent 95775cd commit 15b80b0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
36 changes: 33 additions & 3 deletions scripts/monitor_dbus_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
with respect to their properties.
"""

_INTERFACE_RE = None
_MO = None
_SERVICE = None
_TOP_OBJECT = None
Expand All @@ -41,6 +42,7 @@
# isort: STDLIB
import argparse
import os
import re
import sys
import time
import xml.etree.ElementTree as ET
Expand Down Expand Up @@ -150,7 +152,14 @@ def _make_mo():
the result of calling Properties.GetAll on the top object for
selected interfaces.
"""

mos = _OBJECT_MANAGER.Methods.GetManagedObjects(_TOP_OBJECT, {})

mos = {
o: {k: v for k, v in d.items() if re.fullmatch(_INTERFACE_RE, k)}
for o, d in mos.items()
}

mos[_TOP_OBJECT_PATH] = {}

for interface in _TOP_OBJECT_INTERFACES:
Expand All @@ -170,6 +179,10 @@ def _interfaces_added(object_path, interfaces_added):
:param str object_path: D-Bus object path
:param dict interfaces_added: map of interfaces to D-Bus properties
"""
interfaces_added = {
k: v for k, v in interfaces_added.items() if re.fullmatch(_INTERFACE_RE, k)
}

if object_path == _TOP_OBJECT_PATH:
interfaces_added = {
k: v for k, v in interfaces_added.items() if k in _TOP_OBJECT_INTERFACES
Expand Down Expand Up @@ -202,6 +215,10 @@ def _interfaces_removed(object_path, interfaces):
:param str object_path: D-Bus object path
:param list interfaces: list of interfaces removed
"""
interfaces = {
k: v for k, v in interfaces.items() if re.fullmatch(_INTERFACE_RE, k)
}

if object_path == _TOP_OBJECT_PATH:
interfaces = {
k: v for k, v in interfaces.items() if k in _TOP_OBJECT_INTERFACES
Expand Down Expand Up @@ -248,6 +265,9 @@ def _properties_changed(*props_changed, object_path=None):
return

interface_name = props_changed[0]
if not re.fullmatch(_INTERFACE_RE, interface_name):
return

properties_changed = props_changed[1]
properties_invalidated = props_changed[2]

Expand Down Expand Up @@ -297,23 +317,26 @@ def _properties_changed(*props_changed, object_path=None):
except Exception as exc: # pylint: disable=broad-except
_CALLBACK_ERRORS.append(exc)

def _monitor(service, manager, manager_interfaces):
def _monitor(service, manager, manager_interfaces, interface_re):
"""
Monitor the signals and properties of the manager object.
:param str service: the service to monitor
:param str manager: object path that of the ObjectManager implementor
:param manager_interfaces: list of manager interfaces
:type manager_interfaces: list of str
:param interface_re: regular expression to match interfaces to check
:type interface_re: re.Pattern
"""

global _TOP_OBJECT, _TOP_OBJECT_PATH, _TOP_OBJECT_INTERFACES, _SERVICE, _MO # pylint: disable=global-statement
global _TOP_OBJECT, _TOP_OBJECT_PATH, _TOP_OBJECT_INTERFACES, _SERVICE, _MO, _INTERFACE_RE # pylint: disable=global-statement

dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
_SERVICE = service
_TOP_OBJECT_PATH = manager
_TOP_OBJECT_INTERFACES = manager_interfaces
_INTERFACE_RE = interface_re

while True:
try:
Expand Down Expand Up @@ -387,6 +410,13 @@ def _gen_parser():
help="interface belonging to the top object",
)

parser.add_argument(
"--only-check",
default=".*",
type=re.compile,
help="regular expression that restricts interfaces to check",
)

return parser

def main():
Expand All @@ -398,7 +428,7 @@ def main():

args = parser.parse_args()

_monitor(args.service, args.manager, args.top_interface)
_monitor(args.service, args.manager, args.top_interface, args.only_check)

if __name__ == "__main__":
main()
Expand Down
10 changes: 10 additions & 0 deletions testlib/infra.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,16 @@ def setUp(self):
+ 1
)
)

only_check = (
StratisDbus.BUS_NAME.replace(".", r"\.")
+ r"\."
+ ".*"
+ r"\."
+ f"r[0-{StratisDbus.REVISION_NUMBER}]"
)
command.append(f'--only-check="{only_check}"')

# pylint: disable=consider-using-with
try:
self.trace = subprocess.Popen(
Expand Down

0 comments on commit 15b80b0

Please sign in to comment.