Debian Codegraph @

tests/test_util.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2010, 2011, 2012 Sebastian Wiesner <[email protected]>
3
4 # This library is free software; you can redistribute it and/or modify it
5 # under the terms of the GNU Lesser General Public License as published by the
6 # Free Software Foundation; either version 2.1 of the License, or (at your
7 # option) any later version.
8
9 # This library is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
12 # for more details.
13
14 # You should have received a copy of the GNU Lesser General Public License
15 # along with this library; if not, write to the Free Software Foundation,
16 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17
18 # isort: STDLIB
19 import sys
20
21 # isort: THIRDPARTY
22 import pytest
23 from hypothesis import given, settings, strategies
24
25 # isort: LOCAL
26 from pyudev import Context, _util
27
28 from .utils import is_unicode_string
29
30 try:
31 # isort: STDLIB
32 from unittest.mock import Mock
33 except ImportError:
34 # isort: THIRDPARTY
35 from mock import Mock
36
37
38 _CONTEXT = Context()
39
40
41 def test_ensure_byte_string():
42 assert isinstance(_util.ensure_byte_string("hello world"), bytes)
43 assert _util.ensure_byte_string("hello world") == b"hello world"
44 hello = b"hello world"
45 assert _util.ensure_byte_string(hello) is hello
46
47
48 def test_ensure_byte_string_none():
49 with pytest.raises(AttributeError):
50 _util.ensure_byte_string(None)
51
52
53 def test_ensure_unicode_string():
54 assert is_unicode_string(_util.ensure_unicode_string(b"hello world"))
55 assert _util.ensure_unicode_string(b"hello world") == "hello world"
56 hello = "hello world"
57 assert _util.ensure_unicode_string(hello) is hello
58
59
60 def test_ensure_unicode_string_none():
61 with pytest.raises(AttributeError):
62 _util.ensure_unicode_string(None)
63
64
65 def test_property_value_to_bytes_string():
66 hello = "hello world".encode(sys.getfilesystemencoding())
67 assert _util.property_value_to_bytes(hello) is hello
68 assert isinstance(_util.property_value_to_bytes("hello world"), bytes)
69 assert _util.property_value_to_bytes("hello world") == hello
70
71
72 def test_property_value_to_bytes_int():
73 assert _util.property_value_to_bytes(10000) == b"10000"
74 assert isinstance(_util.property_value_to_bytes(10000), bytes)
75
76
77 def test_property_value_to_bytes_bool():
78 assert _util.property_value_to_bytes(True) == b"1"
79 assert isinstance(_util.property_value_to_bytes(True), bytes)
80 assert _util.property_value_to_bytes(False) == b"0"
81 assert isinstance(_util.property_value_to_bytes(False), bytes)
82
83
84 def test_string_to_bool_true():
85 assert isinstance(_util.string_to_bool("1"), bool)
86 assert _util.string_to_bool("1")
87
88
89 def test_string_to_bool_false():
90 assert isinstance(_util.string_to_bool("0"), bool)
91 assert not _util.string_to_bool("0")
92
93
94 def test_string_to_bool_invalid_value():
95 with pytest.raises(ValueError) as exc_info:
96 _util.string_to_bool("foo")
97 assert str(exc_info.value) == "Not a boolean value: {0!r}".format("foo")
98
99
100 def test_udev_list_iterate_no_entry():
101 assert not list(_util.udev_list_iterate(Mock(), None))
102
103
104 def test_udev_list_iterate_mock():
105 libudev = Mock(name="libudev")
106 items = [("spam", "eggs"), ("foo", "bar")]
107 with pytest.libudev_list(libudev, "udev_enumerate_get_list_entry", items):
108 udev_list = libudev.udev_enumerate_get_list_entry()
109 assert list(_util.udev_list_iterate(libudev, udev_list)) == [
110 ("spam", "eggs"),
111 ("foo", "bar"),
112 ]
113
114
115 def raise_valueerror():
116 raise ValueError("from function")
117
118
119 _CHAR_DEVICES = list(_CONTEXT.list_devices(subsystem="tty"))
120
121
122 @pytest.mark.skipif(len(_CHAR_DEVICES) == 0, reason="no tty devices")
123 @given(strategies.sampled_from(_CHAR_DEVICES))
124 @settings(max_examples=5)
125 def test_get_device_type_character_device(a_device):
126 """
127 Check that the device type of a character device is actually char.
128 """
129 assert _util.get_device_type(a_device.device_node) == "char"
130
131
132 _BLOCK_DEVICES = list(_CONTEXT.list_devices(subsystem="block"))
133
134
135 @pytest.mark.skipif(len(_BLOCK_DEVICES) == 0, reason="no block devices")
136 @given(strategies.sampled_from(_BLOCK_DEVICES))
137 @settings(max_examples=5)
138 def test_get_device_type_block_device(a_device):
139 """
140 Check that the device type of a block device is actually block.
141 """
142 assert _util.get_device_type(a_device.device_node) == "block"
143
144
145 def test_get_device_type_no_device_file(tmpdir):
146 filename = tmpdir.join("test")
147 filename.ensure(file=True)
148 with pytest.raises(ValueError) as excinfo:
149 _util.get_device_type(str(filename))
150 message = "not a device file: {0!r}".format(str(filename))
151 assert str(excinfo.value) == message
152
153
154 def test_get_device_type_not_existing(tmpdir):
155 """
156 Test that an OSError is raised when checking device type using a file
157 that does not actually exist.
158 """
159 filename = tmpdir.join("test_get_device_type_not_existing")
160 assert not tmpdir.check(file=True)
161 with pytest.raises(OSError):
162 _util.get_device_type(str(filename))
163
164
165 def test_eintr_retry_call(tmpdir):
166 # isort: STDLIB
167 import os
168 import select
169 import signal
170
171 def handle_alarm(signum, frame):
172 # pylint: disable=unused-argument
173 pass
174
175 orig_alarm = signal.getsignal(signal.SIGALRM)
176
177 # Open an empty file and use it to wait for exceptions which should
178 # never happen
179 filename = tmpdir.join("test")
180 filename.ensure(file=True)
181 fd = os.open(str(filename), os.O_RDONLY)
182
183 try:
184 signal.signal(signal.SIGALRM, handle_alarm)
185
186 # Ensure that wrapping the call does not raise EINTR
187 signal.alarm(1)
188 assert _util.eintr_retry_call(select.select, [], [], [3], 2) == ([], [], [])
189 finally:
190 os.close(fd)
191 signal.signal(signal.SIGALRM, orig_alarm)