File: __init__.py

package info (click to toggle)
duplicity 3.0.6.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,620 kB
  • sloc: python: 25,580; sh: 976; ansic: 392; makefile: 83
file content (299 lines) | stat: -rw-r--r-- 10,675 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
296
297
298
299
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4; encoding:utf-8 -*-
#
# Copyright 2012 Canonical Ltd
#
# 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 platform
import sys
import time

import pexpect

from duplicity import backend
from duplicity.backends._testbackend import BackendErrors
from duplicity import config
from duplicity import util
from testing import DuplicityTestCase
from testing import _runtest_dir
from testing import _top_dir


class CmdError(Exception):
    """Indicates an error running an external command"""

    def __init__(self, code):
        Exception.__init__(self, code)
        self.exit_status = code


class FunctionalTestCase(DuplicityTestCase):
    _setsid_w = None

    @classmethod
    def _check_setsid(cls):
        if cls._setsid_w is not None:
            return
        if platform.platform().startswith("Linux"):
            # setsid behavior differs between distributions.
            # If setsid supports -w ("wait"), use it.
            import subprocess

            try:
                with open("/dev/null", "w") as sink:
                    subprocess.check_call(["setsid", "-w", "ls"], stdout=sink, stderr=sink)
            except subprocess.CalledProcessError:
                cls._setsid_w = False
            else:
                cls._setsid_w = True

    def setUp(self):
        super().setUp()

        self.unpack_testfiles()

        self.class_args = []
        self.backend_url = f"fortestsonly://{_runtest_dir}/testfiles/output"
        self.last_backup = None
        self.set_environ("PASSPHRASE", self.sign_passphrase)
        self.set_environ("SIGN_PASSPHRASE", self.sign_passphrase)

        backend_inst = backend.get_backend(self.backend_url)
        bl = backend_inst.list()
        if bl:
            backend_inst.delete(backend_inst.list())
        backend_inst.close()
        self._check_setsid()

    def run_duplicity(self, options=None, current_time=None, fail=None, passphrase_input=None, timeout=None):
        """
        Run duplicity binary with given arguments and options
        """

        if options is None:
            options = []
        if passphrase_input is None:
            passphrase_input = []

        # Check all string inputs are unicode -- we will convert to system encoding before running the command
        for item in passphrase_input:
            assert isinstance(item, str), f"item {os.fsdecode(item)} in passphrase_input is not unicode"

        # set python path to be dev directory
        os.environ["PYTHONPATH"] = _top_dir

        cmd_list = []

        # We run under setsid and take input from /dev/null (below) because
        # this way we force a failure if duplicity tries to read from the
        # console unexpectedly (like for gpg password or such).
        if platform.platform().startswith("Linux"):
            cmd_list.extend(["setsid"])
            if self._setsid_w:
                cmd_list.extend(["-w"])

        cmd_list.extend([sys.executable, "-W", "ignore", "-u"])

        if os.environ.get("RUN_COVERAGE", None):
            cmd_list.extend(["-m", "coverage", "run", "--source=duplicity", "-p"])

        cmd_list.extend([os.path.join(_top_dir, "duplicity", "__main__.py")])
        cmd_list.extend(options)

        if os.environ.get("PYDEVD", None):
            cmd_list.extend(["--pydevd"])

        if os.environ.get("TESTDEBUG", False):
            # enable debug logging for trouble shooting
            cmd_list.extend(["-vd"])
        else:
            # keep duplicity quite for a condensed test log.
            cmd_list.extend(["-v0"])
        cmd_list.extend(["--no-print-statistics"])
        cmd_list.extend([f"--archive-dir={_runtest_dir}/testfiles/cache"])

        if current_time:
            cmd_list.extend(["--current-time", current_time])

        cmd_list.extend(self.class_args)

        dup_env = dict(os.environ)
        if fail:
            # cmd_list.extend(["--fail", fail]) replaced by ENV
            dup_env[BackendErrors.FAIL_SYSTEM_EXIT] = f"vol{fail}.difftar"
            cmd_list.extend(["--num-ret=2", "--backend-ret=3"])  # fail faster

        # convert to string and single quote to avoid shell expansion
        cmdline = " ".join([f"'{x}'" for x in cmd_list])

        if not passphrase_input:
            cmdline += " < /dev/null"

        # Set encoding to filesystem encoding and send to spawn.
        # option -f to shell avoids globbing which breaks selection tests.
        # shell used instead of cmdline to allow setsid to be prepended.
        child = pexpect.spawn(
            "/bin/sh",
            ["-f", "-c", cmdline],
            timeout=timeout,
            env=dup_env,  # type: ignore
        )

        for passphrase in passphrase_input:
            child.expect("passphrase.*:")
            child.sendline(passphrase)

        # if the command fails, we need to clear its output
        # so it will terminate cleanly.
        child.expect_exact(pexpect.EOF)
        lines = child.before.splitlines()
        child.wait()
        child.ptyproc.delayafterclose = 0.0
        return_val = child.exitstatus

        print("\n...command:", cmdline, file=sys.stderr)
        print("...cwd:", os.getcwd(), file=sys.stderr)
        print("...output:", file=sys.stderr)
        for line in lines:
            line = line.rstrip()
            if line:
                print(os.fsdecode(line), file=sys.stderr)
        print("...return_val:", return_val, file=sys.stderr)
        if fail:
            self.assertEqual(30, return_val)
        elif return_val:
            raise CmdError(return_val)

    def backup(self, type, input_dir, options=None, **kwargs):  # pylint: disable=redefined-builtin
        """Run duplicity backup to default directory"""
        if options is None:
            options = []
        options = [type, input_dir, self.backend_url, "--volsize=1"] + options
        before_files = self.get_backend_files()

        # If a chain ends with time X and the next full chain begins at time X,
        # we may trigger an assert in dup_collections.py.  If needed, sleep to
        # avoid such problems
        now = time.time()
        if self.last_backup == int(now):
            time.sleep(1)

        self.run_duplicity(options=options, **kwargs)
        self.last_backup = int(time.time())

        after_files = self.get_backend_files()
        return after_files - before_files

    def backup_with_failure(
        self, type, input_dir, failure_type, failure_condition, error_code, options=None, PYDEVD=None, **kwargs
    ):
        """
        using _testbackent to trigger certain failure conditions. See backends/_testbackend.py for possible trigger
        """
        if not options:
            options = [  # lower the retry count to fail faster.
                "--num-ret=2",
                "--backend-ret=3",
            ]

        try:
            env = {failure_type: failure_condition}
            if PYDEVD:  # start debugger in forked duplicity execution. Use for troubleshooting only.
                env["PYDEVD"] = PYDEVD
            with EnvController(**env):
                self.backup(type, input_dir, options, **kwargs)
        except CmdError as e:  # Backup must fail with an exit code != 0
            self.assertEqual(e.exit_status, error_code, str(e))
        else:
            self.fail("Expected CmdError not thrown")

    def restore(self, file_to_restore=None, time=None, options=None, **kwargs):
        if options is None:
            options = []
        assert not os.system(f"rm -rf {_runtest_dir}/testfiles/restore_out")
        options = [
            "restore",
            self.backend_url,
            f"{_runtest_dir}/testfiles/restore_out",
        ] + options
        if file_to_restore:
            options.extend(["--path-to-restore", file_to_restore])
        if time:
            options.extend(["--restore-time", str(time)])
        self.run_duplicity(options=options, **kwargs)

    def verify(self, dirname, file_to_verify=None, time=None, options=None, **kwargs):
        if options is None:
            options = []
        options = ["verify", self.backend_url, dirname] + options
        if file_to_verify:
            options.extend(["--path-to-restore", file_to_verify])
        if time:
            options.extend(["--restore-time", str(time)])
        self.run_duplicity(options=options, **kwargs)

    def cleanup(self, options=None):
        """
        Run duplicity cleanup to default directory
        """
        if options is None:
            options = []
        options = ["cleanup", self.backend_url, "--force"] + options
        self.run_duplicity(options=options)

    def collection_status(self, options=None):
        """
        Run duplicity collection-status to default directory
        """
        if options is None:
            options = []
        options = ["collection-status", self.backend_url] + options
        self.run_duplicity(options=options)

    def get_backend_files(self):
        backend_inst = backend.get_backend(self.backend_url)
        bl = backend_inst.list()
        backend_inst.close()
        return set(bl)

    def make_largefiles(self, count=3, size=2):
        """
        Makes a number of large files in /tmp/testfiles/largefiles that each are
        the specified number of megabytes.
        """
        assert not os.system(f"mkdir {_runtest_dir}/testfiles/largefiles")
        for n in range(count):
            assert not os.system(
                f"dd if=/dev/urandom of={_runtest_dir}/testfiles/largefiles/file{n + 1} "
                f"bs=1024 count={size * 1024} > /dev/null 2>&1"
            )


class EnvController:
    def __init__(self, **kwargs):
        self.env = kwargs

    def __enter__(self):
        for k, v in self.env.items():
            os.environ[k] = v

    def __exit__(self, exc_type, exc_val, exc_tb):
        for k, _ in self.env.items():
            os.unsetenv(k)
            del os.environ[k]