~mcuelenaere/duplicity/duplicity

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
from __future__ import print_function
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
#
# Copyright 2002 Ben Escoto <ben@emerose.org>
# Copyright 2007 Kenneth Loafman <kenneth@loafman.com>
#
# 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 sys
import random
import pytest
import unittest

from duplicity import collections
from duplicity import backend
from duplicity import globals
from duplicity import path
from duplicity import gpg
from duplicity import dup_time
from . import UnitTestCase

filename_list1 = [u"duplicity-full.2002-08-17T16:17:01-07:00.manifest.gpg",
                  u"duplicity-full.2002-08-17T16:17:01-07:00.vol1.difftar.gpg",
                  u"duplicity-full.2002-08-17T16:17:01-07:00.vol2.difftar.gpg",
                  u"duplicity-full.2002-08-17T16:17:01-07:00.vol3.difftar.gpg",
                  u"duplicity-full.2002-08-17T16:17:01-07:00.vol4.difftar.gpg",
                  u"duplicity-full.2002-08-17T16:17:01-07:00.vol5.difftar.gpg",
                  u"duplicity-full.2002-08-17T16:17:01-07:00.vol6.difftar.gpg",
                  u"duplicity-inc.2002-08-17T16:17:01-07:00.to.2002-08-18T00:04:30-07:00.manifest.gpg",
                  u"duplicity-inc.2002-08-17T16:17:01-07:00.to.2002-08-18T00:04:30-07:00.vol1.difftar.gpg",
                  u"Extra stuff to be ignored"]

remote_sigchain_filename_list = [u"duplicity-full-signatures.2002-08-17T16:17:01-07:00.sigtar.gpg",
                                 u"duplicity-new-signatures.2002-08-17T16:17:01-07:00.to.2002-08-18T00:04:30-07:00.sigtar.gpg",
                                 u"duplicity-new-signatures.2002-08-18T00:04:30-07:00.to.2002-08-20T00:00:00-07:00.sigtar.gpg"]

local_sigchain_filename_list = [u"duplicity-full-signatures.2002-08-17T16:17:01-07:00.sigtar.gz",
                                u"duplicity-new-signatures.2002-08-17T16:17:01-07:00.to.2002-08-18T00:04:30-07:00.sigtar.gz",
                                u"duplicity-new-signatures.2002-08-18T00:04:30-07:00.to.2002-08-20T00:00:00-07:00.sigtar.gz"]

# A filename list with some incomplete volumes, an older full volume,
# and a complete chain.
filename_list2 = [u"duplicity-full.2001-01-01T16:17:01-07:00.manifest.gpg",
                  u"duplicity-full.2001-01-01T16:17:01-07:00.vol1.difftar.gpg",
                  u"duplicity-full.2002-08-17T16:17:01-07:00.manifest.gpg",
                  u"duplicity-full.2002-08-17T16:17:01-07:00.vol1.difftar.gpg",
                  u"duplicity-full.2002-08-17T16:17:01-07:00.vol2.difftar.gpg",
                  u"duplicity-full.2002-08-17T16:17:01-07:00.vol3.difftar.gpg",
                  u"duplicity-full.2002-08-17T16:17:01-07:00.vol4.difftar.gpg",
                  u"duplicity-full.2002-08-17T16:17:01-07:00.vol5.difftar.gpg",
                  u"duplicity-full.2002-08-17T16:17:01-07:00.vol6.difftar.gpg",
                  u"duplicity-inc.2002-08-17T16:17:01-07:00.to.2002-08-18T00:04:30-07:00.manifest.gpg",
                  u"duplicity-inc.2002-08-17T16:17:01-07:00.to.2002-08-18T00:04:30-07:00.vol1.difftar.gpg",
                  u"The following are extraneous duplicity files",
                  u"duplicity-new-signatures.2001-08-17T02:05:13-05:00.to.2002-08-17T05:05:14-05:00.sigtar.gpg",
                  u"duplicity-full.2002-08-15T01:01:01-07:00.vol1.difftar.gpg",
                  u"duplicity-inc.2000-08-17T16:17:01-07:00.to.2000-08-18T00:04:30-07:00.manifest.gpg",
                  u"duplicity-inc.2000-08-17T16:17:01-07:00.to.2000-08-18T00:04:30-07:00.vol1.difftar.gpg",
                  u"Extra stuff to be ignored"]


class CollectionTest(UnitTestCase):
    u"""Test collections"""
    def setUp(self):
        super(CollectionTest, self).setUp()

        self.unpack_testfiles()

        col_test_dir = path.Path(u"testfiles/collectionstest")
        archive_dir_path = col_test_dir.append(u"archive_dir")
        self.set_global(u'archive_dir_path', archive_dir_path)
        self.archive_dir_backend = backend.get_backend(u"file://testfiles/collectionstest"
                                                       u"/archive_dir")

        self.real_backend = backend.get_backend(u"file://%s/%s" %
                                                (col_test_dir.name, u"remote_dir"))
        self.output_dir = path.Path(u"testfiles/output")  # used as a temp directory
        self.output_dir_backend = backend.get_backend(u"file://testfiles/output")

    def set_gpg_profile(self):
        u"""Set gpg profile to standard "foobar" sym"""
        self.set_global(u'gpg_profile', gpg.GPGProfile(passphrase=u"foobar"))

    def test_backup_chains(self):
        u"""Test basic backup chain construction"""
        random.shuffle(filename_list1)
        cs = collections.CollectionsStatus(None, globals.archive_dir_path, u"full")
        chains, orphaned, incomplete = cs.get_backup_chains(filename_list1)  # @UnusedVariable
        if len(chains) != 1 or len(orphaned) != 0:
            print(chains)
            print(orphaned)
            assert 0

        chain = chains[0]
        assert chain.end_time == 1029654270
        assert chain.fullset.time == 1029626221

    def test_collections_status(self):
        u"""Test CollectionStatus object's set_values()"""
        def check_cs(cs):
            u"""Check values of collections status"""
            assert cs.values_set

            assert cs.matched_chain_pair
            assert cs.matched_chain_pair[0].end_time == 1029826800
            assert len(cs.all_backup_chains) == 1, cs.all_backup_chains

        cs = collections.CollectionsStatus(self.real_backend, globals.archive_dir_path, u"full").set_values()
        check_cs(cs)
        assert cs.matched_chain_pair[0].islocal()

    def test_sig_chain(self):
        u"""Test a single signature chain"""
        chain = collections.SignatureChain(1, globals.archive_dir_path)
        for filename in local_sigchain_filename_list:
            assert chain.add_filename(filename)
        assert not chain.add_filename(u"duplicity-new-signatures.2002-08-18T00:04:30-07:00.to.2002-08-20T00:00:00-07:00.sigtar.gpg")

    def test_sig_chains(self):
        u"""Test making signature chains from filename list"""
        cs = collections.CollectionsStatus(None, globals.archive_dir_path, u"full")
        chains, orphaned_paths = cs.get_signature_chains(local=1)
        self.sig_chains_helper(chains, orphaned_paths)

    def test_sig_chains2(self):
        u"""Test making signature chains from filename list on backend"""
        cs = collections.CollectionsStatus(self.archive_dir_backend, globals.archive_dir_path, u"full")
        chains, orphaned_paths = cs.get_signature_chains(local=None)
        self.sig_chains_helper(chains, orphaned_paths)

    def sig_chains_helper(self, chains, orphaned_paths):
        u"""Test chains and orphaned_paths values for two above tests"""
        if orphaned_paths:
            for op in orphaned_paths:
                print(op)
            assert 0
        assert len(chains) == 1, chains
        assert chains[0].end_time == 1029826800

    def sigchain_fileobj_get(self, local):
        u"""Return chain, local if local is true with filenames added"""
        if local:
            chain = collections.SignatureChain(1, globals.archive_dir_path)
            for filename in local_sigchain_filename_list:
                assert chain.add_filename(filename)
        else:
            chain = collections.SignatureChain(None, self.real_backend)
            for filename in remote_sigchain_filename_list:
                assert chain.add_filename(filename)
        return chain

    def sigchain_fileobj_check_list(self, chain):
        u"""Make sure the list of file objects in chain has right contents

        The contents of the testfiles/collectiontest/remote_dir have
        to be coordinated with this test.

        """
        fileobjlist = chain.get_fileobjs()
        assert len(fileobjlist) == 3

        def test_fileobj(i, s):
            buf = fileobjlist[i].read()
            fileobjlist[i].close()
            assert buf == s, (buf, s)

        test_fileobj(0, u"Hello, world!")
        test_fileobj(1, u"hello 1")
        test_fileobj(2, u"Hello 2")

    @pytest.mark.usefixtures(u"redirect_stdin")
    def test_sigchain_fileobj(self):
        u"""Test getting signature chain fileobjs from archive_dir_path"""
        self.set_gpg_profile()
        self.sigchain_fileobj_check_list(self.sigchain_fileobj_get(1))
        self.sigchain_fileobj_check_list(self.sigchain_fileobj_get(None))

    def get_filelist2_cs(self):
        u"""Return set CollectionsStatus object from filelist 2"""
        # Set up testfiles/output with files from filename_list2
        for filename in filename_list2:
            p = self.output_dir.append(filename)
            p.touch()

        cs = collections.CollectionsStatus(self.output_dir_backend, globals.archive_dir_path, u"full")
        cs.set_values()
        return cs

    def test_get_extraneous(self):
        u"""Test the listing of extraneous files"""
        cs = self.get_filelist2_cs()
        assert len(cs.orphaned_backup_sets) == 1, cs.orphaned_backup_sets
        assert len(cs.local_orphaned_sig_names) == 0, cs.local_orphaned_sig_names
        assert len(cs.remote_orphaned_sig_names) == 1, cs.remote_orphaned_sig_names
        assert len(cs.incomplete_backup_sets) == 1, cs.incomplete_backup_sets

        right_list = [u"duplicity-new-signatures.2001-08-17T02:05:13-05:00.to.2002-08-17T05:05:14-05:00.sigtar.gpg",
                      u"duplicity-full.2002-08-15T01:01:01-07:00.vol1.difftar.gpg",
                      u"duplicity-inc.2000-08-17T16:17:01-07:00.to.2000-08-18T00:04:30-07:00.manifest.gpg",
                      u"duplicity-inc.2000-08-17T16:17:01-07:00.to.2000-08-18T00:04:30-07:00.vol1.difftar.gpg"]
        local_received_list, remote_received_list = cs.get_extraneous(False)  # @UnusedVariable
        errors = []
        for filename in remote_received_list:
            if filename not in right_list:
                errors.append(u"### Got bad extraneous filename " + filename)
            else:
                right_list.remove(filename)
        for filename in right_list:
            errors.append(u"### Didn't receive extraneous filename " + filename)
        assert not errors, u"\n" + u"\n".join(errors)

    def test_get_olderthan(self):
        u"""Test getting list of files older than a certain time"""
        cs = self.get_filelist2_cs()
        oldsets = cs.get_older_than(
            dup_time.genstrtotime(u"2002-05-01T16:17:01-07:00"))
        oldset_times = [s.get_time() for s in oldsets]
        right_times = [dup_time.genstrtotime(u'2001-01-01T16:17:01-07:00')]
        assert oldset_times == right_times, \
            [oldset_times, right_times]

        oldsets_required = cs.get_older_than_required(
            dup_time.genstrtotime(u"2002-08-17T20:00:00-07:00"))
        oldset_times = [s.get_time() for s in oldsets_required]
        right_times_required = [dup_time.genstrtotime(u'2002-08-17T16:17:01-07:00')]
        assert oldset_times == right_times_required, \
            [oldset_times, right_times_required]


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