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;
194 my $testconf = Installcheck::Config->new();
196 $nb_slot = 3 if !defined $nb_slot;
197 (-d $diskname) or setup_backmeup();
199 setup_new_vtapes($testconf, $nb_slot);
201 setup_vtapes($testconf, $nb_slot);
203 setup_holding($testconf, 25);
204 setup_disklist($testconf);
209 # create the 'backmeup' data
211 my $dir_structure = {
212 '1megabyte' => 1024*1024,
227 mkpath($diskname) or die("Could not create $name");
229 # pick a file for 'random' data -- /dev/urandom or, failing that,
230 # Amanda's ChangeLog.
231 my $randomfile = "/dev/urandom";
232 if (!-r $randomfile) {
233 $randomfile = "../ChangeLog";
238 my ($parent, $contents) = @_;
239 while (my ($name, $val) = each(%$contents)) {
240 my $name = "$parent/$name";
241 if (ref($val) eq 'HASH') {
242 mkpath($name) or die("Could not create $name");
243 $create->($name, $val);
245 my $bytes_needed = $val+0;
246 open(my $wfd, ">", $name) or die("Could not open $name: $!");
248 # read bytes from a source file as a source of "random" data..
249 while ($bytes_needed) {
251 if (!defined($rfd)) {
252 open($rfd, "<", "$randomfile") or die("Could not open $randomfile");
254 my $to_read = $bytes_needed>10240? 10240:$bytes_needed;
255 my $bytes_read = sysread($rfd, $buf, $to_read);
257 if ($bytes_read < $to_read) {
262 $bytes_needed -= $bytes_read;
268 $create->($diskname, $dir_structure);
272 my ($testconf, $ntapes) = @_;
277 # make each of the tape directories
278 for (my $i = 1; $i < $ntapes+1; $i++) {
279 my $tapepath = "$taperoot/slot$i";
285 # set up the appropriate configuration
286 $testconf->add_param("tapedev", "\"file:$taperoot\"");
287 $testconf->add_param("tpchanger", "\"chg-disk\"");
288 $testconf->add_param("changerfile", "\"$CONFIG_DIR/TESTCONF/ignored-filename\"");
289 $testconf->add_param("labelstr", "\"TESTCONF[0-9][0-9]\"");
290 $testconf->add_param("tapecycle", "$ntapes");
292 # this overwrites the existing TEST-TAPE tapetype
293 $testconf->add_tapetype('TEST-TAPE', [
294 'length' => '30 mbytes',
295 'filemark' => '4 kbytes',
299 sub setup_new_vtapes {
300 my ($testconf, $ntapes) = @_;
305 # make each of the tape directories
306 for (my $i = 1; $i < $ntapes+1; $i++) {
307 my $tapepath = "$taperoot/slot$i";
313 # set up the appropriate configuration
314 $testconf->add_param("tpchanger", "\"chg-disk:$taperoot\"");
315 $testconf->add_param("labelstr", "\"TESTCONF[0-9][0-9]\"");
316 $testconf->add_param("tapecycle", "$ntapes");
318 # this overwrites the existing TEST-TAPE tapetype
319 $testconf->add_tapetype('TEST-TAPE', [
320 'length' => '30 mbytes',
321 'filemark' => '4 kbytes',
326 my ($testconf, $mbytes) = @_;
328 if (-d $holdingdir) {
333 $testconf->add_holdingdisk("hd1", [
334 'directory' => "\"$holdingdir\"",
335 'use' => "$mbytes mbytes",
336 'chunksize' => "1 mbyte",
343 $testconf->add_dumptype("installcheck-test", [
345 'compress' => 'none',
346 'program' => '"GNUTAR"',
352 if (defined($slot)) {
353 return "$taperoot/slot$slot";
362 # make the data/ symlink from our taperoot
363 unlink("$taperoot/data");
364 symlink(vtape_dir($slot), "$taperoot/data")
365 or die("Could not create 'data' symlink: $!");
373 my $errtempfile = "$Installcheck::TMP/stderr$$.out";
375 # use a temporary file for error output -- this eliminates synchronization
376 # problems between reading stderr and stdout
377 local (*INFH, *OUTFH, *ERRFH);
378 open(ERRFH, ">", $errtempfile);
380 $app = "$sbindir/$app" unless ($app =~ qr{/});
381 my $pid = IPC::Open3::open3("INFH", "OUTFH", ">&ERRFH",
384 # immediately close the child's stdin
387 # read from stdout until it's closed
388 $stdout = do { local $/; <OUTFH> };
391 # and wait for the kid to die
392 waitpid $pid, 0 or croak("Error waiting for child process to die: $@");
396 # fetch stderr from the temporary file
397 $stderr = slurp($errtempfile);
398 unlink($errtempfile);
400 # and return true if the exit status was zero
401 $exit_code = $status >> 8;
402 return WIFEXITED($status) && $exit_code == 0;
408 # prefer to put stderr in the output
410 $detail .= "\nstderr is:\n$stderr";
412 if ($stdout and length($stdout) < 1024) {
413 $detail .= "\nstdout is:\n$stdout";
416 Test::More::diag("run unexpectedly failed; no output to compare$detail");
427 my $detail = "\nstderr is:\n$stderr";
428 Test::More::diag("run unexpectedly failed; no output to compare$detail");
439 Test::More::diag("run unexpectedly succeeded; no output to compare");
449 Installcheck::Config::cleanup();
454 if (-d $holdingdir) {
463 die "Expect.pm not found" unless $have_expect;
465 $app = "$sbindir/$app" unless ($app =~ qr{^/});
466 my $exp = Expect->new("$app", @args);
474 # try running amreport
475 my $report = "failure-report.txt";
477 my @logfiles = <$CONFIG_DIR/TESTCONF/log/log.*>;
479 run('amreport', 'TESTCONF', '-f', $report, '-l', $logfiles[$#logfiles]);
481 open(my $fh, "<", $report) or return;
482 for my $line (<$fh>) {
483 Test::More::diag($line);
490 # maybe there was a config error
491 config_init($CONFIG_INIT_EXPLICIT_NAME, "TESTCONF");
492 my ($cfgerr_level, @cfgerr_errors) = config_errors();
493 if ($cfgerr_level >= $CFGERR_WARNINGS) {
494 foreach (@cfgerr_errors) {
495 Test::More::diag($_);
501 Test::More::diag("no amreport available, and no config errors");
505 Test::More::BAIL_OUT($msg);
507 die("amdump failed; cannot continue");