summaryrefslogtreecommitdiffstats
path: root/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestDevice.py
blob: 2a935fcbc432d812a5a35fdf1e5d06d84f61aef0 (plain)
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
import os
import copy
import lxml.etree
from mock import Mock, MagicMock, patch
from Bcfg2.Client.Tools.POSIX.Device import *
from .Test__init import get_posix_object
from .Testbase import TestPOSIXTool
from .....common import *

class TestPOSIXDevice(TestPOSIXTool):
    test_obj = POSIXDevice

    def test_fully_specified(self):
        ptool = self.get_obj()
        orig_entry = lxml.etree.Element("Path", name="/test", type="device",
                                        dev_type="fifo")
        self.assertTrue(ptool.fully_specified(orig_entry))
        for dtype in ["block", "char"]:
            for attr in ["major", "minor"]:
                entry = copy.deepcopy(orig_entry)
                entry.set("dev_type", dtype)
                entry.set(attr, "0")
                self.assertFalse(ptool.fully_specified(entry))
            entry = copy.deepcopy(orig_entry)
            entry.set("dev_type", dtype)
            entry.set("major", "0")
            entry.set("minor", "0")
            self.assertTrue(ptool.fully_specified(entry))
    
    @patch("os.major")
    @patch("os.minor")
    @patch("Bcfg2.Client.Tools.POSIX.base.POSIXTool._exists")
    @patch("Bcfg2.Client.Tools.POSIX.base.POSIXTool.verify")
    def test_verify(self, mock_verify, mock_exists, mock_minor, mock_major):
        entry = lxml.etree.Element("Path", name="/test", type="device",
                                   perms='0644', owner='root', group='root',
                                   dev_type="block", major="0", minor="10")
        ptool = self.get_obj()

        def reset():
            mock_exists.reset_mock()
            mock_verify.reset_mock()
            mock_minor.reset_mock()
            mock_major.reset_mock()

        mock_exists.return_value = False
        self.assertFalse(ptool.verify(entry, []))
        mock_exists.assert_called_with(entry)

        reset()
        mock_exists.return_value = MagicMock()
        mock_major.return_value = 0
        mock_minor.return_value = 10
        mock_verify.return_value = True
        self.assertTrue(ptool.verify(entry, []))
        mock_verify.assert_called_with(ptool, entry, [])
        mock_exists.assert_called_with(entry)
        mock_major.assert_called_with(mock_exists.return_value.st_rdev)
        mock_minor.assert_called_with(mock_exists.return_value.st_rdev)

        reset()
        mock_exists.return_value = MagicMock()
        mock_major.return_value = 0
        mock_minor.return_value = 10
        mock_verify.return_value = False
        self.assertFalse(ptool.verify(entry, []))
        mock_verify.assert_called_with(ptool, entry, [])
        mock_exists.assert_called_with(entry)
        mock_major.assert_called_with(mock_exists.return_value.st_rdev)
        mock_minor.assert_called_with(mock_exists.return_value.st_rdev)

        reset()
        mock_verify.return_value = True
        entry = lxml.etree.Element("Path", name="/test", type="device",
                                   perms='0644', owner='root', group='root',
                                   dev_type="fifo")
        self.assertTrue(ptool.verify(entry, []))
        mock_exists.assert_called_with(entry)
        mock_verify.assert_called_with(ptool, entry, [])
        self.assertFalse(mock_major.called)
        self.assertFalse(mock_minor.called)
    
    @patch("os.makedev")
    @patch("os.mknod")
    @patch("Bcfg2.Client.Tools.POSIX.Device.%s._exists" % test_obj.__name__)
    @patch("Bcfg2.Client.Tools.POSIX.base.POSIXTool.install")
    def test_install(self, mock_install, mock_exists, mock_mknod, mock_makedev):
        entry = lxml.etree.Element("Path", name="/test", type="device",
                                   perms='0644', owner='root', group='root',
                                   dev_type="block", major="0", minor="10")
        ptool = self.get_obj()

        mock_exists.return_value = False
        mock_makedev.return_value = Mock()
        mock_install.return_value = True
        self.assertTrue(ptool.install(entry))
        mock_exists.assert_called_with(entry, remove=True)
        mock_makedev.assert_called_with(0, 10)
        mock_mknod.assert_called_with(entry.get("name"),               # 0o644
                                      device_map[entry.get("dev_type")] | 420,
                                      mock_makedev.return_value)
        mock_install.assert_called_with(ptool, entry)

        mock_makedev.reset_mock()
        mock_mknod.reset_mock()
        mock_exists.reset_mock()
        mock_install.reset_mock()
        mock_makedev.side_effect = OSError
        self.assertFalse(ptool.install(entry))

        mock_makedev.reset_mock()
        mock_mknod.reset_mock()
        mock_exists.reset_mock()
        mock_install.reset_mock()
        mock_mknod.side_effect = OSError
        self.assertFalse(ptool.install(entry))
        
        mock_makedev.reset_mock()
        mock_mknod.reset_mock()
        mock_exists.reset_mock()
        mock_install.reset_mock()        
        mock_mknod.side_effect = None
        entry = lxml.etree.Element("Path", name="/test", type="device",
                                   perms='0644', owner='root', group='root',
                                   dev_type="fifo")
        
        self.assertTrue(ptool.install(entry))
        mock_exists.assert_called_with(entry, remove=True)
        mock_mknod.assert_called_with(entry.get("name"),               # 0o644
                                      device_map[entry.get("dev_type")] | 420)
        mock_install.assert_called_with(ptool, entry)