1
Index: upower-0.9.19/src/linux/integration-test
2
===================================================================
3
--- upower-0.9.19.orig/src/linux/integration-test 2013-03-18 09:22:00.000000000 +0100
4
+++ upower-0.9.19/src/linux/integration-test 2013-03-18 09:25:35.714794057 +0100
6
sys.stderr.write('Skipping tests, PyGobject not available for Python 3, or missing GI typelibs: %s\n' % str(e))
10
+ from gi.repository import UMockdev
12
+ sys.stderr.write('Skipping tests, umockdev not available (https://launchpad.net/umockdev/)\n')
15
UP = 'org.freedesktop.UPower'
17
(UP_DEVICE_STATE_UNKNOWN,
19
# run from local build tree if we are in one, otherwise use system instance
20
builddir = os.getenv('top_builddir', '.')
21
if os.access (os.path.join(builddir, 'src', 'upowerd'), os.X_OK):
22
- daemon_path = os.path.join(builddir, 'src', 'upowerd')
23
+ cls.daemon_path = os.path.join(builddir, 'src', 'upowerd')
24
print('Testing binaries from local build tree')
26
print('Testing installed system binaries')
28
+ cls.daemon_path = None
29
with open('/usr/share/dbus-1/system-services/org.freedesktop.UPower.service') as f:
31
if line.startswith('Exec='):
32
- daemon_path = line.split('=', 1)[1].strip()
33
+ cls.daemon_path = line.split('=', 1)[1].strip()
35
- assert daemon_path, 'could not determine daemon path from D-BUS .service file'
36
+ assert cls.daemon_path, 'could not determine daemon path from D-BUS .service file'
38
# fail on CRITICALs on client side
39
GLib.log_set_always_fatal(GLib.LogLevelFlags.LEVEL_WARNING|
40
GLib.LogLevelFlags.LEVEL_ERROR|
41
GLib.LogLevelFlags.LEVEL_CRITICAL)
43
- # if we are root, test the real thing on the system bus, otherwise
44
- # start on the session bus
45
- if os.geteuid() == 0:
46
- # kill running daemons
47
- subprocess.call(['killall', 'upowerd'])
49
- cls.dbus = Gio.bus_get_sync(Gio.BusType.SYSTEM, None)
50
- cls.daemon_argv = [daemon_path]
51
- print('Testing as root on the system bus')
53
- cls.dbus = Gio.bus_get_sync(Gio.BusType.SESSION, None)
54
- cls.daemon_argv = [daemon_path, '--test']
55
+ # set up a fake system D-BUS
56
+ cls.test_bus = Gio.TestDBus.new(Gio.TestDBusFlags.NONE)
58
+ del os.environ['DBUS_SESSION_BUS_ADDRESS']
59
+ os.environ['DBUS_SYSTEM_BUS_ADDRESS'] = cls.test_bus.get_bus_address()
61
- # use dbus-lauch if possible
62
- print('Testing as user on the session bus')
63
+ cls.dbus = Gio.bus_get_sync(Gio.BusType.SYSTEM, None)
66
+ def tearDownClass(cls):
70
- '''Set up a local sysfs tree and determine paths.
71
+ '''Set up a local umockdev testbed.
73
- The sysfs tree is empty initially and needs to be populated with
75
+ The testbed is initially empty.
77
- self.sysfs = tempfile.mkdtemp()
78
+ self.testbed = UMockdev.Testbed.new()
87
- shutil.rmtree(self.sysfs)
89
# on failures, print daemon log
90
- if not self._outcomeForDoCleanups.success:
91
+ if not self._outcomeForDoCleanups.success and self.log:
92
with open(self.log.name) as f:
93
sys.stderr.write('\n-------------- daemon log: ----------------\n')
94
sys.stderr.write(f.read())
95
sys.stderr.write('------------------------------\n')
98
- # Methods for fake sysfs
101
- def add_device(self, subsystem, name, attributes, properties=None):
102
- '''Add a new device to the local sysfs tree.
104
- Return the device path.
106
- dev_dir = os.path.join(self.sysfs, 'devices', name)
107
- if not os.path.isdir(dev_dir):
108
- os.makedirs(dev_dir)
109
- class_dir = os.path.join(self.sysfs, 'class', subsystem)
110
- if not os.path.isdir(class_dir):
111
- os.makedirs(class_dir)
113
- os.symlink(os.path.join('..', '..', 'devices', name), os.path.join(class_dir, name))
114
- os.symlink(os.path.join('..', '..', 'class', subsystem), os.path.join(dev_dir, 'subsystem'))
116
- attributes['uevent'] = self._props_to_str(properties)
118
- for a, v in attributes.items():
119
- self.set_sys_attribute(dev_dir, a, v)
123
- def get_sys_attribute(self, devpath, name):
124
- '''Get device attribute from the local sysfs tree.'''
126
- with open(os.path.join(devpath, name), 'r') as f:
129
- def set_sys_attribute(self, devpath, name, value):
130
- '''Set device attribute in the local sysfs tree.'''
132
- with open(os.path.join(devpath, name), 'w') as f:
135
- def set_sys_property(self, devpath, name, value):
136
- '''Set device udev property in the local sysfs tree.'''
138
- prop_str = self.get_sys_attribute(devpath, 'uevent')
140
- for l in prop_str.splitlines():
141
- (k, v) = l.split('=')
142
- props[k] = v.rstrip()
144
- props[name] = value
146
- self.set_sys_attribute(devpath, 'uevent', self._props_to_str(props))
149
# Daemon control and D-BUS I/O
154
env = os.environ.copy()
155
env['G_DEBUG'] = 'fatal-criticals'
156
- env['SYSFS_PATH'] = self.sysfs
157
+ # note: Python doesn't propagate the setenv from Testbed.new(), so we
158
+ # have to do that ourselves
159
+ env['UMOCKDEV_DIR'] = self.testbed.get_root_dir()
160
self.log = tempfile.NamedTemporaryFile()
161
- self.daemon = subprocess.Popen(self.daemon_argv,
162
+ self.daemon = subprocess.Popen(['umockdev-wrapper', self.daemon_path, '-v'],
163
env=env, stdout=self.log, stderr=subprocess.STDOUT)
165
# wait until the daemon gets online
170
- ac = self.add_device('power_supply', 'AC',
171
- {'type': 'Mains', 'online': '1' })
172
+ ac = self.testbed.add_device('power_supply', 'AC', None,
173
+ ['type', 'Mains', 'online', '1' ], [])
176
devs = self.proxy.EnumerateDevices()
181
- self.set_sys_attribute(ac, 'online', '0')
182
+ self.testbed.set_attribute(ac, 'online', '0')
184
devs = self.proxy.EnumerateDevices()
185
self.assertEqual(len(devs), 1)
186
@@ -274,14 +225,14 @@
189
# offline AC + discharging battery
190
- bat0 = self.add_device('power_supply', 'BAT0',
191
- {'type': 'Battery',
193
- 'status': 'Discharging',
194
- 'energy_full': '60000000',
195
- 'energy_full_design': '80000000',
196
- 'energy_now': '48000000',
197
- 'voltage_now': '12000000'})
198
+ bat0 = self.testbed.add_device('power_supply', 'BAT0', None,
199
+ ['type', 'Battery',
201
+ 'status', 'Discharging',
202
+ 'energy_full', '60000000',
203
+ 'energy_full_design', '80000000',
204
+ 'energy_now', '48000000',
205
+ 'voltage_now', '12000000'], [])
208
devs = self.proxy.EnumerateDevices()
212
# offline AC + discharging low battery
213
- self.set_sys_attribute(bat0, 'energy_now', '1500000')
214
+ self.testbed.set_attribute(bat0, 'energy_now', '1500000')
216
self.assertEqual(self.get_dbus_property('OnBattery'), True)
217
self.assertEqual(self.get_dbus_property('OnLowBattery'), True)
221
# now connect AC again
222
- self.set_sys_attribute(ac, 'online', '1')
223
+ self.testbed.set_attribute(ac, 'online', '1')
225
devs = self.proxy.EnumerateDevices()
226
self.assertEqual(len(devs), 2)
227
@@ -328,23 +279,23 @@
228
'''Multiple batteries'''
230
# one well charged, one low
231
- bat0 = self.add_device('power_supply', 'BAT0',
232
- {'type': 'Battery',
234
- 'status': 'Discharging',
235
- 'energy_full': '60000000',
236
- 'energy_full_design': '80000000',
237
- 'energy_now': '48000000',
238
- 'voltage_now': '12000000'})
240
- bat1 = self.add_device('power_supply', 'BAT1',
241
- {'type': 'Battery',
243
- 'status': 'Discharging',
244
- 'energy_full': '60000000',
245
- 'energy_full_design': '80000000',
246
- 'energy_now': '1500000',
247
- 'voltage_now': '12000000'})
248
+ bat0 = self.testbed.add_device('power_supply', 'BAT0', None,
249
+ ['type', 'Battery',
251
+ 'status', 'Discharging',
252
+ 'energy_full', '60000000',
253
+ 'energy_full_design', '80000000',
254
+ 'energy_now', '48000000',
255
+ 'voltage_now', '12000000'], [])
257
+ bat1 = self.testbed.add_device('power_supply', 'BAT1', None,
258
+ ['type', 'Battery',
260
+ 'status', 'Discharging',
261
+ 'energy_full', '60000000',
262
+ 'energy_full_design', '80000000',
263
+ 'energy_now', '1500000',
264
+ 'voltage_now', '12000000'], [])
267
devs = self.proxy.EnumerateDevices()
271
# now set both to low
272
- self.set_sys_attribute(bat0, 'energy_now', '1500000')
273
+ self.testbed.set_attribute(bat0, 'energy_now', '1500000')
275
self.assertEqual(self.get_dbus_property('OnBattery'), True)
276
self.assertEqual(self.get_dbus_property('OnLowBattery'), True)
277
@@ -366,14 +317,14 @@
278
def test_unknown_battery_status(self):
279
'''Unknown battery charge status'''
281
- bat0 = self.add_device('power_supply', 'BAT0',
282
- {'type': 'Battery',
284
- 'status': 'unknown',
285
- 'energy_full': '60000000',
286
- 'energy_full_design': '80000000',
287
- 'energy_now': '48000000',
288
- 'voltage_now': '12000000'})
289
+ bat0 = self.testbed.add_device('power_supply', 'BAT0', None,
290
+ ['type', 'Battery',
292
+ 'status', 'unknown',
293
+ 'energy_full', '60000000',
294
+ 'energy_full_design', '80000000',
295
+ 'energy_now', '48000000',
296
+ 'voltage_now', '12000000'], [])
298
# with no other power sources, the OnBattery value here is really
299
# arbitrary, so don't test it. The only thing we know for sure is that
300
@@ -383,14 +334,14 @@
303
# However, if we have an AC, we can infer
304
- ac = self.add_device('power_supply', 'AC',
305
- {'type': 'Mains', 'online': '0' })
306
+ ac = self.testbed.add_device('power_supply', 'AC', None,
307
+ ['type', 'Mains', 'online', '0'], [])
309
self.assertEqual(self.get_dbus_property('OnBattery'), True)
310
self.assertEqual(self.get_dbus_property('OnLowBattery'), False)
313
- self.set_sys_attribute(ac, 'online', '1')
314
+ self.testbed.set_attribute(ac, 'online', '1')
316
self.assertEqual(self.get_dbus_property('OnBattery'), False)
317
self.assertEqual(self.get_dbus_property('OnLowBattery'), False)
318
@@ -401,15 +352,15 @@
320
energy_* is in uWh, while charge_* is in uAh.
322
- bat0 = self.add_device('power_supply', 'BAT0',
323
- {'type': 'Battery',
325
- 'status': 'Discharging',
326
- 'charge_full': '10500000',
327
- 'charge_full_design': '11000000',
328
- 'charge_now': '7875000',
329
- 'current_now': '787000',
330
- 'voltage_now': '12000000'})
331
+ bat0 = self.testbed.add_device('power_supply', 'BAT0', None,
332
+ ['type', 'Battery',
334
+ 'status', 'Discharging',
335
+ 'charge_full', '10500000',
336
+ 'charge_full_design', '11000000',
337
+ 'charge_now', '7875000',
338
+ 'current_now', '787000',
339
+ 'voltage_now', '12000000'], [])
342
devs = self.proxy.EnumerateDevices()
343
@@ -431,14 +382,14 @@
344
def test_battery_energy_charge_mixed(self):
345
'''battery which reports current energy, but full charge'''
347
- bat0 = self.add_device('power_supply', 'BAT0',
348
- {'type': 'Battery',
350
- 'status': 'Discharging',
351
- 'charge_full': '10500000',
352
- 'charge_full_design': '11000000',
353
- 'energy_now': '50400000',
354
- 'voltage_now': '12000000'})
355
+ bat0 = self.testbed.add_device('power_supply', 'BAT0', None,
356
+ ['type', 'Battery',
358
+ 'status', 'Discharging',
359
+ 'charge_full', '10500000',
360
+ 'charge_full_design', '11000000',
361
+ 'energy_now', '50400000',
362
+ 'voltage_now', '12000000'], [])
365
devs = self.proxy.EnumerateDevices()
366
@@ -461,12 +412,12 @@
367
'''UPS properties with and without AC'''
370
- ups0 = self.add_device('usb', 'hiddev0', {},
371
- {'DEVNAME': 'null', 'UPOWER_VENDOR': 'APC',
372
- 'UPOWER_BATTERY_TYPE': 'ups',
373
- 'UPOWER_FAKE_DEVICE': '1',
374
- 'UPOWER_FAKE_HID_CHARGING': '1',
375
- 'UPOWER_FAKE_HID_PERCENTAGE': '70'})
376
+ ups0 = self.testbed.add_device('usb', 'hiddev0', None, [],
377
+ ['DEVNAME', 'null', 'UPOWER_VENDOR', 'APC',
378
+ 'UPOWER_BATTERY_TYPE', 'ups',
379
+ 'UPOWER_FAKE_DEVICE', '1',
380
+ 'UPOWER_FAKE_HID_CHARGING', '1',
381
+ 'UPOWER_FAKE_HID_PERCENTAGE', '70'])
384
devs = self.proxy.EnumerateDevices()
388
# now switch to discharging UPS
389
- self.set_sys_property(ups0, 'UPOWER_FAKE_HID_CHARGING', '0')
390
+ self.testbed.set_property(ups0, 'UPOWER_FAKE_HID_CHARGING', '0')
393
devs = self.proxy.EnumerateDevices()
398
- self.set_sys_property(ups0, 'UPOWER_FAKE_HID_PERCENTAGE', '2')
399
+ self.testbed.set_property(ups0, 'UPOWER_FAKE_HID_PERCENTAGE', '2')
401
self.assertEqual(self.get_dbus_dev_property(ups0_up, 'Percentage'), 2.0)
402
self.assertEqual(self.get_dbus_dev_property(ups0_up, 'State'), UP_DEVICE_STATE_DISCHARGING)
406
# now add an offline AC, should still be on battery
407
- ac = self.add_device('power_supply', 'AC',
408
- {'type': 'Mains', 'online': '0' })
409
+ ac = self.testbed.add_device('power_supply', 'AC', None,
410
+ ['type', 'Mains', 'online', '0'], [])
412
devs = self.proxy.EnumerateDevices()
413
self.assertEqual(len(devs), 2)
417
# now plug in the AC, should switch to OnBattery=False
418
- self.set_sys_attribute(ac, 'online', '1')
419
+ self.testbed.set_attribute(ac, 'online', '1')
421
self.assertEqual(self.get_dbus_property('OnBattery'), False)
422
self.assertEqual(self.get_dbus_property('OnLowBattery'), False)