]> git.proxmox.com Git - pve-manager.git/blob - test/vzdump_new_retention_test.pl
test: add tests for retention parameters for vzdump's new()
[pve-manager.git] / test / vzdump_new_retention_test.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use lib '..';
7
8 use Test::More;
9 use Test::MockModule;
10
11 use PVE::VZDump;
12
13 my $vzdump_config;
14 my $storage_config;
15
16 sub prepare_storage_config {
17 my ($param) = @_;
18
19 $storage_config = "dir: local\n";
20 $storage_config .= "\tcontent backup\n";
21 $storage_config .= "\tpath /var/lib/vz\n";
22
23 foreach my $key (keys %{$param}) {
24 my $value = $param->{$key};
25 $storage_config .= "\t${key} ${value}\n";
26 }
27 }
28
29 sub prepare_vzdump_config {
30 my ($param) = @_;
31
32 $vzdump_config = "";
33 foreach my $key (keys %{$param}) {
34 my $value = $param->{$key};
35 $vzdump_config .= "${key}: ${value}\n";
36 }
37 }
38
39 my $pve_vzdump_module = Test::MockModule->new('PVE::VZDump');
40 $pve_vzdump_module->mock(
41 mkpath => sub {
42 return;
43 },
44 check_bin => sub {
45 return;
46 },
47 );
48
49 my $pve_storage_module = Test::MockModule->new('PVE::Storage');
50 $pve_storage_module->mock(
51 activate_storage => sub {
52 return;
53 },
54 );
55
56 my $pve_cluster_module = Test::MockModule->new('PVE::Cluster');
57 $pve_cluster_module->mock(
58 get_config => sub {
59 my ($filename) = @_;
60
61 die "unexpected filename '$filename'\n" if $filename ne 'storage.cfg';
62 return $storage_config;
63 },
64 );
65
66 my $pve_tools_module = Test::MockModule->new('PVE::Tools');
67 $pve_tools_module->mock(
68 file_get_contents => sub {
69 my ($filename) = @_;
70
71 die "unexpected filename '$filename'\n" if $filename ne '/etc/vzdump.conf';
72 return $vzdump_config;
73 },
74 );
75
76 my @tested_options = qw(prune-backups remove);
77
78 # each test consists of the following:
79 # name - unique name for the test
80 # cli_param - CLI parameters to be passed to new(); vmid and storage are hardcoded
81 # storage_param - parameters for the mocked storage configuration
82 # vzdump_param - parameters for the mocked /etc/vzdump.conf
83 # expected - expected options
84 my @tests = (
85 {
86 description => 'no params',
87 expected => {
88 'prune-backups' => {
89 'keep-last' => 1,
90 },
91 remove => 1,
92 },
93 },
94 # TODO make parse error critical?
95 {
96 description => 'maxfiles vzdump 1',
97 vzdump_param => {
98 maxfiles => 0,
99 },
100 expected => {
101 'prune-backups' => {
102 'keep-last' => 1,
103 },
104 remove => 1,
105 },
106 },
107 {
108 description => 'maxfiles vzdump 2',
109 vzdump_param => {
110 maxfiles => 7,
111 },
112 expected => {
113 'prune-backups' => {
114 'keep-last' => 7,
115 },
116 remove => 1,
117 },
118 },
119 {
120 description => 'maxfiles storage 1',
121 storage_param => {
122 maxfiles => 0,
123 },
124 expected => {
125 'prune-backups' => {
126 'keep-all' => 1,
127 },
128 remove => 0,
129 },
130 },
131 {
132 description => 'maxfiles storage 2',
133 storage_param => {
134 maxfiles => 7,
135 },
136 expected => {
137 'prune-backups' => {
138 'keep-last' => 7,
139 },
140 remove => 1,
141 },
142 },
143 {
144 description => 'maxfiles CLI 1',
145 cli_param => {
146 maxfiles => 0,
147 },
148 expected => {
149 'prune-backups' => {
150 'keep-all' => 1,
151 },
152 remove => 0,
153 },
154 },
155 {
156 description => 'maxfiles CLI 2',
157 cli_param => {
158 maxfiles => 7,
159 },
160 expected => {
161 'prune-backups' => {
162 'keep-last' => 7,
163 },
164 remove => 1,
165 },
166 },
167 {
168 description => 'prune-backups storage 1',
169 storage_param => {
170 'prune-backups' => 'keep-last=1,keep-hourly=2,keep-daily=3,' .
171 'keep-weekly=4,keep-monthly=5,keep-yearly=6',
172 },
173 expected => {
174 'prune-backups' => {
175 'keep-last' => 1,
176 'keep-hourly' => 2,
177 'keep-daily' => 3,
178 'keep-weekly' => 4,
179 'keep-monthly' => 5,
180 'keep-yearly' => 6,
181 },
182 remove => 1,
183 },
184 },
185 {
186 description => 'prune-backups storage 2',
187 storage_param => {
188 'prune-backups' => 'keep-last=0,keep-hourly=0,keep-daily=0,' .
189 'keep-weekly=0,keep-monthly=0,keep-yearly=0',
190 },
191 expected => {
192 'prune-backups' => {
193 'keep-all' => 1,
194 },
195 remove => 0,
196 },
197 },
198 {
199 description => 'prune-backups storage 3',
200 storage_param => {
201 'prune-backups' => 'keep-hourly=0,keep-monthly=0,keep-yearly=0',
202 },
203 expected => {
204 'prune-backups' => {
205 'keep-all' => 1,
206 },
207 remove => 0,
208 },
209 },
210 {
211 description => 'both storage 1',
212 storage_param => {
213 'prune-backups' => 'keep-hourly=1,keep-monthly=2,keep-yearly=3',
214 maxfiles => 0,
215 },
216 expected => {
217 'prune-backups' => {
218 'keep-hourly' => 1,
219 'keep-monthly' => 2,
220 'keep-yearly' => 3,
221 },
222 remove => 1,
223 },
224 },
225 {
226 description => 'prune-backups CLI 1',
227 cli_param => {
228 'prune-backups' => 'keep-last=1,keep-hourly=2,keep-daily=3,' .
229 'keep-weekly=4,keep-monthly=5,keep-yearly=6',
230 },
231 expected => {
232 'prune-backups' => {
233 'keep-last' => 1,
234 'keep-hourly' => 2,
235 'keep-daily' => 3,
236 'keep-weekly' => 4,
237 'keep-monthly' => 5,
238 'keep-yearly' => 6,
239 },
240 remove => 1,
241 },
242 },
243 {
244 description => 'prune-backups CLI 2',
245 cli_param => {
246 'prune-backups' => 'keep-last=0,keep-hourly=0,keep-daily=0,' .
247 'keep-weekly=0,keep-monthly=0,keep-yearly=0',
248 },
249 expected => {
250 'prune-backups' => {
251 'keep-all' => 1,
252 },
253 remove => 0,
254 },
255 },
256 {
257 description => 'prune-backups CLI 3',
258 cli_param => {
259 'prune-backups' => 'foo=bar',
260 },
261 expected => "format error\n" .
262 "foo: property is not defined in schema and the schema does not allow additional properties\n",
263 },
264 {
265 description => 'both CLI 1',
266 cli_param => {
267 'prune-backups' => 'keep-hourly=1,keep-monthly=2,keep-yearly=3',
268 maxfiles => 4,
269 },
270 expected => "400 Parameter verification failed.\n" .
271 "prune-backups: option conflicts with option 'maxfiles'\n",
272 },
273 {
274 description => 'mixed 1',
275 vzdump_param => {
276 maxfiles => 7,
277 },
278 storage_param => {
279 'prune-backups' => 'keep-hourly=24',
280 },
281 expected => {
282 'prune-backups' => {
283 'keep-hourly' => 24,
284 },
285 remove => 1,
286 },
287 },
288 # TODO make parse error critical?
289 {
290 description => 'mixed 2',
291 vzdump_param => {
292 maxfiles => 7,
293 },
294 storage_param => {
295 'prune-backups' => 'keephourly=24',
296 },
297 expected => {
298 'prune-backups' => {
299 'keep-last' => 7,
300 },
301 remove => 1,
302 },
303 },
304 {
305 description => 'mixed 3',
306 vzdump_param => {
307 maxfiles => 7,
308 },
309 cli_param => {
310 'prune-backups' => 'keep-all=1',
311 },
312 expected => {
313 'prune-backups' => {
314 'keep-all' => 1,
315 },
316 remove => 0,
317 },
318 },
319 {
320 description => 'mixed 4',
321 vzdump_param => {
322 maxfiles => 7,
323 },
324 storage_param => {
325 'prune-backups' => 'keep-all=0,keep-last=10',
326 },
327 cli_param => {
328 'prune-backups' => 'keep-all=1',
329 },
330 expected => {
331 'prune-backups' => {
332 'keep-all' => 1,
333 },
334 remove => 0,
335 },
336 },
337 {
338 description => 'mixed 5',
339 vzdump_param => {
340 maxfiles => 7,
341 },
342 storage_param => {
343 'prune-backups' => 'keep-all=0,keep-last=10',
344 },
345 expected => {
346 'prune-backups' => {
347 'keep-last' => 10,
348 },
349 remove => 1,
350 },
351 },
352 {
353 description => 'mixed 6',
354 storage_param => {
355 'prune-backups' => 'keep-last=10',
356 },
357 cli_param => {
358 'prune-backups' => 'keep-all=1',
359 },
360 expected => {
361 'prune-backups' => {
362 'keep-all' => 1,
363 },
364 remove => 0,
365 },
366 },
367 {
368 description => 'mixed 7',
369 storage_param => {
370 'prune-backups' => 'keep-all=1',
371 },
372 cli_param => {
373 'prune-backups' => 'keep-last=10',
374 },
375 expected => {
376 'prune-backups' => {
377 'keep-last' => 10,
378 },
379 remove => 1,
380 },
381 },
382 );
383
384 plan tests => scalar @tests;
385
386 foreach my $test (@tests) {
387 prepare_storage_config($test->{storage_param});
388 prepare_vzdump_config($test->{vzdump_param});
389
390 $test->{cli_param}->{vmid} = 100;
391 $test->{cli_param}->{storage} = 'local';
392
393 my $got = eval {
394 PVE::VZDump::verify_vzdump_parameters($test->{cli_param}, 1);
395
396 my $vzdump = PVE::VZDump->new('fake cmdline', $test->{cli_param}, undef);
397
398 my $opts = $vzdump->{opts} or die "did not get options\n";
399 die "maxfiles is defined" if defined($opts->{maxfiles});
400
401 my $res = {};
402 foreach my $opt (@tested_options) {
403 next if !defined($opts->{$opt});
404 $res->{$opt} = $opts->{$opt};
405 }
406 return $res;
407 };
408 $got = $@ if $@;
409
410 is_deeply($got, $test->{expected}, $test->{description}) || diag(explain($got));
411 }
412
413 done_testing();