/
test_backup.py
195 lines (165 loc) · 6.26 KB
/
test_backup.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
import os
import tarfile
import zipfile
import pytest
from django.core.files.base import ContentFile
from ..backup import Backup
BACKUPS_ROOT = 'ideascube/serveradmin/tests/backups'
DATA_ROOT = 'ideascube/serveradmin/tests/data/backup'
BACKUPED_ROOT = 'ideascube/serveradmin/tests/backuped'
def test_backup_init_should_raise_with_malformatted_string():
with pytest.raises(ValueError):
Backup('xxxxx')
def test_underscore_as_separator_should_still_be_supported():
Backup('edoardo-0.0.0-201501231405.zip')
@pytest.mark.parametrize('input,expected', [
('name.zip', 'zip'),
('name.tar', 'tar'),
('name.tar.bz2', 'bztar'),
('name.tar.gz', 'gztar'),
])
def test_guess_format_from_name(input, expected):
assert Backup.guess_file_format(input) == expected
def test_list(monkeypatch, settings):
monkeypatch.setattr('ideascube.serveradmin.backup.Backup.ROOT',
BACKUPS_ROOT)
settings.DATETIME_FORMAT = 'Y'
filename = 'edoardo-0.0.0-201501231405.zip'
good = os.path.join(BACKUPS_ROOT, filename)
bad = os.path.join(BACKUPS_ROOT, 'donotlistme.zip')
try:
os.makedirs(BACKUPS_ROOT)
except OSError:
pass # Already exists.
open(good, mode='w')
open(bad, mode='w')
backups = list(Backup.list())
assert len(backups) == 1
assert backups[0].name == filename
os.remove(good)
os.remove(bad)
os.rmdir(BACKUPS_ROOT)
def test_create_zipfile(monkeypatch, settings):
settings.BACKUPED_ROOT = BACKUPED_ROOT
try:
os.makedirs(BACKUPED_ROOT)
except OSError:
pass
filename = 'edoardo-0.0.0-201501231405.zip'
filepath = os.path.join(BACKUPS_ROOT, filename)
try:
# Make sure it doesn't exist before running backup.
os.remove(filepath)
except OSError:
pass
monkeypatch.setattr('ideascube.serveradmin.backup.Backup.FORMAT', 'zip')
monkeypatch.setattr('ideascube.serveradmin.backup.Backup.ROOT',
BACKUPS_ROOT)
monkeypatch.setattr('ideascube.serveradmin.backup.make_name',
lambda f: filename)
proof_file = os.path.join(settings.BACKUPED_ROOT, 'backup.me')
open(proof_file, mode='w')
Backup.create()
assert os.path.exists(filepath)
assert zipfile.is_zipfile(filepath)
archive = zipfile.ZipFile(filepath)
assert 'backup.me' in archive.namelist()
os.remove(filepath)
os.remove(proof_file)
@pytest.mark.parametrize('format,extension', [
('tar', '.tar'),
('bztar', '.tar.bz2'),
('gztar', '.tar.gz'),
])
def test_create_tarfile(monkeypatch, settings, format, extension):
settings.BACKUPED_ROOT = BACKUPED_ROOT
try:
os.makedirs(BACKUPED_ROOT)
except OSError:
pass
filename = 'edoardo-0.0.0-201501231405' + extension
filepath = os.path.join(BACKUPS_ROOT, filename)
try:
# Make sure it doesn't exist before running backup.
os.remove(filepath)
except OSError:
pass
monkeypatch.setattr('ideascube.serveradmin.backup.Backup.ROOT',
BACKUPS_ROOT)
monkeypatch.setattr('ideascube.serveradmin.backup.Backup.FORMAT', format)
monkeypatch.setattr('ideascube.serveradmin.backup.make_name',
lambda f: filename)
proof_file = os.path.join(settings.BACKUPED_ROOT, 'backup.me')
open(proof_file, mode='w')
Backup.create()
assert os.path.exists(filepath)
assert tarfile.is_tarfile(filepath)
archive = tarfile.open(filepath)
assert './backup.me' in archive.getnames()
archive.close()
os.remove(filepath)
os.remove(proof_file)
@pytest.mark.parametrize('extension', [
('.zip'),
('.tar'),
('.tar.gz'),
('.tar.bz2'),
])
def test_restore(monkeypatch, settings, extension):
monkeypatch.setattr('ideascube.serveradmin.backup.Backup.ROOT', DATA_ROOT)
TEST_BACKUPED_ROOT = 'ideascube/serveradmin/tests/backuped'
settings.BACKUPED_ROOT = TEST_BACKUPED_ROOT
dbpath = os.path.join(TEST_BACKUPED_ROOT, 'default.sqlite')
assert not os.path.exists(dbpath)
backup = Backup('musasa-0.1.0-201501241620' + extension)
assert os.path.exists(backup.path) # Should be shipped in git.
backup.restore()
assert os.path.exists(dbpath)
os.remove(dbpath)
@pytest.mark.parametrize('extension', [
('.zip'),
('.tar'),
('.tar.gz'),
('.tar.bz2'),
])
def test_load(monkeypatch, extension):
monkeypatch.setattr('ideascube.serveradmin.backup.Backup.ROOT',
BACKUPS_ROOT)
backup_name = 'musasa-0.1.0-201501241620' + extension
backup_path = os.path.join(BACKUPS_ROOT, backup_name)
assert not os.path.exists(backup_path)
with open(os.path.join(DATA_ROOT, backup_name), mode='rb') as f:
Backup.load(f)
assert os.path.exists(backup_path)
os.remove(backup_path)
def test_delete(monkeypatch):
monkeypatch.setattr('ideascube.serveradmin.backup.Backup.ROOT',
BACKUPS_ROOT)
backup_name = 'musasa-0.1.0-201501241620.zip'
backup_path = os.path.join(BACKUPS_ROOT, backup_name)
assert not os.path.exists(backup_path)
with open(os.path.join(DATA_ROOT, backup_name), mode='rb') as f:
backup = Backup.load(f)
assert os.path.exists(backup_path)
backup.delete()
assert not os.path.exists(backup_path)
def test_delete_should_not_fail_if_file_is_missing(monkeypatch):
monkeypatch.setattr('ideascube.serveradmin.backup.Backup.ROOT',
BACKUPS_ROOT)
backup = Backup('doesnotexist-0.1.0-201501241620.zip')
backup.delete()
def test_load_should_raise_if_file_is_not_a_zip():
with pytest.raises(ValueError) as excinfo:
Backup.load(ContentFile('xxx', name='musasa_0.1.0_201501241620.zip'))
assert 'Not a zip file' in str(excinfo.value)
def test_load_should_raise_if_file_is_not_a_tar():
bad_file = os.path.join(BACKUPS_ROOT, 'musasa_0.1.0_201501241620.tar')
with pytest.raises(ValueError) as excinfo:
with open(bad_file, 'w') as f:
Backup.load(f)
assert 'Not a tar file' in str(excinfo.value)
os.remove(bad_file)
def test_exists(monkeypatch, settings):
monkeypatch.setattr('ideascube.serveradmin.backup.Backup.ROOT', DATA_ROOT)
assert Backup.exists('musasa-0.1.0-201501241620.zip')
assert not Backup.exists('doesnotexist.zip')