1 # Copyright (c) 2007-2012 Zmanda, Inc. All Rights Reserved.
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.
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
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
16 # Contact information: Zmanda Inc, 465 S. Mathilda Ave., Suite 300
17 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
19 use Test::More tests => 43;
23 use lib "@amperldir@";
25 use Amanda::Cmdline qw( header_matches_dumpspecs );
31 # convert a dumpspec_t object to an array, for easy is_deeply() comparisons
39 $ds->{'write_timestamp'},
43 # test dumpspec_t objects
45 is_deeply([ ds2av(Amanda::Cmdline::dumpspec_t->new("h", "di", "ds", "l", undef)) ],
46 [ "h", "di", "ds", "l", undef ],
47 "dumpspec_t constructor returns a valid dumpspec");
49 is_deeply([ ds2av(Amanda::Cmdline::dumpspec_t->new("h", "di", "ds", undef, undef)) ],
50 [ "h", "di", "ds", undef, undef ],
51 "dumpspec_t constructor returns a valid dumpspec with only 3 args");
53 is_deeply([ ds2av(Amanda::Cmdline::dumpspec_t->new("h", "di", undef, undef, undef)) ],
54 [ "h", "di", undef, undef, undef ],
55 "dumpspec_t constructor returns a valid dumpspec with only 2 args");
57 is_deeply([ ds2av(Amanda::Cmdline::dumpspec_t->new("h", undef, undef, undef, undef)) ],
58 [ "h", undef, undef, undef, undef ],
59 "dumpspec_t constructor returns a valid dumpspec with only 1 arg");
61 is_deeply([ ds2av(Amanda::Cmdline::dumpspec_t->new(undef, undef, undef, undef, "wt")) ],
62 [ undef, undef, undef, undef, "wt" ],
63 "dumpspec_t constructor returns a valid dumpspec with only write_timestamp arg");
65 @specs = Amanda::Cmdline::parse_dumpspecs(["h1", "d1", "h2", "d2"], 0);
66 is(@specs, 2, "parse of four elements with no flags yields 2 specs");
67 is_deeply([ ds2av($specs[0]) ], [ "h1", "d1", undef, undef, undef ], "..first spec is correct");
68 is_deeply([ ds2av($specs[1]) ], [ "h2", "d2", undef, undef, undef ], "..second spec is correct");
70 @specs = Amanda::Cmdline::parse_dumpspecs(["h1", "d1", "ds1", "h2", "d2", "ds2" ], $Amanda::Cmdline::CMDLINE_PARSE_DATESTAMP);
71 is(@specs, 2, "parse of six elements with CMDLINE_PARSE_DATESTAMP yields 2 specs");
72 is_deeply([ ds2av($specs[0]) ], [ "h1", "d1", "ds1", undef, undef ], "..first spec is correct");
73 is_deeply([ ds2av($specs[1]) ], [ "h2", "d2", "ds2", undef, undef ], "..second spec is correct");
75 @specs = Amanda::Cmdline::parse_dumpspecs(["h1", "d1", "ds1", "lv1", "h2", "d2", "ds2", "lv2" ],
76 $Amanda::Cmdline::CMDLINE_PARSE_DATESTAMP | $Amanda::Cmdline::CMDLINE_PARSE_LEVEL);
77 is(@specs, 2, "parse of eight elements with CMDLINE_PARSE_DATESTAMP and CMDLINE_PARSE_LEVEL yields 2 specs");
78 is_deeply([ ds2av($specs[0]) ], [ "h1", "d1", "ds1", "lv1", undef ], "..first spec is correct");
79 is_deeply([ ds2av($specs[1]) ], [ "h2", "d2", "ds2", "lv2", undef ], "..second spec is correct");
81 @specs = Amanda::Cmdline::parse_dumpspecs(["h1", "d1", "ds1", "lv1" ],
82 $Amanda::Cmdline::CMDLINE_PARSE_DATESTAMP | $Amanda::Cmdline::CMDLINE_PARSE_LEVEL);
83 is(@specs, 1, "parse of four elements with CMDLINE_PARSE_DATESTAMP and CMDLINE_PARSE_LEVEL yields one spec");
84 is_deeply([ ds2av($specs[0]) ], [ "h1", "d1", "ds1", "lv1", undef ], "..which is correct");
86 @specs = Amanda::Cmdline::parse_dumpspecs(["h1", "d1", "ds1" ],
87 $Amanda::Cmdline::CMDLINE_PARSE_DATESTAMP | $Amanda::Cmdline::CMDLINE_PARSE_LEVEL);
88 is(@specs, 1, "parse of three elements with CMDLINE_PARSE_DATESTAMP and CMDLINE_PARSE_LEVEL yields one spec");
89 is_deeply([ ds2av($specs[0]) ], [ "h1", "d1", "ds1", undef, undef ], "..which is correct");
91 @specs = Amanda::Cmdline::parse_dumpspecs(["h1", "d1" ],
92 $Amanda::Cmdline::CMDLINE_PARSE_DATESTAMP | $Amanda::Cmdline::CMDLINE_PARSE_LEVEL);
93 is(@specs, 1, "parse of two elements with CMDLINE_PARSE_DATESTAMP and CMDLINE_PARSE_LEVEL yields one spec");
94 is_deeply([ ds2av($specs[0]) ], [ "h1", "d1", undef, undef, undef ], "..which is correct");
96 @specs = Amanda::Cmdline::parse_dumpspecs(["h1" ],
97 $Amanda::Cmdline::CMDLINE_PARSE_DATESTAMP | $Amanda::Cmdline::CMDLINE_PARSE_LEVEL);
98 is(@specs, 1, "parse of one element with CMDLINE_PARSE_DATESTAMP and CMDLINE_PARSE_LEVEL yields one spec");
99 is_deeply([ ds2av($specs[0]) ], [ "h1", undef, undef, undef, undef ], "..which is correct");
101 @specs = Amanda::Cmdline::parse_dumpspecs([],
102 $Amanda::Cmdline::CMDLINE_PARSE_DATESTAMP | $Amanda::Cmdline::CMDLINE_PARSE_LEVEL);
103 is(@specs, 0, "parse of no elements with CMDLINE_PARSE_DATESTAMP and CMDLINE_PARSE_LEVEL yields no specs");
105 @specs = Amanda::Cmdline::parse_dumpspecs([],
106 $Amanda::Cmdline::CMDLINE_PARSE_DATESTAMP | $Amanda::Cmdline::CMDLINE_PARSE_LEVEL
107 | $Amanda::Cmdline::CMDLINE_EMPTY_TO_WILDCARD);
108 is(@specs, 1, "parse of no elements with CMDLINE_EMPTY_TO_WILDCARD yields one spec");
110 # test format_dumpspec_components
112 is(Amanda::Cmdline::format_dumpspec_components("h", "di", "ds", "l"),
114 "format_dumpspec_components works ok");
117 $hdr = Amanda::Header->new();
118 $hdr->{'name'} = 'foo.bar.baz';
119 $hdr->{'disk'} = '/foo/bar/baz';
120 $hdr->{'datestamp'} = '20090102030405';
121 $hdr->{'dumplevel'} = 31;
122 $hdr->{'type'} = $Amanda::Header::F_DUMPFILE;
124 ok(!header_matches_dumpspecs($hdr, []), "header doesn't match empty list of dumpspecs");
126 ok(header_matches_dumpspecs($hdr, [Amanda::Cmdline::dumpspec_t->new('foo.bar.baz', undef, undef, undef, undef)]),
127 'header matches exact host dumpspec');
128 ok(header_matches_dumpspecs($hdr, [Amanda::Cmdline::dumpspec_t->new('foo', undef, undef, undef, undef)]),
129 'header matches partial host dumpspec');
130 ok(header_matches_dumpspecs($hdr, [Amanda::Cmdline::dumpspec_t->new('?a*', undef, undef, undef, undef)]),
131 'header matches host pattern dumpspec');
133 ok(header_matches_dumpspecs($hdr, [Amanda::Cmdline::dumpspec_t->new(undef, '/foo/bar/baz', undef, undef, undef)]),
134 'header matches exact disk dumpspec');
135 ok(header_matches_dumpspecs($hdr, [Amanda::Cmdline::dumpspec_t->new(undef, 'bar', undef, undef, undef)]),
136 'header matches partial disk dumpspec');
137 ok(header_matches_dumpspecs($hdr, [Amanda::Cmdline::dumpspec_t->new(undef, '*a?', undef, undef, undef)]),
138 'header matches disk pattern dumpspec');
140 ok(header_matches_dumpspecs($hdr, [Amanda::Cmdline::dumpspec_t->new(undef, undef, '20090102030405', undef, undef)]),
141 'header matches exact datestamp dumpspec');
142 ok(header_matches_dumpspecs($hdr, [Amanda::Cmdline::dumpspec_t->new(undef, undef, '2009', undef, undef)]),
143 'header matches partial datestamp dumpspec');
144 ok(header_matches_dumpspecs($hdr, [Amanda::Cmdline::dumpspec_t->new(undef, undef, '20090102030404-20090102030406', undef, undef)]),
145 'header matches datestamp range dumpspec');
146 ok(header_matches_dumpspecs($hdr, [Amanda::Cmdline::dumpspec_t->new(undef, undef, '2009-2010', undef, undef)]),
147 'header matches datestamp year-only range dumpspec');
148 ok(!header_matches_dumpspecs($hdr, [Amanda::Cmdline::dumpspec_t->new(undef, undef, '20090102030406-20090102030407', undef, undef)]),
149 "header doesn't match datestamp range dumpspec that it's outside of");
151 ok(header_matches_dumpspecs($hdr, [Amanda::Cmdline::dumpspec_t->new(undef, undef, undef, '31', undef)]),
152 'header matches exact level dumpspec');
153 ok(header_matches_dumpspecs($hdr, [Amanda::Cmdline::dumpspec_t->new(undef, undef, undef, '30-32', undef)]),
154 'header matches small level range dumpspec');
155 ok(header_matches_dumpspecs($hdr, [Amanda::Cmdline::dumpspec_t->new(undef, undef, undef, '4-50', undef)]),
156 'header matches large level range dumpspec');
157 ok(!header_matches_dumpspecs($hdr, [Amanda::Cmdline::dumpspec_t->new(undef, undef, undef, '32-50', undef)]),
158 "header doesn't match level range it's outside of");
160 ok(header_matches_dumpspecs($hdr, [
161 Amanda::Cmdline::dumpspec_t->new('foo.bar.baz', undef, undef, undef, undef),
162 Amanda::Cmdline::dumpspec_t->new(undef, '/foo/bar/baz', undef, undef, undef),
164 'header matches when two dumpspecs are possible matches');
165 ok(!header_matches_dumpspecs($hdr, [
166 Amanda::Cmdline::dumpspec_t->new(undef, undef, '20090102030406-20090102030407', undef, undef),
167 Amanda::Cmdline::dumpspec_t->new(undef, undef, undef, '32-50', undef),
169 'header matches when two dumpspecs are given and neither should match');