-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_repair.py
133 lines (107 loc) · 4.33 KB
/
test_repair.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
import os
import pathlib
import platform
import shutil
import subprocess
from time import sleep
import unittest
from filecmp import cmpfiles
import datenbank
from os.path import exists as path_exists
from filecmp import dircmp
from filecmp import cmpfiles
import info_handler
import test_database_insert
test_database_insert.insert_for_test_database()
device_name = platform.node()
def are_dir_trees_equal(dir1, dir2):
dirs_cmp = dircmp(dir1, dir2)
# überprüfen, ob unterschiedliche Files oder Dirs vom Namen her
if len(dirs_cmp.left_only) > 0 or len(dirs_cmp.right_only) > 0 or len(dirs_cmp.funny_files) > 0:
# 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
config = info_handler.get_json_info(device_name)
database_path = config['destination'][device_name] + '/datenbank.db'
database_path = os.path.abspath(database_path)
class TestDatabase(unittest.TestCase):
# for creating test data in backuptest folder
# cd unitTestFiles
# head -c 10M /dev/urandom > sample.txt
# split sample.txt -n 500
#
@classmethod
def setUpClass(cls):
cls.platform_node = platform.node()
# gonna use this in the future maybe
pass
def test_1_init_database_exists(self):
datenbank.Datenbank()
self.assertTrue(path_exists(database_path))
def test_2_backup(self):
process = subprocess.Popen(
["python3", "execute.py", "backup"], stdout=subprocess.PIPE) # stdout is just to get rid of the prints
process.wait()
process.stdout.close()
for jewel in config["jewel_sources"][self.platform_node]:
jewel_absolute_path = str(pathlib.Path(jewel).resolve())
jewel_name = jewel.rsplit("/", 1)[1]
dest_path = config["destination"][self.platform_node] + \
"/fullBackup"f"{device_name}/{jewel_name}"
try:
self.assertTrue(are_dir_trees_equal(dest_path,
jewel_absolute_path))
except FileNotFoundError:
print("Test A:")
print("File or Directory not Found")
self.assertTrue(False)
def test_3_backup_and_create_redundancy(self):
process = subprocess.Popen(
["python3", "execute.py", "backup"], stdout=subprocess.DEVNULL) # stdout is just to get rid of the prints
process.wait()
# process.close()
for jewel in config["jewel_sources"][self.platform_node]:
jewel_absolute_path = str(pathlib.Path(jewel).resolve())
jewel_name = jewel.rsplit("/", 1)[1]
dest_path = config["destination"][self.platform_node] + \
"/fullBackup"f"{device_name}/{jewel_name}"
try:
self.assertTrue(are_dir_trees_equal(dest_path,
jewel_absolute_path))
except FileNotFoundError:
print("Test A:")
print("File or Directory not Found")
self.assertTrue(False)
process = subprocess.Popen(
["python3", "execute.py", "rs", "-ca"], stdout=subprocess.DEVNULL)
process.wait()
try:
self.assertTrue(are_dir_trees_equal(dest_path,
jewel_absolute_path))
except FileNotFoundError:
print("Test A:")
print("File or Directory not Found")
self.assertTrue(False)
@classmethod
def tearDown(foobar):
config = info_handler.get_json_info(device_name)
shutil.rmtree(config["destination"][device_name])
shutil.rmtree(config["restore_destination"][device_name])
pass
def suite():
suite = unittest.TestSuite()
return suite
if __name__ == "__main__":
unittest.main()
runner = unittest.TextTestRunner(failfast=True)
runner.run(suite())
# run tests with python3 -m unittest test_backup.py