441028f3a79ffff962e114932261a895c30d36dd
[debian/amanda] / installcheck / Amanda_Cmdline.pl
1 # Copyright (c) 2007,2008 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 => 42;
20
21 use lib "@amperldir@";
22 use Amanda::Paths;
23 use Amanda::Cmdline qw( header_matches_dumpspecs );
24 use Amanda::Header;
25
26 # convert a dumpspec_t object to an array, for easy is_deeply() comparisons
27 sub ds2av {
28     my ($ds) = @_;
29     return (
30         $ds->{'host'},
31         $ds->{'disk'},
32         $ds->{'datestamp'},
33         $ds->{'level'},
34     );
35 }
36
37 # test dumpspec_t objects
38
39 is_deeply([ ds2av(Amanda::Cmdline::dumpspec_t->new("h", "di", "ds", "l")) ],
40           [ "h", "di", "ds", "l" ],
41           "dumpspec_t constructor returns a valid dumpspec");
42
43 is_deeply([ ds2av(Amanda::Cmdline::dumpspec_t->new("h", "di", "ds", undef)) ],
44           [ "h", "di", "ds", undef ],
45           "dumpspec_t constructor returns a valid dumpspec with only 3 args");
46
47 is_deeply([ ds2av(Amanda::Cmdline::dumpspec_t->new("h", "di", undef, undef)) ],
48           [ "h", "di", undef, undef ],
49           "dumpspec_t constructor returns a valid dumpspec with only 2 args");
50
51 is_deeply([ ds2av(Amanda::Cmdline::dumpspec_t->new("h", undef, undef, undef)) ],
52           [ "h", undef, undef, undef ],
53           "dumpspec_t constructor returns a valid dumpspec with only 1 arg");
54
55 # TODO: test parse_dumpspecs
56 my @specs;
57
58 @specs = Amanda::Cmdline::parse_dumpspecs(["h1", "d1", "h2", "d2"], 0);
59 is(@specs, 2, "parse of four elements with no flags yields 2 specs");
60 is_deeply([ ds2av($specs[0]) ], [ "h1", "d1", undef, undef ], "..first spec is correct");
61 is_deeply([ ds2av($specs[1]) ], [ "h2", "d2", undef, undef ], "..second spec is correct");
62
63 @specs = Amanda::Cmdline::parse_dumpspecs(["h1", "d1", "ds1", "h2", "d2", "ds2" ], $Amanda::Cmdline::CMDLINE_PARSE_DATESTAMP);
64 is(@specs, 2, "parse of six elements with CMDLINE_PARSE_DATESTAMP yields 2 specs");
65 is_deeply([ ds2av($specs[0]) ], [ "h1", "d1", "ds1", undef ], "..first spec is correct");
66 is_deeply([ ds2av($specs[1]) ], [ "h2", "d2", "ds2", undef ], "..second spec is correct");
67
68 @specs = Amanda::Cmdline::parse_dumpspecs(["h1", "d1", "ds1", "lv1", "h2", "d2", "ds2", "lv2" ],
69                 $Amanda::Cmdline::CMDLINE_PARSE_DATESTAMP | $Amanda::Cmdline::CMDLINE_PARSE_LEVEL);
70 is(@specs, 2, "parse of eight elements with CMDLINE_PARSE_DATESTAMP and CMDLINE_PARSE_LEVEL yields 2 specs");
71 is_deeply([ ds2av($specs[0]) ], [ "h1", "d1", "ds1", "lv1" ], "..first spec is correct");
72 is_deeply([ ds2av($specs[1]) ], [ "h2", "d2", "ds2", "lv2" ], "..second spec is correct");
73
74 @specs = Amanda::Cmdline::parse_dumpspecs(["h1", "d1", "ds1", "lv1" ],
75                 $Amanda::Cmdline::CMDLINE_PARSE_DATESTAMP | $Amanda::Cmdline::CMDLINE_PARSE_LEVEL);
76 is(@specs, 1, "parse of four elements with CMDLINE_PARSE_DATESTAMP and CMDLINE_PARSE_LEVEL yields one spec");
77 is_deeply([ ds2av($specs[0]) ], [ "h1", "d1", "ds1", "lv1" ], "..which is correct");
78
79 @specs = Amanda::Cmdline::parse_dumpspecs(["h1", "d1", "ds1" ],
80                 $Amanda::Cmdline::CMDLINE_PARSE_DATESTAMP | $Amanda::Cmdline::CMDLINE_PARSE_LEVEL);
81 is(@specs, 1, "parse of three elements with CMDLINE_PARSE_DATESTAMP and CMDLINE_PARSE_LEVEL yields one spec");
82 is_deeply([ ds2av($specs[0]) ], [ "h1", "d1", "ds1", undef ], "..which is correct");
83
84 @specs = Amanda::Cmdline::parse_dumpspecs(["h1", "d1" ],
85                 $Amanda::Cmdline::CMDLINE_PARSE_DATESTAMP | $Amanda::Cmdline::CMDLINE_PARSE_LEVEL);
86 is(@specs, 1, "parse of two elements with CMDLINE_PARSE_DATESTAMP and CMDLINE_PARSE_LEVEL yields one spec");
87 is_deeply([ ds2av($specs[0]) ], [ "h1", "d1", undef, undef ], "..which is correct");
88
89 @specs = Amanda::Cmdline::parse_dumpspecs(["h1" ],
90                 $Amanda::Cmdline::CMDLINE_PARSE_DATESTAMP | $Amanda::Cmdline::CMDLINE_PARSE_LEVEL);
91 is(@specs, 1, "parse of one element with CMDLINE_PARSE_DATESTAMP and CMDLINE_PARSE_LEVEL yields one spec");
92 is_deeply([ ds2av($specs[0]) ], [ "h1", undef, undef, undef ], "..which is correct");
93
94 @specs = Amanda::Cmdline::parse_dumpspecs([],
95                 $Amanda::Cmdline::CMDLINE_PARSE_DATESTAMP | $Amanda::Cmdline::CMDLINE_PARSE_LEVEL);
96 is(@specs, 0, "parse of no elements with CMDLINE_PARSE_DATESTAMP and CMDLINE_PARSE_LEVEL yields no specs");
97
98 @specs = Amanda::Cmdline::parse_dumpspecs([],
99                 $Amanda::Cmdline::CMDLINE_PARSE_DATESTAMP | $Amanda::Cmdline::CMDLINE_PARSE_LEVEL
100                 | $Amanda::Cmdline::CMDLINE_EMPTY_TO_WILDCARD);
101 is(@specs, 1, "parse of no elements with CMDLINE_EMPTY_TO_WILDCARD yields one spec");
102
103 # test format_dumpspec_components
104
105 is(Amanda::Cmdline::format_dumpspec_components("h", "di", "ds", "l"),
106    "h di ds l",
107    "format_dumpspec_components works ok");
108
109 # test matching
110 $hdr = Amanda::Header->new();
111 $hdr->{'name'} = 'foo.bar.baz';
112 $hdr->{'disk'} = '/foo/bar/baz';
113 $hdr->{'datestamp'} = '20090102030405';
114 $hdr->{'dumplevel'} = 31;
115 $hdr->{'type'} = $Amanda::Header::F_DUMPFILE;
116
117 ok(!header_matches_dumpspecs($hdr, []), "header doesn't match empty list of dumpspecs");
118
119 ok(header_matches_dumpspecs($hdr, [Amanda::Cmdline::dumpspec_t->new('foo.bar.baz', undef, undef, undef)]),
120    'header matches exact host dumpspec');
121 ok(header_matches_dumpspecs($hdr, [Amanda::Cmdline::dumpspec_t->new('foo', undef, undef, undef)]),
122    'header matches partial host dumpspec');
123 ok(header_matches_dumpspecs($hdr, [Amanda::Cmdline::dumpspec_t->new('?a*', undef, undef, undef)]),
124    'header matches host pattern dumpspec');
125
126 ok(header_matches_dumpspecs($hdr, [Amanda::Cmdline::dumpspec_t->new(undef, '/foo/bar/baz', undef, undef)]),
127    'header matches exact disk dumpspec');
128 ok(header_matches_dumpspecs($hdr, [Amanda::Cmdline::dumpspec_t->new(undef, 'bar', undef, undef)]),
129    'header matches partial disk dumpspec');
130 ok(header_matches_dumpspecs($hdr, [Amanda::Cmdline::dumpspec_t->new(undef, '*a?', undef, undef)]),
131    'header matches disk pattern dumpspec');
132
133 ok(header_matches_dumpspecs($hdr, [Amanda::Cmdline::dumpspec_t->new(undef, undef, '20090102030405', undef)]),
134    'header matches exact datestamp dumpspec');
135 ok(header_matches_dumpspecs($hdr, [Amanda::Cmdline::dumpspec_t->new(undef, undef, '2009', undef)]),
136    'header matches partial datestamp dumpspec');
137 ok(header_matches_dumpspecs($hdr, [Amanda::Cmdline::dumpspec_t->new(undef, undef, '20090102030404-20090102030406', undef)]),
138    'header matches datestamp range dumpspec');
139 ok(header_matches_dumpspecs($hdr, [Amanda::Cmdline::dumpspec_t->new(undef, undef, '2009-2010', undef)]),
140    'header matches datestamp year-only range dumpspec');
141 ok(!header_matches_dumpspecs($hdr, [Amanda::Cmdline::dumpspec_t->new(undef, undef, '20090102030406-20090102030407', undef)]),
142    "header doesn't match datestamp range dumpspec that it's outside of");
143
144 ok(header_matches_dumpspecs($hdr, [Amanda::Cmdline::dumpspec_t->new(undef, undef, undef, '31')]),
145    'header matches exact level dumpspec');
146 ok(header_matches_dumpspecs($hdr, [Amanda::Cmdline::dumpspec_t->new(undef, undef, undef, '30-32')]),
147    'header matches small level range dumpspec');
148 ok(header_matches_dumpspecs($hdr, [Amanda::Cmdline::dumpspec_t->new(undef, undef, undef, '4-50')]),
149    'header matches large level range dumpspec');
150 ok(!header_matches_dumpspecs($hdr, [Amanda::Cmdline::dumpspec_t->new(undef, undef, undef, '32-50')]),
151    "header doesn't match level range it's outside of");
152
153 ok(header_matches_dumpspecs($hdr, [
154     Amanda::Cmdline::dumpspec_t->new('foo.bar.baz', undef, undef, undef),
155     Amanda::Cmdline::dumpspec_t->new(undef, '/foo/bar/baz', undef, undef),
156   ]),
157   'header matches when two dumpspecs are possible matches');
158 ok(!header_matches_dumpspecs($hdr, [
159     Amanda::Cmdline::dumpspec_t->new(undef, undef, '20090102030406-20090102030407', undef),
160     Amanda::Cmdline::dumpspec_t->new(undef, undef, undef, '32-50'),
161   ]),
162   'header matches when two dumpspecs are given and neither should match');