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
|
# -*- coding: utf-8 -*-
# Copyright (C) 2012 Sebastian Wiesner <lunaryorn@gmail.com>
# This library 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 2.1 of the License, or (at your
# option) any later version.
# This library is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
# for more details.
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""
plugins.fake_monitor
====================
Provide a fake :class:`~pyudev.Monitor`.
This fake monitor allows to trigger arbitrary events. Use this class to
test class building upon monitor without the need to rely on real events
generated by privileged operations as provided by the
:mod:`~plugins.privileged` plugin.
.. moduleauthor:: Sebastian Wiesner <lunaryorn@gmail.com>
"""
from __future__ import (print_function, division, unicode_literals,
absolute_import)
import sys
import os
from select import select
class FakeMonitor(object):
"""
A fake :class:`~pyudev.Monitor` which allows you to trigger arbitrary
events.
This fake monitor implements the complete :class:`~pyudev.Monitor`
interface and works on real file descriptors so that you can
:func:`~select.select()` the monitor.
"""
def __init__(self, device_to_emit):
self._event_source, self._event_sink = os.pipe()
self.device_to_emit = device_to_emit
self.started = False
def trigger_event(self):
"""
Trigger an event on clients of this monitor.
"""
os.write(self._event_sink, b'\x01')
def fileno(self):
return self._event_source
def filter_by(self, *args):
pass
def start(self):
self.started = True
def poll(self, timeout=None):
rlist, _, _ = select([self._event_source], [], [], timeout)
if self._event_source in rlist:
os.read(self._event_source, 1)
return self.device_to_emit
def close(self):
"""
Close sockets acquired by this monitor.
"""
try:
os.close(self._event_source)
finally:
os.close(self._event_sink)
def pytest_funcarg__fake_monitor(request):
"""
Return a FakeMonitor, which emits the platform device as returned by
the ``fake_monitor_device`` funcarg on all triggered actions.
.. warning::
To use this funcarg, you have to provide the ``fake_monitor_device``
funcarg!
"""
return FakeMonitor(request.getfuncargvalue('fake_monitor_device'))
|