05aea8530f7f06f622cf1f86cb3b6c27ed3deef2
[debian/amanda] / installcheck / amfetchdump.pl
1 # Copyright (c) 2005-2008 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 Mathlida Ave, Suite 300
17 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
18
19 use Test::More tests => 7;
20
21 use lib "@amperldir@";
22 use Installcheck::Config;
23 use Installcheck::Run qw(run run_get run_err $diskname);
24 use File::Path qw(rmtree mkpath);
25 use Amanda::Paths;
26 use Cwd;
27
28 my $testconf;
29 my $dumpok;
30
31 my $testdir = "$AMANDA_TMPDIR/amfetchdump-installcheck";
32 rmtree($testdir);
33 mkpath($testdir);
34
35 my $origdir = getcwd;
36 chdir($testdir);
37
38 sub cleandir {
39     for my $filename (<$testdir/*>) {
40         unlink($filename);
41     }
42 }
43
44 $testconf = Installcheck::Run::setup();
45 $testconf->add_param('label_new_tapes', '"TESTCONF%%"');
46 $testconf->add_dle("localhost $diskname installcheck-test");
47 $testconf->write();
48
49 run('amdump', 'TESTCONF')
50     or BAIL_OUT("amdump run failed");
51
52 like(run_err('amfetchdump', 'TESTCONF'),
53     qr{^Usage:},
54     "'amfetchdump TESTCONF' gives usage message on stderr");
55
56 SKIP: {
57     skip "Expect.pm not installed", 2
58         unless $Installcheck::Run::have_expect;
59
60     cleandir();
61
62     my $exp = Installcheck::Run::run_expect('amfetchdump', 'TESTCONF', 'localhost');
63     $exp->log_stdout(0);
64
65     my @results;
66     $exp->expect(60,
67         [ qr{1 tape\(s\) needed for restoration}, sub {
68             push @results, "tapes-needed";
69             exp_continue;
70         } ],
71         [ qr{amfetchdump: 1: restoring FILE: date [[:digit:]]+ host localhost disk .*},
72         sub {
73             push @results, "restoring";
74             exp_continue;
75         } ],
76         [ 'Press enter when ready', sub {
77             push @results, "press-enter";
78             $exp->send("\n");
79             exp_continue;
80         }, ],
81         [ 'eof', sub {
82             push @results, "eof";
83         }, ],
84     );
85     is_deeply([ @results ], [ "tapes-needed", "press-enter", "restoring", "eof" ],
86               "simple restore follows the correct steps");
87
88     my @filenames = <localhost.*>;
89     is(scalar @filenames, 1, "..and restored file is present in testdir")
90         or diag(join("\n", @filenames));
91 }
92
93 {
94     cleandir();
95
96     ok(run('amfetchdump', '-a', 'TESTCONF', 'localhost'),
97         "run with -a successful");
98
99     my @filenames = <localhost.*>;
100     is(scalar @filenames, 1, "..and restored file is present in testdir")
101         or diag(join("\n", @filenames));
102 }
103
104 SKIP: {
105     skip "Expect.pm not installed", 2
106         unless $Installcheck::Run::have_expect;
107
108     cleandir();
109     chdir($AMANDA_TMPDIR);
110
111     my $exp = Installcheck::Run::run_expect('amfetchdump', '-O', $testdir, 'TESTCONF', 'localhost');
112     $exp->log_stdout(0);
113
114     my @results;
115     $exp->expect(60,
116         [ qr{1 tape\(s\) needed for restoration}, sub {
117             push @results, "tapes-needed";
118             exp_continue;
119         } ],
120         [ qr{amfetchdump: 1: restoring FILE: date [[:digit:]]+ host localhost disk .*},
121         sub {
122             push @results, "restoring";
123             exp_continue;
124         } ],
125         [ 'Press enter when ready', sub {
126             push @results, "press-enter";
127             $exp->send("\n");
128             exp_continue;
129         }, ],
130         [ 'eof', sub {
131             push @results, "eof";
132         }, ],
133     );
134     is_deeply([ @results ], [ "tapes-needed", "press-enter", "restoring", "eof" ],
135               "restore with -O follows the correct steps");
136
137     chdir($testdir);
138     my @filenames = <localhost.*>;
139     is(scalar @filenames, 1, "..and restored file is present in testdir")
140         or diag(join("\n", @filenames));
141 }
142
143 # TODO:
144 # - test piping (-p),
145 # - test compression (-c and -C)
146 # - test a specified device (-d)
147 # - test splits (regular, -w, -n)
148
149 END {
150     chdir("$testdir/..");
151     rmtree($testdir);
152 }