2 # Copyright (c) 2008, 2009, 2010 Zmanda, Inc. All Rights Reserved.
4 # This program is free software; you can redistribute it and/or modify it
5 # under the terms of the GNU General Public License version 2 as published
6 # by the Free Software Foundation.
8 # This program is distributed in the hope that it will be useful, but
9 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # You should have received a copy of the GNU General Public License along
14 # with this program; if not, write to the Free Software Foundation, Inc.,
15 # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # Contact information: Zmanda Inc, 465 S. Mathilda Ave., Suite 300
18 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
20 package Installcheck::Run;
24 Installcheck::Run - utilities to set up and run amanda dumps and restores
28 use Installcheck::Run;
30 my $testconf = Installcheck::Run::setup();
31 # make any modifications you'd like to the configuration
34 ok(Installcheck::Run::run('amdump', 'TESTCONF'), "amdump completes successfully");
36 # It's generally polite to clean up your mess, although the test
37 # framework will clean up if your tests crash
38 Installcheck::Run::cleanup();
41 skip "Expect.pm not installed", 7
42 unless $Installcheck::Run::have_expect;
44 my $exp = Installcheck::Run::run_expect('amflush', 'TESTCONF');
51 High-level tests generally depend on a full-scale run of Amanda --
52 a fairly messy project. This module simplifies that process by
53 abstracting away the mess. It takes care of:
57 =item Setting up a holding disk;
59 =item Setting up several vtapes; and
61 =item Setting up a DLE pointing to a reasonably-sized subdirectory of the build directory.
65 Most of this magic is in C<setup()>, which returns a configuration object from
66 C<Installcheck::Config>, allowing the test to modify that configuration before
67 writing it out. The hostname for the DLE is "localhost", and the disk name is
68 available in C<$Installcheck::Run::diskname>. This DLE has a subdirectory
69 C<dir> which can be used as a secondary, smaller DLE if needed.
71 This module also provides a convenient Perlish interface for running Amanda
72 commands: C<run($app, $args, ...)>. This function runs $app (from $sbindir if
73 $app is not an absolute path), and returns true if the application exited with
74 a status of zero. The stdout and stderr of the application are left in
75 C<$Installcheck::Run::stdout> and C<stderr>, respectively.
77 To check that a run is successful, and return its stdout (chomped), use
78 C<run_get($app, $args, ...)>. This function returns C<''> if the application
79 returns a nonzero exit status. Since many Amanda applications send normal
80 output to stderr, use C<run_get_err($app, $args, ...)> to check that a run is
81 successful and return its stderr. Similarly, C<run_err> checks that a run
82 returns a nonzero exit status, and then returns its stderr, chomped. If you
83 need both, use a bare C<run> and then check C<$stderr> and C<$stdout> as needed.
85 C<run> and friends can be used whether or not this module's C<setup>
88 Finally, C<cleanup()> cleans up from a run, deleting all backed-up
89 data, holding disks, and configuration. It's just good-neighborly
90 to call this before your test script exits.
94 This module sets up a configuration with three 30M vtapes, replete with
95 the proper vtape directories. These are controlled by C<chg-disk>.
96 The tapes are not labeled, and C<autolabel> is not set by
97 default, although C<labelstr> is set to C<TESTCONF[0-9][0-9]>.
99 The vtapes are created in <$Installcheck::Run::taperoot>, a subdirectory of
100 C<$Installcheck::TMP> for ease of later deletion. The subdirectory for each
101 slot is available from C<vtape_dir($slot)>, while the parent directory is
102 available from C<vtape_dir()>. C<load_vtape($slot)> will "load" the indicated
103 slot just like chg-disk would, and return the resulting path.
107 The holding disk is C<$Installcheck::Run::holdingdir>. It is a 15M holding disk,
108 with a chunksize of 1M (to help exercise the chunker).
112 The disklist is empty by default. Use something like the following
115 $testconf->add_dle("localhost $diskname installcheck-test");
117 The C<installcheck-test> dumptype specifies
122 but of course, it can be modified by the test module.
124 =head2 INTERACTIVE APPLICATIONS
126 This package provides a rudimentary wrapper around C<Expect.pm>, which is not
127 typically included in a perl installation. Consult C<$have_expect> to see if
128 this module is installed, and skip any Expect-based tests if it is not.
130 Otherwise, C<run_expect> takes arguments just like C<run>, but returns an Expect
131 object which you can use as you would like.
135 If your test runs 'amdump', a nonzero exit status may not be very helpful. The
136 function C<amdump_diag> will attempt to figure out what went wrong and display
137 useful information for the user via diag(). If it is given an argument, then
138 it will C<BAIL_OUT> with that message, causing L<Test::Harness> to stop running
139 tests. Otherwise, it will simply die(), which will only terminate this
140 particular test script.
145 use Installcheck::Config;
149 use Cwd qw(abs_path getcwd);
151 use POSIX qw( WIFEXITED );
153 use Amanda::Config qw( :init );
154 use Amanda::Util qw(slurp);
159 @EXPORT_OK = qw(setup
160 run run_get run_get_err run_err
162 $diskname $taperoot $holdingdir
163 $stdout $stderr $exit_code
165 amdump_diag run_expect );
166 @EXPORT = qw(exp_continue exp_continue_timeout);
179 *exp_continue = *ignore;
180 *exp_continue_timeout = *ignore;
186 # common paths (note that Installcheck::Dumpcache assumes these do not change)
187 our $diskname = "$Installcheck::TMP/backmeup";
188 our $taperoot = "$Installcheck::TMP/vtapes";
189 our $holdingdir ="$Installcheck::TMP/holding";
192 my $new_vtapes = shift;
193 my $testconf = Installcheck::Config->new();
195 (-d $diskname) or setup_backmeup();
197 setup_new_vtapes($testconf, 3);
199 setup_vtapes($testconf, 3);
201 setup_holding($testconf, 25);
202 setup_disklist($testconf);
207 # create the 'backmeup' data
209 my $dir_structure = {
210 '1megabyte' => 1024*1024,
225 mkpath($diskname) or die("Could not create $name");
227 # pick a file for 'random' data -- /dev/urandom or, failing that,
228 # Amanda's ChangeLog.
229 my $randomfile = "/dev/urandom";
230 if (!-r $randomfile) {
231 $randomfile = "../ChangeLog";
236 my ($parent, $contents) = @_;
237 while (my ($name, $val) = each(%$contents)) {
238 my $name = "$parent/$name";
239 if (ref($val) eq 'HASH') {
240 mkpath($name) or die("Could not create $name");
241 $create->($name, $val);
243 my $bytes_needed = $val+0;
244 open(my $wfd, ">", $name) or die("Could not open $name: $!");
246 # read bytes from a source file as a source of "random" data..
247 while ($bytes_needed) {
249 if (!defined($rfd)) {
250 open($rfd, "<", "$randomfile") or die("Could not open $randomfile");
252 my $to_read = $bytes_needed>10240? 10240:$bytes_needed;
253 my $bytes_read = sysread($rfd, $buf, $to_read);
255 if ($bytes_read < $to_read) {
260 $bytes_needed -= $bytes_read;
266 $create->($diskname, $dir_structure);
270 my ($testconf, $ntapes) = @_;
275 # make each of the tape directories
276 for (my $i = 1; $i < $ntapes+1; $i++) {
277 my $tapepath = "$taperoot/slot$i";
283 # set up the appropriate configuration
284 $testconf->add_param("tapedev", "\"file:$taperoot\"");
285 $testconf->add_param("tpchanger", "\"chg-disk\"");
286 $testconf->add_param("changerfile", "\"$CONFIG_DIR/TESTCONF/ignored-filename\"");
287 $testconf->add_param("labelstr", "\"TESTCONF[0-9][0-9]\"");
288 $testconf->add_param("tapecycle", "$ntapes");
290 # this overwrites the existing TEST-TAPE tapetype
291 $testconf->add_tapetype('TEST-TAPE', [
292 'length' => '30 mbytes',
293 'filemark' => '4 kbytes',
297 sub setup_new_vtapes {
298 my ($testconf, $ntapes) = @_;
303 # make each of the tape directories
304 for (my $i = 1; $i < $ntapes+1; $i++) {
305 my $tapepath = "$taperoot/slot$i";
311 # set up the appropriate configuration
312 $testconf->add_param("tpchanger", "\"chg-disk:$taperoot\"");
313 $testconf->add_param("labelstr", "\"TESTCONF[0-9][0-9]\"");
314 $testconf->add_param("tapecycle", "$ntapes");
316 # this overwrites the existing TEST-TAPE tapetype
317 $testconf->add_tapetype('TEST-TAPE', [
318 'length' => '30 mbytes',
319 'filemark' => '4 kbytes',
324 my ($testconf, $mbytes) = @_;
326 if (-d $holdingdir) {
331 $testconf->add_holdingdisk("hd1", [
332 'directory' => "\"$holdingdir\"",
333 'use' => "$mbytes mbytes",
334 'chunksize' => "1 mbyte",
341 $testconf->add_dumptype("installcheck-test", [
343 'compress' => 'none',
344 'program' => '"GNUTAR"',
350 if (defined($slot)) {
351 return "$taperoot/slot$slot";
360 # make the data/ symlink from our taperoot
361 unlink("$taperoot/data");
362 symlink(vtape_dir($slot), "$taperoot/data")
363 or die("Could not create 'data' symlink: $!");
371 my $errtempfile = "$Installcheck::TMP/stderr$$.out";
373 # use a temporary file for error output -- this eliminates synchronization
374 # problems between reading stderr and stdout
375 local (*INFH, *OUTFH, *ERRFH);
376 open(ERRFH, ">", $errtempfile);
378 $app = "$sbindir/$app" unless ($app =~ qr{/});
379 my $pid = IPC::Open3::open3("INFH", "OUTFH", ">&ERRFH",
382 # immediately close the child's stdin
385 # read from stdout until it's closed
386 $stdout = do { local $/; <OUTFH> };
389 # and wait for the kid to die
390 waitpid $pid, 0 or croak("Error waiting for child process to die: $@");
394 # fetch stderr from the temporary file
395 $stderr = slurp($errtempfile);
396 unlink($errtempfile);
398 # and return true if the exit status was zero
399 $exit_code = $status >> 8;
400 return WIFEXITED($status) && $exit_code == 0;
406 # prefer to put stderr in the output
408 $detail .= "\nstderr is:\n$stderr";
410 if ($stdout and length($stdout) < 1024) {
411 $detail .= "\nstdout is:\n$stdout";
414 Test::More::diag("run unexpectedly failed; no output to compare$detail");
425 my $detail = "\nstderr is:\n$stderr";
426 Test::More::diag("run unexpectedly failed; no output to compare$detail");
437 Test::More::diag("run unexpectedly succeeded; no output to compare");
447 Installcheck::Config::cleanup();
452 if (-d $holdingdir) {
461 die "Expect.pm not found" unless $have_expect;
463 $app = "$sbindir/$app" unless ($app =~ qr{^/});
464 my $exp = Expect->new("$app", @args);
472 # try running amreport
473 my $report = "failure-report.txt";
475 my @logfiles = <$CONFIG_DIR/TESTCONF/log/log.*>;
477 run('amreport', 'TESTCONF', '-f', $report, '-l', $logfiles[$#logfiles]);
479 open(my $fh, "<", $report) or return;
480 for my $line (<$fh>) {
481 Test::More::diag($line);
488 # maybe there was a config error
489 config_init($CONFIG_INIT_EXPLICIT_NAME, "TESTCONF");
490 my ($cfgerr_level, @cfgerr_errors) = config_errors();
491 if ($cfgerr_level >= $CFGERR_WARNINGS) {
492 foreach (@cfgerr_errors) {
493 Test::More::diag($_);
499 Test::More::diag("no amreport available, and no config errors");
503 Test::More::BAIL_OUT($msg);
505 die("amdump failed; cannot continue");