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
|
#!/usr/bin/python3
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your option) any
# later version. See http://www.gnu.org/copyleft/lgpl.html for the full text
# of the license.
__author__ = 'Martin Pitt'
__email__ = 'martin.pitt@ubuntu.com'
__copyright__ = '(c) 2012 Canonical Ltd.'
__license__ = 'LGPL 3+'
import unittest
import sys
import subprocess
import dbus
import dbusmock
p = subprocess.Popen(['which', 'upower'], stdout=subprocess.PIPE)
p.communicate()
have_upower = (p.returncode == 0)
class TestCLI(dbusmock.DBusTestCase):
'''Test running dbusmock from the command line'''
@classmethod
def setUpClass(klass):
klass.start_system_bus()
klass.start_session_bus()
klass.system_con = klass.get_dbus(True)
klass.session_con = klass.get_dbus()
def setUp(self):
self.p_mock = None
def tearDown(self):
if self.p_mock:
if self.p_mock.stdout:
self.p_mock.stdout.close()
if self.p_mock.stderr:
self.p_mock.stdout.close()
self.p_mock.terminate()
self.p_mock.wait()
self.p_mock = None
def test_session_bus(self):
self.p_mock = subprocess.Popen([sys.executable, '-m', 'dbusmock',
'com.example.Test', '/', 'TestIface'])
self.wait_for_bus_object('com.example.Test', '/')
def test_system_bus(self):
self.p_mock = subprocess.Popen([sys.executable, '-m', 'dbusmock',
'--system', 'com.example.Test', '/', 'TestIface'])
self.wait_for_bus_object('com.example.Test', '/', True)
def test_template_system(self):
self.p_mock = subprocess.Popen([sys.executable, '-m', 'dbusmock',
'--system', '-t', 'upower'],
stdout=subprocess.PIPE,
universal_newlines=True)
self.wait_for_bus_object('org.freedesktop.UPower', '/org/freedesktop/UPower', True)
# check that it actually ran the template, if we have upower
if have_upower:
out = subprocess.check_output(['upower', '--dump'],
universal_newlines=True)
self.assertRegex(out, 'on-battery:\s+no')
mock_out = self.p_mock.stdout.readline()
self.assertTrue('EnumerateDevices' in mock_out or 'GetAll' in mock_out,
mock_out)
def test_object_manager(self):
self.p_mock = subprocess.Popen([sys.executable, '-m', 'dbusmock',
'-m', 'com.example.Test', '/', 'TestIface'],
stdout=subprocess.PIPE)
self.wait_for_bus_object('com.example.Test', '/')
obj = self.session_con.get_object('com.example.Test', '/')
if_om = dbus.Interface(obj, dbusmock.OBJECT_MANAGER_IFACE)
self.assertEqual(if_om.GetManagedObjects(), {})
# add a new object, should appear
obj.AddObject('/a/b', 'org.Test', {'name': 'foo'}, dbus.Array([], signature='(ssss)'))
self.assertEqual(if_om.GetManagedObjects(), {'/a/b': {'org.Test': {'name': 'foo'}}})
def test_no_args(self):
p = subprocess.Popen([sys.executable, '-m', 'dbusmock'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
universal_newlines=True)
(out, err) = p.communicate()
self.assertEqual(out, '')
self.assertTrue('must specify NAME' in err, err)
self.assertNotEqual(p.returncode, 0)
def test_help(self):
p = subprocess.Popen([sys.executable, '-m', 'dbusmock', '--help'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
universal_newlines=True)
(out, err) = p.communicate()
self.assertEqual(err, '')
self.assertTrue('INTERFACE' in out, out)
self.assertTrue('--system' in out, out)
self.assertEqual(p.returncode, 0)
if __name__ == '__main__':
# avoid writing to stderr
unittest.main(testRunner=unittest.TextTestRunner(stream=sys.stdout, verbosity=2))
|