1 # Copyright (c) 2007,2008 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 => 42;
21 use lib "@amperldir@";
23 use Amanda::Cmdline qw( header_matches_dumpspecs );
26 # convert a dumpspec_t object to an array, for easy is_deeply() comparisons
37 # test dumpspec_t objects
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");
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");
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");
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");
55 # TODO: test parse_dumpspecs
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");
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");
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");
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");
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");
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");
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");
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");
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");
103 # test format_dumpspec_components
105 is(Amanda::Cmdline::format_dumpspec_components("h", "di", "ds", "l"),
107 "format_dumpspec_components works ok");
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;
117 ok(!header_matches_dumpspecs($hdr, []), "header doesn't match empty list of dumpspecs");
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');
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');
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");
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");
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),
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'),
162 'header matches when two dumpspecs are given and neither should match');