summaryrefslogtreecommitdiffstats
path: root/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestHardlink.py
blob: e663973c764a5a5c88b80b3d0f73a3078b45c8d6 (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
import os
import copy
import unittest
import lxml.etree
from mock import Mock, MagicMock, patch
from Bcfg2.Client.Tools.POSIX.Hardlink import *
from Test__init import get_posix_object

def call(*args, **kwargs):
    """ the Mock call object is a fairly recent addition, but it's
    very very useful, so we create our own function to create Mock
    calls """
    return (args, kwargs)

def get_hardlink_object(posix=None):
    if posix is None:
        posix = get_posix_object()
    return POSIXHardlink(posix.logger, posix.setup, posix.config)

class TestPOSIXHardlink(unittest.TestCase):
    @patch("os.path.samefile")
    @patch("Bcfg2.Client.Tools.POSIX.base.POSIXTool.verify")
    def test_verify(self, mock_verify, mock_samefile):
        entry = lxml.etree.Element("Path", name="/test", type="hardlink",
                                   to="/dest")
        ptool = get_hardlink_object()

        mock_samefile.return_value = True
        mock_verify.return_value = False
        self.assertFalse(ptool.verify(entry, []))
        mock_samefile.assert_called_with(entry.get("name"),
                                         entry.get("to"))
        mock_verify.assert_called_with(ptool, entry, [])

        mock_samefile.reset_mock()
        mock_verify.reset_mock()
        mock_verify.return_value = True
        self.assertTrue(ptool.verify(entry, []))
        mock_samefile.assert_called_with(entry.get("name"),
                                         entry.get("to"))
        mock_verify.assert_called_with(ptool, entry, [])

        mock_samefile.reset_mock()
        mock_verify.reset_mock()
        mock_samefile.return_value = False
        self.assertFalse(ptool.verify(entry, []))
        mock_samefile.assert_called_with(entry.get("name"),
                                         entry.get("to"))
        mock_verify.assert_called_with(ptool, entry, [])
        
        mock_samefile.reset_mock()
        mock_verify.reset_mock()
        mock_samefile.side_effect = OSError
        self.assertFalse(ptool.verify(entry, []))
        mock_samefile.assert_called_with(entry.get("name"),
                                         entry.get("to"))

    @patch("os.link")
    @patch("Bcfg2.Client.Tools.POSIX.base.POSIXTool.install")
    @patch("Bcfg2.Client.Tools.POSIX.Hardlink.POSIXHardlink._exists")
    def test_install(self, mock_exists, mock_install, mock_link):
        entry = lxml.etree.Element("Path", name="/test", type="hardlink",
                                   to="/dest")
        ptool = get_hardlink_object()

        mock_exists.return_value = False
        mock_install.return_value = True
        self.assertTrue(ptool.install(entry))
        mock_exists.assert_called_with(entry, remove=True)
        mock_link.assert_called_with(entry.get("to"), entry.get("name"))
        mock_install.assert_called_with(ptool, entry)

        mock_link.reset_mock()
        mock_exists.reset_mock()
        mock_install.reset_mock()
        mock_link.side_effect = OSError
        self.assertFalse(ptool.install(entry))
        mock_exists.assert_called_with(entry, remove=True)
        mock_link.assert_called_with(entry.get("to"), entry.get("name"))
        mock_install.assert_called_with(ptool, entry)