File: test_final.py

package info (click to toggle)
duplicity 3.0.6.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,492 kB
  • sloc: python: 25,523; sh: 976; ansic: 392; makefile: 83
file content (277 lines) | stat: -rw-r--r-- 11,830 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
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4; encoding:utf-8 -*-
#
# Copyright 2002 Ben Escoto
# Copyright 2007 Kenneth Loafman
#
# This file is part of duplicity.
#
# Duplicity is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# Duplicity is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with duplicity; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA


import os
import re
import sys
import time
import unittest

import pytest

from duplicity import path
from testing.functional import (
    _runtest_dir,
    CmdError,
    FunctionalTestCase,
)


class FinalTest(FunctionalTestCase):
    """
    Test backup/restore using duplicity binary
    """

    def runtest(self, dirlist, backup_options=None, restore_options=None):
        """Run backup/restore test on directories in dirlist"""
        if backup_options is None:
            backup_options = []
        if restore_options is None:
            restore_options = []

        assert len(dirlist) >= 1

        backup_options += ["--allow-source-mismatch"]

        # Back up directories to local backend
        current_time = 100000
        self.backup("full", dirlist[0], current_time=current_time, options=backup_options)
        for new_dir in dirlist[1:]:
            current_time += 100000
            self.backup("inc", new_dir, current_time=current_time, options=backup_options)

        # Restore each and compare them
        for i in range(len(dirlist)):
            dirname = dirlist[i]
            current_time = 100000 * (i + 1)
            self.restore(time=current_time, options=restore_options)
            self.check_same(dirname, f"{_runtest_dir}/testfiles/restore_out")
            self.verify(dirname, time=current_time, options=restore_options)

    def check_same(self, filename1, filename2):
        """Verify two filenames are the same"""
        path1, path2 = path.Path(filename1), path.Path(filename2)
        assert path1.compare_recursive(path2, verbose=1)

    @pytest.mark.slow
    def test_basic_cycle(self, backup_options=None, restore_options=None, dirlist=None, testfiles=None):
        """Run backup/restore test on basic directories"""
        if backup_options is None:
            backup_options = ["--no-encrypt", "--no-compress"]
        if restore_options is None:
            restore_options = ["--no-encrypt", "--no-compress"]
        if dirlist is None:
            dirlist = [
                f"{_runtest_dir}/testfiles/dir1",
                f"{_runtest_dir}/testfiles/dir2",
                f"{_runtest_dir}/testfiles/dir3",
            ]
        self.runtest(dirlist, backup_options=backup_options, restore_options=restore_options)

        if testfiles is None:
            testfiles = [
                ("symbolic_link", 99999, "dir1"),
                ("directory_to_file", 100100, "dir1"),
                ("directory_to_file", 200100, "dir2"),
                ("largefile", 300000, "dir3"),
            ]
        # Test restoring various sub files
        for filename, time, tfdir in testfiles:
            self.restore(filename, time, options=restore_options)
            self.check_same(f"{_runtest_dir}/testfiles/{tfdir}/{filename}", f"{_runtest_dir}/testfiles/restore_out")
            self.verify(
                f"{_runtest_dir}/testfiles/{tfdir}/{filename}",
                file_to_verify=filename,
                time=time,
                options=restore_options,
            )

    @pytest.mark.slow
    def test_asym_cycle(self):
        """Like test_basic_cycle but use asymmetric encryption and signing"""
        backup_options = ["--encrypt-key", self.encrypt_key1, "--sign-key", self.sign_key]
        restore_options = ["--encrypt-key", self.encrypt_key1, "--sign-key", self.sign_key]
        self.test_basic_cycle(backup_options=backup_options, restore_options=restore_options)

    @pytest.mark.slow
    def test_asym_with_hidden_recipient_cycle(self):
        """Like test_basic_cycle but use asymmetric encryption (hiding key id) and signing"""
        backup_options = ["--hidden-encrypt-key", self.encrypt_key1, "--sign-key", self.sign_key]
        restore_options = ["--hidden-encrypt-key", self.encrypt_key1, "--sign-key", self.sign_key]
        self.test_basic_cycle(backup_options=backup_options, restore_options=restore_options)

    def test_single_regfile(self):
        """Test backing and restoring up a single regular file"""
        self.runtest([f"{_runtest_dir}/testfiles/various_file_types/regular_file"])

    def test_empty_backup(self):
        """Make sure backup works when no files change"""
        self.backup("full", f"{_runtest_dir}/testfiles/empty_dir")
        self.backup("inc", f"{_runtest_dir}/testfiles/empty_dir")

    @pytest.mark.slow
    def test_long_filenames(self):
        """Test backing up a directory with long filenames in it"""
        # Note that some versions of ecryptfs (at least through Ubuntu 11.10)
        # have a bug where they treat the max path segment length as 143
        # instead of 255.  So make sure that these segments don't break that.
        lf_dir = path.Path(f"{_runtest_dir}/testfiles/long_filenames")
        if lf_dir.exists():
            lf_dir.deltree()
        lf_dir.mkdir()
        lf1 = lf_dir.append(
            "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"  # noqa
        )
        lf1.mkdir()
        lf2 = lf1.append(
            "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"  # noqa
        )
        lf2.mkdir()
        lf3 = lf2.append(
            "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"  # noqa
        )
        lf3.mkdir()
        lf4 = lf3.append(
            "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"  # noqa
        )
        lf4.touch()
        lf4_1 = lf3.append(
            "SYMLINK--------------------------------------------------------------------------------------------"  # noqa
        )
        os.symlink(
            "SYMLINK-DESTINATION-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------",  # noqa
            lf4_1.name,
        )
        lf4_1.setdata()
        assert lf4_1.issym()
        lf4_2 = lf3.append(
            "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"  # noqa
        )
        fp = lf4_2.open("wb")
        fp.write(b"hello" * 1000)
        assert not fp.close()

        self.runtest(
            [
                f"{_runtest_dir}/testfiles/empty_dir",
                lf_dir.uc_name,
                f"{_runtest_dir}/testfiles/empty_dir",
                lf_dir.uc_name,
            ]
        )

    def test_empty_restore(self):
        """Make sure error raised when restore doesn't match anything"""
        self.backup("full", f"{_runtest_dir}/testfiles/dir1", options=["--allow-source-mismatch"])
        self.assertRaises(CmdError, self.restore, "this_file_does_not_exist")
        self.backup("inc", f"{_runtest_dir}/testfiles/empty_dir", options=["--allow-source-mismatch"])
        self.assertRaises(CmdError, self.restore, "this_file_does_not_exist")

    @pytest.mark.slow
    def test_remove_older_than(self):
        """Test removing old backup chains"""
        first_chain = self.backup(
            "full", f"{_runtest_dir}/testfiles/dir1", current_time=10000, options=["--allow-source-mismatch"]
        )
        first_chain |= self.backup(
            "inc", f"{_runtest_dir}/testfiles/dir2", current_time=20000, options=["--allow-source-mismatch"]
        )
        second_chain = self.backup(
            "full", f"{_runtest_dir}/testfiles/dir1", current_time=30000, options=["--allow-source-mismatch"]
        )
        second_chain |= self.backup(
            "inc", f"{_runtest_dir}/testfiles/dir3", current_time=40000, options=["--allow-source-mismatch"]
        )

        self.assertEqual(self.get_backend_files(), first_chain | second_chain)

        self.run_duplicity(options=["remove-older-than", "35000", "--force", self.backend_url])
        self.assertEqual(self.get_backend_files(), second_chain)

        # Now check to make sure we can't delete only chain
        self.run_duplicity(options=["remove-older-than", "50000", "--force", self.backend_url])
        self.assertEqual(self.get_backend_files(), second_chain)

    def test_piped_password(self):
        """Make sure that prompting for a password works"""
        self.set_environ("FTP_PASSWORD", None)
        self.set_environ("PASSPHRASE", None)
        self.set_environ("SIGN_PASSPHRASE", None)
        self.set_environ("TESTDEBUG", None)
        self.backup(
            "full",
            f"{_runtest_dir}/testfiles/empty_dir",
            passphrase_input=[self.sign_passphrase, self.sign_passphrase],
        )
        self.restore(passphrase_input=[self.sign_passphrase])

    @pytest.mark.slow
    def test_jsonstat(self):
        """Test cycle with json stats enabled"""
        backup_options = ["--jsonstat"]
        restore_options = ["--jsonstat"]
        self.test_basic_cycle(backup_options=backup_options, restore_options=restore_options)

    def test_jsonstat_missing(self):
        """Make sure collection_status works if one set misses jsonstat"""
        self.backup("full", f"{_runtest_dir}/testfiles/dir1", options=["--jsonstat", "--allow-source-mismatch"])
        self.backup("inc", f"{_runtest_dir}/testfiles/empty_dir", options=["--jsonstat", "--allow-source-mismatch"])
        self.backup("inc", f"{_runtest_dir}/testfiles/dir2", options=["--allow-source-mismatch"])
        self.collection_status(options=["--show-changes-in-set", "-1", "--jsonstat"])

    def run_with_no_change(self, backup_options=None, restore_options=None):
        """Like test_basic_cycle but runs a incremental backup with no changes after full"""
        testfiles = [
            ("symbolic_link", 99999, "dir1"),
            ("directory_to_file", 100100, "dir1"),
            ("directory_to_file", 300100, "dir2"),
            ("largefile", 400000, "dir3"),
        ]
        self.test_basic_cycle(
            backup_options=backup_options,
            restore_options=restore_options,
            dirlist=[
                f"{_runtest_dir}/testfiles/dir1",
                f"{_runtest_dir}/testfiles/dir1",
                f"{_runtest_dir}/testfiles/dir2",
                f"{_runtest_dir}/testfiles/dir3",
            ],
            testfiles=testfiles,
        )

    @pytest.mark.slow
    def test_skip_if_no_change(self):
        self.run_with_no_change(backup_options=["--skip-if-no-change"])

    @pytest.mark.slow
    def test_concurrency(self):
        backup_options = ["--concurrency=2"]
        self.test_basic_cycle(backup_options=backup_options)

    @pytest.mark.slow
    def test_concurrency_and_skip_if_no_change(self):
        backup_options = ["--concurrency=2", "--skip-if-no-change"]
        self.run_with_no_change(backup_options=backup_options)


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