Imported Upstream version 3.2.0
[debian/amanda] / installcheck / amrecover.pl
1 # Copyright (c) 2008, 2009, 2010 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 => 3;
20
21 use lib "@amperldir@";
22 use Installcheck;
23 use Installcheck::Config;
24 use Installcheck::Run qw(run run_get run_err $diskname);
25 use Installcheck::Dumpcache;
26
27 use File::Path qw(rmtree mkpath);
28 use Data::Dumper;
29 use Sys::Hostname;
30
31 use Amanda::Paths;
32 use Amanda::Header;
33 use Amanda::Debug;
34 use warnings;
35 use strict;
36 no strict 'subs';
37
38 if (!$Installcheck::Run::have_expect) {
39     SKIP: {
40         skip("Expect.pm not available",
41             Test::More->builder->expected_tests);
42     }
43     exit 0;
44 }
45
46 # amrecover can successfully set the host if its hostname is localhost
47 my $hostname = hostname;
48 my $set_host_succeed = 0;
49 $set_host_succeed=1 if (   ($hostname eq "localhost")
50                         or ($hostname =~ /localhost\./));
51
52 my $debug = !exists $ENV{'HARNESS_ACTIVE'};
53 diag("logging amrecover conversations to stdout because Test::Harness not in use")
54     if $debug;
55
56 my @results;
57 my $testdir = "$Installcheck::TMP/amfetchdump-installcheck/files";
58 rmtree($testdir);
59 mkpath($testdir);
60 chdir($testdir);
61
62 sub cleandir {
63     for my $filename (<$testdir/*>) {
64         unlink($filename);
65     }
66 }
67
68 sub mkcont($) {
69     my ($msg) = @_;
70     sub {
71         push @results, $msg;
72         exp_continue;
73     };
74 }
75
76 sub run_amrecover {
77     my %params = @_;
78     my ($exp, $continued);
79     my @commands = @{$params{'commands'}};
80
81     cleandir();
82     my @h_opt = ('-h', 'localhost') unless $set_host_succeed;
83     $exp = Installcheck::Run::run_expect('amrecover', 'TESTCONF',
84         @h_opt, '-s', 'localhost', '-t', 'localhost', '-o', 'auth=local');
85     $exp->log_stdout($debug);
86
87     @results = ();
88     $exp->expect(60,
89         [ qr{220.*ready\.}, mkcont "server-ready" ],
90         [ qr{200 Config set to TESTCONF\.}, mkcont "config-set" ],
91         [ qr{Use the sethost command}, mkcont "use-sethost" ],
92         [ qr{syntax error}, mkcont "syntax-err" ],
93         [ qr{Invalid command:}, mkcont "invalid-cmd" ],
94         [ qr{200 Dump host set}, mkcont "host-set" ],
95         [ qr{200 Disk set to}, mkcont "disk-set" ],
96         [ qr{Added file /1kilobyte}, mkcont "added-file" ],
97         [ qr{The following tapes are needed:}, mkcont "tapes-needed" ],
98         [ qr{Load tape \S+ now}, mkcont "load-tape" ],
99         [ qr{Restoring files into directory}, mkcont "restoring-into" ],
100         [ qr{All existing files.*can be deleted}, mkcont "can-delete" ],
101         [ qr{\./1kilobyte}, mkcont "restored-file" ],
102         [ qr{200 Good bye}, mkcont "bye" ],
103
104         [ qr{amrecover> }, sub {
105             my $cmd = shift @commands or die "out of commands!";
106             push @results, "> $cmd";
107             $exp->send("$cmd\n");
108             exp_continue;
109         } ],
110         [ qr{Continue \[\?/Y/n/s/d\]\?}, sub {
111             die "multiple Continue requests" if $continued;
112             $continued = 1;
113
114             push @results, "> continue-Y";
115             $exp->send("Y\n");
116             exp_continue;
117         } ],
118         [ qr{Continue \[\?/Y/n\]\?}, sub { # the "all files" question
119             push @results, "> deletall-Y";
120             $exp->send("Y\n");
121             exp_continue;
122         } ],
123     );
124     return @results;
125 }
126
127 ## plain vanilla amrecover run
128
129 Installcheck::Dumpcache::load("basic");
130
131 run_amrecover(
132         commands => [
133             "sethost localhost",
134             "setdisk $diskname",
135             "add 1kilobyte",
136             "extract",
137             "quit",
138         ]);
139
140 is_deeply([ @results ], [
141         'server-ready', 'config-set',
142         'host-set',
143         '> sethost localhost',
144         'host-set',
145         "> setdisk $diskname",
146         'disk-set',
147         '> add 1kilobyte',
148         'added-file',
149         '> extract',
150         'tapes-needed', 'load-tape',
151         '> continue-Y',
152         'restoring-into', 'can-delete',
153         '> deletall-Y',
154         'restored-file',
155         '> quit',
156         'bye'
157     ],
158     "simple restore follows the correct steps")
159     or diag Dumper([@results]);
160
161 ok((-f "1kilobyte" and ! -z "1kilobyte"),
162     "..restored file appears in current directory");
163
164 ## parser check
165
166 run_amrecover(
167         commands => [
168             "sethost localhost ", # <-- note extra space
169             "sethost localhost",
170             "quit",
171         ]);
172
173 is_deeply([ @results ], [
174         'server-ready', 'config-set',
175         'host-set',
176         '> sethost localhost ',
177         'host-set',
178         '> sethost localhost',
179         'host-set',
180         '> quit',
181         'bye'
182     ],
183     "check trailling space")
184     or die Dumper([@results]);
185
186 ## cleanup
187
188 chdir("/");
189 rmtree $testdir;
190 Installcheck::Run::cleanup();