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

class TestPOSIXNonexistent(TestPOSIXTool):
    test_obj = POSIXNonexistent

    @patch("os.path.lexists")
    def test_verify(self, mock_lexists):
        entry = lxml.etree.Element("Path", name="/test", type="nonexistent")

        for val in [True, False]:
            mock_lexists.reset_mock()
            mock_lexists.return_value = val
            self.assertEqual(self.ptool.verify(entry, []), not val)
            mock_lexists.assert_called_with(entry.get("name"))

    @patch("os.rmdir")
    @patch("os.remove")
    @patch("os.path.isdir")
    @patch("shutil.rmtree")
    def test_install(self, mock_rmtree, mock_isdir, mock_remove, mock_rmdir):
        entry = lxml.etree.Element("Path", name="/test", type="nonexistent")

        def reset():
            mock_isdir.reset_mock()
            mock_remove.reset_mock()
            mock_rmdir.reset_mock()
            mock_rmtree.reset_mock()

        mock_isdir.return_value = False
        self.assertTrue(self.ptool.install(entry))
        mock_remove.assert_called_with(entry.get("name"))

        reset()
        mock_remove.side_effect = OSError
        self.assertFalse(self.ptool.install(entry))
        mock_remove.assert_called_with(entry.get("name"))

        reset()
        mock_isdir.return_value = True
        self.assertTrue(self.ptool.install(entry))
        mock_rmdir.assert_called_with(entry.get("name"))

        reset()
        mock_rmdir.side_effect = OSError
        self.assertFalse(self.ptool.install(entry))
        mock_rmdir.assert_called_with(entry.get("name"))

        reset()
        entry.set("recursive", "true")
        self.assertTrue(self.ptool.install(entry))
        mock_rmtree.assert_called_with(entry.get("name"))

        reset()
        mock_rmtree.side_effect = OSError
        self.assertFalse(self.ptool.install(entry))
        mock_rmtree.assert_called_with(entry.get("name"))

        reset()
        child_entry = lxml.etree.Element("Path", name="/test/foo",
                                         type="nonexistent")
        ptool = self.get_obj(posix=get_posix_object(config=get_config([child_entry])))
        mock_rmtree.side_effect = None
        self.assertTrue(ptool.install(entry))
        mock_rmtree.assert_called_with(entry.get("name"))

        reset()
        child_entry = lxml.etree.Element("Path", name="/test/foo",
                                         type="file")
        ptool = self.get_obj(posix=get_posix_object(config=get_config([child_entry])))
        mock_rmtree.side_effect = None
        self.assertFalse(ptool.install(entry))