Imported Upstream version 3.2.0
[debian/amanda] / installcheck / Amanda_Changer_compat.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 => 30;
20 use File::Path;
21 use strict;
22 use warnings;
23
24 use lib "@amperldir@";
25 use Installcheck;
26 use Installcheck::Config;
27 use Installcheck::Run;
28 use Installcheck::Changer;
29 use Amanda::Paths;
30 use Amanda::Device;
31 use Amanda::Debug qw( :logging );
32 use Amanda::MainLoop;
33 use Amanda::Config qw( :init :getconf config_dir_relative );
34 use Amanda::Changer;
35
36 # set up debugging so debug output doesn't interfere with test results
37 Amanda::Debug::dbopen("installcheck");
38 Installcheck::log_test_output();
39
40 # and disable Debug's die() and warn() overrides
41 Amanda::Debug::disable_die_override();
42
43 my $changer_filename = "$Installcheck::TMP/chg-test";
44 my $result_file = "$Installcheck::TMP/chg-test.result";
45
46 # Set up a 'test' changer; several of these are defined below.
47 sub setup_changer {
48     my ($changer_script) = @_;
49
50     open my $chg_test, ">", $changer_filename or die("Could not create test changer");
51
52     $changer_script =~ s/\$Installcheck::TMP/$Installcheck::TMP/g;
53
54     print $chg_test "#! /bin/sh\n";
55     print $chg_test $changer_script;
56
57     close $chg_test;
58     chmod 0755, $changer_filename;
59 }
60
61 # slurp the $result_file
62 sub slurp_result {
63     return '' unless (-r $result_file);
64
65     open(my $fh, "<", $result_file) or die("open $result_file: $!");
66     my $result = do { local $/; <$fh> };
67     close($fh);
68
69     return $result;
70 }
71
72 # Functions to invoke the changer and later verify the result
73 my ($check_res_cb, $check_finished_cb);
74 {
75     my $expected_err_info;
76     my $expected_dev;
77     my $msg;
78     my $quit;
79
80     $check_res_cb = make_cb('check_res_cb' => sub {
81         my ($err, $res) = @_;
82
83         if ($err) {
84             if (defined($expected_err_info)) {
85                 chg_err_like($err, $expected_err_info, $msg);
86             } else {
87                 fail($msg);
88                 diag("Unexpected error: $err");
89             }
90         } else {
91             if (defined($expected_dev)) {
92                 is($res->{'device'}->device_name, $expected_dev, $msg);
93             } else {
94                 fail($msg);
95                 diag("Unexpected reservation");
96             }
97         }
98
99         if ($res) {
100             $res->release(finished_cb => $quit);
101         } else {
102             $quit->();
103         }
104     });
105
106     $check_finished_cb = make_cb('check_finished_cb' => sub {
107         my ($err, $res) = @_;
108
109         if ($err) {
110             if (defined($expected_err_info)) {
111                 chg_err_like($err, $expected_err_info, $msg);
112             } else {
113                 fail($msg);
114                 diag("Unexpected error: $err");
115             }
116         } else {
117             if (!defined($expected_err_info)) {
118                 pass($msg);
119             } else {
120                 fail($msg);
121                 diag("Unexpected success");
122             }
123         }
124
125         if ($res) {
126             $res->release(finished_cb => $quit);
127         } else {
128             $quit->();
129         }
130     });
131
132     $quit = make_cb(quit => sub {
133         my ($err) = @_;
134         die $err if $err;
135
136         Amanda::MainLoop::quit();
137     });
138
139     sub try_run_changer {
140         my $sub;
141         ($sub, $expected_err_info, $expected_dev, $msg) = @_;
142
143         Amanda::MainLoop::call_later($sub);
144         Amanda::MainLoop::run();
145     }
146 }
147
148 # OK, let's get started with some simple stuff
149 setup_changer <<'EOC';
150 case "${1}" in
151     -slot)
152         case "${2}" in
153             1) echo "1 null:fake1"; exit 0;;
154             2) echo "<ignored> slot 2 is empty"; exit 1;;
155             3) echo "1"; exit 0;; # test missing 'device' portion
156             4) echo "1 bogus:dev"; exit 0;;
157             5) echo "<error> multiline error"; echo "line 2"; exit 1;;
158             current) echo "1 null:current"; exit 0;;
159             next) echo "1 null:next"; exit 0;;
160         esac;;
161     -reset)
162         echo "reset" > $Installcheck::TMP/chg-test.result
163         echo "reset ignored";;
164     -eject)
165         echo "eject" > $Installcheck::TMP/chg-test.result
166         echo "eject ignored";;
167     -clean)
168         echo "clean" > $Installcheck::TMP/chg-test.result
169         echo "clean ignored";;
170     -label)
171         case "${2}" in
172             foo?bar) echo "1 ok"; exit 0;;
173             *) echo "<error> bad label"; exit 1;;
174         esac;;
175     -info) echo "7 10 1 1"; exit 0;;
176     -search)
177         case "${2}" in
178             TAPE?01) echo "5 null:fakedev"; exit 0;;
179             fatal) echo "<error> game over"; exit 2;;
180             *) echo "<error> not found"; exit 1;;
181         esac;;
182 esac
183 EOC
184
185 # set up a config for this changer, implicitly using Amanda::Changer::Compat
186 my $testconf;
187 $testconf = Installcheck::Config->new();
188 $testconf->add_param("tpchanger", "\"$changer_filename\"");
189 $testconf->write();
190
191 my $cfg_result = config_init($CONFIG_INIT_EXPLICIT_NAME, 'TESTCONF');
192 if ($cfg_result != $CFGERR_OK) {
193     my ($level, @errors) = Amanda::Config::config_errors();
194     die(join "\n", @errors);
195 }
196
197 my $chg = Amanda::Changer->new();
198 die($chg) if $chg->isa("Amanda::Changer::Error");
199
200 try_run_changer(
201     sub { $chg->load(label => 'TAPE-01', res_cb => $check_res_cb); },
202     undef,
203     "null:fakedev",
204     "search by label succeeds");
205
206 try_run_changer(
207     sub { $chg->load(label => 'TAPE-99', res_cb => $check_res_cb); },
208     { message => "not found", type => 'failed', reason => 'notfound' },
209     undef,
210     "search by label; nonexistent tape");
211
212 try_run_changer(
213     sub { $chg->load(slot => '1', res_cb => $check_res_cb); },
214     undef,
215     "null:fake1",
216     "search by slot");
217
218 try_run_changer(
219     sub { $chg->load(slot => '2', res_cb => $check_res_cb); },
220     { message => "slot 2 is empty", type => 'failed', reason => 'notfound' },
221     undef,
222     "search by slot; empty slot");
223
224 try_run_changer(
225     sub { $chg->load(slot => '3', res_cb => $check_res_cb); },
226     { message => "changer script did not provide a device name", type => 'fatal' },
227     undef,
228     "search by slot; no device in response");
229
230 try_run_changer(
231     sub { $chg->load(slot => '1', res_cb => $check_res_cb); },
232     { message => "changer script did not provide a device name", type => 'fatal' },
233     undef,
234     "fatal error is sticky");
235
236 $chg->{'fatal_error'} = undef; # reset the fatal error
237
238 try_run_changer(
239     sub { $chg->load(slot => '4', res_cb => $check_res_cb); },
240     { message => "opening 'bogus:dev': Device type bogus is not known.",
241       type => 'failed',
242       reason => 'device' },
243     undef,
244     "search by slot; bogus device leads to 'failed' error");
245
246 $chg->{'fatal_error'} = undef; # reset the fatal error
247
248 try_run_changer(
249     sub { $chg->load(slot => '5', res_cb => $check_res_cb); },
250     { message => "multiline error\nline 2",
251       type => 'failed',
252       reason => 'notfound' },
253     undef,
254     "multiline error response captured in its entirety");
255
256 $chg->{'fatal_error'} = undef; # reset the fatal error
257
258 try_run_changer(
259     sub { $chg->load(label => 'fatal', res_cb => $check_res_cb); },
260     { message => "game over", type => 'fatal' },
261     undef,
262     "search by label with fatal error");
263
264 # reset the fatal error
265 $chg->{'fatal_error'} = undef;
266
267 try_run_changer(
268     sub { $chg->eject(finished_cb => $check_finished_cb); },
269     undef, undef, "chg->eject doesn't fail");
270 like(slurp_result(), qr/eject/, ".. and calls chg-test -eject");
271
272 try_run_changer(
273     sub { $chg->reset(finished_cb => $check_finished_cb); },
274     undef, undef, "chg->reset doesn't fail");
275 like(slurp_result(), qr/reset/, ".. and calls chg-test -reset");
276
277 try_run_changer(
278     sub { $chg->clean(finished_cb => $check_finished_cb); },
279     undef, undef, "chg->clean doesn't fail");
280 like(slurp_result(), qr/clean/, ".. and calls chg-test -clean");
281
282 try_run_changer(
283     sub { $chg->update(finished_cb => $check_finished_cb); },
284     undef, undef, "chg->update doesn't fail");
285
286 try_run_changer(
287     sub { $chg->inventory(inventory_cb => $check_finished_cb); },
288     { message => "'chg-compat:' does not support inventory",
289             type => 'failed', reason => 'notimpl' },
290     undef,
291     "inventory not implemented");
292
293
294 # make sure only one reservation can be held at once
295 {
296     my $first_res;
297
298     my ($load_1, $load_2, $check_load_2, $check_eject);
299
300     $load_1 = make_cb('load_1' => sub {
301         $chg->load(slot => 1, res_cb => $load_2);
302     });
303
304     $load_2 = make_cb('load_2' => sub {
305         my ($err, $res) = @_;
306         die $err if ($err);
307
308         # keep this in scope through the next load
309         $first_res = $res;
310
311         $chg->load(slot => 2, res_cb => $check_load_2);
312     });
313
314     $check_load_2 = make_cb('check_load_2' => sub {
315         my ($err, $res) = @_;
316
317         like($err, qr/Changer is already reserved/,
318             "mulitple simultaneous reservations not alowed");
319
320         $first_res->release(eject => 1, finished_cb => $check_eject);
321     });
322
323     $check_eject = make_cb('check_eject' => sub {
324         my ($err) = @_;
325
326         ok(!defined $err, "release with eject succeeds");
327
328         like(slurp_result(), qr/eject/, "..and calls chg-test -eject");
329
330         Amanda::MainLoop::quit();
331     });
332
333     $load_1->();
334     Amanda::MainLoop::run();
335 }
336
337 ## check chg-disk
338
339 # Installcheck::Run sets up the whole chg-disk thing for us
340 $testconf = Installcheck::Run::setup();
341 $testconf->write();
342
343 $cfg_result = config_init($CONFIG_INIT_EXPLICIT_NAME, 'TESTCONF');
344 if ($cfg_result != $CFGERR_OK) {
345     my ($level, @errors) = Amanda::Config::config_errors();
346     die(join "\n", @errors);
347 }
348
349 $chg = Amanda::Changer->new();
350 die($chg) if $chg->isa("Amanda::Changer::Error");
351
352 {
353     my $res;
354     my ($get_info, $load_current, $label_current, $load_next,
355         $released1, $release_next, $load_by_label, $check_by_label);
356
357     $get_info = make_cb('get_info' => sub {
358         $chg->info(info_cb => $load_current, info => [ 'num_slots', 'fast_search' ]);
359     });
360
361     $load_current = make_cb('load_current' => sub {
362         my $err = shift;
363         my %results = @_;
364         die($err) if defined($err);
365
366         is_deeply({ %results },
367             { num_slots => 3, fast_search => 0 }, # old chg-disk is not searchable
368             "info() returns the correct num_slots and fast_search");
369
370         $chg->load(slot => "1", res_cb => $label_current);
371     });
372
373     $label_current = make_cb('label_current' => sub {
374         (my $err, $res) = @_;
375         die $err if ($err);
376
377         pass("seek to current slot succeeded");
378
379         my $dev = $res->{'device'};
380         $dev->start($Amanda::Device::ACCESS_WRITE, "TESTCONF18", undef)
381             or die $dev->error_or_status();
382         $dev->finish()
383             or die $dev->error_or_status();
384
385         is($res->{'this_slot'}, "1", "this slot is '1'");
386         $res->set_label(label => "TESTCONF18", finished_cb => $load_next);
387     });
388
389     $load_next = make_cb('load_next' => sub {
390         my ($err) = @_;
391         die $err if ($err);
392
393         pass("set_label succeeded");
394
395         $res->release(finished_cb => $released1);
396     });
397
398     $released1 = make_cb(released1 => sub {
399         my ($err) = @_;
400         die $err if $err;
401
402         $chg->load(relative_slot => "next", res_cb => $release_next);
403     });
404
405     $release_next = make_cb('release_next' => sub {
406         (my $err, $res) = @_;
407         die $err if ($err);
408
409         pass("load relative slot 'next' succeeded");
410
411         $res->release(finished_cb => $load_by_label);
412     });
413
414     $load_by_label = make_cb('load_by_label' => sub {
415         my ($err) = @_;
416         die $err if ($err);
417
418         pass("release loaded");
419
420         $chg->load(label => "TESTCONF18", res_cb => $check_by_label);
421     });
422
423     $check_by_label = make_cb('check_by_label' => sub {
424         (my $err, $res) = @_;
425         die $err if ($err);
426
427         pass("load by label succeeded");
428
429         my $dev = $res->{'device'};
430         $dev->read_label() == 0
431             or die $dev->error_or_status();
432
433         is($dev->volume_label(), "TESTCONF18",
434             "..and finds the right volume");
435
436         $res->release(finished_cb => sub {
437             my ($err) = @_;
438             die $err if $err;
439
440             Amanda::MainLoop::quit();
441         });
442     });
443
444     $get_info->();
445     Amanda::MainLoop::run();
446 }
447
448 # test two simultaneous invocations of info()
449
450 $chg = Amanda::Changer->new();
451 die($chg) if $chg->isa("Amanda::Changer::Error");
452
453 sub test_get_infos {
454     my ($finished_cb) = @_;
455     my $n_info_results = 0;
456
457     my $steps = define_steps
458         cb_ref => \$finished_cb;
459
460     step get_infos => sub {
461         # convince the changer that it has not gotten any info yet
462         $chg->{'got_info'} = 0;
463
464         $chg->info(info_cb => $steps->{'got_info_result'}, info => [ 'num_slots' ]);
465         $chg->info(info_cb => $steps->{'got_info_result'}, info => [ 'fast_search' ]);
466     };
467
468     step got_info_result => sub {
469         my ($err, %info) = @_;
470         die $err if $err;
471         ++$n_info_results;
472         if ($n_info_results >= 2) {
473             pass("two simultaneous info() invocations are successful");
474             $finished_cb->();
475         }
476     };
477 }
478 test_get_infos(\&Amanda::MainLoop::quit);
479 Amanda::MainLoop::run();
480
481 # scan the changer using except_slots
482 sub test_except_slots {
483     my ($finished_cb) = @_;
484     my $slot;
485     my %except_slots;
486
487     my $steps = define_steps
488         cb_ref => \$finished_cb;
489
490     step start => sub {
491         $chg->load(relative_slot => "current",
492                    except_slots => { %except_slots },
493                    res_cb => $steps->{'loaded'});
494     };
495
496     step loaded => sub {
497         my ($err, $res) = @_;
498         if ($err) {
499             if ($err->notfound) {
500                 # this means the scan is done
501                 return $steps->{'quit'}->();
502             } elsif ($err->volinuse and defined $err->{'slot'}) {
503                 $slot = $err->{'slot'};
504             } else {
505                 die $err;
506             }
507         } else {
508             $slot = $res->{'this_slot'};
509         }
510
511         $except_slots{$slot} = 1;
512
513         if ($res) {
514             $res->release(finished_cb => $steps->{'released'});
515         } else {
516             $steps->{'released'}->();
517         }
518     };
519
520     step released => sub {
521         my ($err) = @_;
522         die $err if $err;
523
524         $chg->load(relative_slot => 'next', slot => $slot,
525                    except_slots => { %except_slots },
526                    res_cb => $steps->{'loaded'});
527     };
528
529     step quit => sub {
530         is_deeply({ %except_slots }, { 1=>1, 2=>1, 3=>1 },
531                 "scanning with except_slots works");
532         $finished_cb->();
533     };
534 }
535 test_except_slots(\&Amanda::MainLoop::quit);
536 Amanda::MainLoop::run();
537
538 unlink($changer_filename);
539 unlink($result_file);