Imported Upstream version 3.1.0
[debian/amanda] / installcheck / amgetconf.pl
1 # Copyright (c) 2007,2008,2009 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 => 82;
20
21 use lib "@amperldir@";
22 use Installcheck;
23 use Installcheck::Config;
24 use Installcheck::Run qw(run run_get run_err);
25 use Amanda::Paths;
26 use Cwd;
27
28 # this is re-created for each test
29 my $testconf;
30
31 ##
32 # First, try amgetconf out without a config
33
34 ok(!run('amgetconf'), "bare amgetconf fails");
35 like($Installcheck::Run::stdout, qr(\AUsage: )i, 
36     ".. and gives usage message on stdout");
37 like(run_err('amgetconf', 'this-probably-doesnt-exist', 'tapedev'),
38     qr(could not open conf file)i, 
39     "error message when configuration parameter doesn't exist");
40
41 ##
42 # Next, work against a basically empty config
43
44 $testconf = Installcheck::Config->new();
45 $testconf->write();
46
47 # test some defaults
48 is(run_get('amgetconf', 'TESTCONF', "reserve"), "100", 
49     "reserve defaults to 100");
50 is(run_get('amgetconf', 'TESTCONF', "tapelist"), "tapelist", 
51     "tapelist defaults to 'tapelist'");
52 is(run_get('amgetconf', 'TESTCONF', "usetimestamps"), "yes", 
53     "usetimestamps defaults to 'yes'");
54 is(run_get('amgetconf', 'TESTCONF', "send_amreport_on"), "ALL",
55     "send_amreport_on defaults to 'ALL'"); # (enum value is 0)
56 is(run_get('amgetconf', 'TESTCONF', "taperalgo"), "FIRST",
57     "taperalgo defaults to 'ALL'"); # (enum value is 0)
58 is(run_get('amgetconf', 'TESTCONF', "printer"), "",
59     "printer defaults to empty string, which is not an error");
60
61 # test command-line parsing
62 is(run_get('amgetconf', 'TESTCONF', '--execute-where', 'client', 'amandates'),
63    $Amanda::Constants::DEFAULT_AMANDATES_FILE,
64     "--execute-where client");
65 is(run_get('amgetconf', 'TESTCONF', '--execute-where=client', 'amandates'),
66    $Amanda::Constants::DEFAULT_AMANDATES_FILE,
67     "--execute-where=client");
68 is(run_get('amgetconf', 'TESTCONF', '--client', 'amandates'),
69    $Amanda::Constants::DEFAULT_AMANDATES_FILE,
70     "--client");
71
72 is(run_get('amgetconf', 'TESTCONF', '--execute-where', 'server', 'reserve'), "100",
73     "--execute-where server");
74 is(run_get('amgetconf', 'TESTCONF', '--execute-where=server', 'reserve'), "100",
75     "--execute-where=server");
76 is(run_get('amgetconf', 'TESTCONF', '--execute-where=server', '--execute-where=server', 'reserve'), "100",
77     "--execute-where=server --execute-where=server");
78 is(run_get('amgetconf', 'TESTCONF', '--execute-where=client', '--execute-where=client', 'amandates'),
79    $Amanda::Constants::DEFAULT_AMANDATES_FILE,
80     "--execute-where=client --execute-where=client");
81
82 like(run_err('amgetconf', 'TESTCONF', '--execute-where=server', '--execute-where=client'),
83     qr/conflicts with/,
84     "handles conflict --execute-where=server --execute-where=client");
85 like(run_err('amgetconf', 'TESTCONF', '--execute-where=client', '--execute-where=server'),
86     qr/conflicts with/,
87     "handles conflict --execute-where=client --execute-where=server");
88 like(run_err('amgetconf', 'TESTCONF', '--execute-where=server', '--client'),
89      qr/conflicts with/,
90     "handles conflict --execute-where=server --client");
91 like(run_err('amgetconf', 'TESTCONF', '--client', '--execute-where=server'),
92     qr/conflicts with/, 
93     "handles conflict --client --execute-where=server");
94
95 is(run_get('amgetconf', 'TESTCONF', '-o', 'reserve=50', 'reserve'), "50",
96     "-o reserve=50");
97 is(run_get('amgetconf', 'TESTCONF', '-oreserve=50', 'reserve'), "50",
98     "-oreserve=50");
99 is(run_get('amgetconf', '-o', 'reserve=50', 'TESTCONF', 'reserve'), "50",
100     "-oreserve=50 before config name");
101 is(run_get('amgetconf', 'TESTCONF', 'reserve', 'a', 'table', 'for', 'two', '-o', 'reserve=50'), "50",
102     "extra command-line arguments are ignored");
103
104 # test a nonexistent parameter
105 like(run_err('amgetconf', 'TESTCONF', "foos_per_bar"), qr/no such parameter/, 
106     "handles nonexistent parameters as an error");
107 like(run_err('amgetconf', 'TESTCONF', "build.foos_per_bar"), qr/no such parameter/, 
108     "handles nonexistent build parameters as an error");
109
110 # Test build parameters that we can determine easily.  Testing all parameters
111 # would be more of a maintenance bother than a help.
112 is(run_get('amgetconf', 'TESTCONF', "build.bindir"), $bindir,
113     "build.bindir is correct");
114 is(run_get('amgetconf', 'TESTCONF', "build.sbindir"), $sbindir,
115     "build.sbindir is correct");
116 is(run_get('amgetconf', 'TESTCONF', "build.libexecdir"), $libexecdir,
117     "build.libexecdir is correct");
118 is(run_get('amgetconf', 'TESTCONF', "build.amlibexecdir"), $amlibexecdir,
119     "build.amlibexecdir is correct");
120 is(run_get('amgetconf', 'TESTCONF', "build.mandir"), $mandir,
121     "build.mandir is correct");
122 is(run_get('amgetconf', 'TESTCONF', "build.AMANDA_DBGDIR"), $AMANDA_DBGDIR,
123     "build.AMANDA_DBGDIR is correct");
124 is(run_get('amgetconf', 'TESTCONF', "build.AMANDA_TMPDIR"), $AMANDA_TMPDIR,
125     "build.AMDNA_TMPDIR is correct");
126 is(run_get('amgetconf', 'TESTCONF', "build.CONFIG_DIR"), $CONFIG_DIR,
127     "build.CONFIG_DIR is correct");
128 is(run_get('amgetconf', 'TESTCONF', "build.__empty"), "",
129     "empty build variables handled correctly");
130
131 like(run_err('amgetconf', 'TESTCONF', "build.bogus-param"), qr(no such parameter),
132     "bogus build parameters result in an error");
133
134 is(run_get('amgetconf', 'TESTCONF', "build.config_dir"), $CONFIG_DIR, 
135     "build parameters are case-insensitive");
136
137 is(run_get('amgetconf', "build.bindir"), $bindir, "build variables are available without a config");
138
139 # empty --list should return nothing
140 is_deeply([sort(+split(/\n/, run_get('amgetconf', 'TESTCONF', '--list', 'holdingdisk')))], [ ],
141         "--list returns an empty list when there's nothing to return");
142
143 # dbopen, dbclose
144 my $dbfile = run_get('amgetconf', 'TESTCONF', "dbopen.foo");
145 chomp $dbfile;
146 like($dbfile, qr(^\Q$AMANDA_DBGDIR\E/server/foo.[0-9]*.debug$),
147     "'amgetconf dbopen.foo' returns a proper debug filename");
148 SKIP: {
149     skip "dbopen didn't work, so I'll skip the rest", 3
150         unless (-f $dbfile);
151     ok(!run('amgetconf', 'TESTCONF', "dbclose.foo"),
152         "dbclose without filename fails");
153     is(run_get('amgetconf', 'TESTCONF', "dbclose.foo:$dbfile"), $dbfile, 
154         "'amgetconf dbclose.foo:<filename>' returns the debug filename");
155
156     # sometimes shell scripts pass a full path as appname..
157     $dbfile = run_get('amgetconf', 'TESTCONF', 'dbopen./sbin/foo');
158     like($dbfile, qr(^\Q$AMANDA_DBGDIR\E/server/_sbin_foo.[0-9]*.debug$),
159         "'amgetconf dbopen./sbin/foo' doesn't get confused by the slashes");
160 }
161
162 ##
163 # Test an invalid config file
164
165 $testconf = Installcheck::Config->new();
166 $testconf->add_param("foos_per_bar", "10");
167 $testconf->write();
168
169 like(run_err('amgetconf', 'TESTCONF', "foos_per_bar"), qr/errors processing config file/, 
170     "gives error on invalid configuration");
171
172 ##
173 # Now let's fill in some interesting values
174
175 $testconf = Installcheck::Config->new();
176 $testconf->add_param("reserved-udp-port", '100,200');
177 $testconf->add_param("printer", '"/dev/lp"');
178 $testconf->add_param("reserve", '27');
179 $testconf->write();
180
181 is(run_get('amgetconf', 'TESTCONF', "reserved-udp-port"), "100,200", 
182     "correctly returns intrange parameters from the file");
183 is(run_get('amgetconf', 'TESTCONF', "printer"), "/dev/lp", 
184     "correctly returns string parameters from the file");
185 is(run_get('amgetconf', 'TESTCONF', "reserve"), "27", 
186     "correctly returns integer parameters from the file");
187 is(run_get('amgetconf', 'TESTCONF', "rEsErVe"), "27", 
188     "is case-insensitive");
189 is(run_get('amgetconf', 'TESTCONF', "reserved_udp_port"), "100,200", 
190     "treats _ and - identically");
191
192 # check runs without a config
193 my $olddir = getcwd();
194 chdir("$CONFIG_DIR/TESTCONF") or die("Could not 'cd' to TESTCONF directory");
195 is(run_get('amgetconf', "printer"), "/dev/lp", 
196     "uses current directory when no configuration name is given");
197 chdir($olddir) or die("Could not 'cd' back to my original directory");
198
199 ##
200 # device_property can appear multiple times
201
202 $testconf = Installcheck::Config->new();
203 $testconf->add_param("device_property", '"power" "on"');
204 $testconf->add_param("device_property", '"turbo" "engaged"');
205 $testconf->write();
206
207 is_deeply([sort(+split(qr/\n/, run_get('amgetconf', 'TESTCONF', 'device_property')))],
208           [sort('"power" "on"', '"turbo" "engaged"')],
209     "device_property can have multiple values");
210
211 ##
212 # Subsections
213
214 $testconf = Installcheck::Config->new();
215 $testconf->add_tapetype("cassette", [ length => "32 k" ]);
216 $testconf->add_tapetype("reel2reel", [ length => "1 M" ]);
217 $testconf->add_tapetype("scotch", [ length => "500 bytes" ]); # (use a sharpie)
218 $testconf->add_dumptype("testdump", [ comment => '"testdump-dumptype"',
219                                       auth => '"bsd"' ]);
220 $testconf->add_dumptype("testdump1", [ inherit => 'testdump' ]);
221 $testconf->add_interface("testiface", [ use => '10' ]);
222 $testconf->add_holdingdisk("hd17", [ chunksize => '128' ]);
223 $testconf->add_application('app_amgtar', [ plugin => '"amgtar"' ]);
224 $testconf->add_application('app_amstar', [ plugin => '"amstar"' ]);
225 $testconf->add_script('my_script', [ "execute-on" => 'pre-dle-amcheck', 'plugin' => '"foo"' ]);
226 $testconf->add_device('my_device', [ "tapedev" => '"foo:/bar"' ]);
227 $testconf->write();
228
229 is_deeply([sort(+split(/\n/, run_get('amgetconf', 'TESTCONF', '--list', 'tapetype')))],
230           [sort("cassette", "reel2reel", "scotch", "TEST-TAPE")],
231         "--list returns correct set of tapetypes");
232 is(run_get('amgetconf', 'TESTCONF', 'tapetype:scotch:length'), '500', 
233     "returns tapetype parameter correctly");
234
235 ok(scalar(grep { $_ eq 'testdump' } 
236         split(/\n/, 
237             run_get('amgetconf', 'TESTCONF', '--list', 'dumptype'))),
238         "--list returns a test dumptype among the default dumptypes");
239 is(run_get('amgetconf', 'TESTCONF', 'dumptype:testdump:comment'), 'testdump-dumptype', 
240     "returns dumptype parameter correctly");
241
242 is_deeply([sort(+split(/\n/, run_get('amgetconf', 'TESTCONF', '--list', 'interface')))], 
243           [sort("testiface", "default")],
244         "--list returns correct set of interfaces");
245 is(run_get('amgetconf', 'TESTCONF', 'interface:testiface:use'), '10', 
246     "returns interface parameter correctly");
247
248 is_deeply([sort(+split(/\n/, run_get('amgetconf', 'TESTCONF', '--list', 'holdingdisk')))], 
249           [sort("hd17")], 
250         "--list returns correct set of holdingdisks");
251 is(run_get('amgetconf', 'TESTCONF', 'holdingdisk:hd17:chunksize'), '128',
252     "returns holdingdisk parameter correctly");
253
254 like(run_get('amgetconf', 'TESTCONF', '--list', 'build'), qr(.*version.*),
255         "'--list build' lists build variables");
256
257 is_deeply([sort(+split(/\n/, run_get('amgetconf', 'TESTCONF', '--list', 'application')))],
258           [sort("app_amgtar", "app_amstar")],
259         "--list returns correct set of applications");
260
261 is(run_get('amgetconf', 'TESTCONF', 'application-tool:app_amgtar:plugin'), 'amgtar',
262     "returns application-tool parameter correctly");
263
264 is_deeply([sort(+split(/\n/, run_get('amgetconf', 'TESTCONF', '--list', 'script')))],
265           [sort("my_script")],
266         "--list returns correct set of scripts");
267
268 # test the old names
269 is_deeply([sort(+split(/\n/, run_get('amgetconf', 'TESTCONF', '--list', 'script-tool')))],
270           [sort("my_script")],
271         "--list returns correct set of scripts, using the name script-tool");
272
273 is_deeply([sort(+split(/\n/, run_get('amgetconf', 'TESTCONF', '--list', 'application-tool')))],
274           [sort("app_amgtar", "app_amstar")],
275         "--list returns correct set of applications, using the name 'application-tool'");
276
277 is(run_get('amgetconf', 'TESTCONF', 'script-tool:my_script:execute-on'), 'PRE-DLE-AMCHECK',
278     "returns script-tool parameter correctly");
279 is(run_get('amgetconf', 'TESTCONF', 'script_tOOl:my_script:execute-on'), 'PRE-DLE-AMCHECK',
280     "insensitive to case in subsec_type");
281 is(run_get('amgetconf', 'TESTCONF', 'script-tool:my_script:execute-on'), 'PRE-DLE-AMCHECK',
282     "insensitive to -/_ in subsec_type");
283 is(run_get('amgetconf', 'TESTCONF', 'script_tOOl:my_script:eXECute-on'), 'PRE-DLE-AMCHECK',
284     "insensitive to case in subsec_key");
285 is(run_get('amgetconf', 'TESTCONF', 'script-tool:my_script:execute_on'), 'PRE-DLE-AMCHECK',
286     "insensitive to -/_ in subsec_key");
287 is(run_get('amgetconf', 'TESTCONF', 'dumptype:testdump1:auth', '-odumptype:testdump:auth=SSH'), 'SSH',
288     "inherited setting are overrided");
289 is(run_get('amgetconf', 'TESTCONF', 'dumptype:testdump1:compress', '-odumptype:testdump:compress=SERVER BEST'), 'SERVER BEST',
290     "inherited default are overrided");
291
292 is_deeply([sort(split(/\n/, run_get('amgetconf', 'TESTCONF', '--list', 'device')))],
293           [sort("my_device")],
294         "--list returns correct set of devices");
295
296 is(run_get('amgetconf', 'TESTCONF', 'device:my_device:tapedev'), 'foo:/bar',
297     "returns device parameter correctly");
298
299 # non-existent subsection types, names, and parameters
300 like(run_err('amgetconf', 'TESTCONF', 'NOSUCHTYPE:testiface:comment'), qr/no such parameter/, 
301     "handles bad subsection type");
302 like(run_err('amgetconf', 'TESTCONF', 'dumptype:NOSUCHDUMP:comment'), qr/no such parameter/, 
303     "handles bad dumptype namek");
304 like(run_err('amgetconf', 'TESTCONF', 'dumptype:testdump:NOSUCHPARAM'), qr/no such parameter/, 
305     "handles bad dumptype parameter name");
306 like(run_err('amgetconf', 'TESTCONF', 'application-tool:app_amgtar:NOSUCHPARAM'), qr/no such parameter/, 
307     "handles bad application-tool parameter name");
308 like(run_err('amgetconf', 'TESTCONF', 'script-tool:my-script:NOSUCHPARAM'), qr/no such parameter/, 
309     "handles bad script-tool parameter name");
310
311 like(run_err('amgetconf', 'TESTCONF', '--list', 'frogs'), qr/no such parameter/,
312         "--list fails given an invalid subsection name");
313
314 ##
315 # exclude lists are a bit funny, too
316
317 $testconf = Installcheck::Config->new();
318 $testconf->add_dumptype("testdump", [
319     "exclude file optional" => '"f1"', # this optional will have no effect
320     "exclude file append" => '"f2"',
321     "exclude list" => '"l1"',
322     "exclude list append" => '"l2"',
323     "include file" => '"ifo"',
324     "include list optional" => '"ilo"',
325     ]);
326 $testconf->write();
327
328 is_deeply([sort(+split(qr/\n/, run_get('amgetconf', 'TESTCONF', 'dumptype:testdump:exclude')))],
329           [sort('FILE "f1" "f2"',
330                 'LIST "l1" "l2"')],
331     "exclude files and lists displayed correctly; a non-final optional is ignored");
332
333 is_deeply([sort(+split(qr/\n/, run_get('amgetconf', 'TESTCONF', 'dumptype:testdump:include')))],
334           [sort('FILE OPTIONAL "ifo"',
335                 'LIST OPTIONAL "ilo"')],
336     "a final 'OPTIONAL' makes the whole include/exclude optional")
337