1 # Copyright (c) 2008, 2009, 2010 Zmanda, Inc. All Rights Reserved.
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.
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
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
16 # Contact information: Zmanda Inc, 465 S. Mathilda Ave., Suite 300
17 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
19 use Test::More tests => 30;
24 use lib "@amperldir@";
26 use Installcheck::Config;
27 use Installcheck::Run;
28 use Installcheck::Changer;
31 use Amanda::Debug qw( :logging );
33 use Amanda::Config qw( :init :getconf config_dir_relative );
36 # set up debugging so debug output doesn't interfere with test results
37 Amanda::Debug::dbopen("installcheck");
38 Installcheck::log_test_output();
40 # and disable Debug's die() and warn() overrides
41 Amanda::Debug::disable_die_override();
43 my $changer_filename = "$Installcheck::TMP/chg-test";
44 my $result_file = "$Installcheck::TMP/chg-test.result";
46 # Set up a 'test' changer; several of these are defined below.
48 my ($changer_script) = @_;
50 open my $chg_test, ">", $changer_filename or die("Could not create test changer");
52 $changer_script =~ s/\$Installcheck::TMP/$Installcheck::TMP/g;
54 print $chg_test "#! /bin/sh\n";
55 print $chg_test $changer_script;
58 chmod 0755, $changer_filename;
61 # slurp the $result_file
63 return '' unless (-r $result_file);
65 open(my $fh, "<", $result_file) or die("open $result_file: $!");
66 my $result = do { local $/; <$fh> };
72 # Functions to invoke the changer and later verify the result
73 my ($check_res_cb, $check_finished_cb);
75 my $expected_err_info;
80 $check_res_cb = make_cb('check_res_cb' => sub {
84 if (defined($expected_err_info)) {
85 chg_err_like($err, $expected_err_info, $msg);
88 diag("Unexpected error: $err");
91 if (defined($expected_dev)) {
92 is($res->{'device'}->device_name, $expected_dev, $msg);
95 diag("Unexpected reservation");
100 $res->release(finished_cb => $quit);
106 $check_finished_cb = make_cb('check_finished_cb' => sub {
107 my ($err, $res) = @_;
110 if (defined($expected_err_info)) {
111 chg_err_like($err, $expected_err_info, $msg);
114 diag("Unexpected error: $err");
117 if (!defined($expected_err_info)) {
121 diag("Unexpected success");
126 $res->release(finished_cb => $quit);
132 $quit = make_cb(quit => sub {
136 Amanda::MainLoop::quit();
139 sub try_run_changer {
141 ($sub, $expected_err_info, $expected_dev, $msg) = @_;
143 Amanda::MainLoop::call_later($sub);
144 Amanda::MainLoop::run();
148 # OK, let's get started with some simple stuff
149 setup_changer <<'EOC';
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;;
162 echo "reset" > $Installcheck::TMP/chg-test.result
163 echo "reset ignored";;
165 echo "eject" > $Installcheck::TMP/chg-test.result
166 echo "eject ignored";;
168 echo "clean" > $Installcheck::TMP/chg-test.result
169 echo "clean ignored";;
172 foo?bar) echo "1 ok"; exit 0;;
173 *) echo "<error> bad label"; exit 1;;
175 -info) echo "7 10 1 1"; exit 0;;
178 TAPE?01) echo "5 null:fakedev"; exit 0;;
179 fatal) echo "<error> game over"; exit 2;;
180 *) echo "<error> not found"; exit 1;;
185 # set up a config for this changer, implicitly using Amanda::Changer::Compat
187 $testconf = Installcheck::Config->new();
188 $testconf->add_param("tpchanger", "\"$changer_filename\"");
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);
197 my $chg = Amanda::Changer->new();
198 die($chg) if $chg->isa("Amanda::Changer::Error");
201 sub { $chg->load(label => 'TAPE-01', res_cb => $check_res_cb); },
204 "search by label succeeds");
207 sub { $chg->load(label => 'TAPE-99', res_cb => $check_res_cb); },
208 { message => "not found", type => 'failed', reason => 'notfound' },
210 "search by label; nonexistent tape");
213 sub { $chg->load(slot => '1', res_cb => $check_res_cb); },
219 sub { $chg->load(slot => '2', res_cb => $check_res_cb); },
220 { message => "slot 2 is empty", type => 'failed', reason => 'notfound' },
222 "search by slot; empty slot");
225 sub { $chg->load(slot => '3', res_cb => $check_res_cb); },
226 { message => "changer script did not provide a device name", type => 'fatal' },
228 "search by slot; no device in response");
231 sub { $chg->load(slot => '1', res_cb => $check_res_cb); },
232 { message => "changer script did not provide a device name", type => 'fatal' },
234 "fatal error is sticky");
236 $chg->{'fatal_error'} = undef; # reset the fatal error
239 sub { $chg->load(slot => '4', res_cb => $check_res_cb); },
240 { message => "opening 'bogus:dev': Device type bogus is not known.",
242 reason => 'device' },
244 "search by slot; bogus device leads to 'failed' error");
246 $chg->{'fatal_error'} = undef; # reset the fatal error
249 sub { $chg->load(slot => '5', res_cb => $check_res_cb); },
250 { message => "multiline error\nline 2",
252 reason => 'notfound' },
254 "multiline error response captured in its entirety");
256 $chg->{'fatal_error'} = undef; # reset the fatal error
259 sub { $chg->load(label => 'fatal', res_cb => $check_res_cb); },
260 { message => "game over", type => 'fatal' },
262 "search by label with fatal error");
264 # reset the fatal error
265 $chg->{'fatal_error'} = undef;
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");
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");
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");
283 sub { $chg->update(finished_cb => $check_finished_cb); },
284 undef, undef, "chg->update doesn't fail");
287 sub { $chg->inventory(inventory_cb => $check_finished_cb); },
288 { message => "'chg-compat:' does not support inventory",
289 type => 'failed', reason => 'notimpl' },
291 "inventory not implemented");
294 # make sure only one reservation can be held at once
298 my ($load_1, $load_2, $check_load_2, $check_eject);
300 $load_1 = make_cb('load_1' => sub {
301 $chg->load(slot => 1, res_cb => $load_2);
304 $load_2 = make_cb('load_2' => sub {
305 my ($err, $res) = @_;
308 # keep this in scope through the next load
311 $chg->load(slot => 2, res_cb => $check_load_2);
314 $check_load_2 = make_cb('check_load_2' => sub {
315 my ($err, $res) = @_;
317 like($err, qr/Changer is already reserved/,
318 "mulitple simultaneous reservations not alowed");
320 $first_res->release(eject => 1, finished_cb => $check_eject);
323 $check_eject = make_cb('check_eject' => sub {
326 ok(!defined $err, "release with eject succeeds");
328 like(slurp_result(), qr/eject/, "..and calls chg-test -eject");
330 Amanda::MainLoop::quit();
334 Amanda::MainLoop::run();
339 # Installcheck::Run sets up the whole chg-disk thing for us
340 $testconf = Installcheck::Run::setup();
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);
349 $chg = Amanda::Changer->new();
350 die($chg) if $chg->isa("Amanda::Changer::Error");
354 my ($get_info, $load_current, $label_current, $load_next,
355 $released1, $release_next, $load_by_label, $check_by_label);
357 $get_info = make_cb('get_info' => sub {
358 $chg->info(info_cb => $load_current, info => [ 'num_slots', 'fast_search' ]);
361 $load_current = make_cb('load_current' => sub {
364 die($err) if defined($err);
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");
370 $chg->load(slot => "1", res_cb => $label_current);
373 $label_current = make_cb('label_current' => sub {
374 (my $err, $res) = @_;
377 pass("seek to current slot succeeded");
379 my $dev = $res->{'device'};
380 $dev->start($Amanda::Device::ACCESS_WRITE, "TESTCONF18", undef)
381 or die $dev->error_or_status();
383 or die $dev->error_or_status();
385 is($res->{'this_slot'}, "1", "this slot is '1'");
386 $res->set_label(label => "TESTCONF18", finished_cb => $load_next);
389 $load_next = make_cb('load_next' => sub {
393 pass("set_label succeeded");
395 $res->release(finished_cb => $released1);
398 $released1 = make_cb(released1 => sub {
402 $chg->load(relative_slot => "next", res_cb => $release_next);
405 $release_next = make_cb('release_next' => sub {
406 (my $err, $res) = @_;
409 pass("load relative slot 'next' succeeded");
411 $res->release(finished_cb => $load_by_label);
414 $load_by_label = make_cb('load_by_label' => sub {
418 pass("release loaded");
420 $chg->load(label => "TESTCONF18", res_cb => $check_by_label);
423 $check_by_label = make_cb('check_by_label' => sub {
424 (my $err, $res) = @_;
427 pass("load by label succeeded");
429 my $dev = $res->{'device'};
430 $dev->read_label() == 0
431 or die $dev->error_or_status();
433 is($dev->volume_label(), "TESTCONF18",
434 "..and finds the right volume");
436 $res->release(finished_cb => sub {
440 Amanda::MainLoop::quit();
445 Amanda::MainLoop::run();
448 # test two simultaneous invocations of info()
450 $chg = Amanda::Changer->new();
451 die($chg) if $chg->isa("Amanda::Changer::Error");
454 my ($finished_cb) = @_;
455 my $n_info_results = 0;
457 my $steps = define_steps
458 cb_ref => \$finished_cb;
460 step get_infos => sub {
461 # convince the changer that it has not gotten any info yet
462 $chg->{'got_info'} = 0;
464 $chg->info(info_cb => $steps->{'got_info_result'}, info => [ 'num_slots' ]);
465 $chg->info(info_cb => $steps->{'got_info_result'}, info => [ 'fast_search' ]);
468 step got_info_result => sub {
469 my ($err, %info) = @_;
472 if ($n_info_results >= 2) {
473 pass("two simultaneous info() invocations are successful");
478 test_get_infos(\&Amanda::MainLoop::quit);
479 Amanda::MainLoop::run();
481 # scan the changer using except_slots
482 sub test_except_slots {
483 my ($finished_cb) = @_;
487 my $steps = define_steps
488 cb_ref => \$finished_cb;
491 $chg->load(relative_slot => "current",
492 except_slots => { %except_slots },
493 res_cb => $steps->{'loaded'});
497 my ($err, $res) = @_;
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'};
508 $slot = $res->{'this_slot'};
511 $except_slots{$slot} = 1;
514 $res->release(finished_cb => $steps->{'released'});
516 $steps->{'released'}->();
520 step released => sub {
524 $chg->load(relative_slot => 'next', slot => $slot,
525 except_slots => { %except_slots },
526 res_cb => $steps->{'loaded'});
530 is_deeply({ %except_slots }, { 1=>1, 2=>1, 3=>1 },
531 "scanning with except_slots works");
535 test_except_slots(\&Amanda::MainLoop::quit);
536 Amanda::MainLoop::run();
538 unlink($changer_filename);
539 unlink($result_file);