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