102
102
mandos_serv_object_manager = dbus.Interface(
103
103
mandos_dbus_object, dbus_interface=dbus.OBJECT_MANAGER_IFACE)
105
log.debug("D-Bus: %s:%s:%s.GetManagedObjects()", dbus_busname,
106
server_dbus_path, dbus.OBJECT_MANAGER_IFACE)
108
with SilenceLogger("dbus.proxies"):
109
managed_objects = (mandos_serv_object_manager
110
.GetManagedObjects())
111
except dbus.exceptions.DBusException as e:
112
log.critical("Failed to access Mandos server through D-Bus:"
105
managed_objects = get_managed_objects(mandos_serv_object_manager)
117
108
for path, ifs_and_props in managed_objects.items():
446
437
return mandos_dbus_object
440
def get_managed_objects(object_manager):
441
log.debug("D-Bus: %s:%s:%s.GetManagedObjects()", dbus_busname,
442
server_dbus_path, dbus.OBJECT_MANAGER_IFACE)
444
with SilenceLogger("dbus.proxies"):
445
managed_objects = object_manager.GetManagedObjects()
446
except dbus.exceptions.DBusException as e:
447
log.critical("Failed to access Mandos server through D-Bus:"
450
return managed_objects
449
453
class SilenceLogger(object):
450
454
"Simple context manager to silence a particular logger"
451
455
def __init__(self, loggername):
1047
1051
return not is_critical
1054
class Test_get_managed_objects(unittest.TestCase):
1055
def test_calls_and_returns_GetManagedObjects(self):
1056
managed_objects = {"/clients/foo": { "Name": "foo"}}
1057
class MockObjectManager(object):
1059
def GetManagedObjects():
1060
return managed_objects
1061
retval = get_managed_objects(MockObjectManager())
1062
self.assertDictEqual(managed_objects, retval)
1064
def test_logs_and_exits_on_dbus_error(self):
1065
class MockObjectManagerFailing(object):
1067
def GetManagedObjects():
1068
raise dbus.exceptions.DBusException("Test")
1070
if hasattr(self, "assertLogs"):
1071
with self.assertLogs(log, logging.CRITICAL):
1072
with self.assertRaises(SystemExit):
1073
get_managed_objects(MockObjectManagerFailing())
1075
critical_filter = self.CriticalFilter()
1076
log.addFilter(critical_filter)
1078
with self.assertRaises(SystemExit) as e:
1079
get_managed_objects(MockObjectManagerFailing())
1081
log.removeFilter(critical_filter)
1082
self.assertTrue(critical_filter.found)
1083
if isinstance(e.exception.code, int):
1084
self.assertNotEqual(e.exception.code, 0)
1086
self.assertIsNotNone(e.exception.code)
1088
class CriticalFilter(logging.Filter):
1089
"""Don't show, but register, critical messages"""
1091
def filter(self, record):
1092
is_critical = record.levelno >= logging.CRITICAL
1093
self.found = is_critical or self.found
1094
return not is_critical
1050
1097
class Test_SilenceLogger(unittest.TestCase):
1051
1098
loggername = "mandos-ctl.Test_SilenceLogger"
1052
1099
log = logging.getLogger(loggername)