1
import sys, random, unittest
2
sys.path.insert(0, "../src")
3
import collections, backends, path, gpg, globals
5
filename_list1 = ["duplicity-full.2002-08-17T16:17:01-07:00.manifest.gpg",
6
"duplicity-full.2002-08-17T16:17:01-07:00.vol1.difftar.gpg",
7
"duplicity-full.2002-08-17T16:17:01-07:00.vol2.difftar.gpg",
8
"duplicity-full.2002-08-17T16:17:01-07:00.vol3.difftar.gpg",
9
"duplicity-full.2002-08-17T16:17:01-07:00.vol4.difftar.gpg",
10
"duplicity-full.2002-08-17T16:17:01-07:00.vol5.difftar.gpg",
11
"duplicity-full.2002-08-17T16:17:01-07:00.vol6.difftar.gpg",
12
"duplicity-inc.2002-08-17T16:17:01-07:00.to.2002-08-18T00:04:30-07:00.manifest.gpg",
13
"duplicity-inc.2002-08-17T16:17:01-07:00.to.2002-08-18T00:04:30-07:00.vol1.difftar.gpg",
14
"Extra stuff to be ignored"]
16
remote_sigchain_filename_list = ["duplicity-full-signatures.2002-08-17T16:17:01-07:00.sigtar.gpg",
17
"duplicity-new-signatures.2002-08-17T16:17:01-07:00.to.2002-08-18T00:04:30-07:00.sigtar.gpg",
18
"duplicity-new-signatures.2002-08-18T00:04:30-07:00.to.2002-08-20T00:00:00-07:00.sigtar.gpg"]
20
local_sigchain_filename_list = ["duplicity-full-signatures.2002-08-17T16:17:01-07:00.sigtar.gz",
21
"duplicity-new-signatures.2002-08-17T16:17:01-07:00.to.2002-08-18T00:04:30-07:00.sigtar.gz",
22
"duplicity-new-signatures.2002-08-18T00:04:30-07:00.to.2002-08-20T00:00:00-07:00.sigtar.gz"]
25
col_test_dir = path.Path("testfiles/collectionstest")
26
archive_dir = col_test_dir.append("archive_dir")
27
archive_dir_backend = backends.get_backend("file://testfiles/collectionstest"
31
real_backend = backends.LocalBackend(col_test_dir.append("remote_dir").name)
34
class CollectionTest(unittest.TestCase):
35
"""Test collections"""
36
def set_gpg_profile(self):
37
"""Set gpg profile to standard "foobar" sym"""
38
globals.gpg_profile = gpg.GPGProfile(passphrase = "foobar")
40
def test_backup_chains(self):
41
"""Test basic backup chain construction"""
42
random.shuffle(filename_list1)
43
cs = collections.CollectionsStatus(dummy_backend, archive_dir)
44
chains, orphaned, incomplete = cs.get_backup_chains(filename_list1)
45
if len(chains) != 1 or len(orphaned) != 0:
51
assert chain.end_time == 1029654270L
52
assert chain.fullset.time == 1029626221L
54
def test_collections_status(self):
55
"""Test CollectionStatus object's set_values()"""
57
"""Check values of collections status"""
60
assert cs.matched_chain_pair
61
assert cs.matched_chain_pair[0].end_time == 1029826800L
62
assert len(cs.all_backup_chains) == 1, cs.all_backup_chains
64
cs1 = collections.CollectionsStatus(real_backend).set_values()
66
assert not cs1.matched_chain_pair[0].islocal()
68
cs2 = collections.CollectionsStatus(real_backend, archive_dir).set_values()
70
assert cs2.matched_chain_pair[0].islocal()
72
def test_sig_chain(self):
73
"""Test a single signature chain"""
74
chain = collections.SignatureChain(1, archive_dir)
75
for filename in local_sigchain_filename_list:
76
assert chain.add_filename(filename)
77
assert not chain.add_filename("duplicity-new-signatures.2002-08-18T00:04:30-07:00.to.2002-08-20T00:00:00-07:00.sigtar.gpg")
79
def test_sig_chains(self):
80
"""Test making signature chains from filename list"""
81
cs = collections.CollectionsStatus(dummy_backend, archive_dir)
82
chains, orphaned_paths = cs.get_signature_chains(local = 1)
83
self.sig_chains_helper(chains, orphaned_paths)
85
def test_sig_chains2(self):
86
"""Test making signature chains from filename list on backend"""
87
cs = collections.CollectionsStatus(archive_dir_backend)
88
chains, orphaned_paths = cs.get_signature_chains(local = None)
89
self.sig_chains_helper(chains, orphaned_paths)
91
def sig_chains_helper(self, chains, orphaned_paths):
92
"""Test chains and orphaned_paths values for two above tests"""
94
for op in orphaned_paths: print op
96
assert len(chains) == 1, chains
97
assert chains[0].end_time == 1029826800L
99
def sigchain_fileobj_get(self, local):
100
"""Return chain, local if local is true with filenames added"""
102
chain = collections.SignatureChain(1, archive_dir)
103
for filename in local_sigchain_filename_list:
104
assert chain.add_filename(filename)
106
chain = collections.SignatureChain(None, real_backend)
107
for filename in remote_sigchain_filename_list:
108
assert chain.add_filename(filename)
111
def sigchain_fileobj_testlist(self, chain):
112
"""Make sure the list of file objects in chain has right contents
114
The contents of the testfiles/collectiontest/remote_dir have
115
to be coordinated with this test.
118
fileobjlist = chain.get_fileobjs()
119
assert len(fileobjlist) == 3
120
def test_fileobj(i, s):
121
buf = fileobjlist[i].read()
122
fileobjlist[i].close()
123
assert buf == s, (buf, s)
124
test_fileobj(0, "Hello, world!")
125
test_fileobj(1, "hello 1")
126
test_fileobj(2, "Hello 2")
128
def test_sigchain_fileobj(self):
129
"""Test getting signature chain fileobjs from archive_dir"""
130
self.set_gpg_profile()
131
self.sigchain_fileobj_testlist(self.sigchain_fileobj_get(1))
132
self.sigchain_fileobj_testlist(self.sigchain_fileobj_get(None))
136
if __name__ == "__main__": unittest.main()