summaryrefslogtreecommitdiff
path: root/rdiff-backup/testing/iterfiletest.py
blob: a0a326012a01f6795ee3991767f8bafa984d62c1 (plain)
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
import unittest, StringIO
from commontest import *
from rdiff_backup.iterfile import *
from rdiff_backup import lazy

class FileException:
	"""Like a file, but raise exception after certain # bytes read"""
	def __init__(self, max):
		self.count = 0
		self.max = max
	def read(self, l):
		self.count += l
		if self.count > self.max: raise IOError(13, "Permission Denied")
		return "a"*l
	def close(self): return None


class testIterFile(unittest.TestCase):
	def setUp(self):
		self.iter1maker = lambda: iter(range(50))
		self.iter2maker = lambda: iter(map(str, range(50)))

	def testConversion(self):
		"""Test iter to file conversion"""
		for itm in [self.iter1maker, self.iter2maker]:
			assert lazy.Iter.equal(itm(),
								   IterWrappingFile(FileWrappingIter(itm())))

	def testFile(self):
		"""Test sending files through iters"""
		buf1 = "hello"*10000
		file1 = StringIO.StringIO(buf1)
		buf2 = "goodbye"*10000
		file2 = StringIO.StringIO(buf2)
		file_iter = FileWrappingIter(iter([file1, file2]))

		new_iter = IterWrappingFile(file_iter)
		assert new_iter.next().read() == buf1
		assert new_iter.next().read() == buf2
		self.assertRaises(StopIteration, new_iter.next)

	def testFileException(self):
		"""Test encoding a file which raises an exception"""
		f = FileException(200*1024) # size depends on buffer size
		new_iter = IterWrappingFile(FileWrappingIter(iter([f, "foo"])))
		f_out = new_iter.next()
		assert f_out.read(50000) == "a"*50000
		try: buf = f_out.read(190*1024)
		except IOError: pass
		else: assert 0, len(buf)

		assert new_iter.next() == "foo"
		self.assertRaises(StopIteration, new_iter.next)


class testMiscIters(unittest.TestCase):
	"""Test sending rorpiter back and forth"""
	def setUp(self):
		"""Make testfiles/output directory and a few files"""
		Myrm("testfiles/output")
		self.outputrp = rpath.RPath(Globals.local_connection,
									"testfiles/output")
		self.regfile1 = self.outputrp.append("reg1")
		self.regfile2 = self.outputrp.append("reg2")
		self.regfile3 = self.outputrp.append("reg3")

		self.outputrp.mkdir()

		fp = self.regfile1.open("wb")
		fp.write("hello")
		fp.close()
		self.regfile1.setfile(self.regfile1.open("rb"))

		self.regfile2.touch()
		self.regfile2.setfile(self.regfile2.open("rb"))

		fp = self.regfile3.open("wb")
		fp.write("goodbye")
		fp.close()
		self.regfile3.setfile(self.regfile3.open("rb"))
		
		self.regfile1.setdata()
		self.regfile2.setdata()
		self.regfile3.setdata()
		
	def print_MiscIterFile(self, rpiter_file):
		"""Print the given rorpiter file"""
		while 1:
			buf = rpiter_file.read()
			sys.stdout.write(buf)
			if buf[0] == "z": break

	def testBasic(self):
		"""Test basic conversion"""
		l = [self.outputrp, self.regfile1, self.regfile2, self.regfile3]
		i_out = FileToMiscIter(MiscIterToFile(iter(l)))

		out1 = i_out.next()
		assert out1 == self.outputrp

		out2 = i_out.next()
		assert out2 == self.regfile1
		fp = out2.open("rb")
		assert fp.read() == "hello"
		assert not fp.close()

		out3 = i_out.next()
		assert out3 == self.regfile2
		fp = out3.open("rb")
		assert fp.read() == ""
		assert not fp.close()

		i_out.next()
		self.assertRaises(StopIteration, i_out.next)

	def testMix(self):
		"""Test a mix of RPs and ordinary objects"""
		l = [5, self.regfile3, "hello"]
		s = MiscIterToFile(iter(l)).read()
		i_out = FileToMiscIter(StringIO.StringIO(s))

		out1 = i_out.next()
		assert out1 == 5, out1

		out2 = i_out.next()
		assert out2 == self.regfile3
		fp = out2.open("rb")
		assert fp.read() == "goodbye"
		assert not fp.close()

		out3 = i_out.next()
		assert out3 == "hello", out3

		self.assertRaises(StopIteration, i_out.next)

	def testFlush(self):
		"""Test flushing property of MiscIterToFile"""
		l = [self.outputrp, MiscIterFlush, self.outputrp]
		filelike = MiscIterToFile(iter(l))
		new_filelike = StringIO.StringIO((filelike.read() + "z" +
										  C.long2str(0L)))

		i_out = FileToMiscIter(new_filelike)
		assert i_out.next() == self.outputrp
		self.assertRaises(StopIteration, i_out.next)

		i_out2 = FileToMiscIter(filelike)
		assert i_out2.next() == self.outputrp
		self.assertRaises(StopIteration, i_out2.next)

	def testFlushRepeat(self):
		"""Test flushing like above, but have Flush obj emerge from iter"""
		l = [self.outputrp, MiscIterFlushRepeat, self.outputrp]
		filelike = MiscIterToFile(iter(l))
		new_filelike = StringIO.StringIO((filelike.read() + "z" +
										  C.long2str(0L)))

		i_out = FileToMiscIter(new_filelike)
		assert i_out.next() == self.outputrp
		assert i_out.next() is MiscIterFlushRepeat
		self.assertRaises(StopIteration, i_out.next)

		i_out2 = FileToMiscIter(filelike)
		assert i_out2.next() == self.outputrp
		self.assertRaises(StopIteration, i_out2.next)



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