94
94
log.setLevel(logging.DEBUG)
97
bus = dbus.SystemBus()
98
log.debug("D-Bus: Connect to: (busname=%r, path=%r)",
99
dbus_busname, server_dbus_path)
100
mandos_dbus_objc = bus.get_object(dbus_busname,
102
except dbus.exceptions.DBusException:
103
log.critical("Could not connect to Mandos server")
106
mandos_serv = dbus.Interface(mandos_dbus_objc,
107
dbus_interface=server_dbus_interface)
96
bus = dbus.SystemBus()
98
mandos_dbus_object = get_mandos_dbus_object(bus)
100
mandos_serv = dbus.Interface(
101
mandos_dbus_object, dbus_interface=server_dbus_interface)
108
102
mandos_serv_object_manager = dbus.Interface(
109
mandos_dbus_objc, dbus_interface=dbus.OBJECT_MANAGER_IFACE)
103
mandos_dbus_object, dbus_interface=dbus.OBJECT_MANAGER_IFACE)
112
106
log.debug("D-Bus: %s:%s:%s.GetManagedObjects()", dbus_busname,
436
430
options.remove = True
433
def get_mandos_dbus_object(bus):
435
log.debug("D-Bus: Connect to: (busname=%r, path=%r)",
436
dbus_busname, server_dbus_path)
437
mandos_dbus_object = bus.get_object(dbus_busname,
439
except dbus.exceptions.DBusException:
440
log.critical("Could not connect to Mandos server")
443
return mandos_dbus_object
439
446
class SilenceLogger(object):
440
447
"Simple context manager to silence a particular logger"
441
448
def __init__(self, loggername):
988
995
self.check_option_syntax(options)
998
class Test_get_mandos_dbus_object(unittest.TestCase):
999
def test_calls_and_returns_get_object_on_bus(self):
1000
class MockBus(object):
1002
def get_object(mockbus_self, busname, dbus_path):
1003
# Note that "self" is still the testcase instance,
1004
# this MockBus instance is in "mockbus_self".
1005
self.assertEqual(busname, dbus_busname)
1006
self.assertEqual(dbus_path, server_dbus_path)
1007
mockbus_self.called = True
1010
mockbus = get_mandos_dbus_object(bus=MockBus())
1011
self.assertIsInstance(mockbus, MockBus)
1012
self.assertTrue(mockbus.called)
1014
def test_logs_and_exits_on_dbus_error(self):
1015
class MockBusFailing(object):
1016
def get_object(self, busname, dbus_path):
1017
raise dbus.exceptions.DBusException("Test")
1019
# assertLogs only exists in Python 3.4
1020
if hasattr(self, "assertLogs"):
1021
with self.assertLogs(log, logging.CRITICAL):
1022
with self.assertRaises(SystemExit) as e:
1023
bus = get_mandos_dbus_object(bus=MockBus())
1025
critical_filter = self.CriticalFilter()
1026
log.addFilter(critical_filter)
1028
with self.assertRaises(SystemExit) as e:
1029
get_mandos_dbus_object(bus=MockBusFailing())
1031
log.removeFilter(critical_filter)
1032
self.assertTrue(critical_filter.found)
1033
if isinstance(e.exception.code, int):
1034
self.assertNotEqual(e.exception.code, 0)
1036
self.assertIsNotNone(e.exception.code)
1038
class CriticalFilter(logging.Filter):
1039
"""Don't show, but register, critical messages"""
1041
def filter(self, record):
1042
is_critical = record.levelno >= logging.CRITICAL
1043
self.found = is_critical or self.found
1044
return not is_critical
991
1047
class Test_SilenceLogger(unittest.TestCase):
992
1048
loggername = "mandos-ctl.Test_SilenceLogger"
993
1049
log = logging.getLogger(loggername)