1 # Copyright (c) 2007, 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 => 47;
25 use lib "@amperldir@";
26 use Installcheck::Config;
31 use Amanda::Config qw( :init :getconf config_dir_relative );
34 # set up debugging so debug output doesn't interfere with test results
35 Amanda::Debug::dbopen("installcheck");
36 Installcheck::log_test_output();
38 # and disable Debug's die() and warn() overrides
39 Amanda::Debug::disable_die_override();
42 # define a "test" changer for purposes of this installcheck
44 package Amanda::Changer::test;
46 @ISA = qw( Amanda::Changer );
48 # monkey-patch our test changer into Amanda::Changer, and indicate that
49 # the module has already been required by adding a key to %INC
50 $INC{'Amanda/Changer/test.pm'} = "Amanda_Changer";
54 my ($config, $tpchanger) = @_;
59 slots => [ 'TAPE-00', 'TAPE-01', 'TAPE-02', 'TAPE-03' ],
63 bless ($self, $class);
71 my $cb = $params{'res_cb'};
73 if (exists $params{'label'}) {
76 my $label = $params{'label'};
78 for my $i (0 .. $#{$self->{'slots'}}) {
79 if ($self->{'slots'}->[$i] eq $label) {
85 $cb->("No such label '$label'", undef);
89 # check that it's not in use
90 for my $used_slot (@{$self->{'reserved_slots'}}) {
91 if ($used_slot == $slot) {
92 $cb->("Volume with label '$label' is already in use", undef);
98 push @{$self->{'reserved_slots'}}, $slot;
100 if (exists $params{'set_current'} && $params{'set_current'}) {
101 $self->{'curslot'} = $slot;
104 $cb->(undef, Amanda::Changer::test::Reservation->new($self, $slot, $label));
105 } elsif (exists $params{'slot'} or exists $params{'relative_slot'}) {
106 my $slot = $params{'slot'};
107 if (exists $params{'relative_slot'}) {
108 if ($params{'relative_slot'} eq "current") {
109 $slot = $self->{'curslot'};
110 } elsif ($params{'relative_slot'} eq "next") {
111 $slot = ($self->{'curslot'} + 1) % (scalar @{$self->{'slots'}});
113 die "invalid relative_slot";
117 if (grep { $_ == $slot } @{$self->{'reserved_slots'}}) {
118 $cb->("Slot $slot is already in use", undef);
121 my $label = $self->{'slots'}->[$slot];
122 push @{$self->{'reserved_slots'}}, $slot;
124 if (exists $params{'set_current'} && $params{'set_current'}) {
125 $self->{'curslot'} = $slot;
128 $cb->(undef, Amanda::Changer::test::Reservation->new($self, $slot, $label));
130 die "No label or slot parameter given";
136 my ($key, %params) = @_;
139 if ($key eq 'num_slots') {
141 } elsif ($key eq 'mkerror1') {
142 return $self->make_error("failed", $params{'info_cb'},
145 } elsif ($key eq 'mkerror2') {
146 return $self->make_error("failed", $params{'info_cb'},
151 $params{'info_cb'}->(undef, %results) if $params{'info_cb'};
158 $self->{'curslot'} = 0;
160 $params{'finished_cb'}->(undef) if $params{'finished_cb'};
167 $self->{'clean'} = 1;
169 $params{'finished_cb'}->(undef) if $params{'finished_cb'};
176 Amanda::MainLoop::call_later($params{'inventory_cb'},
188 package Amanda::Changer::test::Reservation;
190 @ISA = qw( Amanda::Changer::Reservation );
194 my ($chg, $slot, $label) = @_;
195 my $self = Amanda::Changer::Reservation::new($class);
197 $self->{'chg'} = $chg;
198 $self->{'slot'} = $slot;
199 $self->{'label'} = $label;
201 $self->{'device'} = Amanda::Device->new("null:slot-$slot");
202 $self->{'this_slot'} = $slot;
210 my $slot = $self->{'slot'};
211 my $chg = $self->{'chg'};
213 $chg->{'reserved_slots'} = [ grep { $_ != $slot } @{$chg->{'reserved_slots'}} ];
215 $params{'finished_cb'}->(undef) if $params{'finished_cb'};
221 my $slot = $self->{'slot'};
222 my $chg = $self->{'chg'};
224 $self->{'chg'}->{'slots'}->[$self->{'slot'}] = $params{'label'};
225 $self->{'label'} = $params{'label'};
227 $params{'finished_cb'}->(undef) if $params{'finished_cb'};
231 # back to the perl tests..
235 # work against a config specifying our test changer, to work out the kinks
236 # when it opens devices to check their labels
238 $testconf = Installcheck::Config->new();
239 $testconf->add_changer("mychanger", [
240 'tpchanger' => '"chg-test:/foo"',
241 'property' => '"testprop" "testval"',
245 my $cfg_result = config_init($CONFIG_INIT_EXPLICIT_NAME, 'TESTCONF');
246 if ($cfg_result != $CFGERR_OK) {
247 my ($level, @errors) = Amanda::Config::config_errors();
248 die(join "\n", @errors);
251 # check out the relevant changer properties
252 my $chg = Amanda::Changer->new("mychanger");
253 is($chg->{'config'}->get_property("testprop"), "testval",
254 "changer properties are correctly represented");
256 # test loading by label
260 my ($getres, $rq_reserved, $relres);
262 $getres = make_cb('getres' => sub {
264 return $rq_reserved->();
267 my $label = pop @labels;
269 $chg->load(label => $label,
270 set_current => ($label eq "TAPE-02"),
272 my ($err, $res) = @_;
273 ok(!$err, "no error loading $label")
276 # keep this reservation
277 push @reservations, $res if $res;
279 # and start on the next
284 $rq_reserved = make_cb(rq_reserved => sub {
285 # try to load an already-reserved volume
286 $chg->load(label => 'TAPE-00',
288 my ($err, $res) = @_;
289 ok($err, "error when requesting already-reserved volume");
290 push @reservations, $res if $res;
296 $relres = make_cb('relres' => sub {
297 if (!@reservations) {
298 return Amanda::MainLoop::quit();
301 my $res = pop @reservations;
302 $res->release(finished_cb => sub {
311 @labels = ( 'TAPE-02', 'TAPE-00', 'TAPE-03' );
313 Amanda::MainLoop::run();
316 Amanda::MainLoop::run();
318 @labels = ( 'TAPE-00', 'TAPE-01' );
320 Amanda::MainLoop::run();
322 # explicitly release the reservations (without using the callback)
323 for my $res (@reservations) {
328 # test loading by slot
330 my ($start, $first_cb, $released, $second_cb, $quit);
333 # reserves the current slot
334 $start = make_cb('start' => sub {
335 $chg->load(res_cb => $first_cb, relative_slot => "current");
338 # gets a reservation for the "current" slot
339 $first_cb = make_cb('first_cb' => sub {
340 my ($err, $res) = @_;
343 is($res->{'this_slot'}, 2,
344 "'current' slot loads slot 2");
345 is($res->{'device'}->device_name, "null:slot-2",
346 "..device is correct");
348 $slot = $res->{'this_slot'};
349 $res->release(finished_cb => $released);
352 $released = make_cb(released => sub {
355 $chg->load(res_cb => $second_cb, relative_slot => 'next',
356 slot => $slot, set_current => 1);
359 # gets a reservation for the "next" slot
360 $second_cb = make_cb('second_cb' => sub {
361 my ($err, $res) = @_;
364 is($res->{'this_slot'}, 3,
365 "next slot loads slot 3");
366 is($chg->{'curslot'}, 3,
367 "..which is also now the current slot");
369 $res->release(finished_cb => $quit);
372 $quit = make_cb(quit => sub {
376 Amanda::MainLoop::quit();
380 Amanda::MainLoop::run();
385 my ($start, $load1_cb, $set_cb, $released, $load2_cb, $released2, $load3_cb);
389 $start = make_cb('start' => sub {
390 $chg->load(res_cb => $load1_cb, label => "TAPE-00");
393 # rename it to TAPE-99
394 $load1_cb = make_cb('load1_cb' => sub {
395 (my $err, $res) = @_;
398 pass("loaded TAPE-00");
399 $res->set_label(label => "TAPE-99", finished_cb => $set_cb);
402 $set_cb = make_cb('set_cb' => sub {
405 $res->release(finished_cb => $released);
408 # try to load TAPE-00
409 $released = make_cb('released' => sub {
413 pass("relabeled TAPE-00 to TAPE-99");
414 $chg->load(res_cb => $load2_cb, label => "TAPE-00");
417 # try to load TAPE-99
418 $load2_cb = make_cb('load2_cb' => sub {
419 (my $err, $res) = @_;
420 ok($err, "loading TAPE-00 is now an error");
422 $chg->load(res_cb => $load3_cb, label => "TAPE-99");
426 $load3_cb = make_cb('load3_cb' => sub {
427 (my $err, $res) = @_;
430 pass("but loading TAPE-99 is ok");
432 $res->release(finished_cb => $released2);
435 $released2 = make_cb(released2 => sub {
439 Amanda::MainLoop::quit();
443 Amanda::MainLoop::run();
446 # test reset and clean and inventory
448 my ($finished_cb) = @_;
450 my $steps = define_steps
451 cb_ref => \$finished_cb;
453 step do_reset => sub {
454 $chg->reset(finished_cb => sub {
455 is($chg->{'curslot'}, 0,
456 "reset() resets to slot 0");
457 $steps->{'do_clean'}->();
461 step do_clean => sub {
462 $chg->clean(finished_cb => sub {
463 ok($chg->{'clean'}, "clean 'cleaned' the changer");
464 $steps->{'do_inventory'}->();
468 step do_inventory => sub {
469 $chg->inventory(inventory_cb => sub {
478 }], "inventory returns an inventory");
483 test_simple(\&Amanda::MainLoop::quit);
484 Amanda::MainLoop::run();
488 my ($do_info, $check_info, $do_info_err, $check_info_err);
490 $do_info = make_cb('do_info' => sub {
491 $chg->info(info_cb => $check_info,
492 info => [ 'num_slots' ]);
495 $check_info = make_cb('check_info' => sub {
496 my ($err, %results) = @_;
498 is_deeply(\%results, { 'num_slots' => 13 },
503 $do_info_err = make_cb('do_info_err' => sub {
504 $chg->info(info_cb => $check_info_err,
505 info => [ 'mkerror1', 'mkerror2' ]);
508 $check_info_err = make_cb('check_info_err' => sub {
509 my ($err, %results) = @_;
511 "While getting info key 'mkerror1': err1; While getting info key 'mkerror2': err2",
512 "info errors are handled correctly");
513 is($err->{'type'}, 'failed', "error has type 'failed'");
514 ok($err->failed, "\$err->failed is true");
515 ok(!$err->fatal, "\$err->fatal is false");
516 is($err->{'reason'}, 'unknown', "\$err->{'reason'} is 'unknown'");
517 ok($err->unknown, "\$err->unknown is true");
518 ok(!$err->notimpl, "\$err->notimpl is false");
519 Amanda::MainLoop::quit();
523 Amanda::MainLoop::run();
526 # Test the various permutations of configuration setup, with a patched
527 # _new_from_uri so we can monitor the result
528 sub my_new_from_uri {
529 my ($uri, $cc, $name) = @_;
530 return $uri if (ref $uri and $uri->isa("Amanda::Changer::Error"));
531 return [ $uri, $cc? "cc" : undef ];
533 *saved_new_from_uri = *Amanda::Changer::_new_from_uri;
534 *Amanda::Changer::_new_from_uri = *my_new_from_uri;
537 my ($global_tapedev, $global_tpchanger, $defn_tpchanger, $custom_defn) = @_;
539 $testconf = Installcheck::Config->new();
541 if (defined($global_tapedev)) {
542 $testconf->add_param('tapedev', "\"$global_tapedev\"")
545 if (defined($global_tpchanger)) {
546 $testconf->add_param('tpchanger', "\"$global_tpchanger\"")
549 if (defined($defn_tpchanger)) {
550 $testconf->add_changer("mychanger", [
551 'tpchanger' => "\"$defn_tpchanger\"",
555 if (defined($custom_defn)) {
556 $testconf->add_changer("customchanger", $custom_defn);
557 $testconf->add_param('tpchanger', '"customchanger"');
562 my $cfg_result = config_init($CONFIG_INIT_EXPLICIT_NAME, 'TESTCONF');
563 if ($cfg_result != $CFGERR_OK) {
564 my ($level, @errors) = Amanda::Config::config_errors();
565 die(join "\n", @errors);
570 my ($global_tapedev, $global_tpchanger, $defn_tpchanger, $custom_defn,
571 $name, $regexp, $msg) = @_;
572 loadconfig($global_tapedev, $global_tpchanger, $defn_tpchanger, $custom_defn);
573 my $err = Amanda::Changer->new($name);
574 if ($err->isa("Amanda::Changer::Error")) {
575 like($err->{'message'}, $regexp, $msg);
577 diag("Amanda::Changer->new did not return an Error object:");
578 diag("".Dumper($err));
583 assert_invalid(undef, undef, undef, undef, undef,
584 qr/You must specify one of 'tapedev' or 'tpchanger'/,
585 "supplying a nothing is invalid");
587 loadconfig(undef, "file:/foo", undef, undef);
588 is_deeply( Amanda::Changer->new(), [ "chg-single:file:/foo", undef ],
589 "default changer with global tpchanger naming a device");
591 loadconfig(undef, "chg-disk:/foo", undef, undef);
592 is_deeply( Amanda::Changer->new(), [ "chg-disk:/foo", undef ],
593 "default changer with global tpchanger naming a changer");
595 loadconfig(undef, "mychanger", "chg-disk:/bar", undef);
596 is_deeply( Amanda::Changer->new(), [ "chg-disk:/bar", "cc" ],
597 "default changer with global tpchanger naming a defined changer with a uri");
599 loadconfig(undef, "mychanger", "chg-zd-mtx", undef);
600 is_deeply( Amanda::Changer->new(), [ "chg-compat:chg-zd-mtx", "cc" ],
601 "default changer with global tpchanger naming a defined changer with a compat script");
603 loadconfig(undef, "chg-zd-mtx", undef, undef);
604 is_deeply( Amanda::Changer->new(), [ "chg-compat:chg-zd-mtx", undef ],
605 "default changer with global tpchanger naming a compat script");
607 loadconfig("tape:/dev/foo", undef, undef, undef);
608 is_deeply( Amanda::Changer->new(), [ "chg-single:tape:/dev/foo", undef ],
609 "default changer with global tapedev naming a device and no tpchanger");
611 assert_invalid("tape:/dev/foo", "tape:/dev/foo", undef, undef, undef,
612 qr/Cannot specify both 'tapedev' and 'tpchanger'/,
613 "supplying a device for both tpchanger and tapedev is invalid");
615 assert_invalid("tape:/dev/foo", "chg-disk:/foo", undef, undef, undef,
616 qr/Cannot specify both 'tapedev' and 'tpchanger'/,
617 "supplying a device for tapedev and a changer for tpchanger is invalid");
619 loadconfig("tape:/dev/foo", 'chg-zd-mtx', undef, undef);
620 is_deeply( Amanda::Changer->new(), [ "chg-compat:chg-zd-mtx", undef ],
621 "default changer with global tapedev naming a device and a global tpchanger naming a compat script");
623 assert_invalid("chg-disk:/foo", "tape:/dev/foo", undef, undef, undef,
624 qr/Cannot specify both 'tapedev' and 'tpchanger'/,
625 "supplying a changer for tapedev and a device for tpchanger is invalid");
627 loadconfig("chg-disk:/foo", undef, undef, undef);
628 is_deeply( Amanda::Changer->new(), [ "chg-disk:/foo", undef ],
629 "default changer with global tapedev naming a device");
631 loadconfig("mychanger", undef, "chg-disk:/bar", undef);
632 is_deeply( Amanda::Changer->new(), [ "chg-disk:/bar", "cc" ],
633 "default changer with global tapedev naming a defined changer with a uri");
635 loadconfig("mychanger", undef, "chg-zd-mtx", undef);
636 is_deeply( Amanda::Changer->new(), [ "chg-compat:chg-zd-mtx", "cc" ],
637 "default changer with global tapedev naming a defined changer with a compat script");
639 loadconfig(undef, undef, "chg-disk:/foo", undef);
640 is_deeply( Amanda::Changer->new("mychanger"), [ "chg-disk:/foo", "cc" ],
641 "named changer loads the proper definition");
643 loadconfig(undef, undef, undef, [
644 tapedev => '"chg-disk:/foo"',
646 is_deeply( Amanda::Changer->new(), [ "chg-disk:/foo", "cc" ],
647 "defined changer with tapedev loads the proper definition");
649 loadconfig(undef, undef, undef, [
650 tpchanger => '"chg-disk:/bar"',
652 is_deeply( Amanda::Changer->new(), [ "chg-disk:/bar", "cc" ],
653 "defined changer with tpchanger loads the proper definition");
655 assert_invalid(undef, undef, undef, [
656 tpchanger => '"chg-disk:/bar"',
657 tapedev => '"file:/bar"',
659 qr/Cannot specify both 'tapedev' and 'tpchanger'/,
660 "supplying both a new tpchanger and tapedev in a definition is invalid");
662 assert_invalid(undef, undef, undef, [
663 property => '"this" "will not work"',
665 qr/You must specify one of 'tapedev' or 'tpchanger'/,
666 "supplying neither a tpchanger nor tapedev in a definition is invalid");
668 *Amanda::Changer::_new_from_uri = *saved_new_from_uri;
670 # test with_locked_state *within* a process
672 sub test_locked_state {
673 my ($finished_cb) = @_;
675 my $stfile = "$Installcheck::TMP/test-statefile";
676 my $num_outstanding = 0;
678 my $steps = define_steps
679 cb_ref => \$finished_cb;
682 $chg = Amanda::Changer->new("chg-null:");
684 for my $num (qw( one two three )) {
686 $chg->with_locked_state($stfile, $steps->{'maybe_done'}, sub {
687 my ($state, $maybe_done) = @_;
689 $state->{$num} = $num;
692 Amanda::MainLoop::call_after(50, $maybe_done, undef, $state);
697 step maybe_done => sub {
698 my ($err, $state) = @_;
701 return if (--$num_outstanding);
708 }, "state is maintained correctly (within a process)");
710 unlink($stfile) if -f $stfile;
715 test_locked_state(\&Amanda::MainLoop::quit);
716 Amanda::MainLoop::run();