-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmock_modules.py
More file actions
232 lines (185 loc) · 8.91 KB
/
mock_modules.py
File metadata and controls
232 lines (185 loc) · 8.91 KB
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
"""
Mock versions of existing modules, for testing the JSON API.
The Web UI isn't tested in this way because: browser
They're all in one files since they're small and don't do anything meaningful.
"""
import database
import file_manager
import uuid
import logging
import threading
from backupchan_server import models
from datetime import datetime
class MockDatabase(database.Database):
def __init__(self):
self.targets: list[models.BackupTarget] = []
self.backups: list[models.Backup] = []
self.lock = threading.RLock() # since validate_target uses it
self.logger = logging.getLogger("mockdb")
def reset(self):
self.targets = []
self.backups = []
self.logger.info("Reset")
def add_target(self, name: str, target_type: models.BackupType, recycle_criteria: models.BackupRecycleCriteria, recycle_value: int | None, recycle_action: models.BackupRecycleAction | None, location: str, name_template: str, deduplicate: bool, alias: str | None, min_backups: int | None, tags: list[str] | None) -> str:
self.validate_target(name, name_template, location, None, alias)
target_id = str(uuid.uuid4())
target = models.BackupTarget(target_id, name, target_type, recycle_criteria, recycle_value, recycle_action, location, name_template, deduplicate, alias, min_backups, tags or [])
self.targets.append(target)
self.logger.info("Add target: %s", target)
return target_id
def edit_target(
self,
id: str,
name: str,
recycle_criteria: models.BackupRecycleCriteria,
recycle_value: int | None,
recycle_action: models.BackupRecycleAction | None,
location: str,
name_template: str,
deduplicate: bool,
alias: str | None,
min_backups: int | None,
tags: list[str] | None
):
self.validate_target(name, name_template, location, id, alias)
target = self.get_target(id)
target.name = name
target.recycle_criteria = recycle_criteria
target.recycle_value = recycle_value
target.recycle_action = recycle_action
target.location = location
target.name_template = name_template
target.deduplicate = deduplicate
target.alias = alias
target.min_backups = min_backups
target.tags = tags
self.logger.info("Edit target {%s} to %s", id, target)
def get_target(self, id: str) -> models.BackupTarget:
for target in self.targets:
if target.id == id or target.alias == id:
return target
return None
def list_targets(self, _ = None) -> list[models.BackupTarget]:
return {
"targets": self.targets,
"has_more": False
}
def list_targets_all(self) -> list[models.BackupTarget]:
return self.targets
def delete_target(self, id: str):
for target in self.targets:
if target.id == id:
self.targets.remove(target)
self.logger.info("Delete target {%s}", id)
def count_targets(self) -> int:
return len(self.targets)
def delete_target_backups(self, id: str):
for backup in self.backups:
if backup.target_id == id:
self.backups.remove(backup)
def add_backup(self, target_id: str, manual: bool, created_at: datetime | None = None) -> str:
if created_at is None:
created_at = datetime.now()
backup_id = str(uuid.uuid4())
backup = models.Backup(backup_id, target_id, created_at, manual, False, 123456, hash(backup_id), False)
self.backups.append(backup)
self.logger.info("Add backup %s", backup)
return backup_id
def set_backup_filesize(self, backup_id: str, filesize: int):
self.get_backup(backup_id).filesize = filesize
def get_backup(self, id: str) -> None | models.Backup:
for backup in self.backups:
if backup.id == id:
return backup
return None
def delete_backup(self, id: str):
self.backups.remove(self.get_backup(id))
self.logger.info("Delete backup {%s}", id)
def recycle_backup(self, id: str, recycled: bool):
self.get_backup(id).is_recycled = recycled
self.logger.info("Recycle backup {%s} -> %s", id, recycled)
def list_backups(self) -> list[models.Backup]:
return self.backups
def list_backups_target(self, target_id: str) -> list[models.Backup]:
backups = []
for backup in self.backups:
if backup.target_id == target_id:
backups.append(backup)
return backups
def list_recycled_backups(self) -> list[models.Backup]:
backups = []
for backup in self.backups:
if backup.is_recycled:
backups.append(backup)
return backups
def list_backups_target_is_recycled(self, target_id: str, is_recycled: bool) -> list[models.Backup]:
backups = []
for backup in self.backups:
if backup.target_id == target_id and backup.is_recycled == is_recycled:
backups.append(backup)
return backups
def count_backups(self) -> int:
return len(self.backups)
def count_recycled_backups(self) -> int:
return len(self.list_recycled_backups())
def __del__(self):
pass # Override because this does not initialize a real db connection.
class MockFileManager(file_manager.FileManager):
def __init__(self, db: MockDatabase):
self.db = db
self.logger = logging.getLogger("mockfm")
def add_backup(self, backup_id: str, filename: str):
backup = self.db.get_backup(backup_id)
if backup is None:
raise file_manager.FileManagerError(f"Backup {backup_id} does not exist")
target = self.db.get_target(backup.target_id)
if target is None:
return file_manager.FileManagerError(f"Backup {backup_id} points to nonexistent target")
self.logger.info("Upload %s to backup {%s}", filename, backup_id)
def delete_backup(self, backup_id: str):
backup = self.db.get_backup(backup_id)
if backup is None:
raise file_manager.FileManagerError(f"Backup {backup_id} does not exist")
target = self.db.get_target(backup.target_id)
if target is None:
return file_manager.FileManagerError(f"Backup {backup_id} points to nonexistent target")
self.logger.info("Delete backup {%s}", backup_id)
def delete_target_backups(self, target_id: str):
target = self.db.get_target(target_id)
if target is None:
raise file_manager.FileManagerError(f"Target {target_id} does not exist")
self.logger.info("Delete all backups for target {%s}", target_id)
def update_backup_locations(self, target: models.BackupTarget, new_name_template: str, new_location: str, old_name_template: str, old_location: str):
self.logger.info("Move target {%s} backups. Location '%s' -> '%s'; name template '%s' -> '%s'", target.id, old_location, new_location, old_name_template, new_name_template)
self.db.validate_target(target.name, new_name_template, new_location, target.id, target.alias)
def recycle_backup(self, backup_id: int):
backup = self.db.get_backup(backup_id)
if backup is None:
raise file_manager.FileManagerError(f"Backup {backup_id} does not exist")
target = self.db.get_target(backup.target_id)
if target is None:
raise file_manager.FileManagerError(f"Backup {backup_id} points to nonexistent target")
self.logger.info("Recycle backup {%s}", backup_id)
def unrecycle_backup(self, backup_id: int):
backup = self.db.get_backup(backup_id)
if backup is None:
raise file_manager.FileManagerError(f"Backup {backup_id} does not exist")
target = self.db.get_target(backup.target_id)
if target is None:
raise file_manager.FileManagerError(f"Backup {backup_id} points to nonexistent target")
self.logger.info("Unrecycle backup {%s}", backup_id)
def get_backup_size(self, backup_id: str) -> int:
backup = self.db.get_backup(backup_id)
if backup is None:
raise file_manager.FileManagerError(f"Backup {backup_id} does not exist")
target = self.db.get_target(backup.target_id)
if target is None:
raise file_manager.FileManagerError(f"Backup {backup_id} points to nonexistent target")
return 123456
def get_target_size(self, target_id: str) -> int:
target = self.db.get_target(target_id)
if target is None:
raise file_manager.FileManagerError(f"Target {target_id} does not exist")
return 123456
def get_backup_list_size(self, backups: list[models.Backup]) -> int:
return 123456