-
Notifications
You must be signed in to change notification settings - Fork 140
/
udiskstestcase.py
615 lines (478 loc) · 20.4 KB
/
udiskstestcase.py
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
from __future__ import print_function
import unittest
import dbus
import subprocess
import os
import time
import re
import sys
from datetime import datetime
from systemd import journal
if sys.version_info.major == 3 and sys.version_info.minor >= 3:
from time import monotonic
else:
from monotonic import monotonic
import gi
gi.require_version('GUdev', '1.0')
from gi.repository import GUdev
test_devs = None
FLIGHT_RECORD_FILE = "flight_record.log"
def run_command(command):
res = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out, err = res.communicate()
if res.returncode != 0:
output = out.decode().strip() + "\n\n" + err.decode().strip()
else:
output = out.decode().strip()
return (res.returncode, output)
def get_call_long(call):
def call_long(*args, **kwargs):
"""Do an async call with a very long timeout (unless specified otherwise)"""
kwargs['timeout'] = 100 # seconds
return call(*args, **kwargs)
return call_long
def get_version_from_lsb():
ret, out = run_command("lsb_release -rs")
if ret != 0:
raise RuntimeError("Cannot get distro version from lsb_release output: '%s'" % out)
return out.split(".")[0]
def get_version_from_pretty_name(pretty_name):
""" Try to get distro and version from 'OperatingSystemPrettyName'
hostname property.
It should look like this:
- "Debian GNU/Linux 9 (stretch)"
- "Fedora 27 (Workstation Edition)"
- "CentOS Linux 7 (Core)"
So just return first word as distro and first number as version.
"""
distro = pretty_name.split()[0].lower()
match = re.search(r"\d+", pretty_name)
if match is not None:
version = match.group(0)
else:
version = get_version_from_lsb()
return (distro, version)
def get_version():
""" Try to get distro and version
"""
bus = dbus.SystemBus()
# get information about the distribution from systemd (hostname1)
sys_info = bus.get_object("org.freedesktop.hostname1", "/org/freedesktop/hostname1")
cpe = str(sys_info.Get("org.freedesktop.hostname1", "OperatingSystemCPEName", dbus_interface=dbus.PROPERTIES_IFACE))
if cpe:
# 2nd to 4th fields from e.g. "cpe:/o:fedoraproject:fedora:25" or "cpe:/o:redhat:enterprise_linux:7.3:GA:server"
_project, distro, version = tuple(cpe.split(":")[2:5])
# we want just the major version, so remove all decimal places (if any)
version = str(int(float(version)))
else:
pretty_name = str(sys_info.Get("org.freedesktop.hostname1", "OperatingSystemPrettyName", dbus_interface=dbus.PROPERTIES_IFACE))
distro, version = get_version_from_pretty_name(pretty_name)
return (distro, version)
def skip_on(skip_on_distros, skip_on_version="", reason=""):
"""A function returning a decorator to skip some test on a given distribution-version combination
:param skip_on_distros: distro(s) to skip the test on
:type skip_on_distros: str or tuple of str
:param str skip_on_version: version of distro(s) to skip the tests on (only
checked on distribution match)
"""
if isinstance(skip_on_distros, str):
skip_on_distros = (skip_on_distros,)
distro, version = get_version()
def decorator(func):
if distro in skip_on_distros and (not skip_on_version or skip_on_version == version):
msg = "not supported on this distribution in this version" + (": %s" % reason if reason else "")
return unittest.skip(msg)(func)
else:
return func
return decorator
def unstable_test(test):
"""Decorator for unstable tests
Failures of tests decorated with this decorator are silently ignored unless
the ``UNSTABLE_TESTS_FATAL`` environment variable is defined.
"""
def decorated_test(*args):
try:
test(*args)
except unittest.SkipTest:
# make sure skipped tests are just skipped as usual
raise
except Exception as e:
# and swallow everything else, just report a failure of an unstable
# test, unless told otherwise
if "UNSTABLE_TESTS_FATAL" in os.environ:
raise
print("unstable-fail: Ignoring exception '%s'\n" % e, end="", file=sys.stderr)
return decorated_test
class DBusProperty(object):
TIMEOUT = 5
def __init__(self, obj, iface, prop):
self.obj = obj
self.iface = iface
self.prop = prop
self._value = None
@property
def value(self):
if self._value is None:
self._update_value()
return self._value
def _update_value(self):
self._value = self.obj.Get(self.iface, self.prop, dbus_interface=dbus.PROPERTIES_IFACE)
def _check(self, timeout, check_fn):
for _ in range(int(timeout / 0.5)):
try:
self._update_value()
if check_fn(self.value):
return True
except Exception:
# ignore all exceptions -- they might be result of property
# not having the expected type (e.g. 'None' when checking for len)
pass
time.sleep(0.5)
return False
def assertEqual(self, value, timeout=TIMEOUT, getter=None):
if getter is not None:
check_fn = lambda x: getter(x) == value
else:
check_fn = lambda x: x == value
ret = self._check(timeout, check_fn)
if not ret:
if getter is not None:
raise AssertionError('%s != %s' % (getter(self._value), value))
else:
raise AssertionError('%s != %s' % (self._value, value))
def assertNotEqual(self, value, timeout=TIMEOUT, getter=None):
if getter is not None:
check_fn = lambda x: getter(x) != value
else:
check_fn = lambda x: x != value
ret = self._check(timeout, check_fn)
if not ret:
if getter is not None:
raise AssertionError('%s == %s' % (getter(self._value), value))
else:
raise AssertionError('%s == %s' % (self._value, value))
def assertAlmostEqual(self, value, delta, timeout=TIMEOUT, getter=None):
if getter is not None:
check_fn = lambda x: abs(getter(x) - value) <= delta
else:
check_fn = lambda x: abs(x - value) <= delta
ret = self._check(timeout, check_fn)
if not ret:
if getter is not None:
raise AssertionError('%s is not almost equal to %s (delta = %s)' % (getter(self._value),
value, delta))
else:
raise AssertionError('%s is not almost equal to %s (delta = %s)' % (self._value,
value, delta))
def assertGreater(self, value, timeout=TIMEOUT):
check_fn = lambda x: x > value
ret = self._check(timeout, check_fn)
if not ret:
raise AssertionError('%s is not greater than %s' % (self._value, value))
def assertLess(self, value, timeout=TIMEOUT):
check_fn = lambda x: x < value
ret = self._check(timeout, check_fn)
if not ret:
raise AssertionError('%s is not less than %s' % (self._value, value))
def assertIn(self, lst, timeout=TIMEOUT):
check_fn = lambda x: x in lst
ret = self._check(timeout, check_fn)
if not ret:
raise AssertionError('%s not found in %s' % (self._value, lst))
def assertNotIn(self, lst, timeout=TIMEOUT):
check_fn = lambda x: x not in lst
ret = self._check(timeout, check_fn)
if not ret:
raise AssertionError('%s unexpectedly found in %s' % (self._value, lst))
def assertTrue(self, timeout=TIMEOUT):
check_fn = lambda x: bool(x)
ret = self._check(timeout, check_fn)
if not ret:
raise AssertionError('%s is not true' % self._value)
def assertFalse(self, timeout=TIMEOUT):
check_fn = lambda x: not bool(x)
ret = self._check(timeout, check_fn)
if not ret:
raise AssertionError('%s is not false' % self._value)
def assertIsNone(self, timeout=TIMEOUT):
check_fn = lambda x: x is None
ret = self._check(timeout, check_fn)
if not ret:
raise AssertionError('%s is not None' % self._value)
def assertIsNotNone(self, timeout=TIMEOUT):
check_fn = lambda x: x is not None
ret = self._check(timeout, check_fn)
if not ret:
raise AssertionError('unexpectedly None')
def assertLen(self, length, timeout=TIMEOUT):
check_fn = lambda x: len(x) == length
ret = self._check(timeout, check_fn)
if not ret:
if not hasattr(self._value, '__len__'):
raise AssertionError('%s has no length' % type(self._value))
else:
raise AssertionError('Expected length %d, but %s has length %d' % (length,
self._value,
len(self._value)))
def assertContains(self, member, timeout=TIMEOUT):
check_fn = lambda x: member in x
ret = self._check(timeout, check_fn)
if not ret:
raise AssertionError('%s does not contain %s' % (self._value, member))
class UdisksTestCase(unittest.TestCase):
iface_prefix = None
path_prefix = None
bus = None
vdevs = None
distro = (None, None, None) # (project, distro_name, version)
no_options = dbus.Dictionary(signature="sv")
@classmethod
def setUpClass(self):
self.iface_prefix = 'org.freedesktop.UDisks2'
self.path_prefix = '/org/freedesktop/UDisks2'
self.bus = dbus.SystemBus()
self.distro = get_version()
self._orig_call_async = self.bus.call_async
self._orig_call_blocking = self.bus.call_blocking
self.bus.call_async = get_call_long(self._orig_call_async)
self.bus.call_blocking = get_call_long(self._orig_call_blocking)
self.vdevs = test_devs
assert len(self.vdevs) > 3;
@classmethod
def tearDownClass(self):
self.bus.call_async = self._orig_call_async
self.bus.call_blocking = self._orig_call_blocking
def run(self, *args):
record = []
now = datetime.now()
now_mono = monotonic()
with open(FLIGHT_RECORD_FILE, "a") as record_f:
record_f.write("================%s[%0.8f] %s.%s.%s================\n" % (now.strftime('%Y-%m-%d %H:%M:%S'),
now_mono,
self.__class__.__module__,
self.__class__.__name__,
self._testMethodName))
with JournalRecorder("journal", record):
with CmdFlightRecorder("udisksctl monitor", ["udisksctl", "monitor"], record):
with CmdFlightRecorder("udevadm monitor", ["udevadm", "monitor"], record):
super(UdisksTestCase, self).run(*args)
record_f.write("".join(record))
self.udev_settle()
@classmethod
def get_object(self, path_suffix):
# if given full path, just use it, otherwise prepend the prefix
if path_suffix.startswith(self.path_prefix):
path = path_suffix
else:
path = self.path_prefix + path_suffix
try:
# self.iface_prefix is the same as the DBus name we acquire
obj = self.bus.get_object(self.iface_prefix, path)
except:
obj = None
return obj
@classmethod
def get_interface(self, obj, iface_suffix):
"""Get interface for the given object either specified by an object path suffix
(appended to the common UDisks2 prefix) or given as the object
itself.
:param obj: object to get the interface for
:type obj: str or dbus.proxies.ProxyObject
:param iface_suffix: suffix appended to the common UDisks2 interface prefix
:type iface_suffix: str
"""
if isinstance(obj, str):
obj = self.get_object(obj)
return dbus.Interface(obj, self.iface_prefix + iface_suffix)
@classmethod
def get_property(self, obj, iface_suffix, prop):
return DBusProperty(obj, self.iface_prefix + iface_suffix, prop)
@classmethod
def get_property_raw(self, obj, iface_suffix, prop):
res = obj.Get(self.iface_prefix + iface_suffix, prop, dbus_interface=dbus.PROPERTIES_IFACE)
return res
@classmethod
def get_device(self, dev_name):
"""Get block device object for a given device (e.g. "sda")"""
dev = self.get_object('/block_devices/' + os.path.basename(dev_name))
return dev
@classmethod
def get_drive_name(self, device):
"""Get drive name for the given block device object"""
drive_name = self.get_property_raw(device, '.Block', 'Drive').split('/')[-1]
return drive_name
@classmethod
def udev_settle(self):
self.run_command('udevadm settle')
@classmethod
def wipe_fs(self, device):
for _ in range(10):
ret, _out = self.run_command('wipefs -a %s' % device)
if ret == 0:
return True
time.sleep(1)
return False
@classmethod
def read_file(self, filename):
with open(filename, 'r') as f:
content = f.read()
return content
@classmethod
def write_file(self, filename, content, ignore_nonexistent=False, binary=False):
try:
with open(filename, 'wb' if binary else 'w') as f:
f.write(content)
except OSError as e:
if not ignore_nonexistent:
raise e
@classmethod
def remove_file(self, filename, ignore_nonexistent=False):
try:
os.remove(filename)
except OSError as e:
if not ignore_nonexistent:
raise e
@classmethod
def run_command(self, command):
return run_command(command)
@classmethod
def check_module_loaded(self, module):
manager_obj = self.get_object('/Manager')
manager = self.get_interface(manager_obj, '.Manager')
manager_intro = dbus.Interface(manager_obj, "org.freedesktop.DBus.Introspectable")
intro_data = manager_intro.Introspect()
modules_loaded = 'interface name="org.freedesktop.UDisks2.Manager.%s"' % module in intro_data
if not modules_loaded:
manager.EnableModules(dbus.Boolean(True))
intro_data = manager_intro.Introspect()
return 'interface name="org.freedesktop.UDisks2.Manager.%s"' % module in intro_data
else:
return True
@classmethod
def ay_to_str(self, ay):
"""Convert a bytearray (terminated with '\0') to a string"""
return ''.join(chr(x) for x in ay[:-1])
@classmethod
def str_to_ay(self, string, terminate=True):
"""Convert a string to a bytearray (terminated with '\0')"""
if terminate:
string += '\0'
return dbus.Array([dbus.Byte(ord(c)) for c in string],
signature=dbus.Signature('y'), variant_level=1)
@classmethod
def bytes_to_ay(self, bytes):
"""Convert Python bytes to a DBus bytearray"""
return dbus.Array([dbus.Byte(b) for b in bytes],
signature=dbus.Signature('y'), variant_level=1)
@classmethod
def set_udev_property(self, device, prop, value):
udev = GUdev.Client()
dev = udev.query_by_device_file(device)
serial = dev.get_property("ID_SERIAL")
try:
os.makedirs("/run/udev/rules.d/")
except OSError:
# already exists
pass
self.write_file("/run/udev/rules.d/99-udisks_test.rules",
'ENV{ID_SERIAL}=="%s", ENV{%s}="%s"\n' % (serial, prop, value))
self.run_command("udevadm control --reload")
uevent_path = os.path.join(dev.get_sysfs_path(), "uevent")
self.write_file(uevent_path, "change\n")
self.udev_settle()
os.unlink("/run/udev/rules.d/99-udisks_test.rules")
self.run_command("udevadm control --reload")
@classmethod
def assertHasIface(self, obj, iface):
obj_intro = dbus.Interface(obj, "org.freedesktop.DBus.Introspectable")
intro_data = obj_intro.Introspect()
for _ in range(20):
if ('interface name="%s"' % iface) in intro_data:
return
time.sleep(0.5)
raise AssertionError("Object '%s' has no interface '%s'" % (obj.object_path, iface))
def assertStartswith(self, val, prefix):
if not val.startswith(prefix):
raise AssertionError("'%s' does not start with '%s'" % (val, prefix))
class FlightRecorder(object):
"""Context manager for recording data/logs
This is the abstract implementation that does nothing. Subclasses are
expected to override the methods below to actually do something useful.
"""
def __init__(self, desc):
"""
:param str desc: description of the recorder
"""
self._desc = desc
def _start(self):
"""Start recording"""
def _stop(self):
"""Stop recording"""
pass
def _save(self):
"""Save the record"""
pass
def __enter__(self):
self._start()
def __exit__(self, exc_type, exc_val, exc_tb):
self._stop()
self._save()
# Returning False means that the exception we have potentially been
# given as arguments was not handled
return False
class CmdFlightRecorder(FlightRecorder):
"""Flight recorder running a command and gathering its standard and error output"""
def __init__(self, desc, argv, store):
"""
:param str desc: description of the recorder
:param argv: command and arguments to run
:type argv: list of str
:param store: a list-like object to append the data/logs to
"""
super(CmdFlightRecorder, self).__init__(desc)
self._argv = argv
self._store = store
self._proc = None
def _start(self):
self._proc = subprocess.Popen(self._argv, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
def _stop(self):
self._proc.terminate()
def _save(self):
# err is in out (see above)
out, _err = self._proc.communicate()
rec = '<<<<< ' + self._desc + ' >>>>>' + '\n' + out.decode() + '\n\n'
self._store.append(rec)
class JournalRecorder(FlightRecorder):
"""Flight recorder for gathering logs (journal)"""
def __init__(self, desc, store):
"""
:param str desc: description of the recorder
:param store: a list-like object to append the data/logs to
"""
super(JournalRecorder, self).__init__(desc)
self._store = store
self._started = None
self._stopped = None
def _start(self):
self._started = monotonic()
def _stop(self):
self._stopped = monotonic()
def _save(self):
j = journal.Reader(converters={"MESSAGE": lambda x: x.decode(errors="replace")})
j.this_boot()
j.seek_monotonic(self._started)
journal_data = ""
entry = j.get_next()
# entry["__MONOTONIC_TIMESTAMP"] is a tuple of (datetime.timedelta, boot_id)
while entry and entry["__MONOTONIC_TIMESTAMP"][0].seconds <= int(self._stopped):
if "_COMM" in entry and "_PID" in entry:
source = "%s[%d]" % (entry["_COMM"], entry["_PID"])
else:
source = "kernel"
journal_data += "%s[%0.8f] %s: %s\n" % (entry["__REALTIME_TIMESTAMP"].strftime("%H:%M:%S"),
entry["__MONOTONIC_TIMESTAMP"][0].total_seconds(),
source, entry["MESSAGE"])
entry = j.get_next()
rec = '<<<<< ' + self._desc + ' >>>>>' + '\n' + journal_data + '\n\n\n'
self._store.append(rec)