-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathforget_test.py
287 lines (257 loc) · 12.1 KB
/
forget_test.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
import unittest
from unittest import mock
import restic.errors
from restic.internal import forget
class ForgetTest(unittest.TestCase):
def setUp(self):
self.maxDiff = None
@mock.patch.object(forget.command_executor, 'execute')
def test_forget(self, mock_execute):
mock_execute.return_value = '{}'
restic.forget()
mock_execute.assert_called_with(['restic', '--json', 'forget'])
@mock.patch.object(forget.command_executor, 'execute')
def test_forget_empty_return(self, mock_execute):
mock_execute.return_value = ''
restic.forget()
mock_execute.assert_called_with(['restic', '--json', 'forget'])
@mock.patch.object(forget.command_executor, 'execute')
def test_dry_run(self, mock_execute):
mock_execute.return_value = '{}'
restic.forget(dry_run=True)
mock_execute.assert_called_with(
['restic', '--json', 'forget', '--dry-run'])
@mock.patch.object(forget.command_executor, 'execute')
def test_forget_with_group_by(self, mock_execute):
mock_execute.return_value = '{}'
restic.forget(prune=True, group_by='host')
mock_execute.assert_called_with(
['restic', '--json', 'forget', '--group-by', 'host', '--prune'])
@mock.patch.object(forget.command_executor, 'execute')
def test_forget_with_single_tag(self, mock_execute):
mock_execute.return_value = '{}'
restic.forget(prune=True, tags=['musician1'])
mock_execute.assert_called_with(
['restic', '--json', 'forget', '--tag', 'musician1', '--prune'])
# See restic documentation:
# https://restic.readthedocs.io/en/latest/060_forget.html#removing-snapshots-according-to-a-policy
#
# Remove all but the last snapshot of all snapshots that have either
# the foo or bar tag set:
# 'restic forget --tag foo --tag bar --keep-last 1'
@mock.patch.object(forget.command_executor, 'execute')
def test_forget_with_multiple_tags_with_or_relation(self, mock_execute):
mock_execute.return_value = '{}'
restic.forget(prune=True, tags=['musician1', 'musician2'])
mock_execute.assert_called_with([
'restic', '--json', 'forget', '--tag', 'musician1', '--tag',
'musician2', '--prune'
])
# See restic documentation:
# https://restic.readthedocs.io/en/latest/060_forget.html#removing-snapshots-according-to-a-policy
#
# Remove all but the last snapshot of all snapshots that have both
# the tag foo and bar set:
# 'restic forget --tag foo,bar --keep-last 1'
@mock.patch.object(forget.command_executor, 'execute')
def test_forget_with_multiple_tags_with_and_relation(self, mock_execute):
mock_execute.return_value = '{}'
restic.forget(prune=True, tags=['musician1,musician2'])
mock_execute.assert_called_with([
'restic', '--json', 'forget', '--tag', 'musician1,musician2',
'--prune'
])
@mock.patch.object(forget.command_executor, 'execute')
def test_forget_with_host(self, mock_execute):
mock_execute.return_value = '{}'
restic.forget(prune=True, host='myhost')
mock_execute.assert_called_with(
['restic', '--json', 'forget', '--host', 'myhost', '--prune'])
@mock.patch.object(forget.command_executor, 'execute')
def test_forget_keep_last_10(self, mock_execute):
mock_execute.return_value = '{}'
restic.forget(keep_last=10)
mock_execute.assert_called_with(
['restic', '--json', 'forget', '--keep-last', '10'])
@mock.patch.object(forget.command_executor, 'execute')
def test_forget_keep_hourly_10(self, mock_execute):
mock_execute.return_value = '{}'
restic.forget(keep_hourly=10)
mock_execute.assert_called_with(
['restic', '--json', 'forget', '--keep-hourly', '10'])
@mock.patch.object(forget.command_executor, 'execute')
def test_forget_all_except_last_30_days(self, mock_execute):
mock_execute.return_value = '{}'
restic.forget(keep_daily=30)
mock_execute.assert_called_with(
['restic', '--json', 'forget', '--keep-daily', '30'])
@mock.patch.object(forget.command_executor, 'execute')
def test_forget_keep_weekly_10(self, mock_execute):
mock_execute.return_value = '{}'
restic.forget(keep_weekly=10)
mock_execute.assert_called_with(
['restic', '--json', 'forget', '--keep-weekly', '10'])
@mock.patch.object(forget.command_executor, 'execute')
def test_forget_keep_monthly_10(self, mock_execute):
mock_execute.return_value = '{}'
restic.forget(keep_monthly=10)
mock_execute.assert_called_with(
['restic', '--json', 'forget', '--keep-monthly', '10'])
@mock.patch.object(forget.command_executor, 'execute')
def test_forget_keep_yearly_10(self, mock_execute):
mock_execute.return_value = '{}'
restic.forget(keep_yearly=10)
mock_execute.assert_called_with(
['restic', '--json', 'forget', '--keep-yearly', '10'])
@mock.patch.object(forget.command_executor, 'execute')
def test_forget_all_except_last_30_days_and_prune(self, mock_execute):
mock_execute.return_value = '{}'
restic.forget(prune=True, keep_daily=30)
mock_execute.assert_called_with(
['restic', '--json', 'forget', '--keep-daily', '30', '--prune'])
@mock.patch.object(forget.command_executor, 'execute')
def test_forget_and_prune(self, mock_execute):
mock_execute.return_value = '{}'
restic.forget(prune=True)
mock_execute.assert_called_with(
['restic', '--json', 'forget', '--prune'])
@mock.patch.object(forget.command_executor, 'execute')
def test_forget_and_keep_within(self, mock_execute):
mock_execute.return_value = '{}'
restic.forget(prune=True, keep_within='60d')
mock_execute.assert_called_with(
['restic', '--json', 'forget', '--keep-within', '60d', '--prune'])
@mock.patch.object(forget.command_executor, 'execute')
def test_parses_result_json(self, mock_execute):
# Ignore complaint about too long of a line.
# pylint: disable=C0301
mock_execute.return_value = """
[{"tags": null, "host": "ecb5551395ae", "paths": ["/tmp/tmp6ew1vzp2/mydata.txt"], "keep": [{"time": "2021-03-16T00:10:37.015657013Z", "tree": "4483c2c6c1386abb9f47497cf108bab19e09c42430d32cd640a4f6f97137841f", "paths": ["/tmp/tmp6ew1vzp2/mydata.txt"], "hostname": "ecb5551395ae", "username": "circleci", "uid": 3434, "gid": 3434, "id": "3f6de49c6461ffd42900a204655708a3e136a3814abe298c07f27e412e2b6a43", "short_id": "3f6de49c"}], "remove": null, "reasons": [{"snapshot": {"time": "2021-03-16T00:10:37.015657013Z", "tree": "4483c2c6c1386abb9f47497cf108bab19e09c42430d32cd640a4f6f97137841f", "paths": ["/tmp/tmp6ew1vzp2/mydata.txt"], "hostname": "ecb5551395ae", "username": "circleci", "uid": 3434, "gid": 3434}, "matches": ["daily snapshot"], "counters": {"daily": 4}}]}]
""".strip()
forget_result = restic.forget()
self.assertEqual([{
'tags':
None,
'host':
'ecb5551395ae',
'paths': ['/tmp/tmp6ew1vzp2/mydata.txt'],
'keep': [{
'time':
'2021-03-16T00:10:37.015657013Z',
'tree':
'4483c2c6c1386abb9f47497cf108bab19e09c42430d32cd640a4f6f97137841f',
'paths': ['/tmp/tmp6ew1vzp2/mydata.txt'],
'hostname':
'ecb5551395ae',
'username':
'circleci',
'uid':
3434,
'gid':
3434,
'id':
'3f6de49c6461ffd42900a204655708a3e136a3814abe298c07f27e412e2b6a43',
'short_id':
'3f6de49c'
}],
'remove':
None,
'reasons': [{
'snapshot': {
'time':
'2021-03-16T00:10:37.015657013Z',
'tree':
'4483c2c6c1386abb9f47497cf108bab19e09c42430d32cd640a4f6f97137841f',
'paths': ['/tmp/tmp6ew1vzp2/mydata.txt'],
'hostname':
'ecb5551395ae',
'username':
'circleci',
'uid':
3434,
'gid':
3434
},
'matches': ['daily snapshot'],
'counters': {
'daily': 4
}
}]
}], forget_result)
@mock.patch.object(forget.command_executor, 'execute')
def test_parses_result_json_and_ignores_text_afterwards(self, mock_execute):
# Ignore complaint about too long of a line.
# pylint: disable=C0301
mock_execute.return_value = """
[{"tags": null, "host": "ecb5551395ae", "paths": ["/tmp/tmp6ew1vzp2/mydata.txt"], "keep": [{"time": "2021-03-16T00:10:37.015657013Z", "tree": "4483c2c6c1386abb9f47497cf108bab19e09c42430d32cd640a4f6f97137841f", "paths": ["/tmp/tmp6ew1vzp2/mydata.txt"], "hostname": "ecb5551395ae", "username": "circleci", "uid": 3434, "gid": 3434, "id": "3f6de49c6461ffd42900a204655708a3e136a3814abe298c07f27e412e2b6a43", "short_id": "3f6de49c"}], "remove": null, "reasons": [{"snapshot": {"time": "2021-03-16T00:10:37.015657013Z", "tree": "4483c2c6c1386abb9f47497cf108bab19e09c42430d32cd640a4f6f97137841f", "paths": ["/tmp/tmp6ew1vzp2/mydata.txt"], "hostname": "ecb5551395ae", "username": "circleci", "uid": 3434, "gid": 3434}, "matches": ["daily snapshot"], "counters": {"daily": 4}}]}]
loading indexes...
loading all snapshots...
finding data that is still in use for 2 snapshots
[0:00] 100.00% 2 / 2 snapshots
searching used packs...
collecting packs for deletion and repacking
[0:00] 100.00% 6 / 6 packs processed
to repack: 0 blobs / 0 B
this removes 0 blobs / 0 B
to delete: 0 blobs / 0 B
total prune: 0 blobs / 0 B
remaining: 49 blobs / 4.890 MiB
unused size after prune: 0 B (0.00% of remaining size)
done
""".lstrip()
forget_result = restic.forget()
self.assertEqual([{
'tags':
None,
'host':
'ecb5551395ae',
'paths': ['/tmp/tmp6ew1vzp2/mydata.txt'],
'keep': [{
'time':
'2021-03-16T00:10:37.015657013Z',
'tree':
'4483c2c6c1386abb9f47497cf108bab19e09c42430d32cd640a4f6f97137841f',
'paths': ['/tmp/tmp6ew1vzp2/mydata.txt'],
'hostname':
'ecb5551395ae',
'username':
'circleci',
'uid':
3434,
'gid':
3434,
'id':
'3f6de49c6461ffd42900a204655708a3e136a3814abe298c07f27e412e2b6a43',
'short_id':
'3f6de49c'
}],
'remove':
None,
'reasons': [{
'snapshot': {
'time':
'2021-03-16T00:10:37.015657013Z',
'tree':
'4483c2c6c1386abb9f47497cf108bab19e09c42430d32cd640a4f6f97137841f',
'paths': ['/tmp/tmp6ew1vzp2/mydata.txt'],
'hostname':
'ecb5551395ae',
'username':
'circleci',
'uid':
3434,
'gid':
3434
},
'matches': ['daily snapshot'],
'counters': {
'daily': 4
}
}]
}], forget_result)
@mock.patch.object(forget.command_executor, 'execute')
def test_forget_raises_exception_when_response_is_invalid_json(
self, mock_execute):
mock_execute.return_value = '{{{{{{{[[[[[['
with self.assertRaises(restic.errors.UnexpectedResticResponse):
restic.forget()