| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
import random |
| 20 |
import syslog |
| 21 |
|
| 22 |
|
| 23 |
from tests._constants import _UDEV_TEST |
| 24 |
from tests.utils import is_unicode_string |
| 25 |
|
| 26 |
|
| 27 |
from pyudev import udev_version |
| 28 |
|
| 29 |
try: |
| 30 |
|
| 31 |
from unittest import mock |
| 32 |
except ImportError: |
| 33 |
|
| 34 |
import mock |
| 35 |
|
| 36 |
|
| 37 |
def test_udev_version(): |
| 38 |
assert isinstance(udev_version(), int) |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
assert udev_version() > 150 |
| 43 |
|
| 44 |
|
| 45 |
class TestContext: |
| 46 |
def test_sys_path(self, context): |
| 47 |
assert is_unicode_string(context.sys_path) |
| 48 |
assert context.sys_path == "/sys" |
| 49 |
|
| 50 |
def test_device_path(self, context): |
| 51 |
assert is_unicode_string(context.device_path) |
| 52 |
assert context.device_path == "/dev" |
| 53 |
|
| 54 |
@_UDEV_TEST(167, "test_run_path") |
| 55 |
def test_run_path(self, context): |
| 56 |
assert is_unicode_string(context.run_path) |
| 57 |
assert context.run_path == "/run/udev" |
| 58 |
|
| 59 |
def test_log_priority_get(self, context): |
| 60 |
assert isinstance(context.log_priority, int) |
| 61 |
assert syslog.LOG_EMERG <= context.log_priority <= syslog.LOG_DEBUG |
| 62 |
|
| 63 |
def test_log_priority_get_mock(self, context): |
| 64 |
spec = lambda c: None |
| 65 |
funcname = "udev_get_log_priority" |
| 66 |
with mock.patch.object(context._libudev, funcname, autospec=spec) as func: |
| 67 |
func.return_value = mock.sentinel.log_priority |
| 68 |
assert context.log_priority is mock.sentinel.log_priority |
| 69 |
func.assert_called_once_with(context) |
| 70 |
|
| 71 |
def test_log_priority_set_mock(self, context): |
| 72 |
spec = lambda c, p: None |
| 73 |
funcname = "udev_set_log_priority" |
| 74 |
with mock.patch.object(context._libudev, funcname, autospec=spec) as func: |
| 75 |
context.log_priority = mock.sentinel.log_priority |
| 76 |
func.assert_called_once_with(context, mock.sentinel.log_priority) |
| 77 |
|
| 78 |
def test_log_priority_roundtrip(self, context): |
| 79 |
|
| 80 |
old_priority = context.log_priority |
| 81 |
available_levels = [ |
| 82 |
l |
| 83 |
for l in range(syslog.LOG_EMERG, syslog.LOG_DEBUG + 1) |
| 84 |
if l != old_priority |
| 85 |
] |
| 86 |
new_priority = random.choice(available_levels) |
| 87 |
assert new_priority != old_priority |
| 88 |
try: |
| 89 |
context.log_priority = new_priority |
| 90 |
assert context.log_priority == new_priority |
| 91 |
finally: |
| 92 |
context.log_priority = old_priority |