-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_backup_2.py
291 lines (227 loc) · 11.5 KB
/
test_backup_2.py
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import pathlib
import unittest
from backup import Backup
import info_handler as ih
import os
import filecmp
from filecmp import dircmp
from filecmp import cmpfiles
from datetime import datetime as date
import shutil
import subprocess
from subprocess import run
import argparse
import test_backup_2_insert
from os import walk
test_backup_2_insert.insert_for_test_backup_2()
device_name = "testCases"
def are_dir_trees_equal(dir1, dir2):
"""
For comparing, the content of two directories. If they are equal or not.
Detailed description:
The Function travers two directories and compare the names of the their files and folders, if they are equal or not.
Furthermore, the content from their files will compare
Parameter:
two fullpath of directories
Return:
if they are equal, true or false
"""
dirs_cmp = dircmp(dir1, dir2)
if len(dirs_cmp.left_only) > 0 or len(dirs_cmp.right_only) > 0 or len(dirs_cmp.funny_files) > 0: # überprüfen, ob unterschiedliche Files oder Dirs vom Namen her
#existieren
return False
(match, mismatch, errors) = cmpfiles(dir1, dir2, dirs_cmp.common_files, shallow=False)
if len(mismatch) > 0 or len(errors) > 0: # nach Inhalt überprüfen
return False
for common_dir in dirs_cmp.common_dirs:
new_dir1 = os.path.join(dir1, common_dir)
new_dir2 = os.path.join(dir2, common_dir)
if not are_dir_trees_equal(new_dir1, new_dir2):
return False
return True
class TestBackup(unittest.TestCase):
"""
in this test the backup process is tested.
the user is asked for the values number of folders, data, byte or giga byte and amount of memory.
The pipeline will be start with the -p flag. The programm starts the test with predefined values.
The user can start the programm as well with -p.
Parameter:
none
Return:
Error, true, false..
"""
@classmethod
def setUpClass(cls):
global directory_count
global data_count
global data_size
global storage_unit
cls.config = ih.get_json_info(device_name)
cls.workingDirectory = str(pathlib.Path(__file__).parent.resolve())
jewel_list = cls.config["jewel_sources"][device_name]
for (index, element) in enumerate(jewel_list):
jewel_list[index] = cls.workingDirectory + element
# call bash script to create test data
with open("./test_Files_Backup/init.sh", 'rb') as file:
script = file.read()
subprocess.call(script, shell=True)
os.utime(f"{cls.workingDirectory}/test_Files_Backup/jewel")
os.utime(f"{cls.workingDirectory}/test_Files_Backup/jewel2")
os.utime(f"{cls.workingDirectory}/test_Files_Backup/jewel3")
cls.backup = Backup(jewel_list, cls.workingDirectory + '/' + cls.config["destination"][device_name], True)
cls.backup.initialize_backup(1)
# process = subprocess.run(
# ["python3", "./execute.py", "backup"])
parser = argparse.ArgumentParser(description = "whether the pipeline executes")
parser.add_argument("-p", nargs='?', const = True, default = False, help = "to start it in pipeline",
dest="pipeline")
args = parser.parse_args()
# for the pipline
if(args.pipeline):
directory_count = 3
data_count = 3
storage_unit = 'b'
data_size = 1
# for user
else:
while(True):
directory_count = input("enter desired number of folders\n")
try:
directory_count = int(directory_count)
if(directory_count >= 1):
break
else:
print("it has to be greater than 0")
except:
print("must be an integer")
fail_condition = input("do you want to quit the test ? press q or anyting else for continue\n")
if(fail_condition == 'q'):
cls.skipTest("test skipped")
while(True):
data_count = input("enter desired number of data files\n")
try:
data_count = int(data_count)
if(data_count >= 1):
break
else:
print("it has to be greater than 0")
except:
print("must be an integer")
fail_condition = input("do you want to quit the test ? press q or anyting else for continue\n")
if(fail_condition == 'q'):
cls.skipTest("test skipped")
while(True):
storage_unit = input("should the data in byte (b) or gigabyte (g) ?\n")
if(storage_unit not in ['g', 'b']):
print("you have to press g for GigaByte or b Byte")
else:
break
fail_condition = input("do you want to quit the test ? press q or anyting else for continue\n")
if(fail_condition == 'q'):
cls.skipTest("test skipped")
while(True):
storage_unit = "Gigabyte" if storage_unit == 'g' else 'Byte'
data_size = input("enter desired data size in "f"{storage_unit}\n")
try:
data_size = int(data_size)
if(data_size >= 0):
if(storage_unit == 'Gigabyte'):
data_size = data_size * 1000000000
print("in g")
break
else:
print("it has to be greater than 0")
except:
print("must be an integer")
fail_condition = input("do you want to quit the test ? press q or anyting else for continue\n")
if(fail_condition == 'q'):
cls.skipTest("test skipped")
def test_a_fullbackup_jewel(self):
try:
self.assertTrue(are_dir_trees_equal(str(pathlib.Path(__file__).parent.resolve()) + '/' + "test_Files_Backup/jewel",
str(pathlib.Path(__file__).parent.resolve()) + '/' + "test_Files_Backup/backup_Location/fullBackup"f"{device_name}/jewel"))
except FileNotFoundError:
print("Test A:")
print("File or Directory not Found")
self.assertTrue(False)
def test_b_fullbackup_jewel2(self):
try:
self.assertTrue(are_dir_trees_equal(str(pathlib.Path(__file__).parent.resolve()) + '/' + "test_Files_Backup/jewel2",
str(pathlib.Path(__file__).parent.resolve()) + '/' + "test_Files_Backup/backup_Location/fullBackup"f"{device_name}/jewel2"))
except FileNotFoundError:
print("Test B:")
print("File or Directory not Found")
self.assertTrue(False)
# test for blacklist
def test_c_fullbackup_blacklist(self):
config = ih.get_json_info(device_name)
working_directory = str(pathlib.Path(__file__).parent.resolve())
blacklist_dirs = config["blacklist"]["directories"]
blacklist_files = config["blacklist"]["files"]
print(blacklist_dirs)
print(blacklist_files)
for(root, dir_names, file_names) in walk(working_directory + '/'
+ "test_Files_Backup/backup_Location/fullBackup"f"{device_name}/jewel3"):
for file in file_names:
to_cmp_file = os.path.basename(file)
for black_file in blacklist_files:
if black_file == to_cmp_file:
self.assertTrue(False)
for dir in dir_names:
to_cmp_dir = os.path.basename(dir)
for black_dir in blacklist_dirs:
if to_cmp_dir == black_dir:
self.assertTrue(False)
self.assertTrue(True)
def test_d_diffBackup_jewel(self):
subprocess.check_call(["./test_Files_Backup/execScripts.sh", str(directory_count), str(data_count), str(data_size)])
os.utime(f"{str(pathlib.Path(__file__).parent.resolve())}/test_Files_Backup/jewel")
os.utime(f"{str(pathlib.Path(__file__).parent.resolve())}/test_Files_Backup/jewel2")
os.utime(f"{str(pathlib.Path(__file__).parent.resolve())}/test_Files_Backup/jewel3")
# time.sleep(1)
#global date_Format
#date_Format = date.now().strftime('%d-%m-%Y-%H-%M')
self.backup.initialize_backup(1)
# process = subprocess.run(
# ["python3", "./execute.py", "backup"])
# time.sleep(1)
global diffPath
result = run(["find ./test_Files_Backup/backup_Location -type d -name 'diff*'"], capture_output=True, shell=True)
diffPath = str(result.stdout)
diffPath = diffPath[3:len(diffPath) - 3]
try:
self.assertTrue(are_dir_trees_equal(str(pathlib.Path(__file__).parent.resolve()) + '/' + "test_Files_Backup/jewel/make_Data_Dir"
,str(pathlib.Path(__file__).parent.resolve()) + diffPath + "/jewel/make_Data_Dir"))
except FileNotFoundError:
print("Test D:")
print("File or Directory not Found")
self.assertTrue(False)
def test_e_diffBackup_jewel2(self):
try:
self.assertTrue(are_dir_trees_equal(str(pathlib.Path(__file__).parent.resolve()) + '/' + "test_Files_Backup/jewel2/data_And_Link_Dir"
, str(pathlib.Path(__file__).parent.resolve()) + diffPath + "/jewel2/data_And_Link_Dir"))
except FileNotFoundError:
print("Test E")
print("File or Directory not Found")
self.assertTrue(False)
@classmethod
def tearDownClass(cls):
os.remove("./test_Files_Backup/backup_Location/datenbank.db")
os.remove("./test_Files_Backup/backup_Location/db.log")
rel_path = cls.config["destination"][device_name]
for root, dirs, files in os.walk(cls.workingDirectory + '/' + rel_path, topdown=False):
for name in dirs:
shutil.rmtree(os.path.join(root, name))
shutil.rmtree(cls.config["restore_destination"][device_name])
jewel_list = cls.config["jewel_sources"][device_name]
for element in jewel_list:
for root, dirs, files in os.walk(element, topdown=False):
for name in dirs:
shutil.rmtree(os.path.join(root, name))
for name in files:
os.remove(os.path.join(root, name))
if __name__ == "__main__":
# unittest.main()
runner = unittest.TextTestRunner()
itersuite = unittest.TestLoader().loadTestsFromTestCase(TestBackup)
runner.run(itersuite)