File: privileged.py

package info (click to toggle)
pyudev 0.16.1-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 524 kB
  • ctags: 723
  • sloc: python: 2,978; makefile: 16
file content (75 lines) | stat: -rw-r--r-- 2,224 bytes parent folder | download | duplicates (3)
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
# -*- 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.privileged
    ==================

    Support privileged operations to trigger real udev events.

    This plugin adds :func:`load_dummy` and :func:`unload_dummy` to the
    :mod:`pytest` namespace.

    .. moduleauthor::  Sebastian Wiesner  <lunaryorn@gmail.com>
"""


from __future__ import (print_function, division, unicode_literals,
                        absolute_import)

from subprocess import call

import pytest


def pytest_addoption(parser):
    group = parser.getgroup('privileged', 'tests with privileged operations')
    group.addoption('--enable-privileged', action='store_true',
                    help='Enable tests that required privileged operations',
                    default=False)


def check_privileges_or_skip():
    if not pytest.config.option.enable_privileged:
        pytest.skip('privileged tests disabled')


def load_dummy():
    """
    Load the ``dummy`` module.

    If privileged tests are disabled, the current test is skipped.
    """
    check_privileges_or_skip()
    call(['sudo', 'modprobe', 'dummy'])


def unload_dummy():
    """
    Unload the ``dummy`` module.

    If privileged tests are disabled, the current test is skipped.
    """
    check_privileges_or_skip()
    call(['sudo', 'modprobe', '-r', 'dummy'])


EXPOSED_FUNCTIONS = [load_dummy, unload_dummy]


def pytest_namespace():
    return dict((f.__name__, f) for f in EXPOSED_FUNCTIONS)