Imported Upstream version 3.1.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     $exp = Installcheck::Run::run_expect('amrecover', 'TESTCONF',
83         '-t', 'localhost', '-o', 'auth=local');
84     $exp->log_stdout($debug);
85
86     @results = ();
87     $exp->expect(60,
88         [ qr{220.*ready\.}, mkcont "server-ready" ],
89         [ qr{200 Config set to TESTCONF\.}, mkcont "config-set" ],
90         [ qr{Use the sethost command}, mkcont "use-sethost" ],
91         [ qr{syntax error}, mkcont "syntax-err" ],
92         [ qr{Invalid command:}, mkcont "invalid-cmd" ],
93         [ qr{200 Dump host set}, mkcont "host-set" ],
94         [ qr{200 Disk set to}, mkcont "disk-set" ],
95         [ qr{Added file /1kilobyte}, mkcont "added-file" ],
96         [ qr{The following tapes are needed:}, mkcont "tapes-needed" ],
97         [ qr{Load tape \S+ now}, mkcont "load-tape" ],
98         [ qr{Restoring files into directory}, mkcont "restoring-into" ],
99         [ qr{All existing files.*can be deleted}, mkcont "can-delete" ],
100         [ qr{\./1kilobyte}, mkcont "restored-file" ],
101         [ qr{200 Good bye}, mkcont "bye" ],
102
103         [ qr{amrecover> }, sub {
104             my $cmd = shift @commands or die "out of commands!";
105             push @results, "> $cmd";
106             $exp->send("$cmd\n");
107             exp_continue;
108         } ],
109         [ qr{Continue \[\?/Y/n/s/d\]\?}, sub {
110             die "multiple Continue requests" if $continued;
111             $continued = 1;
112
113             push @results, "> continue-Y";
114             $exp->send("Y\n");
115             exp_continue;
116         } ],
117         [ qr{Continue \[\?/Y/n\]\?}, sub { # the "all files" question
118             push @results, "> deletall-Y";
119             $exp->send("Y\n");
120             exp_continue;
121         } ],
122     );
123     return @results;
124 }
125
126 ## plain vanilla amrecover run
127
128 Installcheck::Dumpcache::load("basic");
129
130 run_amrecover(
131         commands => [
132             "sethost localhost",
133             "setdisk $diskname",
134             "add 1kilobyte",
135             "extract",
136             "quit",
137         ]);
138
139 is_deeply([ @results ], [
140         'server-ready', 'config-set',
141         ($set_host_succeed) ? 'host-set' : 'use-sethost',
142         '> sethost localhost',
143         'host-set',
144         "> setdisk $diskname",
145         'disk-set',
146         '> add 1kilobyte',
147         'added-file',
148         '> extract',
149         'tapes-needed', 'load-tape',
150         '> continue-Y',
151         'restoring-into', 'can-delete',
152         '> deletall-Y',
153         'restored-file',
154         '> quit',
155         'bye'
156     ],
157     "simple restore follows the correct steps")
158     or diag Dumper([@results]);
159
160 ok((-f "1kilobyte" and ! -z "1kilobyte"),
161     "..restored file appears in current directory");
162
163 ## parser check
164
165 run_amrecover(
166         commands => [
167             "sethost localhost ", # <-- note extra space
168             "sethost localhost",
169             "quit",
170         ]);
171
172 is_deeply([ @results ], [
173         'server-ready', 'config-set',
174         ($set_host_succeed) ? 'host-set' : 'use-sethost',
175         '> sethost localhost ',
176         'host-set',
177         '> sethost localhost',
178         'host-set',
179         '> quit',
180         'bye'
181     ],
182     "check trailling space")
183     or die Dumper([@results]);
184
185 ## cleanup
186
187 chdir("/");
188 rmtree $testdir;
189 Installcheck::Run::cleanup();