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
|
# -*- 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 config
import sys, os, unittest, gzip
sys.path.insert(0, "../")
from duplicity import dup_temp
from duplicity import file_naming
config.setup()
prefix = "testfiles/output"
class TempTest(unittest.TestCase):
"""Test various temp files methods"""
def setUp(self):
assert not os.system("tar xzf testfiles.tar.gz > /dev/null 2>&1")
def tearDown(self):
assert not os.system("rm -rf testfiles tempdir temp2.tar")
def del_tmp(self):
"""Delete testfiles/output and recreate"""
assert not os.system("rm -rf " + prefix)
assert not os.system("mkdir " + prefix)
def test_temppath(self):
"""Allocate new temppath, try open_with_delete"""
tp = dup_temp.new_temppath()
assert not tp.exists()
fileobj = tp.open("wb")
fileobj.write("hello, there")
fileobj.close()
tp.setdata()
assert tp.isreg()
fin = tp.open_with_delete("rb")
buf = fin.read()
assert buf == "hello, there", buf
fin.close()
assert not tp.exists()
def test_tempduppath(self):
"""Allocate new tempduppath, then open_with_delete"""
# pr indicates file is gzipped
pr = file_naming.ParseResults("inc", manifest = 1,
start_time = 1, end_time = 3,
compressed = 1)
tdp = dup_temp.new_tempduppath(pr)
assert not tdp.exists()
fout = tdp.filtered_open("wb")
fout.write("hello, there")
fout.close()
tdp.setdata()
assert tdp.isreg()
fin1 = gzip.GzipFile(tdp.name, "rb")
buf = fin1.read()
assert buf == "hello, there", buf
fin1.close()
fin2 = tdp.filtered_open_with_delete("rb")
buf2 = fin2.read()
assert buf2 == "hello, there", buf
fin2.close()
assert not tdp.exists()
if __name__ == "__main__":
unittest.main()
|