31ea2041366e446f01cf6481b7ca7be2701c6e48
[debian/amanda] / installcheck / amgetconf.pl
1 # Copyright (c) 2006 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, 505 N Mathlida Ave, Suite 120
17 # Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
18
19 use Test::More qw(no_plan);
20
21 use Amconfig;
22 use lib "@amperldir@";
23 use Amanda::Paths;
24
25 # wrapper to call amgetconf and return the results
26 sub amgetconf {
27     # open amgetconf and read from it
28     my $cmd = "$sbindir/amgetconf " . join(" ", @_) . " 2>&1";
29     my $result = `$cmd`;
30     chomp $result;
31     return $result;
32 }
33
34 # this is re-created for each test
35 my $testconf;
36
37 ##
38 # First, try amgetconf out without a config
39
40 like(amgetconf(), qr(\AUsage: )i, 
41     "bare 'amgetconf' gives usage message");
42 like(amgetconf("this-probably-doesnt-exist"), qr(could not open conf file)i, 
43     "error message when configuration parameter doesn't exist");
44
45 ##
46 # Next, work against a basically empty config
47
48 $testconf = Amconfig->new();
49 $testconf->write();
50
51 # test some defaults
52 is(amgetconf('TESTCONF', "reserve"), "100", 
53     "reserve defaults to 100");
54 is(amgetconf('TESTCONF', "tapelist"), "tapelist", 
55     "tapelist defaults to 'tapelist'");
56 is(amgetconf('TESTCONF', "usetimestamps"), "yes", 
57     "usetimestamps defaults to 'yes'");
58
59 # test a nonexistent parameter
60 like(amgetconf('TESTCONF', "foos_per_bar"), qr/no such parameter/, 
61     "handles nonexistent parameters");
62
63 # test build parameters (just the most common)
64 is(amgetconf('TESTCONF', "build.bindir"), $bindir, "build.bindir is correct");
65 is(amgetconf('TESTCONF', "build.sbindir"), $sbindir, "build.sbindir is correct");
66 is(amgetconf('TESTCONF', "build.amlibexecdir"), $amlibexecdir, "build.amlibexecdir is correct");
67 is(amgetconf('TESTCONF', "build.mandir"), $mandir, "build.mandir is correct");
68 is(amgetconf('TESTCONF', "build.AMANDA_DBGDIR"), $AMANDA_DBGDIR, "build.AMANDA_DBGDIR is correct");
69 is(amgetconf('TESTCONF', "build.AMANDA_TMPDIR"), $AMANDA_TMPDIR, "build.AMANDA_TMPDIR is correct");
70 is(amgetconf('TESTCONF', "build.CONFIG_DIR"), $CONFIG_DIR, "build.CONFIG_DIR is correct");
71
72 # dbopen, dbclose
73 my $dbfile = amgetconf('TESTCONF', "dbopen.foo");
74 like($dbfile, qr(^$AMANDA_DBGDIR/server/foo.[0-9]*.debug$),
75     "'amgetconf dbopen.foo' returns a proper debug filename");
76 ok(-f $dbfile,
77     "'amgetconf dbopen.foo' creates the debug file");
78 like(amgetconf('TESTCONF', "dbclose.foo"), qr/cannot parse/,
79     "dbclose without filename fails");
80 is(amgetconf('TESTCONF', "dbclose.foo:$dbfile"), $dbfile, 
81     "'amgetconf dbclose.foo:<filename>' returns the debug filename");
82
83 ##
84 # Test an invalid config file
85
86 $testconf = Amconfig->new();
87 $testconf->add_param("foos_per_bar", "10");
88 $testconf->write();
89
90 like(amgetconf('TESTCONF', "foos_per_bar"), qr/errors processing config file/, 
91     "gives error on invalid configuration");
92
93 ##
94 # Now let's fill in some interesting values
95
96 $testconf = Amconfig->new();
97 $testconf->add_param("reserved-udp-port", '100,200');
98 $testconf->add_param("printer", '"/dev/lp"');
99 $testconf->add_param("reserve", '27');
100 $testconf->write();
101
102 is(amgetconf('TESTCONF', "reserved-udp-port"), "100,200", 
103     "correctly returns intrange parameters from the file");
104 is(amgetconf('TESTCONF', "printer"), "/dev/lp", 
105     "correctly returns string parameters from the file");
106 is(amgetconf('TESTCONF', "reserve"), "27", 
107     "correctly returns integer parameters from the file");
108 is(amgetconf('TESTCONF', "rEsErVe"), "27", 
109     "is case-insensitive");
110
111 ##
112 # device_property can appear multiple times
113
114 $testconf = Amconfig->new();
115 $testconf->add_param("device_property", '"power" "on"');
116 $testconf->add_param("device_property", '"turbo" "engaged"');
117 $testconf->write();
118
119 is_deeply([sort(+split(qr/\n/, amgetconf('TESTCONF', 'device_property')))],
120           [sort('"power" "on"', '"turbo" "engaged"')],
121     "device_property can have multiple values");
122
123 ##
124 # Subsections
125
126 $testconf = Amconfig->new();
127 $testconf->add_tapetype("cassette", [ length => "32 k" ]);
128 $testconf->add_tapetype("reel2reel", [ length => "1 M" ]);
129 $testconf->add_tapetype("scotch", [ length => "500 bytes" ]); # (use a sharpie)
130 $testconf->add_dumptype("testdump", [ comment => '"testdump-dumptype"' ]);
131 $testconf->add_interface("testiface", [ use => '10' ]);
132 $testconf->add_holdingdisk("hd17", [ chunksize => '128' ]);
133 $testconf->write();
134
135 is_deeply([sort(+split(/\n/, amgetconf('TESTCONF', '--list', 'tapetype')))],
136           [sort("cassette", "reel2reel", "scotch", "TEST-TAPE")],
137         "--list returns correct set of tapetypes");
138 is(amgetconf('TESTCONF', 'tapetype:scotch:length'), '500', 
139     "returns tapetype parameter correctly");
140
141 ok(grep { $_ eq 'testdump' } split(/\n/, amgetconf('TESTCONF', '--list', 'dumptype')),
142         "--list returns a test dumptype among the default dumptypes");
143 is(amgetconf('TESTCONF', 'dumptype:testdump:comment'), 'testdump-dumptype', 
144     "returns dumptype parameter correctly");
145
146 is_deeply([sort(+split(/\n/, amgetconf('TESTCONF', '--list', 'interface')))],
147           [sort("testiface", "default")],
148         "--list returns correct set of interfaces");
149 is(amgetconf('TESTCONF', 'interface:testiface:use'), '10', 
150     "returns interface parameter correctly");
151
152 is_deeply([sort(+split(/\n/, amgetconf('TESTCONF', '--list', 'holdingdisk')))],
153           [sort("hd17")],
154         "--list returns correct set of holdingdisks");
155 is(amgetconf('TESTCONF', 'holdingdisk:hd17:chunksize'), '128',
156     "returns holdingdisk parameter correctly");
157
158 # non-existent subsection types, names, and parameters
159 like(amgetconf('TESTCONF', 'NOSUCHTYPE:testiface:comment'), qr/no such parameter/, 
160     "handles bad subsection type");
161 like(amgetconf('TESTCONF', 'dumptype:NOSUCHDUMP:comment'), qr/no such parameter/, 
162     "handles bad dumptype namek");
163 like(amgetconf('TESTCONF', 'dumptype:testdump:NOSUCHPARAM'), qr/no such parameter/, 
164     "handles bad dumptype parameter name");
165
166 ##
167 # exclude lists are a bit funny, too
168
169 $testconf = Amconfig->new();
170 $testconf->add_dumptype("testdump", [
171     "exclude file optional" => '"f1"', # this optional will have no effect
172     "exclude file append" => '"f2"',
173     "exclude list" => '"l1"',
174     "exclude list append" => '"l2"',
175     "include file" => '"ifo"',
176     "include list optional" => '"ilo"',
177     ]);
178 $testconf->write();
179
180 is_deeply([sort(+split(qr/\n/, amgetconf('TESTCONF', 'dumptype:testdump:exclude')))],
181           [sort('FILE "f1" "f2"',
182                 'LIST "l1" "l2"')],
183     "exclude files and lists displayed correctly; a non-final optional is ignored");
184
185 is_deeply([sort(+split(qr/\n/, amgetconf('TESTCONF', 'dumptype:testdump:include')))],
186           [sort('FILE OPTIONAL "ifo"',
187                 'LIST OPTIONAL "ilo"')],
188     "a final 'OPTIONAL' makes the whole include/exclude optional")
189