File: test_discover.py

package info (click to toggle)
pyudev 0.24.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 464 kB
  • sloc: python: 2,412; makefile: 4
file content (172 lines) | stat: -rw-r--r-- 5,373 bytes parent folder | download | duplicates (2)
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
# -*- coding: utf-8 -*-
# Copyright (C) 2015 Anne Mulhern <amulhern@redhat.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
"""
    tests.test_discover
    ===================

    Tests discovering what device is meant by somewhat unspecific information.

    .. moduleauthor:: mulhern <amulhern@redhat.com>
"""

# isort: STDLIB
import os

# isort: THIRDPARTY
import pytest
from hypothesis import assume, given, settings, strategies

# isort: LOCAL
import pyudev
from pyudev import (
    DeviceFileHypothesis,
    DeviceNameHypothesis,
    DeviceNumberHypothesis,
    DevicePathHypothesis,
    Discovery,
)

_CONTEXT = pyudev.Context()
_DEVICES = [d for d in _CONTEXT.list_devices()]

NUM_TESTS = 5


class TestUtilities:
    """
    Some utilities used by the actual tests.
    """

    @staticmethod
    def get_device_numbers(a_device, a_string):
        """
        Get the device number from the device in a few formats.

        :param :class:`Device` a_device: the device
        :param  str a_string: a likely string between the major and minor
        :returns: a tuple of device numbers
        :rtype: tuple of str
        """
        device_number = a_device.device_number
        major_number = os.major(device_number)
        minor_number = os.minor(device_number)
        pair_number = "%s%s%s" % (major_number, a_string, minor_number)
        return (str(device_number), pair_number)

    @staticmethod
    def get_paths(a_device):
        """
        Get some variants on a device path.

        :param :class:`Device` a_device: the device
        :returns: a tuple of paths in sysfs
        :rtype: tuple of str
        """
        sys_path = a_device.sys_path
        (_, _, truncated_path) = sys_path[1:].partition("/")
        return (sys_path, truncated_path, a_device.device_path)

    @staticmethod
    def get_files(a_device):
        """
        Get a bunch of files, including device nodes and links.

        :param :class:`Device` a_device: the device
        :returns: a list of files
        :rtype: list of str
        """
        links = list(a_device.device_links)
        names = [os.path.basename(l) for l in links]
        links.extend(names)

        device_node = a_device.device_node
        if device_node:
            links.append(device_node)
        return links


class TestDiscovery:
    """
    Test discovery of an object from limited bits of its description.
    """

    _CONTEXT = pyudev.Context()
    _DISCOVER = Discovery()
    _DISCOVER.setup(_CONTEXT)

    @given(
        strategies.sampled_from(_DEVICES).filter(lambda x: x.device_number),
        strategies.text(":, -/+=").filter(lambda x: x),
    )
    @settings(max_examples=NUM_TESTS)
    def test_device_number(self, a_device, a_string):
        """
        Test lookup by a device number.

        Note that device number is per class, so there may be two
        devices with the same device number.
        """
        for number in TestUtilities.get_device_numbers(a_device, a_string):
            res = DeviceNumberHypothesis.get_devices(self._CONTEXT, number)
            assert a_device in res

    @given(strategies.sampled_from(_DEVICES))
    @settings(max_examples=NUM_TESTS)
    def test_path(self, a_device):
        """
        Test lookup by path.
        """
        for path in TestUtilities.get_paths(a_device):
            res = DevicePathHypothesis.get_devices(self._CONTEXT, path)
            assert res == set((a_device,))

    @given(strategies.sampled_from(_DEVICES))
    @settings(max_examples=NUM_TESTS)
    def test_name(self, a_device):
        """
        Test lookup by device name.

        Note that there may be multiple devices corresponding to the name
        in different subsystems.
        """
        name = a_device.sys_name
        res = DeviceNameHypothesis.get_devices(self._CONTEXT, name)
        assert a_device in res

    _devices = [d for d in _DEVICES if list(d.device_links)]

    @given(
        strategies.sampled_from(_DEVICES),
        strategies.text(":, -/+=").filter(lambda x: x),
    )
    @settings(max_examples=NUM_TESTS)
    def test_anything(self, a_device, a_string):
        """
        Grab any of the likely candidates for looking up a device.
        """
        assume(not "DM_MULTIPATH_TIMESTAMP" in a_device.properties)

        values = list(TestUtilities.get_device_numbers(a_device, a_string))
        values.extend(TestUtilities.get_paths(a_device))
        values.append(a_device.sys_name)
        values.extend(TestUtilities.get_files(a_device))

        results = frozenset(
            d for v in values for d in self._DISCOVER.get_devices(self._CONTEXT, v)
        )

        assert a_device in results