File: action_backuprestore_test.py

package info (click to toggle)
rdiff-backup 2.2.6-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,640 kB
  • sloc: python: 24,129; javascript: 9,512; sh: 1,230; ansic: 580; makefile: 36
file content (295 lines) | stat: -rw-r--r-- 13,506 bytes parent folder | download
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
"""
Test the basic backup and restore actions with api version >= 201
"""
import glob
import os
import unittest

import commontest as comtst
import fileset


class ActionBackupRestoreTest(unittest.TestCase):
    """
    Test that rdiff-backup really restores what has been backed-up
    """

    def setUp(self):
        self.base_dir = os.path.join(comtst.abs_test_dir,
                                     b"action_backuprestore")
        # Windows can't handle too long filenames
        long_multi = 10 if os.name == "nt" else 25
        self.from1_struct = {
            "from1": {"contents": {
                "fileA": {"content": "initial", "inode": "fileA"},
                "fileB": {},
                "dirOld": {"type": "dir"},
                "itemX": {"type": "dir"},
                "itemY": {"type": "file"},
                "longdirnam" * long_multi: {"type": "dir"},
                "longfilnam" * long_multi: {"content": "not so long content"},
                # "somehardlink": {"inode": "fileA"},
            }}
        }
        self.from1_path = os.path.join(self.base_dir, b"from1")
        self.from2_struct = {
            "from2": {"contents": {
                "fileA": {"content": "modified", "inode": "fileA"},
                "fileC": {},
                "dirNew": {"type": "dir"},
                "itemX": {"type": "file"},
                "itemY": {"type": "dir"},
                "longdirnam" * long_multi: {"type": "dir"},
                "longfilnam" * long_multi: {"content": "differently long"},
                # "somehardlink": {"inode": "fileA"},
            }}
        }
        self.from2_path = os.path.join(self.base_dir, b"from2")
        if os.name != "nt":
            # rdiff-backup can't handle (yet) hardlinks under Windows
            self.from1_struct["from1"]["contents"]["somehardlink"] = {
                "inode": "fileA"}
            self.from2_struct["from2"]["contents"]["somehardlink"] = {
                "inode": "fileA"}
        fileset.create_fileset(self.base_dir, self.from1_struct)
        fileset.create_fileset(self.base_dir, self.from2_struct)
        fileset.remove_fileset(self.base_dir, {"bak": {"type": "dir"}})
        fileset.remove_fileset(self.base_dir, {"to1": {"type": "dir"}})
        fileset.remove_fileset(self.base_dir, {"to2": {"type": "dir"}})
        fileset.remove_fileset(self.base_dir, {"to3": {"type": "dir"}})
        fileset.create_fileset(self.base_dir, {"to4": {"type": "file"}})
        self.bak_path = os.path.join(self.base_dir, b"bak")
        self.to1_path = os.path.join(self.base_dir, b"to1")
        self.to2_path = os.path.join(self.base_dir, b"to2")
        self.to3_path = os.path.join(self.base_dir, b"to3")
        self.to4_path = os.path.join(self.base_dir, b"to4")
        self.success = False

    def test_action_backuprestore(self):
        """test the "backup" and "restore" actions"""
        # we backup twice to the same backup repository at different times
        self.assertEqual(comtst.rdiff_backup_action(
            False, False, self.from1_path, self.bak_path,
            ("--api-version", "201", "--current-time", "10000"),
            b"backup", ()), 0)
        self.assertEqual(comtst.rdiff_backup_action(
            False, True, self.from2_path, self.bak_path,
            ("--api-version", "201", "--current-time", "20000"),
            b"backup", ()), 0)

        # then we restore the increment and the last mirror to two directories
        self.assertEqual(comtst.rdiff_backup_action(
            True, False, self.bak_path, self.to1_path,
            ("--api-version", "201", "--no-ssh-compression"),
            b"restore", ("--at", "1B")), 0)
        self.assertEqual(comtst.rdiff_backup_action(
            True, True, self.bak_path, self.to2_path,
            ("--api-version", "201", "--remote-tempdir", self.base_dir),
            b"restore", ()), 0)
        dir_old_inc = glob.glob(
            os.path.join(self.bak_path,
                         b'rdiff-backup-data', b'increments', b'dirOld.*'))[0]
        self.assertEqual(comtst.rdiff_backup_action(
            True, True, dir_old_inc, self.to3_path,
            ("--api-version", "201"),
            b"restore", ()), 0)

        self.assertFalse(fileset.compare_paths(self.from1_path, self.to1_path))
        self.assertFalse(fileset.compare_paths(self.from2_path, self.to2_path))

        # all tests were successful
        self.success = True

    def test_action_backup_errorcases(self):
        """test the "backup" actions in error cases"""
        # we backup twice to the same backup repository at different times
        self.assertEqual(comtst.rdiff_backup_action(
            False, False, self.from1_path, self.bak_path,
            ("--api-version", "201", "--current-time", "10000"),
            b"backup", ()), 0)
        self.assertNotEqual(comtst.rdiff_backup_action(
            False, True, self.from2_path, self.bak_path,
            ("--api-version", "201", "--current-time", "10000"),
            b"backup", ()), 0)  # can't backup at same time
        self.assertNotEqual(comtst.rdiff_backup_action(
            False, True, self.from2_path, self.bak_path,
            ("--api-version", "201", "--current-time", "20000"),
            b"backup", ("--exclude", "not-in-from")), 0)  # can't match
        self.assertNotEqual(comtst.rdiff_backup_action(
            False, True, self.from2_path, self.bak_path,
            ("--api-version", "201", "--current-time", "20001"),
            b"backup", ("--include", "**")), 0)  # redundant inclusion

    def test_action_restore_errorcases(self):
        """test the "restore" actions in error cases"""
        # we backup twice to the same backup repository at different times
        self.assertEqual(comtst.rdiff_backup_action(
            False, False, self.from1_path, self.bak_path,
            ("--api-version", "201", "--current-time", "10000"),
            b"backup", ()), 0)
        self.assertEqual(comtst.rdiff_backup_action(
            False, True, self.from2_path, self.bak_path,
            ("--api-version", "201", "--current-time", "20000"),
            b"backup", ()), 0)

        # then we generate some error cases while restoring
        self.assertNotEqual(comtst.rdiff_backup_action(
            True, False, self.bak_path, self.to1_path,
            ("--api-version", "201"),
            b"restore", ("--at", "xyz")), 0)  # bad time string
        self.assertNotEqual(comtst.rdiff_backup_action(
            True, True, self.bak_path, self.to4_path,  # can't write to file
            ("--api-version", "201"),
            b"restore", ()), 0)
        self.assertEqual(comtst.rdiff_backup_action(
            True, True, self.bak_path, self.to4_path,  # force write to file
            ("--api-version", "201", "--force"),
            b"restore", ()), 0)
        self.assertNotEqual(comtst.rdiff_backup_action(
            True, False, self.bak_path, self.to1_path,
            ("--api-version", "201"),
            b"restore", ("--at", "1B", "--increment")), 0)  # both not allowed
        self.assertNotEqual(comtst.rdiff_backup_action(
            True, True, self.bak_path, self.to2_path,
            ("--api-version", "201"),
            b"restore", ("--increment",)), 0)  # not an increment!
        self.assertNotEqual(comtst.rdiff_backup_action(
            True, False, b"/does-not-exist", self.to1_path,
            ("--api-version", "201"),
            b"restore", ()), 0)  # restoring from non-existing repository
        self.assertNotEqual(comtst.rdiff_backup_action(
            True, False, self.to4_path, self.to1_path,
            ("--api-version", "201"),
            b"restore", ()), 0)  # restoring from non-repository directory
        # can't combine increment restore and file selection
        dir_old_inc = glob.glob(
            os.path.join(self.bak_path,
                         b'rdiff-backup-data', b'increments', b'dirOld.*'))[0]
        self.assertNotEqual(comtst.rdiff_backup_action(
            True, True, dir_old_inc, self.to3_path,
            ("--api-version", "201"),
            b"restore", ("--exclude-regexp", "*.forbidden")), 0)

        # all tests were successful
        self.success = True

    def test_action_backuprestore_quoted(self):
        """
        test the backup and restore actions with quoted repository
        """
        # we backup using a specific chars-to-quote
        self.assertEqual(comtst.rdiff_backup_action(
            False, False, self.from1_path, self.bak_path,
            ("--api-version", "201", "--current-time", "10000",
             "--chars-to-quote", "A:"),  # colon for Windows compatibility
            b"backup", ()), 0)

        # then we restore once the full repo, once a sub-path
        self.assertEqual(comtst.rdiff_backup_action(
            True, False, os.path.join(self.bak_path, b"itemX"), self.to1_path,
            ("--api-version", "201"),
            b"restore", ()), 0)
        self.assertEqual(comtst.rdiff_backup_action(
            True, True, self.bak_path, self.to2_path,
            ("--api-version", "201"),
            b"restore", ()), 0)

        # all tests were successful
        self.success = True

    def tearDown(self):
        # we clean-up only if the test was successful
        if self.success:
            fileset.remove_fileset(self.base_dir, self.from1_struct)
            fileset.remove_fileset(self.base_dir, self.from2_struct)
            fileset.remove_fileset(self.base_dir, {"bak": {"type": "dir"}})
            fileset.remove_fileset(self.base_dir, {"to1": {"type": "dir"}})
            fileset.remove_fileset(self.base_dir, {"to2": {"type": "dir"}})
            fileset.remove_fileset(self.base_dir, {"to3": {"type": "dir"}})
            fileset.remove_fileset(self.base_dir, {"to4": {"type": "dir"}})


class PreQuotingTest(unittest.TestCase):
    """
    Test rdiff-backup's quoting mechanisms cf. #275
    """

    def setUp(self):
        self.base_dir = os.path.join(comtst.abs_test_dir,
                                     b"action_backuprestore")
        self.from1_struct = {
            "from1": {"contents": {  # CODE.txt pre-quoted
                ";067;079;068;069.txt": {"content": "initial"},
            }}
        }
        self.from1_path = os.path.join(self.base_dir, b"from1")
        self.from2_struct = {
            "from2": {"contents": {  # CODE.txt pre-quoted and non-quoted
                ";067;079;068;069.txt": {"content": "modified"},
                "CODE.txt": {"content": "whatever"},
            }}
        }
        self.from2_path = os.path.join(self.base_dir, b"from2")
        fileset.create_fileset(self.base_dir, self.from1_struct)
        fileset.create_fileset(self.base_dir, self.from2_struct)
        fileset.remove_fileset(self.base_dir, {"bak": {"type": "dir"}})
        fileset.remove_fileset(self.base_dir, {"to1": {"type": "dir"}})
        fileset.remove_fileset(self.base_dir, {"to2": {"type": "dir"}})
        self.bak_path = os.path.join(self.base_dir, b"bak")
        self.to1_path = os.path.join(self.base_dir, b"to1")
        self.to2_path = os.path.join(self.base_dir, b"to2")
        self.success = False

    def test_pre_quoted_files(self):
        # we backup using a specific chars-to-quote
        self.assertEqual(comtst.rdiff_backup_action(
            False, False, self.from1_path, self.bak_path,
            ("--api-version", "201", "--current-time", "10000",
             "--chars-to-quote", "A-Z:"),  # colon for Windows compatibility
            b"backup", ()), 0)
        self.assertEqual(comtst.rdiff_backup_action(
            True, True, self.from2_path, self.bak_path,
            ("--api-version", "201", "--current-time", "20000",
             "--chars-to-quote", "A-Z:"),  # colon for Windows compatibility
            b"backup", ()), 0)

        # then we restore once the full repo, once a sub-path
        self.assertEqual(comtst.rdiff_backup_action(
            True, False, self.bak_path, self.to1_path,
            ("--api-version", "201"),
            b"restore", ("--at", "10000")), 0)
        self.assertEqual(comtst.rdiff_backup_action(
            True, True, self.bak_path, self.to2_path,
            ("--api-version", "201"),
            b"restore", ("--at", "20000")), 0)
        # use sets to avoid issues with directory ordering
        self.assertEqual(set(os.listdir(self.to1_path)),
                         {b';067;079;068;069.txt'})
        self.assertEqual(set(os.listdir(self.to2_path)),
                         {b';067;079;068;069.txt', b'CODE.txt'})
        self.success = True

    def tearDown(self):
        # we clean-up only if the test was successful
        if self.success:
            fileset.remove_fileset(self.base_dir, self.from1_struct)
            fileset.remove_fileset(self.base_dir, self.from2_struct)
            fileset.remove_fileset(self.base_dir, {"bak": {"type": "dir"}})
            fileset.remove_fileset(self.base_dir, {"to1": {"type": "dir"}})
            fileset.remove_fileset(self.base_dir, {"to2": {"type": "dir"}})


class ConnectionHandlingTest(unittest.TestCase):
    """
    Test handling of wrong connection
    """

    def test_wrong_connection(self):
        # we backup using a non existing destination location
        self.assertNotEqual(comtst.rdiff_backup_action(
            True, True, ".", "doesnotexist::/some_dir",
            ("--api-version", "201"), b"backup", ()), 0)


if __name__ == "__main__":
    unittest.main()