1 # Copyright (c) 2008-2012 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 => 31;
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");
200 is($chg->have_inventory(), '', "changer have inventory");
203 sub { $chg->load(label => 'TAPE-01', res_cb => $check_res_cb); },
206 "search by label succeeds");
209 sub { $chg->load(label => 'TAPE-99', res_cb => $check_res_cb); },
210 { message => "not found", type => 'failed', reason => 'notfound' },
212 "search by label; nonexistent tape");
215 sub { $chg->load(slot => '1', res_cb => $check_res_cb); },
221 sub { $chg->load(slot => '2', res_cb => $check_res_cb); },
222 { message => "slot 2 is empty", type => 'failed', reason => 'notfound' },
224 "search by slot; empty slot");
227 sub { $chg->load(slot => '3', res_cb => $check_res_cb); },
228 { message => "changer script did not provide a device name", type => 'fatal' },
230 "search by slot; no device in response");
233 sub { $chg->load(slot => '1', res_cb => $check_res_cb); },
234 { message => "changer script did not provide a device name", type => 'fatal' },
236 "fatal error is sticky");
238 $chg->{'fatal_error'} = undef; # reset the fatal error
241 sub { $chg->load(slot => '4', res_cb => $check_res_cb); },
242 { message => "opening 'bogus:dev': Device type bogus is not known.",
244 reason => 'device' },
246 "search by slot; bogus device leads to 'failed' error");
248 $chg->{'fatal_error'} = undef; # reset the fatal error
251 sub { $chg->load(slot => '5', res_cb => $check_res_cb); },
252 { message => "multiline error\nline 2",
254 reason => 'notfound' },
256 "multiline error response captured in its entirety");
258 $chg->{'fatal_error'} = undef; # reset the fatal error
261 sub { $chg->load(label => 'fatal', res_cb => $check_res_cb); },
262 { message => "game over", type => 'fatal' },
264 "search by label with fatal error");
266 # reset the fatal error
267 $chg->{'fatal_error'} = undef;
270 sub { $chg->eject(finished_cb => $check_finished_cb); },
271 undef, undef, "chg->eject doesn't fail");
272 like(slurp_result(), qr/eject/, ".. and calls chg-test -eject");
275 sub { $chg->reset(finished_cb => $check_finished_cb); },
276 undef, undef, "chg->reset doesn't fail");
277 like(slurp_result(), qr/reset/, ".. and calls chg-test -reset");
280 sub { $chg->clean(finished_cb => $check_finished_cb); },
281 undef, undef, "chg->clean doesn't fail");
282 like(slurp_result(), qr/clean/, ".. and calls chg-test -clean");
285 sub { $chg->update(finished_cb => $check_finished_cb); },
286 undef, undef, "chg->update doesn't fail");
289 sub { $chg->inventory(inventory_cb => $check_finished_cb); },
290 { message => "'chg-compat:' does not support inventory",
291 type => 'failed', reason => 'notimpl' },
293 "inventory not implemented");
296 # make sure only one reservation can be held at once
300 my ($load_1, $load_2, $check_load_2, $check_eject);
302 $load_1 = make_cb('load_1' => sub {
303 $chg->load(slot => 1, res_cb => $load_2);
306 $load_2 = make_cb('load_2' => sub {
307 my ($err, $res) = @_;
310 # keep this in scope through the next load
313 $chg->load(slot => 2, res_cb => $check_load_2);
316 $check_load_2 = make_cb('check_load_2' => sub {
317 my ($err, $res) = @_;
319 like($err, qr/Changer is already reserved/,
320 "mulitple simultaneous reservations not alowed");
322 $first_res->release(eject => 1, finished_cb => $check_eject);
325 $check_eject = make_cb('check_eject' => sub {
328 ok(!defined $err, "release with eject succeeds");
330 like(slurp_result(), qr/eject/, "..and calls chg-test -eject");
332 Amanda::MainLoop::quit();
336 Amanda::MainLoop::run();
341 # Installcheck::Run sets up the whole chg-disk thing for us
342 $testconf = Installcheck::Run::setup();
345 $cfg_result = config_init($CONFIG_INIT_EXPLICIT_NAME, 'TESTCONF');
346 if ($cfg_result != $CFGERR_OK) {
347 my ($level, @errors) = Amanda::Config::config_errors();
348 die(join "\n", @errors);
352 $chg = Amanda::Changer->new();
353 die($chg) if $chg->isa("Amanda::Changer::Error");
357 my ($get_info, $load_current, $label_current, $load_next,
358 $released1, $release_next, $load_by_label, $check_by_label);
360 $get_info = make_cb('get_info' => sub {
361 $chg->info(info_cb => $load_current, info => [ 'num_slots', 'fast_search' ]);
364 $load_current = make_cb('load_current' => sub {
367 die($err) if defined($err);
369 is_deeply({ %results },
370 { num_slots => 3, fast_search => 0 }, # old chg-disk is not searchable
371 "info() returns the correct num_slots and fast_search");
373 $chg->load(slot => "1", res_cb => $label_current);
376 $label_current = make_cb('label_current' => sub {
377 (my $err, $res) = @_;
380 pass("seek to current slot succeeded");
382 my $dev = $res->{'device'};
383 $dev->start($Amanda::Device::ACCESS_WRITE, "TESTCONF18", undef)
384 or die $dev->error_or_status();
386 or die $dev->error_or_status();
388 is($res->{'this_slot'}, "1", "this slot is '1'");
389 $res->set_label(label => "TESTCONF18", finished_cb => $load_next);
392 $load_next = make_cb('load_next' => sub {
396 pass("set_label succeeded");
398 $res->release(finished_cb => $released1);
401 $released1 = make_cb(released1 => sub {
405 $chg->load(relative_slot => "next", res_cb => $release_next);
408 $release_next = make_cb('release_next' => sub {
409 (my $err, $res) = @_;
412 pass("load relative slot 'next' succeeded");
414 $res->release(finished_cb => $load_by_label);
417 $load_by_label = make_cb('load_by_label' => sub {
421 pass("release loaded");
423 $chg->load(label => "TESTCONF18", res_cb => $check_by_label);
426 $check_by_label = make_cb('check_by_label' => sub {
427 (my $err, $res) = @_;
430 pass("load by label succeeded");
432 my $dev = $res->{'device'};
433 $dev->read_label() == 0
434 or die $dev->error_or_status();
436 is($dev->volume_label(), "TESTCONF18",
437 "..and finds the right volume");
439 $res->release(finished_cb => sub {
443 Amanda::MainLoop::quit();
448 Amanda::MainLoop::run();
452 # test two simultaneous invocations of info()
454 $chg = Amanda::Changer->new();
455 die($chg) if $chg->isa("Amanda::Changer::Error");
458 my ($finished_cb) = @_;
459 my $n_info_results = 0;
461 my $steps = define_steps
462 cb_ref => \$finished_cb;
464 step get_infos => sub {
465 # convince the changer that it has not gotten any info yet
466 $chg->{'got_info'} = 0;
468 $chg->info(info_cb => $steps->{'got_info_result'}, info => [ 'num_slots' ]);
469 $chg->info(info_cb => $steps->{'got_info_result'}, info => [ 'fast_search' ]);
472 step got_info_result => sub {
473 my ($err, %info) = @_;
476 if ($n_info_results >= 2) {
477 pass("two simultaneous info() invocations are successful");
482 test_get_infos(\&Amanda::MainLoop::quit);
483 Amanda::MainLoop::run();
485 # scan the changer using except_slots
486 sub test_except_slots {
487 my ($finished_cb) = @_;
491 my $steps = define_steps
492 cb_ref => \$finished_cb;
495 $chg->load(relative_slot => "current",
496 except_slots => { %except_slots },
497 res_cb => $steps->{'loaded'});
501 my ($err, $res) = @_;
503 if ($err->notfound) {
504 # this means the scan is done
505 return $steps->{'quit'}->();
506 } elsif ($err->volinuse and defined $err->{'slot'}) {
507 $slot = $err->{'slot'};
512 $slot = $res->{'this_slot'};
515 $except_slots{$slot} = 1;
518 $res->release(finished_cb => $steps->{'released'});
520 $steps->{'released'}->();
524 step released => sub {
528 $chg->load(relative_slot => 'next', slot => $slot,
529 except_slots => { %except_slots },
530 res_cb => $steps->{'loaded'});
534 is_deeply({ %except_slots }, { 1=>1, 2=>1, 3=>1 },
535 "scanning with except_slots works");
539 test_except_slots(\&Amanda::MainLoop::quit);
540 Amanda::MainLoop::run();
543 unlink($changer_filename);
544 unlink($result_file);