Imported Upstream version 3.2.0
[debian/amanda] / installcheck / Amanda_DB_Catalog.pl
1 # Copyright (c) 2008, 2010 Zmanda, Inc.  All Rights Reserved.
2 #
3 # This program is free software; you can redistribute it and/or modify it
4 # under the terms of the GNU General Public License version 2 as published
5 # by the Free Software Foundation.
6 #
7 # This program is distributed in the hope that it will be useful, but
8 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
9 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
10 # for more details.
11 #
12 # You should have received a copy of the GNU General Public License along
13 # with this program; if not, write to the Free Software Foundation, Inc.,
14 # 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 #
16 # Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
17 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
18
19 use Test::More tests => 85;
20 use File::Path;
21 use Data::Dumper;
22 use strict;
23 use warnings;
24
25 use lib "@amperldir@";
26 use Installcheck;
27 use Installcheck::Run;
28 use Installcheck::Catalogs;
29 use Amanda::Config qw( :init :getconf config_dir_relative );
30 use Amanda::DB::Catalog;
31 use Amanda::Cmdline;
32 use Amanda::Xfer qw( :constants );
33
34 # send xfer logging somewhere
35 Amanda::Debug::dbopen("installcheck");
36 Installcheck::log_test_output();
37
38 # set up and load a simple config
39 my $testconf = Installcheck::Run->setup();
40 $testconf->write();
41 config_init($CONFIG_INIT_EXPLICIT_NAME, 'TESTCONF') == $CFGERR_OK
42     or die("Could not load test config");
43
44 # test functions against an empty set of logfiles
45
46 is_deeply([ Amanda::DB::Catalog::get_write_timestamps() ], [],
47     "No write_timestamps in an empty catalog");
48
49 is_deeply(Amanda::DB::Catalog::get_latest_write_timestamp(), undef,
50     "No latest write_timestamp in an empty catalog");
51
52 is_deeply(Amanda::DB::Catalog::get_latest_write_timestamp(type => 'amvault'), undef,
53     "No latest write_timestamp in an empty catalog, even of a specific type");
54
55 is_deeply([ Amanda::DB::Catalog::get_parts() ], [],
56     "No parts in an empty catalog");
57
58 # and add some logfiles to query, and a corresponding tapelist, while also gathering
59 # a list of parts and dumps for comparison with the results from Amanda::DB::Catalog.
60 # also add some files to holding disk
61 my @dumpspecs;
62
63 # install the bigdb catalog
64 my $cat = Installcheck::Catalogs::load("bigdb");
65 $cat->install();
66 my %dumps = $cat->get_dumps();
67 my %parts = $cat->get_parts();
68
69 Amanda::DB::Catalog::_clear_cache();
70
71 sub partstr {
72     my ($part) = @_;
73     if (exists $part->{'holding_file'}) {
74         return "$part->{holding_file}: " .
75                "$part->{dump}->{hostname} $part->{dump}->{diskname} " .
76                "w$part->{dump}->{write_timestamp} d$part->{dump}->{dump_timestamp}";
77    } else {
78         return "$part->{label}:$part->{filenum}: " .
79                "$part->{dump}->{hostname} $part->{dump}->{diskname} $part->{dump}->{orig_kb} " .
80                "w$part->{dump}->{write_timestamp} d$part->{dump}->{dump_timestamp}";
81    }
82 }
83
84 # filter out recursive links from part->dump->parts, without changing
85 # the original objects.  Note that this removes the object-identity of
86 # dumps.
87 sub filter_parts {
88     my ($parts) = @_;
89
90     my @rv;
91     for my $p (@$parts) {
92         $p = do { my %t = %$p; \%t; }; # copy hash
93         $p->{'dump'} = do { my %t = %{$p->{'dump'}}; \%t; };
94         $p->{'dump'}->{'parts'} = undef;
95         push @rv, $p;
96     }
97
98     return \@rv;
99 }
100
101 sub got_parts {
102     my ($got, $exp, $msg, %params) = @_;
103
104     # give a warning if we accidentally select zero parts
105     if (@$exp == 0) {
106         diag("warning: zero parts expected: $msg")
107             unless $params{'zero_parts_expected'};
108     }
109
110     # filter recursive references to avoid confusing old is_deeply instances
111     if (!is_deeply(filter_parts($got), filter_parts($exp), $msg)) {
112         diag("got parts:");
113         my $i = 0;
114         for (@$got) {
115             diag(" [$i]  " . partstr($_));
116             $i++;
117         }
118
119         diag("expected parts:");
120         $i = 0;
121         for (@$exp) {
122             diag(" [$i]  " . partstr($_));
123             $i++;
124         }
125         return '';
126     }
127     return 1;
128 }
129
130 # get parts filtered by a regexp on the key
131 sub parts_named($) {
132     my ($expr) = @_; 
133     my @selected_keys = grep { $_ =~ $expr } keys %parts;
134     return map { $parts{$_} } @selected_keys;
135 }
136
137 # get parts filtered by an expression on the dumpfile itself
138 sub parts_matching(&) {
139     my ($block) = @_; 
140     return grep { &$block } values %parts;
141 }
142
143 # put @_ in a canonical order
144 sub sortparts {
145     map {
146         # convert bigints to strings and on to integers so is_deeply doesn't get confused
147         $_->{'dump'}->{'level'} = "$_->{dump}->{level}" + 0;
148         $_->{'dump'}->{'kb'} = "$_->{dump}->{kb}" + 0;
149         $_->{'dump'}->{'orig_kb'} = "$_->{dump}->{orig_kb}" + 0;
150         if (!defined $_->{filenum}) {
151             $_->{'filenum'} = 0;
152         } else {
153             $_->{'filenum'} = "$_->{filenum}" + 0;
154         }
155         $_->{'kb'} = "$_->{kb}" + 0;
156         $_->{'orig_kb'} = "$_->{orig_kb}" + 0;
157         $_->{'partnum'} = "$_->{partnum}" + 0;
158         $_;
159     } sort {
160         if (exists $a->{'holding_file'} and exists $b->{'holding_file'}) {
161             return $a->{'holding_file'} cmp $b->{'holding_file'};
162         } elsif (not exists $a->{'holding_file'} and not exists $b->{'holding_file'}) {
163             return ($a->{'label'} cmp $b->{'label'})
164                 || ($a->{'filenum'} <=> $b->{'filenum'});
165         } else {
166             return (exists $a->{'holding_file'})? 1 : -1;
167         }
168     }
169     @_;
170 }
171
172 sub dumpstr {
173     my ($dump) = @_;
174     return "$dump->{hostname} $dump->{diskname} " .
175            "$dump->{dump_timestamp} $dump->{level}";
176 }
177
178 # filter out recursive links from dump->parts->dump, without changing
179 # the original objects.
180 sub filter_dumps {
181     my ($dumps) = @_;
182
183     my @rv;
184     for my $d (@$dumps) {
185         $d = do { my %t = %$d; \%t; }; # copy hash
186         my @dparts = map {
187                 return undef unless defined $_;
188                 my $p = do { my %t = %$_; \%t }; # copy part
189                 $p->{'dump'} = undef;
190                 $p;
191             } @{$d->{'parts'}};
192         $d->{'parts'} = [ @dparts ];
193         push @rv, $d;
194     }
195
196     return \@rv;
197 }
198
199 sub got_dumps {
200     my ($got, $exp, $msg, %params) = @_;
201
202     # give a warning if we accidentally select zero dumps
203     if (@$exp == 0) {
204         diag("warning: zero dumps expected: $msg")
205             unless $params{'zero_dumps_expected'};
206     }
207
208     # filter recursive references to avoid confusing old is_deeply instances
209     if (!is_deeply(filter_dumps($got), filter_dumps($exp), $msg)) {
210         diag("got dumps:");
211         for (@$got) {
212             diag("  " . dumpstr($_));
213         }
214         diag("expected dumps:");
215         for (@$exp) {
216             diag("  " . dumpstr($_));
217         }
218         return '';
219     }
220     return 1;
221 }
222
223 # get dumps filtered by a regexp on the key
224 sub dumps_named($) {
225     my ($expr) = @_; 
226     my @selected_keys = grep { $_ =~ $expr } keys %dumps;
227     return map { $dumps{$_} } @selected_keys;
228 }
229
230 # get dumps filtered by an expression on the dumpfile itself
231 sub dumps_matching(&) {
232     my ($block) = @_; 
233     return grep { &$block } values %dumps;
234 }
235
236 # put @_ in a canonical order
237 sub sortdumps {
238     map {
239         # convert bigints to strings and on to integers so is_deeply doesn't get confused
240         $_->{'level'} = "$_->{level}" + 0;
241         $_->{'kb'} = "$_->{kb}" + 0;
242         $_->{'orig_kb'} = "$_->{orig_kb}" + 0;
243         $_->{'nparts'} = "$_->{nparts}" + 0;
244         $_;
245     } sort {
246         $a->{'write_timestamp'} cmp $b->{'write_timestamp'}
247             or $a->{'hostname'} cmp $b->{'hostname'}
248             or $a->{'diskname'} cmp $b->{'diskname'}
249             or $a->{'level'} <=> $b->{'level'}
250     }
251     @_;
252 }
253
254 ##
255 # Test the timestamps
256
257 is_deeply([ Amanda::DB::Catalog::get_write_timestamps(), ],
258     [ '20080111000000', '20080222222222', '20080313133333',
259       '20080414144444', '20080515155555', '20080616166666',
260       '20100722000000' ],
261     "get_write_timestamps returns all logfile datestamps in proper order, with zero-padding");
262
263 is(Amanda::DB::Catalog::get_latest_write_timestamp(), '20100722000000',
264     "get_latest_write_timestamp correctly returns the latest write timestamp");
265
266 is(Amanda::DB::Catalog::get_latest_write_timestamp(type => 'amdump'), '20100722000000',
267     "get_latest_write_timestamp correctly returns the latest write timestamp of type amdump");
268
269 is(Amanda::DB::Catalog::get_latest_write_timestamp(type => 'amflush'), '20080111000000',
270     "get_latest_write_timestamp correctly returns the latest write timestamp of type amflush");
271
272 is(Amanda::DB::Catalog::get_latest_write_timestamp(types => [qw(amvault amflush)]),
273     '20080222222222',
274     "get_latest_write_timestamp correctly returns the latest write timestamp of a set of ts's");
275
276 is(Amanda::DB::Catalog::get_run_type('20080222222222'), "amvault",
277     "get_run_type detects amvault");
278
279 is(Amanda::DB::Catalog::get_run_type('20080111'), "amflush",
280     "get_run_type detects amflush (short ts)");
281
282 is(Amanda::DB::Catalog::get_run_type('20080111000000'), "amflush",
283     "get_run_type detects amflush (long ts)");
284
285 ##
286 # test get_parts and sort_parts
287
288 got_parts([ sortparts Amanda::DB::Catalog::get_parts() ],
289     [ sortparts parts_named qr/.*/ ],
290     "get_parts returns all parts when given no parameters");
291 got_parts([ sortparts Amanda::DB::Catalog::get_parts(write_timestamp => '20080111000000') ],
292     [ sortparts parts_named qr/somebox_lib_20080111/ ],
293     "get_parts parameter write_timestamp");
294 got_parts([ sortparts Amanda::DB::Catalog::get_parts(write_timestamp => '20080111') ],
295     [ sortparts parts_named qr/somebox_lib_20080111/ ],
296     "get_parts accepts a short write_timestamp and zero-pads it");
297 got_parts([ sortparts Amanda::DB::Catalog::get_parts(write_timestamps => ['20080111000000','20080222222222']) ],
298     [ sortparts parts_named qr/(20080111|20080222222222_p\d*)$/ ],
299     "get_parts parameter write_timestamps");
300 got_parts([ sortparts Amanda::DB::Catalog::get_parts(write_timestamp => '20080111', holding => 1) ],
301     [ ],
302     "get_parts parameter write_timestamp + holding => 1 returns nothing",
303     zero_parts_expected => 1);
304
305 got_parts([ sortparts Amanda::DB::Catalog::get_parts(dump_timestamp => '20080111000000') ],
306     [ sortparts parts_named qr/somebox_lib_20080111/ ],
307     "get_parts parameter dump_timestamp");
308 got_parts([ sortparts Amanda::DB::Catalog::get_parts(dump_timestamp => '20080111') ],
309     [ sortparts parts_named qr/somebox_lib_20080111/ ],
310     "get_parts accepts a short dump_timestamp and zero-pads it");
311 got_parts([ sortparts Amanda::DB::Catalog::get_parts(dump_timestamps => ['20080111000000','20080222222222']) ],
312     [ sortparts parts_named qr/(20080111|20080222222222_p\d*)$/ ],
313     "get_parts parameter dump_timestamps");
314 got_parts([ sortparts Amanda::DB::Catalog::get_parts(dump_timestamp_match => '200801-2') ],
315     [ sortparts parts_named qr/20080[12]/ ],
316     "get_parts parameter dump_timestamp_match");
317
318 got_parts([ sortparts Amanda::DB::Catalog::get_parts(hostname => 'otherbox') ],
319     [ sortparts parts_named qr/^otherbox_/ ],
320     "get_parts parameter hostname");
321 got_parts([ sortparts Amanda::DB::Catalog::get_parts(hostnames => ['otherbox','somebox']) ],
322     [ sortparts parts_named qr/^(otherbox_|somebox_)/ ],
323     "get_parts parameter hostnames");
324 got_parts([ sortparts Amanda::DB::Catalog::get_parts(hostname_match => '*box') ],
325     [ sortparts parts_named qr/box/ ],
326     "get_parts parameter hostname_match");
327
328 got_parts([ sortparts Amanda::DB::Catalog::get_parts(diskname => '/lib') ],
329     [ sortparts parts_named qr/_lib_/ ],
330     "get_parts parameter diskname");
331 got_parts([ sortparts Amanda::DB::Catalog::get_parts(disknames => ['/lib','/usr/bin']) ],
332     [ sortparts parts_named qr/(_lib_|_usr_bin_)/ ],
333     "get_parts parameter disknames");
334 got_parts([ sortparts Amanda::DB::Catalog::get_parts(diskname_match => '/usr') ],
335     [ sortparts parts_named qr/_usr_/ ],
336     "get_parts parameter diskname_match");
337
338 got_parts([ sortparts Amanda::DB::Catalog::get_parts(label => 'Conf-001') ],
339     [ sortparts parts_matching { defined $_->{'label'} and $_->{'label'} eq 'Conf-001' } ],
340     "get_parts parameter label");
341 got_parts([ sortparts Amanda::DB::Catalog::get_parts(labels => ['Conf-002','Conf-003']) ],
342     [ sortparts parts_matching { defined $_->{'label'} and ($_->{'label'} eq 'Conf-002' or $_->{'label'} eq 'Conf-003') } ],
343     "get_parts parameter labels");
344
345 got_parts([ sortparts Amanda::DB::Catalog::get_parts(level => 0) ],
346     [ sortparts parts_matching { $_->{'dump'}->{'level'} == 0 } ],
347     "get_parts parameter level");
348 got_parts([ sortparts Amanda::DB::Catalog::get_parts(levels => [ 1 ]) ],
349     [ sortparts parts_matching { $_->{'dump'}->{'level'} == 1 } ],
350     "get_parts parameter levels");
351
352 got_parts([ sortparts Amanda::DB::Catalog::get_parts(status => "OK") ],
353     [ sortparts parts_matching { $_->{'status'} eq "OK" } ],
354     "get_parts parameter status = OK");
355
356 got_parts([ sortparts Amanda::DB::Catalog::get_parts(status => "PARTIAL") ],
357     [ sortparts parts_matching { $_->{'status'} eq "PARTIAL" } ],
358     "get_parts parameter status = PARTIAL");
359
360 got_parts([ sortparts Amanda::DB::Catalog::get_parts(hostname => "oldbox") ],
361     [ sortparts parts_named qr/^oldbox_/ ],
362     "get_parts finds a holding-disk dump");
363
364 got_parts([ sortparts Amanda::DB::Catalog::get_parts(hostname => "oldbox", holding => 0) ],
365     [ ],
366     "get_parts ignores a holding-disk dump if holding is false",
367     zero_parts_expected => 1);
368 got_parts([ sortparts Amanda::DB::Catalog::get_parts(hostname => "oldbox", holding => 1) ],
369     [ sortparts parts_named qr/^oldbox_/ ],
370     "get_parts supplies a holding-disk dump if holding is true");
371 got_parts([ sortparts Amanda::DB::Catalog::get_parts(hostnames => [ "oldbox", "somebox" ]) ],
372     [ sortparts (parts_named qr/^oldbox_.*_holding/, parts_named qr/^somebox_/) ],
373     "get_parts returns both holding and on-media dumps");
374 got_parts([ sortparts Amanda::DB::Catalog::get_parts(hostnames => [ "oldbox", "somebox" ],
375                                                      holding => 1) ],
376     [ sortparts parts_named qr/^oldbox_.*_holding/ ],
377     "get_parts ignores an on-media dump if holding is true");
378
379 got_parts([ sortparts Amanda::DB::Catalog::get_parts(hostnames => [ "oldbox", "somebox" ],
380                                                      holding => 0) ],
381     [ sortparts parts_named qr/^somebox_/ ],
382     "get_parts ignores an holding dump if holding is false");
383
384 @dumpspecs = Amanda::Cmdline::parse_dumpspecs([".*", "/lib"], 0);
385 got_parts([ sortparts Amanda::DB::Catalog::get_parts(dumpspecs => [ @dumpspecs ]) ],
386     [ sortparts parts_named qr/_lib_/ ],
387     "get_parts parameter dumpspecs with one dumpspec");
388
389 @dumpspecs = Amanda::Cmdline::parse_dumpspecs([".*", "/lib", "somebox"], 0);
390 got_parts([ sortparts Amanda::DB::Catalog::get_parts(dumpspecs => [ @dumpspecs ]) ],
391     [ sortparts parts_matching { $_->{'dump'}->{'diskname'} eq '/lib'
392                               or $_->{'dump'}->{'hostname'} eq 'somebox' } ],
393     "get_parts parameter dumpspecs with two dumpspecs");
394
395 @dumpspecs = Amanda::Cmdline::parse_dumpspecs(["otherbox", "*", "somebox"], 0);
396 got_parts([ sortparts Amanda::DB::Catalog::get_parts(dumpspecs => [ @dumpspecs ]) ],
397     [ sortparts parts_matching { $_->{'dump'}->{'hostname'} eq 'otherbox'
398                               or $_->{'dump'}->{'hostname'} eq 'somebox' } ],
399     "get_parts parameter dumpspecs with two non-overlapping dumpspecs");
400
401 @dumpspecs = Amanda::Cmdline::parse_dumpspecs(["otherbox", "*", "somebox"], 0);
402 got_parts([ sortparts Amanda::DB::Catalog::get_parts(dumpspecs => [ @dumpspecs ], holding => 1), ],
403     [ sortparts parts_matching { $_->{'dump'}->{'hostname'} eq 'otherbox'
404                              and exists $_->{'holding_file'} } ],
405     "get_parts parameter dumpspecs with two non-overlapping dumpspecs, but holding files only");
406
407 ## more complex, multi-parameter queries
408
409 got_parts([ sortparts Amanda::DB::Catalog::get_parts(hostname => 'somebox',
410                                                      diskname_match => '/lib') ],
411     [ sortparts parts_named qr/^somebox_lib_/ ],
412     "get_parts parameters hostname and diskname_match");
413
414 got_parts([ sortparts Amanda::DB::Catalog::get_parts(write_timestamp => '20080313133333',
415                                                      dump_timestamp => '20080311131133') ],
416     [ sortparts parts_matching { $_->{'dump'}->{'dump_timestamp'} eq '20080311131133' 
417                     and $_->{'dump'}->{'write_timestamp'} eq '20080313133333' } ],
418     "get_parts parameters write_timestamp and dump_timestamp");
419
420 got_parts([ sortparts Amanda::DB::Catalog::get_parts(write_timestamp => '20080414144444',
421                                                      status => 'OK') ],
422     [ ], # there were no OK dumps on that date
423     "get_parts parameters write_timestamp status",
424     zero_parts_expected => 1);
425
426 ## test part sorting
427
428 got_parts([ Amanda::DB::Catalog::sort_parts(['write_timestamp'],
429                 @parts{'somebox_lib_20080222222222_p1','somebox_lib_20080111'}) ],
430               [ @parts{'somebox_lib_20080111','somebox_lib_20080222222222_p1'} ],
431     "sort by write_timestamps");
432 got_parts([ Amanda::DB::Catalog::sort_parts(['-write_timestamp'],
433                 @parts{'somebox_lib_20080111','somebox_lib_20080222222222_p1'}) ],
434               [ @parts{'somebox_lib_20080222222222_p1','somebox_lib_20080111'} ],
435     "sort by write_timestamps, reverse");
436
437 got_parts([ Amanda::DB::Catalog::sort_parts(['hostname', '-diskname', 'write_timestamp'],
438                 @parts{
439                     'somebox_lib_20080222222222_p1',
440                     'somebox_usr_bin_20080313133333',
441                     'somebox_lib_20080313133333_p4',
442                     'otherbox_lib_20080313133333',
443                     'somebox_lib_20080111',
444                     }) ],
445               [ @parts{
446                     'otherbox_lib_20080313133333',
447                     'somebox_usr_bin_20080313133333',
448                     'somebox_lib_20080111',
449                     'somebox_lib_20080222222222_p1',
450                     'somebox_lib_20080313133333_p4',
451                     } ],
452     "multi-key sort");
453
454 got_parts([ Amanda::DB::Catalog::sort_parts(['filenum'],
455                 @parts{
456                     'somebox_lib_20080313133333_p9',
457                     'somebox_lib_20080313133333_p10',
458                     'somebox_lib_20080313133333_p1',
459                     }) ],
460               [ @parts{
461                     'somebox_lib_20080313133333_p1',
462                     'somebox_lib_20080313133333_p9',
463                     'somebox_lib_20080313133333_p10',
464                     } ],
465                 "filenum is sorted numerically, not lexically");
466
467 got_parts([ Amanda::DB::Catalog::sort_parts(['-partnum'],
468                 @parts{
469                     'somebox_lib_20080313133333_p9',
470                     'somebox_lib_20080313133333_p10',
471                     'somebox_lib_20080313133333_p1',
472                     }) ],
473               [ @parts{
474                     'somebox_lib_20080313133333_p10',
475                     'somebox_lib_20080313133333_p9',
476                     'somebox_lib_20080313133333_p1',
477                     } ],
478                 "partnum is sorted numerically (and in reverse!), not lexically");
479
480 got_parts([ Amanda::DB::Catalog::sort_parts(['nparts'],
481                 @parts{
482                     'somebox_lib_20080313133333_p9', # nparts=10
483                     'somebox_lib_20080222222222_p2', # nparts=2
484                     }) ],
485               [ @parts{
486                     'somebox_lib_20080222222222_p2', # nparts=2
487                     'somebox_lib_20080313133333_p9', # nparts=10
488                     } ],
489                 "nparts is sorted numerically, not lexically");
490
491 got_parts([ Amanda::DB::Catalog::sort_parts(['label'],
492                 @parts{
493                     'somebox_lib_20080313133333_p9', # Conf-003
494                     'somebox_lib_20080222222222_p2', # Conf-002
495                     }) ],
496               [ @parts{
497                     'somebox_lib_20080222222222_p2', # Conf-002
498                     'somebox_lib_20080313133333_p9', # Conf-003
499                     } ],
500                 "labels sort correctly");
501
502 ### test dump selecting
503
504 got_dumps([ sortdumps Amanda::DB::Catalog::get_dumps() ],
505     [ sortdumps dumps_named qr/.*/ ],
506     "get_dumps returns all dumps when given no parameters");
507
508 got_dumps([ sortdumps Amanda::DB::Catalog::get_dumps(write_timestamp => '20080111000000') ],
509     [ sortdumps dumps_named qr/somebox_lib_20080111/ ],
510     "get_dumps parameter write_timestamp");
511
512 got_dumps([ sortdumps Amanda::DB::Catalog::get_dumps(write_timestamp => '20080111') ],
513     [ sortdumps dumps_named qr/somebox_lib_20080111/ ],
514     "get_dumps accepts a short write_timestamp and zero-pads it");
515
516 got_dumps([ sortdumps Amanda::DB::Catalog::get_dumps(write_timestamps => ['20080111000000','20080222222222']) ],
517     [ sortdumps dumps_named qr/(20080111|20080222222222)$/ ],
518     "get_dumps parameter write_timestamps");
519
520 got_dumps([ sortdumps Amanda::DB::Catalog::get_dumps(hostname => 'otherbox') ],
521     [ sortdumps dumps_named qr/^otherbox/ ],
522     "get_dumps parameter hostname");
523
524 got_dumps([ sortdumps Amanda::DB::Catalog::get_dumps(hostname => 'oldbox') ],
525     [ sortdumps dumps_named qr/^oldbox_.*_holding/ ],
526     "get_dumps parameter hostname, holding");
527
528 got_dumps([ sortdumps Amanda::DB::Catalog::get_dumps(hostnames => ['notthere', 'otherbox']) ],
529     [ sortdumps dumps_named qr/^otherbox/ ],
530     "get_dumps parameter hostnames");
531
532 got_dumps([ sortdumps Amanda::DB::Catalog::get_dumps(hostname_match => 'other*') ],
533     [ sortdumps dumps_named qr/^otherbox/ ],
534     "get_dumps parameter hostname_match");
535
536 got_dumps([ sortdumps Amanda::DB::Catalog::get_dumps(diskname => '/lib') ],
537     [ sortdumps dumps_named qr/^[^_]*_lib_/ ],
538     "get_dumps parameter diskname");
539
540 got_dumps([ sortdumps Amanda::DB::Catalog::get_dumps(disknames => ['/lib', '/usr/bin']) ],
541     [ sortdumps dumps_named qr/^[^_]*_(usr_bin|lib)_/ ],
542     "get_dumps parameter disknames");
543
544 got_dumps([ sortdumps Amanda::DB::Catalog::get_dumps(diskname_match => 'bin') ],
545     [ sortdumps dumps_named qr/.*_bin_/ ],
546     "get_dumps parameter diskname_match");
547
548 got_dumps([ sortdumps Amanda::DB::Catalog::get_dumps(dump_timestamp => '20080414144444') ],
549     [ sortdumps dumps_matching { $_->{'dump_timestamp'} eq '20080414144444' } ],
550     "get_dumps parameter dump_timestamp");
551
552 got_dumps([ sortdumps Amanda::DB::Catalog::get_dumps(
553                         dump_timestamps => ['20080414144444', '20080311131133']) ],
554     [ sortdumps dumps_matching { $_->{'dump_timestamp'} eq '20080414144444' 
555                               or $_->{'dump_timestamp'} eq '20080311131133' } ],
556     "get_dumps parameter dump_timestamps");
557
558 got_dumps([ sortdumps Amanda::DB::Catalog::get_dumps(dump_timestamp_match => '200804-7') ],
559     [ sortdumps dumps_matching { $_->{'dump_timestamp'} =~ /^20080[4567]/ } ],
560     "get_dumps parameter dump_timestamp_match");
561
562 got_dumps([ sortdumps Amanda::DB::Catalog::get_dumps(level => 0) ],
563     [ sortdumps dumps_matching { $_->{'level'} == 0 } ],
564     "get_dumps parameter level");
565
566 got_dumps([ sortdumps Amanda::DB::Catalog::get_dumps(levels => [ 1 ]) ],
567     [ sortdumps dumps_matching { $_->{'level'} == 1 } ],
568     "get_dumps parameter levels");
569
570 got_dumps([ sortdumps Amanda::DB::Catalog::get_dumps(status => "OK") ],
571     [ sortdumps dumps_matching { $_->{'status'} eq "OK" } ],
572     "get_dumps parameter status = OK");
573
574 got_dumps([ sortdumps Amanda::DB::Catalog::get_dumps(status => "PARTIAL") ],
575     [ sortdumps dumps_matching { $_->{'status'} eq "PARTIAL" } ],
576     "get_dumps parameter status = PARTIAL");
577
578 got_dumps([ sortdumps Amanda::DB::Catalog::get_dumps(status => "FAIL") ],
579     [ sortdumps dumps_matching { $_->{'status'} eq "FAIL" } ],
580     "get_dumps parameter status = FAIL");
581
582 @dumpspecs = Amanda::Cmdline::parse_dumpspecs([".*", "/lib"], 0);
583 got_dumps([ sortdumps Amanda::DB::Catalog::get_dumps(dumpspecs => [ @dumpspecs ]) ],
584     [ sortdumps dumps_named qr/_lib_/ ],
585     "get_dumps parameter dumpspecs with one dumpspec");
586
587 @dumpspecs = Amanda::Cmdline::parse_dumpspecs([".*", "/lib"], 0);
588 got_dumps([ sortdumps Amanda::DB::Catalog::get_dumps(dumpspecs => [ @dumpspecs ], holding => 1) ],
589     [ sortdumps dumps_named qr/_lib_.*_holding/ ],
590     "get_dumps parameter dumpspecs with one dumpspec");
591
592 @dumpspecs = Amanda::Cmdline::parse_dumpspecs([".*", "/lib", "somebox"], 0);
593 got_dumps([ sortdumps Amanda::DB::Catalog::get_dumps(dumpspecs => [ @dumpspecs ]) ],
594     [ sortdumps dumps_matching { $_->{'diskname'} eq '/lib'
595                               or $_->{'hostname'} eq 'somebox' } ],
596     "get_dumps parameter dumpspecs with two dumpspecs");
597
598 @dumpspecs = Amanda::Cmdline::parse_dumpspecs(["otherbox", "*", "somebox"], 0);
599 got_dumps([ sortdumps Amanda::DB::Catalog::get_dumps(dumpspecs => [ @dumpspecs ]) ],
600     [ sortdumps dumps_matching { $_->{'hostname'} eq 'otherbox'
601                               or $_->{'hostname'} eq 'somebox' } ],
602     "get_dumps parameter dumpspecs with two non-overlapping dumpspecs");
603
604 @dumpspecs = Amanda::Cmdline::parse_dumpspecs(["does-not-exist"], 0);
605 got_dumps([ sortdumps Amanda::DB::Catalog::get_dumps(dumpspecs => [ @dumpspecs ]) ],
606     [ ],
607     "get_dumps parameter dumpspecs with a dumpspec that matches nothing",
608     zero_dumps_expected => 1);
609
610 @dumpspecs = Amanda::Cmdline::dumpspec_t->new(undef, undef, undef, undef, '20080222222222');
611 got_dumps([ sortdumps Amanda::DB::Catalog::get_dumps(dumpspecs => [ @dumpspecs ]) ],
612     [ sortdumps dumps_matching { $_->{'write_timestamp'} eq '20080222222222' }],
613     "get_dumps parameter dumpspecs with write_timestamp");
614
615 ## test dump sorting
616
617 got_dumps([ Amanda::DB::Catalog::sort_dumps(['write_timestamp'],
618                 @dumps{'somebox_lib_20080222222222','somebox_lib_20080111'}) ],
619               [ @dumps{'somebox_lib_20080111','somebox_lib_20080222222222'} ],
620     "sort dumps by write_timestamps");
621 got_dumps([ Amanda::DB::Catalog::sort_dumps(['-write_timestamp'],
622                 @dumps{'somebox_lib_20080111','somebox_lib_20080222222222'}) ],
623               [ @dumps{'somebox_lib_20080222222222','somebox_lib_20080111'} ],
624     "sort dumps by write_timestamps, reverse");
625
626 got_dumps([ Amanda::DB::Catalog::sort_dumps(['hostname', '-diskname', 'write_timestamp'],
627                 @dumps{
628                     'somebox_lib_20080222222222',
629                     'somebox_usr_bin_20080313133333',
630                     'somebox_lib_20080313133333',
631                     'otherbox_lib_20080313133333',
632                     'somebox_lib_20080111',
633                     }) ],
634               [ @dumps{
635                     'otherbox_lib_20080313133333',
636                     'somebox_usr_bin_20080313133333',
637                     'somebox_lib_20080111',
638                     'somebox_lib_20080222222222',
639                     'somebox_lib_20080313133333',
640                     } ],
641                 "multi-key dump sort");
642
643 got_dumps([ Amanda::DB::Catalog::sort_dumps(['nparts'],
644                 @dumps{
645                     'somebox_lib_20080313133333', # nparts=10
646                     'somebox_lib_20080222222222', # nparts=2
647                     }) ],
648               [ @dumps{
649                     'somebox_lib_20080222222222', # nparts=2
650                     'somebox_lib_20080313133333', # nparts=10
651                     } ],
652                 "dumps' nparts is sorted numerically, not lexically");
653
654 got_dumps([ Amanda::DB::Catalog::sort_dumps(['-level'],
655                 @dumps{
656                     'somebox_lib_20080313133333', # level=0
657                     'somebox_usr_bin_20080313133333', # level=1
658                     }) ],
659               [ @dumps{
660                     'somebox_usr_bin_20080313133333', # level=1
661                     'somebox_lib_20080313133333', # level=0
662                     } ],
663                 "sort dumps by level, reversed");
664
665 got_dumps([ Amanda::DB::Catalog::sort_dumps(['dump_timestamp'],
666                 @dumps{
667                     'somebox_lib_20080313133333', # dts=20080313133333
668                     'otherbox_usr_bin_20080313133333_1', # dts=20080311131133
669                     }) ],
670               [ @dumps{
671                     'otherbox_usr_bin_20080313133333_1', # dts=20080311131133
672                     'somebox_lib_20080313133333', # dts=20080313133333
673                     } ],
674                 "sort dumps by write_timestamp");
675
676
677 # install the multi-taper catalog
678 $cat = Installcheck::Catalogs::load("multi-taper");
679 $cat->install();
680 %dumps = $cat->get_dumps();
681 %parts = $cat->get_parts();
682
683 Amanda::DB::Catalog::_clear_cache();
684
685 got_parts([ sortparts Amanda::DB::Catalog::get_parts() ],
686         [ sortparts parts_named qr/.*/ ],
687         "get_parts returns all parts when given no parameters");
688