1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Test index handling."""
from collections import namedtuple
import os
import re
import sys
import unittest
import dbus
from gi.repository import GLib
from aptdaemon import worker
from aptdaemon import core
from aptdaemon import enums
from aptdaemon import test
REGEX_SIG = "([ibxsdt])|(a{[ibxsdt]+?})|(a[ixbsdt]+?)|(\([ibxsdt]+?\))"
REGEX_IFACE = r"\n(org\.debian\.apt[a-z\.]*) --- "
REGEX_ATTRIB = (r"\n\.\.\s+attribute::\s+(?P<name>[a-zA-Z]+)\s+:\s+"
"(?P<sig>[a-z\(\)\{\}]+)")
# Setup the DBus main loop
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
DOC_PATH = os.path.join(test.get_tests_dir(), "../doc")
class DBusTypeTest(test.AptDaemonTestCase):
"""Make sure that the specified types are returned over D-Bus."""
def setUp(self):
# Extract the property type specification from the documentation
self.ifaces = {}
with open(os.path.join(DOC_PATH, "source/dbus.rst")) as rst_file:
docu = rst_file.read()
doc = ""
iface = ""
for match in re.split(REGEX_IFACE, docu, re.MULTILINE):
if match.startswith("org.debian.apt"):
iface = match
self.ifaces[iface] = {}
doc = ""
else:
doc = match
if doc and iface:
for match_attrib in re.finditer(REGEX_ATTRIB, doc):
name = match_attrib.group("name")
sig = match_attrib.group("sig")
self.ifaces[iface][name] = sig
self.start_dbus_daemon()
self.dbus = dbus.bus.BusConnection(self.dbus_address)
self.loop = GLib.MainLoop()
self.error = None
def _on_property_changed(self, name, value, iface):
if name == "Progress" and value == 100:
self.loop.quit()
try:
self._check_property_type(iface, name, value)
except:
self.loop.quit()
raise
def _check_property_type(self, iface, name, value, signature=None):
if signature is None:
signature = self.ifaces[iface][name]
if isinstance(value, dbus.String):
self.assertEqual(signature, "s",
"Property %s on %s doesnt' comply with the "
"spec: %s" % (name, iface, value))
elif isinstance(value, dbus.String):
self.assertEqual(signature, "s",
"Property %s on %s doesnt' comply with the "
"spec: %s" % (name, iface, value))
elif isinstance(value, dbus.Int32):
self.assertEqual(signature, "i",
"Property %s on %s doesnt' comply with the "
"spec: %s" % (name, iface, value))
elif isinstance(value, dbus.Int64):
self.assertEqual(signature, "x",
"Property %s on %s doesnt' comply with the "
"spec: %s" % (name, iface, value))
elif isinstance(value, dbus.UInt64):
self.assertEqual(signature, "t",
"Property %s on %s doesnt' comply with the "
"spec: %s" % (name, iface, value))
elif isinstance(value, dbus.Double):
self.assertEqual(signature, "d",
"Property %s on %s doesnt' comply with the "
"spec: %s" % (name, iface, value))
elif isinstance(value, dbus.Boolean):
self.assertEqual(signature, "b",
"Property %s on %s doesnt' comply with the "
"spec: %s" % (name, iface, value))
elif isinstance(value, dbus.Dictionary):
self.assertEqual(signature, "a{%s}" % value.signature,
"Property %s on %s doesnt' comply with the "
"spec: %s" % (name, iface, value))
elif isinstance(value, dbus.Struct):
if value.signature:
self.assertEqual(signature, "s(%s)" % value.signature,
"Property %s on %s doesnt' comply with the "
"spec: %s" % (name, iface, value))
else:
# The dbus proxy doesn't set the signature property
for val, sig in map(lambda x, y: (x, y), value,
["".join(matches) for matches in
re.findall(REGEX_SIG, signature[1:-1])]):
self._check_property_type(iface, name, val, sig)
elif isinstance(value, dbus.Array):
self.assertEqual(signature, "a%s" % value.signature,
"Property %s on %s doesnt' comply with the "
"spec: %s" % (name, iface, value))
else:
raise Exception("Unkown type %s for property %s of %s" %
(type(value), name, iface))
def _error_cb(self, error):
"""Errback of the GetAll call."""
self.loop.quit()
raise error
def _get_all_cb(self, iface, props):
"""Callback of the GetAll call."""
try:
for name, value in props.items():
self._check_property_type(iface, name, value)
except Exception as error:
self.error = error
raise
finally:
self.loop.quit()
@unittest.skip("Requires to be convert to a C based test client")
def test_transaction_properties(self):
"""Test object properties."""
trans = core.Transaction(None, enums.ROLE_REMOVE_PACKAGES, None,
os.getpid(), os.getuid(), sys.argv[0],
"org.debian.apt.test", bus=self.dbus)
proxy = self.dbus.get_object(core.APTDAEMON_DBUS_INTERFACE,
trans.tid)
iface = core.APTDAEMON_TRANSACTION_DBUS_INTERFACE
proxy.GetAll(iface,
reply_handler=lambda x: self._get_all_cb(iface, x),
error_handler=self._error_cb,
dbus_interface=dbus.PROPERTIES_IFACE)
self.loop.run()
self.assertEqual(self.error, None, self.error)
@unittest.skip("Requires to be convert to a C based test client")
def test_transaction_signals(self):
"""Test signal emittion."""
trans = core.Transaction(None, enums.ROLE_COMMIT_PACKAGES, None,
os.getpid(), os.getuid(), sys.argv[0],
"org.debian.apt.test", bus=self.dbus,
packages=[["silly-base"], [], [], [], [], []])
proxy = self.dbus.get_object("org.debian.apt", trans.tid)
proxy.connect_to_signal("PropertyChanged",
self._on_property_changed,
dbus_interface="org.debian.apt.transaction",
interface_keyword="iface")
chroot = test.Chroot()
self.addCleanup(chroot.remove)
chroot.setup()
chroot.add_test_repository()
apt_worker = worker.AptWorker(load_plugins=False,
chroot=chroot.path)
apt_worker.run(trans)
self.loop.run()
self.assertEqual(self.error, None)
@unittest.skip("Requires to be convert to a C based test client")
def test_aptdaemon_properties(self):
"""Test aptdaemon properties."""
Options = namedtuple("Options", "dummy")
opt = Options(True)
self.daemon = core.AptDaemon(opt, bus=self.dbus)
proxy = self.dbus.get_object(core.APTDAEMON_DBUS_SERVICE,
core.APTDAEMON_DBUS_PATH)
iface = core.APTDAEMON_DBUS_INTERFACE
proxy.GetAll(iface,
reply_handler=lambda x: self._get_all_cb(iface, x),
error_handler=self._error_cb,
dbus_interface=dbus.PROPERTIES_IFACE)
self.loop.run()
self.assertEqual(self.error, None)
if __name__ == "__main__":
unittest.main()
# vim: ts=4 et sts=4
|