summaryrefslogtreecommitdiffstats
path: root/testsuite/Testsrc/Testlib/TestClient/TestTools/TestPOSIX/TestNonexistent.py
blob: 3d1ddbf843226db5e7374ca7cd3a636f6b70abdd (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
import os
import sys
import copy
import lxml.etree
from mock import Mock, MagicMock, patch
from Bcfg2.Client.Tools.POSIX.Nonexistent import *

# add all parent testsuite directories to sys.path to allow (most)
# relative imports in python 2.4
path = os.path.dirname(__file__)
while path != "/":
    if os.path.basename(path).lower().startswith("test"):
        sys.path.append(path)
    if os.path.basename(path) == "testsuite":
        break
    path = os.path.dirname(path)
from Test__init import get_config
from Testbase import TestPOSIXTool
from common import *


class TestPOSIXNonexistent(TestPOSIXTool):
    test_obj = POSIXNonexistent

    @patch("os.path.lexists")
    def test_verify(self, mock_lexists):
        ptool = self.get_obj()
        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(ptool.verify(entry, []), not val)
            mock_lexists.assert_called_with(entry.get("name"))

    def test_install(self):
        entry = lxml.etree.Element("Path", name="/test", type="nonexistent")

        ptool = self.get_obj()
        ptool._remove = Mock()

        def reset():
            ptool._remove.reset_mock()

        self.assertTrue(ptool.install(entry))
        ptool._remove.assert_called_with(entry, recursive=False)

        reset()
        entry.set("recursive", "true")
        self.assertTrue(ptool.install(entry))
        ptool._remove.assert_called_with(entry, recursive=True)

        reset()
        child_entry = lxml.etree.Element("Path", name="/test/foo",
                                         type="nonexistent")
        ptool = self.get_obj(config=get_config([child_entry]))
        ptool._remove = Mock()
        self.assertTrue(ptool.install(entry))
        ptool._remove.assert_called_with(entry, recursive=True)

        reset()
        child_entry = lxml.etree.Element("Path", name="/test/foo",
                                         type="file")
        ptool = self.get_obj(config=get_config([child_entry]))
        ptool._remove = Mock()
        self.assertFalse(ptool.install(entry))
        self.assertFalse(ptool._remove.called)

        reset()
        entry.set("recursive", "false")
        ptool._remove.side_effect = OSError
        self.assertFalse(ptool.install(entry))
        ptool._remove.assert_called_with(entry, recursive=False)