summaryrefslogtreecommitdiffstats
path: root/testsuite/Testlib/TestClient/TestTools/TestPOSIX/TestFile.py
blob: a2cd52dd5b8bca5f57189f91514ccfbde4044d7b (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import os
import copy
import binascii
import unittest
import lxml.etree
from mock import Mock, MagicMock, patch
from Bcfg2.Client.Tools.POSIX.File 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_file_object(posix=None):
    if posix is None:
        posix = get_posix_object()
    return POSIXFile(posix.logger, posix.setup, posix.config)

class TestPOSIXFile(unittest.TestCase):
    def test_fully_specified(self):
        entry = lxml.etree.Element("Path", name="/test", type="file")
        ptool = get_file_object()
        self.assertFalse(ptool.fully_specified(entry))

        entry.set("empty", "true")
        self.assertTrue(ptool.fully_specified(entry))

        entry.set("empty", "false")
        entry.text = "text"
        self.assertTrue(ptool.fully_specified(entry))
    
    def test_is_string(self):
        ptool = get_file_object()
        for char in range(8) + range(14, 32):
            self.assertFalse(ptool._is_string("foo" + chr(char) + "bar",
                                              'utf_8'))
        for char in range(9, 14) + range(33, 128):
            self.assertTrue(ptool._is_string("foo" + chr(char) + "bar",
                                             'utf_8'))
        self.assertFalse(ptool._is_string("foo" + chr(128) + "bar",
                                          'ascii'))
        ustr = '\xef\xa3\x91 + \xef\xa3\x92'
        self.assertTrue(ptool._is_string(ustr, 'utf_8'))
        self.assertFalse(ptool._is_string(ustr, 'ascii'))

    def test_get_data(self):
        orig_entry = lxml.etree.Element("Path", name="/test", type="file")
        setup = dict(encoding="ascii", ppath='/', max_copies=5)
        ptool = get_file_object(posix=get_posix_object(setup=setup))

        entry = copy.deepcopy(orig_entry)
        entry.text = binascii.b2a_base64("test")
        entry.set("encoding", "base64")
        self.assertEqual(ptool._get_data(entry), ("test", True))

        entry = copy.deepcopy(orig_entry)
        entry.set("empty", "true")
        self.assertEqual(ptool._get_data(entry), ("", False))

        entry = copy.deepcopy(orig_entry)
        entry.text = "test"
        self.assertEqual(ptool._get_data(entry), ("test", False))

        ustr = u'\uf8d1 + \uf8d2'
        entry = copy.deepcopy(orig_entry)
        entry.text = ustr
        self.assertEqual(ptool._get_data(entry), (ustr, False))

        setup['encoding'] = "utf_8"
        ptool = get_file_object(posix=get_posix_object(setup=setup))
        entry = copy.deepcopy(orig_entry)
        entry.text = ustr
        self.assertEqual(ptool._get_data(entry),
                         ('\xef\xa3\x91 + \xef\xa3\x92', False))

    @patch("__builtin__.open")
    @patch("Bcfg2.Client.Tools.POSIX.base.POSIXTool.verify")
    @patch("Bcfg2.Client.Tools.POSIX.File.POSIXFile._exists")
    @patch("Bcfg2.Client.Tools.POSIX.File.POSIXFile._get_data")
    @patch("Bcfg2.Client.Tools.POSIX.File.POSIXFile._get_diffs")
    def test_verify(self, mock_get_diffs, mock_get_data, mock_exists,
                    mock_verify, mock_open):
        entry = lxml.etree.Element("Path", name="/test", type="file")
        setup = dict(interactive=False, ppath='/', max_copies=5)
        ptool = get_file_object(posix=get_posix_object(setup=setup))

        def reset():
            mock_get_diffs.reset_mock()
            mock_get_data.reset_mock()
            mock_exists.reset_mock()
            mock_verify.reset_mock()
            mock_open.reset_mock()

        mock_get_data.return_value = ("test", False)
        mock_exists.return_value = False
        mock_verify.return_value = True
        self.assertFalse(ptool.verify(entry, []))
        mock_exists.assert_called_with(entry)
        mock_verify.assert_called_with(ptool, entry, [])
        mock_get_diffs.assert_called_with(entry, interactive=False,
                                          sensitive=False,
                                          is_binary=False,
                                          content="")

        reset()
        exists_rv = MagicMock()
        exists_rv.__getitem__.return_value = 5
        mock_exists.return_value = exists_rv
        mock_get_data.return_value = ("test", True)
        self.assertFalse(ptool.verify(entry, []))
        mock_exists.assert_called_with(entry)
        mock_verify.assert_called_with(ptool, entry, [])
        mock_get_diffs.assert_called_with(entry, interactive=False,
                                          sensitive=False,
                                          is_binary=True,
                                          content=None)
        
        reset()
        mock_get_data.return_value = ("test", False)
        exists_rv.__getitem__.return_value = 4
        entry.set("sensitive", "true")
        open_rv = Mock()
        open_rv.read.return_value = "tart"
        mock_open.return_value = open_rv
        self.assertFalse(ptool.verify(entry, []))
        mock_exists.assert_called_with(entry)
        mock_verify.assert_called_with(ptool, entry, [])
        mock_open.assert_called_with(entry.get("name"))
        open_rv.assert_any_call()
        mock_get_diffs.assert_called_with(entry, interactive=False,
                                          sensitive=True,
                                          is_binary=False,
                                          content="tart")

        reset()
        open_rv.read.return_value = "test"
        mock_open.return_value = open_rv
        self.assertTrue(ptool.verify(entry, []))
        mock_exists.assert_called_with(entry)
        mock_verify.assert_called_with(ptool, entry, [])
        mock_open.assert_called_with(entry.get("name"))
        open_rv.assert_any_call()
        self.assertFalse(mock_get_diffs.called)

        reset()
        mock_open.side_effect = IOError
        self.assertFalse(ptool.verify(entry, []))
        mock_exists.assert_called_with(entry)
        mock_open.assert_called_with(entry.get("name"))
    
    @patch("os.fdopen")
    @patch("tempfile.mkstemp")
    @patch("Bcfg2.Client.Tools.POSIX.File.POSIXFile._get_data")
    def test_write_tmpfile(self, mock_get_data, mock_mkstemp, mock_fdopen):
        entry = lxml.etree.Element("Path", name="/test", type="file",
                                   perms='0644', owner='root', group='root')
        ptool = get_file_object()
        newfile = "/foo/bar"

        def reset():
            mock_get_data.reset_mock()
            mock_mkstemp.reset_mock()
            mock_fdopen.reset_mock()

        mock_get_data.return_value = ("test", False)
        mock_mkstemp.return_value = (5, newfile)
        self.assertEqual(ptool._write_tmpfile(entry), newfile)
        mock_get_data.assert_called_with(entry)
        mock_mkstemp.assert_called_with(prefix='test', dir='/')
        mock_fdopen.assert_called_with(5, 'w')
        mock_fdopen.return_value.write.assert_called_with("test")

        reset()
        mock_mkstemp.side_effect = OSError
        self.assertFalse(ptool._write_tmpfile(entry))
        mock_mkstemp.assert_called_with(prefix='test', dir='/')

        reset()
        mock_mkstemp.side_effect = None
        mock_fdopen.side_effect = OSError
        self.assertFalse(ptool._write_tmpfile(entry))
        mock_mkstemp.assert_called_with(prefix='test', dir='/')
        mock_get_data.assert_called_with(entry)
        mock_fdopen.assert_called_with(5, 'w')
        
    @patch("os.rename")
    @patch("os.unlink")
    def test_rename_tmpfile(self, mock_unlink, mock_rename):
        entry = lxml.etree.Element("Path", name="/test", type="file",
                                   perms='0644', owner='root', group='root')
        ptool = get_file_object()
        newfile = "/foo/bar"

        self.assertTrue(ptool._rename_tmpfile(newfile, entry))
        mock_rename.assert_called_with(newfile, entry.get("name"))
        
        mock_rename.reset_mock()
        mock_unlink.reset_mock()
        mock_rename.side_effect = OSError
        self.assertFalse(ptool._rename_tmpfile(newfile, entry))
        mock_rename.assert_called_with(newfile, entry.get("name"))
        mock_unlink.assert_called_with(newfile)

        # even if the unlink fails, return false gracefully
        mock_rename.reset_mock()
        mock_unlink.reset_mock()
        mock_unlink.side_effect = OSError
        self.assertFalse(ptool._rename_tmpfile(newfile, entry))
        mock_rename.assert_called_with(newfile, entry.get("name"))
        mock_unlink.assert_called_with(newfile)

    @patch("os.path.exists")
    @patch("Bcfg2.Client.Tools.POSIX.base.POSIXTool.install")
    @patch("Bcfg2.Client.Tools.POSIX.File.POSIXFile._makedirs")
    @patch("Bcfg2.Client.Tools.POSIX.File.POSIXFile._set_perms")
    @patch("Bcfg2.Client.Tools.POSIX.File.POSIXFile._write_tmpfile")
    @patch("Bcfg2.Client.Tools.POSIX.File.POSIXFile._rename_tmpfile")
    def test_install(self, mock_rename, mock_write, mock_set_perms,
                     mock_makedirs, mock_install, mock_exists):
        entry = lxml.etree.Element("Path", name="/test", type="file",
                                   perms='0644', owner='root', group='root')
        ptool = get_file_object()

        def reset():
            mock_rename.reset_mock()
            mock_write.reset_mock()
            mock_set_perms.reset_mock()
            mock_makedirs.reset_mock()
            mock_install.reset_mock()
            mock_exists.reset_mock()

        mock_exists.return_value = False
        mock_makedirs.return_value = False
        self.assertFalse(ptool.install(entry))
        mock_exists.assert_called_with("/")
        mock_makedirs.assert_called_with(entry, path="/")
        
        reset()
        mock_makedirs.return_value = True
        mock_write.return_value = False
        self.assertFalse(ptool.install(entry))
        mock_exists.assert_called_with("/")
        mock_makedirs.assert_called_with(entry, path="/")
        mock_write.assert_called_with(entry)

        reset()
        newfile = '/test.X987yS'
        mock_write.return_value = newfile
        mock_set_perms.return_value = False
        mock_rename.return_value = False
        self.assertFalse(ptool.install(entry))
        mock_exists.assert_called_with("/")
        mock_makedirs.assert_called_with(entry, path="/")
        mock_write.assert_called_with(entry)
        mock_set_perms.assert_called_with(entry, path=newfile)
        mock_rename.assert_called_with(newfile, entry)

        reset()
        mock_rename.return_value = True
        mock_install.return_value = False
        self.assertFalse(ptool.install(entry))
        mock_exists.assert_called_with("/")
        mock_makedirs.assert_called_with(entry, path="/")
        mock_write.assert_called_with(entry)
        mock_set_perms.assert_called_with(entry, path=newfile)
        mock_rename.assert_called_with(newfile, entry)
        mock_install.assert_called_with(ptool, entry)

        reset()
        mock_install.return_value = True
        self.assertFalse(ptool.install(entry))
        mock_exists.assert_called_with("/")
        mock_makedirs.assert_called_with(entry, path="/")
        mock_write.assert_called_with(entry)
        mock_set_perms.assert_called_with(entry, path=newfile)
        mock_rename.assert_called_with(newfile, entry)
        mock_install.assert_called_with(ptool, entry)

        reset()
        mock_set_perms.return_value = True
        self.assertTrue(ptool.install(entry))
        mock_exists.assert_called_with("/")
        mock_makedirs.assert_called_with(entry, path="/")
        mock_write.assert_called_with(entry)
        mock_set_perms.assert_called_with(entry, path=newfile)
        mock_rename.assert_called_with(newfile, entry)
        mock_install.assert_called_with(ptool, entry)

        reset()
        mock_exists.return_value = True
        self.assertTrue(ptool.install(entry))
        mock_exists.assert_called_with("/")
        self.assertFalse(mock_makedirs.called)
        mock_write.assert_called_with(entry)
        mock_set_perms.assert_called_with(entry, path=newfile)
        mock_rename.assert_called_with(newfile, entry)
        mock_install.assert_called_with(ptool, entry)

    def test_diff(self):
        ptool = get_file_object()
        content1 = "line1\nline2"
        content2 = "line3"
        rv = ["line1", "line2", "line3"]
        func = Mock()
        func.return_value = rv
        self.assertItemsEqual(ptool._diff(content1, content2, func), rv)
        func.assert_called_with(["line1", "line2"], ["line3"])

        func.reset_mock()
        def slow_diff(content1, content2):
            for i in range(1, 10):
                time.sleep(5)
                yield "line%s" % i
        func.side_effect = slow_diff
        self.assertFalse(ptool._diff(content1, content2, func), rv)
        func.assert_called_with(["line1", "line2"], ["line3"])