summaryrefslogtreecommitdiff
path: root/rdiff-backup/testing/hardlinktest.py
blob: a45e4271e4c94ba49ee431dfa028e741a29300e5 (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
import os, unittest
execfile("commontest.py")
rbexec("main.py")


Log.setverbosity(7)

class HardlinkTest(unittest.TestCase):
	"""Test cases for Hard links"""
	outputrp = RPath(Globals.local_connection, "testfiles/output")
	hardlink_dir1 = RPath(Globals.local_connection, "testfiles/hardlinks/dir1")
	hardlink_dir1copy = \
		RPath(Globals.local_connection, "testfiles/hardlinks/dir1copy")
	hardlink_dir2 = RPath(Globals.local_connection, "testfiles/hardlinks/dir2")
	hardlink_dir3 = RPath(Globals.local_connection, "testfiles/hardlinks/dir3")

	def reset_output(self):
		"""Erase and recreate testfiles/output directory"""
		os.system(MiscDir+'/myrm testfiles/output')
		self.outputrp.mkdir()

	def testEquality(self):
		"""Test rorp_eq function in conjunction with CompareRecursive"""
		assert CompareRecursive(self.hardlink_dir1, self.hardlink_dir1copy)
		assert CompareRecursive(self.hardlink_dir1, self.hardlink_dir2,
								compare_hardlinks = None)
		assert not CompareRecursive(self.hardlink_dir1, self.hardlink_dir2,
								compare_hardlinks = 1)

	def testCheckpointing(self):
		"""Test saving and recovering of various dictionaries"""
		d1 = {1:1}
		d2 = {2:2}
		d3 = {3:3}
		d4 = {}

		Hardlink._src_inode_indicies = d1
		Hardlink._src_index_indicies = d2
		Hardlink._dest_inode_indicies = d3
		Hardlink._dest_index_indicies = d4

		self.reset_output()
		Time.setcurtime(12345)
		Globals.isbackup_writer = 1
		Hardlink.final_checkpoint(self.outputrp)

		reset_hardlink_dicts()
		assert Hardlink.retrieve_checkpoint(self.outputrp, 12345)
		assert Hardlink._src_inode_indicies == d1, \
			   Hardlink._src_inode_indicies
		assert Hardlink._src_index_indicies == d2, \
			   Hardlink._src_index_indicies
		assert Hardlink._dest_inode_indicies == d3, \
			   Hardlink._dest_inode_indicies
		assert Hardlink._dest_index_indicies == d4, \
			   Hardlink._dest_index_indicies

	def testFinalwrite(self):
		"""Test writing of the final database"""
		Globals.isbackup_writer = 1
		Time.setcurtime(123456)
		Globals.rbdir = self.outputrp
		finald = Hardlink._src_index_indicies = {'hello':'world'}
		
		self.reset_output()
		Hardlink.final_writedata()

		Hardlink._src_index_indicies = None
		assert Hardlink.retrieve_final(123456)
		assert Hardlink._src_index_indicies == finald

	def testBuildingDict(self):
		"""See if the partial inode dictionary is correct"""
		Globals.preserve_hardlinks = 1
		reset_hardlink_dicts()
		for dsrp in Select(DSRPath(1, self.hardlink_dir3)).set_iter():
			Hardlink.add_rorp(dsrp, 1)
		
		assert len(Hardlink._src_inode_indicies.keys()) == 3, \
			   Hardlink._src_inode_indicies
		assert len(Hardlink._src_index_indicies.keys()) == 3, \
			   Hardlink._src_index_indicies
		vals1 = Hardlink._src_inode_indicies.values()
		vals2 = Hardlink._src_index_indicies.values()
		vals1.sort()
		vals2.sort()
		assert vals1 == vals2

	def testBuildingDict2(self):
		"""Same as testBuildingDict but test destination building"""
		Globals.preserve_hardlinks = 1
		reset_hardlink_dicts()
		for dsrp in Select(DSRPath(None, self.hardlink_dir3)).set_iter():
			Hardlink.add_rorp(dsrp, None)
		
		assert len(Hardlink._dest_inode_indicies.keys()) == 3, \
			   Hardlink._dest_inode_indicies
		assert len(Hardlink._dest_index_indicies.keys()) == 3, \
			   Hardlink._dest_index_indicies
		vals1 = Hardlink._dest_inode_indicies.values()
		vals2 = Hardlink._dest_index_indicies.values()
		vals1.sort()
		vals2.sort()
		assert vals1 == vals2

	def testCompletedDict(self):
		"""See if the hardlink dictionaries are built correctly"""
		reset_hardlink_dicts()
		for dsrp in Select(DSRPath(1, self.hardlink_dir1)).set_iter():
			Hardlink.add_rorp(dsrp, 1)
		assert Hardlink._src_inode_indicies == {}, \
			   Hardlink._src_inode_indicies

		hll1 = [('file1',), ('file2',), ('file3',)]
		hll2 = [('file4',), ('file5',), ('file6',)]
		dict = {}
		for index in hll1: dict[index] = hll1
		for index in hll2: dict[index] = hll2
		assert Hardlink._src_index_indicies == dict

		reset_hardlink_dicts()
		for dsrp in Select(DSRPath(1, self.hardlink_dir2)).set_iter():
			Hardlink.add_rorp(dsrp, 1)
		assert Hardlink._src_inode_indicies == {}, \
			   Hardlink._src_inode_indicies

		hll1 = [('file1',), ('file3',), ('file4',)]
		hll2 = [('file2',), ('file5',), ('file6',)]
		dict = {}
		for index in hll1: dict[index] = hll1
		for index in hll2: dict[index] = hll2
		assert Hardlink._src_index_indicies == dict

	def testSeries(self):
		"""Test hardlink system by backing up and restoring a few dirs"""
		dirlist = ['testfiles/hardlinks/dir1',
				   'testfiles/hardlinks/dir2',
				   'testfiles/hardlinks/dir3',
				   'testfiles/various_file_types']
		BackupRestoreSeries(None, None, dirlist, compare_hardlinks=1)
		BackupRestoreSeries(1, 1, dirlist, compare_hardlinks=1)



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