1# -*- coding: utf-8 -*-
2# Copyright (C) 2010, 2011, 2012 Sebastian Wiesner <lunaryorn@gmail.com>
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
18from __future__ import (print_function, division, unicode_literals,
19                        absolute_import)
20
21import random
22import syslog
23
24import mock
25
26from pyudev import udev_version
27
28from tests._constants import _UDEV_TEST
29
30from tests.utils import is_unicode_string
31
32
33def test_udev_version():
34    assert isinstance(udev_version(), int)
35    # just to make sure, that udev versioning works.  pyudev itself should be
36    # compatible with earlier versions of pyudev.  However, 150 is currently
37    # the earliest udev release, I'm testing against (using Ubuntu 10.04)
38    assert udev_version() > 150
39
40
41class TestContext(object):
42    def test_sys_path(self, context):
43        assert is_unicode_string(context.sys_path)
44        assert context.sys_path == '/sys'
45
46    def test_device_path(self, context):
47        assert is_unicode_string(context.device_path)
48        assert context.device_path == '/dev'
49
50    @_UDEV_TEST(167, "test_run_path")
51    def test_run_path(self, context):
52        assert is_unicode_string(context.run_path)
53        assert context.run_path == '/run/udev'
54
55    def test_log_priority_get(self, context):
56        assert isinstance(context.log_priority, int)
57        assert syslog.LOG_EMERG <= context.log_priority <= syslog.LOG_DEBUG
58
59    def test_log_priority_get_mock(self, context):
60        spec = lambda c: None
61        funcname = 'udev_get_log_priority'
62        with mock.patch.object(
63                context._libudev, funcname, autospec=spec) as func:
64            func.return_value = mock.sentinel.log_priority
65            assert context.log_priority is mock.sentinel.log_priority
66            func.assert_called_once_with(context)
67
68    def test_log_priority_set_mock(self, context):
69        spec = lambda c, p: None
70        funcname = 'udev_set_log_priority'
71        with mock.patch.object(
72                context._libudev, funcname, autospec=spec) as func:
73            context.log_priority = mock.sentinel.log_priority
74            func.assert_called_once_with(context, mock.sentinel.log_priority)
75
76    def test_log_priority_roundtrip(self, context):
77        # FIXME: This adds UDEV_LOG properties?!
78        old_priority = context.log_priority
79        available_levels = [
80            l for l in range(syslog.LOG_EMERG, syslog.LOG_DEBUG + 1)
81            if l != old_priority
82        ]
83        new_priority = random.choice(available_levels)
84        assert new_priority != old_priority
85        try:
86            context.log_priority = new_priority
87            assert context.log_priority == new_priority
88        finally:
89            context.log_priority = old_priority
90