summaryrefslogtreecommitdiffstats
path: root/t/handle_added_archives_with_db.t
blob: 40f79c724f7586e8a0aeee625e167fd4638cef16 (plain)
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
use strictures 2;

use Log::Any::Adapter ('TAP');
use POSIX qw(tzset);
use Test::Differences;
use Test::MockObject;
use Test::More;

use App::BorgRestore;
use App::BorgRestore::Settings;

for my $in_memory (0,1) {
	my $db = App::BorgRestore::DB->new(":memory:", 0);

	$ENV{TZ} = 'UTC';
	tzset;

	my $borg = Test::MockObject->new();
	$borg->set_list('borg_list', ['archive-1']);
	$borg->mock('list_archive', sub {
			my ($self, $archive, $cb) = @_;
			$cb->("XXX, 1970-01-01 00:00:05 .");
			$cb->("XXX, 1970-01-01 00:00:10 boot");
			$cb->("XXX, 1970-01-01 00:00:20 boot/grub");
			$cb->("XXX, 1970-01-01 00:00:08 boot/grub/grub.cfg");
			$cb->("XXX, 1970-01-01 00:00:09 boot/grub/grub.cfg.sig");
			$cb->("XXX, 1970-01-01 00:00:13 boot/foo");
			$cb->("XXX, 1970-01-01 00:00:13 boot/foo/blub");
			$cb->("XXX, 1970-01-01 00:00:19 boot/foo/bar");
			$cb->("XXX, 1970-01-01 00:00:02 boot/test1");
			$cb->("XXX, 1970-01-01 00:00:03 boot/test1/f1");
			$cb->("XXX, 1970-01-01 00:00:04 boot/test1/f2");
			$cb->("XXX, 1970-01-01 00:00:03 boot/test1/f3");
			$cb->("XXX, 1970-01-01 00:00:02 boot/test1/f4");
			$cb->("XXX, 1970-01-01 00:00:10 boot1");
			$cb->("XXX, 1970-01-01 00:00:03 etc");
			$cb->("XXX, 1970-01-01 00:00:02 etc/foo");
			$cb->("XXX, 1970-01-01 00:00:01 etc/foo/bar");
			$cb->("XXX, 1970-01-01 00:00:01 etc/foo/blub");
		} );

	# Call the actual function we want to test
	my $app = App::BorgRestore->new_no_defaults({borg => $borg, db => $db}, {cache => {prepare_data_in_memory => $in_memory}});
	$app->_handle_added_archives(['archive-1']);

	# check database content
	is($db->get_archive_row_count(), 17, 'correct row count (17 paths with timestamps)');
	eq_or_diff($db->get_archives_for_path('.'), [{archive => 'archive-1', modification_time => undef},]);
	eq_or_diff($db->get_archives_for_path('boot'), [{archive => 'archive-1', modification_time => 20},]);
	eq_or_diff($db->get_archives_for_path('boot/foo'), [{archive => 'archive-1', modification_time => 19},]);
	eq_or_diff($db->get_archives_for_path('boot/foo/bar'), [{archive => 'archive-1', modification_time => 19},]);
	eq_or_diff($db->get_archives_for_path('boot/foo/blub'), [{archive => 'archive-1', modification_time => 13},]);
	eq_or_diff($db->get_archives_for_path('boot/grub'), [{archive => 'archive-1', modification_time => 20},]);
	eq_or_diff($db->get_archives_for_path('boot/grub/grub.cfg'), [{archive => 'archive-1', modification_time => 8},]);
	eq_or_diff($db->get_archives_for_path('boot/grub/grub.cfg.sig'), [{archive => 'archive-1', modification_time => 9},]);
	eq_or_diff($db->get_archives_for_path('boot/test1'), [{archive => 'archive-1', modification_time => 4},]);
	eq_or_diff($db->get_archives_for_path('boot/test1/f1'), [{archive => 'archive-1', modification_time => 3},]);
	eq_or_diff($db->get_archives_for_path('boot/test1/f2'), [{archive => 'archive-1', modification_time => 4},]);
	eq_or_diff($db->get_archives_for_path('boot/test1/f3'), [{archive => 'archive-1', modification_time => 3},]);
	eq_or_diff($db->get_archives_for_path('boot/test1/f4'), [{archive => 'archive-1', modification_time => 2},]);
	eq_or_diff($db->get_archives_for_path('boot1'), [{archive => 'archive-1', modification_time => 10},]);
	eq_or_diff($db->get_archives_for_path('etc'), [{archive => 'archive-1', modification_time => 3},]);
	eq_or_diff($db->get_archives_for_path('etc/foo'), [{archive => 'archive-1', modification_time => 2},]);
	eq_or_diff($db->get_archives_for_path('etc/foo/bar'), [{archive => 'archive-1', modification_time => 1},]);
	eq_or_diff($db->get_archives_for_path('etc/foo/blub'), [{archive => 'archive-1', modification_time => 1},]);
	eq_or_diff($db->get_archives_for_path('lulz'), [{archive => 'archive-1', modification_time => undef},], 'test non-existant path');


	# add second archive
	$borg->set_list('borg_list', ['archive-1', 'archive-2']);
	$borg->mock('list_archive', sub {
			my ($self, $archive, $cb) = @_;
			$cb->("XXX, 1970-01-01 00:00:05 .");
			$cb->("XXX, 1970-01-01 00:00:10 boot");
			$cb->("XXX, 1970-01-01 00:00:20 boot/grub");
			$cb->("XXX, 1970-01-01 00:00:08 boot/grub/grub.cfg");
			$cb->("XXX, 1970-01-01 00:00:09 boot/grub/grub.cfg.sig");
			$cb->("XXX, 1970-01-01 00:00:13 boot/foo");
			$cb->("XXX, 1970-01-01 00:00:13 boot/foo/blub");
			$cb->("XXX, 1970-01-01 00:00:19 boot/foo/bar");
			$cb->("XXX, 1970-01-01 00:00:02 boot/test1");
			$cb->("XXX, 1970-01-01 00:00:03 boot/test1/f1");
			$cb->("XXX, 1970-01-01 00:00:05 boot/test1/f2");
			$cb->("XXX, 1970-01-01 00:00:03 boot/test1/f3");
			$cb->("XXX, 1970-01-01 00:00:02 boot/test1/f4");
			$cb->("XXX, 1970-01-01 00:00:07 boot/test1/f5");
			$cb->("XXX, 1970-01-01 00:00:10 boot1");
			$cb->("XXX, 1970-01-01 00:00:03 etc");
			$cb->("XXX, 1970-01-01 00:00:02 etc/foo");
			$cb->("XXX, 1970-01-01 00:00:01 etc/foo/bar");
		} );
	$app->_handle_added_archives(['archive-2']);

	# check database content
	is($db->get_archive_row_count(), 18, 'correct row count (18 paths with timestamps)');
	eq_or_diff($db->get_archives_for_path('.'), [
		{archive => 'archive-1', modification_time => undef},
		{archive => 'archive-2', modification_time => undef},
	]);
	eq_or_diff($db->get_archives_for_path('boot'), [
		{archive => 'archive-1', modification_time => 20},
		{archive => 'archive-2', modification_time => 20},
	]);
	eq_or_diff($db->get_archives_for_path('boot/foo'), [
		{archive => 'archive-1', modification_time => 19},
		{archive => 'archive-2', modification_time => 19},
	]);
	eq_or_diff($db->get_archives_for_path('boot/foo/bar'), [
		{archive => 'archive-1', modification_time => 19},
		{archive => 'archive-2', modification_time => 19},
	]);
	eq_or_diff($db->get_archives_for_path('boot/foo/blub'), [
		{archive => 'archive-1', modification_time => 13},
		{archive => 'archive-2', modification_time => 13},
	]);
	eq_or_diff($db->get_archives_for_path('boot/grub'), [
		{archive => 'archive-1', modification_time => 20},
		{archive => 'archive-2', modification_time => 20},
	]);
	eq_or_diff($db->get_archives_for_path('boot/grub/grub.cfg'), [
		{archive => 'archive-1', modification_time => 8},
		{archive => 'archive-2', modification_time => 8},
	]);
	eq_or_diff($db->get_archives_for_path('boot/grub/grub.cfg.sig'), [
		{archive => 'archive-1', modification_time => 9},
		{archive => 'archive-2', modification_time => 9},
	]);
	eq_or_diff($db->get_archives_for_path('boot/test1'), [
		{archive => 'archive-1', modification_time => 4},
		{archive => 'archive-2', modification_time => 7},
	]);
	eq_or_diff($db->get_archives_for_path('boot/test1/f1'), [
		{archive => 'archive-1', modification_time => 3},
		{archive => 'archive-2', modification_time => 3},
	]);
	eq_or_diff($db->get_archives_for_path('boot/test1/f2'), [
		{archive => 'archive-1', modification_time => 4},
		{archive => 'archive-2', modification_time => 5},
	]);
	eq_or_diff($db->get_archives_for_path('boot/test1/f3'), [
		{archive => 'archive-1', modification_time => 3},
		{archive => 'archive-2', modification_time => 3},
	]);
	eq_or_diff($db->get_archives_for_path('boot/test1/f4'), [
		{archive => 'archive-1', modification_time => 2},
		{archive => 'archive-2', modification_time => 2},
	]);
	eq_or_diff($db->get_archives_for_path('boot/test1/f5'), [
		{archive => 'archive-1', modification_time => undef},
		{archive => 'archive-2', modification_time => 7},
	]);
	eq_or_diff($db->get_archives_for_path('boot1'), [
		{archive => 'archive-1', modification_time => 10},
		{archive => 'archive-2', modification_time => 10},
	]);
	eq_or_diff($db->get_archives_for_path('etc'), [
		{archive => 'archive-1', modification_time => 3},
		{archive => 'archive-2', modification_time => 3},
	]);
	eq_or_diff($db->get_archives_for_path('etc/foo'), [
		{archive => 'archive-1', modification_time => 2},
		{archive => 'archive-2', modification_time => 2},
	]);
	eq_or_diff($db->get_archives_for_path('etc/foo/bar'), [
		{archive => 'archive-1', modification_time => 1},
		{archive => 'archive-2', modification_time => 1},
	]);
	eq_or_diff($db->get_archives_for_path('etc/foo/blub'), [
		{archive => 'archive-1', modification_time => 1},
		{archive => 'archive-2', modification_time => undef},
	]);
	eq_or_diff($db->get_archives_for_path('lulz'), [
		{archive => 'archive-1', modification_time => undef},
		{archive => 'archive-2', modification_time => undef},
	], 'test non-existant path');

	# remove first archive
	$app->_handle_removed_archives(['archive-2']);

	# check database contents
	is($db->get_archive_row_count(), 17, 'correct row count (17 paths with timestamps)');
	eq_or_diff($db->get_archives_for_path('.'), [{archive => 'archive-2', modification_time => undef},]);
	eq_or_diff($db->get_archives_for_path('boot'), [{archive => 'archive-2', modification_time => 20},]);
	eq_or_diff($db->get_archives_for_path('boot/foo'), [{archive => 'archive-2', modification_time => 19},]);
	eq_or_diff($db->get_archives_for_path('boot/foo/bar'), [{archive => 'archive-2', modification_time => 19},]);
	eq_or_diff($db->get_archives_for_path('boot/foo/blub'), [{archive => 'archive-2', modification_time => 13},]);
	eq_or_diff($db->get_archives_for_path('boot/grub'), [{archive => 'archive-2', modification_time => 20},]);
	eq_or_diff($db->get_archives_for_path('boot/grub/grub.cfg'), [{archive => 'archive-2', modification_time => 8},]);
	eq_or_diff($db->get_archives_for_path('boot/grub/grub.cfg.sig'), [{archive => 'archive-2', modification_time => 9},]);
	eq_or_diff($db->get_archives_for_path('boot/test1'), [{archive => 'archive-2', modification_time => 7},]);
	eq_or_diff($db->get_archives_for_path('boot/test1/f1'), [{archive => 'archive-2', modification_time => 3},]);
	eq_or_diff($db->get_archives_for_path('boot/test1/f2'), [{archive => 'archive-2', modification_time => 5},]);
	eq_or_diff($db->get_archives_for_path('boot/test1/f3'), [{archive => 'archive-2', modification_time => 3},]);
	eq_or_diff($db->get_archives_for_path('boot/test1/f4'), [{archive => 'archive-2', modification_time => 2},]);
	eq_or_diff($db->get_archives_for_path('boot/test1/f5'), [{archive => 'archive-2', modification_time => 7},]);
	eq_or_diff($db->get_archives_for_path('boot1'), [{archive => 'archive-2', modification_time => 10},]);
	eq_or_diff($db->get_archives_for_path('etc'), [{archive => 'archive-2', modification_time => 3},]);
	eq_or_diff($db->get_archives_for_path('etc/foo'), [{archive => 'archive-2', modification_time => 2},]);
	eq_or_diff($db->get_archives_for_path('etc/foo/bar'), [{archive => 'archive-2', modification_time => 1},]);
	eq_or_diff($db->get_archives_for_path('etc/foo/blub'), [{archive => 'archive-2', modification_time => undef},]);
	eq_or_diff($db->get_archives_for_path('lulz'), [{archive => 'archive-2', modification_time => undef},], 'test non-existant path');

	# run remove again. shouldn't change anything
	$app->_handle_removed_archives(['archive-2']);
	is($db->get_archive_row_count(), 17, 'correct row count (17 paths with timestamps)');
	eq_or_diff($db->get_archives_for_path('.'), [{archive => 'archive-2', modification_time => undef},]);
	eq_or_diff($db->get_archives_for_path('boot'), [{archive => 'archive-2', modification_time => 20},]);
	eq_or_diff($db->get_archives_for_path('boot/foo'), [{archive => 'archive-2', modification_time => 19},]);
	eq_or_diff($db->get_archives_for_path('boot/foo/bar'), [{archive => 'archive-2', modification_time => 19},]);
	eq_or_diff($db->get_archives_for_path('boot/foo/blub'), [{archive => 'archive-2', modification_time => 13},]);
	eq_or_diff($db->get_archives_for_path('boot/grub'), [{archive => 'archive-2', modification_time => 20},]);
	eq_or_diff($db->get_archives_for_path('boot/grub/grub.cfg'), [{archive => 'archive-2', modification_time => 8},]);
	eq_or_diff($db->get_archives_for_path('boot/grub/grub.cfg.sig'), [{archive => 'archive-2', modification_time => 9},]);
	eq_or_diff($db->get_archives_for_path('boot/test1'), [{archive => 'archive-2', modification_time => 7},]);
	eq_or_diff($db->get_archives_for_path('boot/test1/f1'), [{archive => 'archive-2', modification_time => 3},]);
	eq_or_diff($db->get_archives_for_path('boot/test1/f2'), [{archive => 'archive-2', modification_time => 5},]);
	eq_or_diff($db->get_archives_for_path('boot/test1/f3'), [{archive => 'archive-2', modification_time => 3},]);
	eq_or_diff($db->get_archives_for_path('boot/test1/f4'), [{archive => 'archive-2', modification_time => 2},]);
	eq_or_diff($db->get_archives_for_path('boot/test1/f5'), [{archive => 'archive-2', modification_time => 7},]);
	eq_or_diff($db->get_archives_for_path('boot1'), [{archive => 'archive-2', modification_time => 10},]);
	eq_or_diff($db->get_archives_for_path('etc'), [{archive => 'archive-2', modification_time => 3},]);
	eq_or_diff($db->get_archives_for_path('etc/foo'), [{archive => 'archive-2', modification_time => 2},]);
	eq_or_diff($db->get_archives_for_path('etc/foo/bar'), [{archive => 'archive-2', modification_time => 1},]);
	eq_or_diff($db->get_archives_for_path('etc/foo/blub'), [{archive => 'archive-2', modification_time => undef},]);
	eq_or_diff($db->get_archives_for_path('lulz'), [{archive => 'archive-2', modification_time => undef},], 'test non-existant path');

	# remove all archives from db (no overlap between archives in db and "current" archives)
	$app->_handle_removed_archives(['archive-3']);

	# check database contents
	is($db->get_archive_row_count(), 0, 'db should be empty');
	eq_or_diff($db->get_archives_for_path('.'), []);
	eq_or_diff($db->get_archives_for_path('boot'), []);
	eq_or_diff($db->get_archives_for_path('boot/foo'), []);
	eq_or_diff($db->get_archives_for_path('boot/foo/bar'), []);
	eq_or_diff($db->get_archives_for_path('boot/foo/blub'), []);
	eq_or_diff($db->get_archives_for_path('boot/grub'), []);
	eq_or_diff($db->get_archives_for_path('boot/grub/grub.cfg'), []);
	eq_or_diff($db->get_archives_for_path('boot/grub/grub.cfg.sig'), []);
	eq_or_diff($db->get_archives_for_path('boot/test1'), []);
	eq_or_diff($db->get_archives_for_path('boot/test1/f1'), []);
	eq_or_diff($db->get_archives_for_path('boot/test1/f2'), []);
	eq_or_diff($db->get_archives_for_path('boot/test1/f3'), []);
	eq_or_diff($db->get_archives_for_path('boot/test1/f4'), []);
	eq_or_diff($db->get_archives_for_path('boot/test1/f5'), []);
	eq_or_diff($db->get_archives_for_path('boot1'), []);
	eq_or_diff($db->get_archives_for_path('etc'), []);
	eq_or_diff($db->get_archives_for_path('etc/foo'), []);
	eq_or_diff($db->get_archives_for_path('etc/foo/bar'), []);
	eq_or_diff($db->get_archives_for_path('etc/foo/blub'), []);
	eq_or_diff($db->get_archives_for_path('lulz'), []);
}


done_testing;