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 => 54;
25 use lib "@amperldir@";
26 use Installcheck::Config;
28 use Amanda::Device qw( :constants );;
31 use Amanda::Config qw( :init :getconf config_dir_relative );
35 # set up debugging so debug output doesn't interfere with test results
36 Amanda::Debug::dbopen("installcheck");
37 Installcheck::log_test_output();
39 # and disable Debug's die() and warn() overrides
40 Amanda::Debug::disable_die_override();
43 # define a "test" changer for purposes of this installcheck
45 package Amanda::Changer::test;
47 @ISA = qw( Amanda::Changer );
49 # monkey-patch our test changer into Amanda::Changer, and indicate that
50 # the module has already been required by adding a key to %INC
51 $INC{'Amanda/Changer/test.pm'} = "Amanda_Changer";
55 my ($config, $tpchanger) = @_;
60 slots => [ 'TAPE-00', 'TAPE-01', 'TAPE-02', 'TAPE-03' ],
64 bless ($self, $class);
72 my $cb = $params{'res_cb'};
74 if (exists $params{'label'}) {
77 my $label = $params{'label'};
79 for my $i (0 .. $#{$self->{'slots'}}) {
80 if ($self->{'slots'}->[$i] eq $label) {
86 $cb->("No such label '$label'", undef);
90 # check that it's not in use
91 for my $used_slot (@{$self->{'reserved_slots'}}) {
92 if ($used_slot == $slot) {
93 $cb->("Volume with label '$label' is already in use", undef);
99 push @{$self->{'reserved_slots'}}, $slot;
101 if (exists $params{'set_current'} && $params{'set_current'}) {
102 $self->{'curslot'} = $slot;
105 $cb->(undef, Amanda::Changer::test::Reservation->new($self, $slot, $label));
106 } elsif (exists $params{'slot'} or exists $params{'relative_slot'}) {
107 my $slot = $params{'slot'};
108 if (exists $params{'relative_slot'}) {
109 if ($params{'relative_slot'} eq "current") {
110 $slot = $self->{'curslot'};
111 } elsif ($params{'relative_slot'} eq "next") {
112 $slot = ($self->{'curslot'} + 1) % (scalar @{$self->{'slots'}});
114 die "invalid relative_slot";
118 if (grep { $_ == $slot } @{$self->{'reserved_slots'}}) {
119 $cb->("Slot $slot is already in use", undef);
122 my $label = $self->{'slots'}->[$slot];
123 push @{$self->{'reserved_slots'}}, $slot;
125 if (exists $params{'set_current'} && $params{'set_current'}) {
126 $self->{'curslot'} = $slot;
129 $cb->(undef, Amanda::Changer::test::Reservation->new($self, $slot, $label));
131 die "No label or slot parameter given";
137 my ($key, %params) = @_;
140 if ($key eq 'num_slots') {
142 } elsif ($key eq 'mkerror1') {
143 return $self->make_error("failed", $params{'info_cb'},
146 } elsif ($key eq 'mkerror2') {
147 return $self->make_error("failed", $params{'info_cb'},
152 $params{'info_cb'}->(undef, %results) if $params{'info_cb'};
159 $self->{'curslot'} = 0;
161 $params{'finished_cb'}->(undef) if $params{'finished_cb'};
168 $self->{'clean'} = 1;
170 $params{'finished_cb'}->(undef) if $params{'finished_cb'};
177 Amanda::MainLoop::call_later($params{'inventory_cb'},
189 package Amanda::Changer::test::Reservation;
191 @ISA = qw( Amanda::Changer::Reservation );
195 my ($chg, $slot, $label) = @_;
196 my $self = Amanda::Changer::Reservation::new($class);
198 $self->{'chg'} = $chg;
199 $self->{'slot'} = $slot;
200 $self->{'label'} = $label;
202 $self->{'device'} = Amanda::Device->new("null:slot-$slot");
203 $self->{'this_slot'} = $slot;
211 my $slot = $self->{'slot'};
212 my $chg = $self->{'chg'};
214 $chg->{'reserved_slots'} = [ grep { $_ != $slot } @{$chg->{'reserved_slots'}} ];
216 $params{'finished_cb'}->(undef) if $params{'finished_cb'};
222 my $slot = $self->{'slot'};
223 my $chg = $self->{'chg'};
225 $self->{'chg'}->{'slots'}->[$self->{'slot'}] = $params{'label'};
226 $self->{'label'} = $params{'label'};
228 $params{'finished_cb'}->(undef) if $params{'finished_cb'};
232 # back to the perl tests..
236 # work against a config specifying our test changer, to work out the kinks
237 # when it opens devices to check their labels
239 $testconf = Installcheck::Config->new();
240 $testconf->add_changer("mychanger", [
241 'tpchanger' => '"chg-test:/foo"',
242 'property' => '"testprop" "testval"',
246 my $cfg_result = config_init($CONFIG_INIT_EXPLICIT_NAME, 'TESTCONF');
247 if ($cfg_result != $CFGERR_OK) {
248 my ($level, @errors) = Amanda::Config::config_errors();
249 die(join "\n", @errors);
252 # check out the relevant changer properties
253 my $tlf = Amanda::Config::config_dir_relative(getconf($CNF_TAPELIST));
254 my $tl = Amanda::Tapelist->new($tlf);
255 my $chg = Amanda::Changer->new("mychanger", tapelist => $tl);
256 is($chg->{'config'}->get_property("testprop"), "testval",
257 "changer properties are correctly represented");
258 is($chg->have_inventory(), 1, "changer have inventory");
259 is($chg->make_new_tape_label(), undef, "no make_new_tape_label");
260 is($chg->make_new_meta_label(), undef, "no make_new_meta_label");
262 $chg = Amanda::Changer->new("mychanger", tapelist => $tl,
263 labelstr => "TESTCONF-[0-9][0-9][0-9]-[a-z][a-z][a-z]-[0-9][0-9][0-9]",
264 autolabel => { template => '$c-$m-$b-%%%',
269 meta_autolabel => "%%%");
270 my $meta = $chg->make_new_meta_label();
271 is($meta, "001", "meta 001");
272 my $label = $chg->make_new_tape_label(meta => $meta, barcode => 'aaa');
273 is($label, 'TESTCONF-001-aaa-001', "label TESTCONF-001-aaa-001");
275 is($chg->volume_is_labelable($DEVICE_STATUS_VOLUME_UNLABELED, $Amanda::Header::F_EMPTY),
276 1, "empty volume is labelable");
277 is($chg->volume_is_labelable($DEVICE_STATUS_VOLUME_ERROR, undef),
278 0, "empty volume is labelable");
280 # test loading by label
284 my ($getres, $rq_reserved, $relres);
286 $getres = make_cb('getres' => sub {
288 return $rq_reserved->();
291 my $label = pop @labels;
293 $chg->load(label => $label,
294 set_current => ($label eq "TAPE-02"),
296 my ($err, $res) = @_;
297 ok(!$err, "no error loading $label")
300 # keep this reservation
301 push @reservations, $res if $res;
303 # and start on the next
308 $rq_reserved = make_cb(rq_reserved => sub {
309 # try to load an already-reserved volume
310 $chg->load(label => 'TAPE-00',
312 my ($err, $res) = @_;
313 ok($err, "error when requesting already-reserved volume");
314 push @reservations, $res if $res;
320 $relres = make_cb('relres' => sub {
321 if (!@reservations) {
322 return Amanda::MainLoop::quit();
325 my $res = pop @reservations;
326 $res->release(finished_cb => sub {
335 @labels = ( 'TAPE-02', 'TAPE-00', 'TAPE-03' );
337 Amanda::MainLoop::run();
340 Amanda::MainLoop::run();
342 @labels = ( 'TAPE-00', 'TAPE-01' );
344 Amanda::MainLoop::run();
346 # explicitly release the reservations (without using the callback)
347 for my $res (@reservations) {
352 # test loading by slot
354 my ($start, $first_cb, $released, $second_cb, $quit);
357 # reserves the current slot
358 $start = make_cb('start' => sub {
359 $chg->load(res_cb => $first_cb, relative_slot => "current");
362 # gets a reservation for the "current" slot
363 $first_cb = make_cb('first_cb' => sub {
364 my ($err, $res) = @_;
367 is($res->{'this_slot'}, 2,
368 "'current' slot loads slot 2");
369 is($res->{'device'}->device_name, "null:slot-2",
370 "..device is correct");
372 $slot = $res->{'this_slot'};
373 $res->release(finished_cb => $released);
376 $released = make_cb(released => sub {
379 $chg->load(res_cb => $second_cb, relative_slot => 'next',
380 slot => $slot, set_current => 1);
383 # gets a reservation for the "next" slot
384 $second_cb = make_cb('second_cb' => sub {
385 my ($err, $res) = @_;
388 is($res->{'this_slot'}, 3,
389 "next slot loads slot 3");
390 is($chg->{'curslot'}, 3,
391 "..which is also now the current slot");
393 $res->release(finished_cb => $quit);
396 $quit = make_cb(quit => sub {
400 Amanda::MainLoop::quit();
404 Amanda::MainLoop::run();
409 my ($start, $load1_cb, $set_cb, $released, $load2_cb, $released2, $load3_cb);
413 $start = make_cb('start' => sub {
414 $chg->load(res_cb => $load1_cb, label => "TAPE-00");
417 # rename it to TAPE-99
418 $load1_cb = make_cb('load1_cb' => sub {
419 (my $err, $res) = @_;
422 pass("loaded TAPE-00");
423 $res->set_label(label => "TAPE-99", finished_cb => $set_cb);
426 $set_cb = make_cb('set_cb' => sub {
429 $res->release(finished_cb => $released);
432 # try to load TAPE-00
433 $released = make_cb('released' => sub {
437 pass("relabeled TAPE-00 to TAPE-99");
438 $chg->load(res_cb => $load2_cb, label => "TAPE-00");
441 # try to load TAPE-99
442 $load2_cb = make_cb('load2_cb' => sub {
443 (my $err, $res) = @_;
444 ok($err, "loading TAPE-00 is now an error");
446 $chg->load(res_cb => $load3_cb, label => "TAPE-99");
450 $load3_cb = make_cb('load3_cb' => sub {
451 (my $err, $res) = @_;
454 pass("but loading TAPE-99 is ok");
456 $res->release(finished_cb => $released2);
459 $released2 = make_cb(released2 => sub {
463 Amanda::MainLoop::quit();
467 Amanda::MainLoop::run();
470 # test reset and clean and inventory
472 my ($finished_cb) = @_;
474 my $steps = define_steps
475 cb_ref => \$finished_cb;
477 step do_reset => sub {
478 $chg->reset(finished_cb => sub {
479 is($chg->{'curslot'}, 0,
480 "reset() resets to slot 0");
481 $steps->{'do_clean'}->();
485 step do_clean => sub {
486 $chg->clean(finished_cb => sub {
487 ok($chg->{'clean'}, "clean 'cleaned' the changer");
488 $steps->{'do_inventory'}->();
492 step do_inventory => sub {
493 $chg->inventory(inventory_cb => sub {
502 }], "inventory returns an inventory");
507 test_simple(\&Amanda::MainLoop::quit);
508 Amanda::MainLoop::run();
512 my ($do_info, $check_info, $do_info_err, $check_info_err);
514 $do_info = make_cb('do_info' => sub {
515 $chg->info(info_cb => $check_info,
516 info => [ 'num_slots' ]);
519 $check_info = make_cb('check_info' => sub {
520 my ($err, %results) = @_;
522 is_deeply(\%results, { 'num_slots' => 13 },
527 $do_info_err = make_cb('do_info_err' => sub {
528 $chg->info(info_cb => $check_info_err,
529 info => [ 'mkerror1', 'mkerror2' ]);
532 $check_info_err = make_cb('check_info_err' => sub {
533 my ($err, %results) = @_;
535 "While getting info key 'mkerror1': err1; While getting info key 'mkerror2': err2",
536 "info errors are handled correctly");
537 is($err->{'type'}, 'failed', "error has type 'failed'");
538 ok($err->failed, "\$err->failed is true");
539 ok(!$err->fatal, "\$err->fatal is false");
540 is($err->{'reason'}, 'unknown', "\$err->{'reason'} is 'unknown'");
541 ok($err->unknown, "\$err->unknown is true");
542 ok(!$err->notimpl, "\$err->notimpl is false");
543 Amanda::MainLoop::quit();
547 Amanda::MainLoop::run();
551 # Test the various permutations of configuration setup, with a patched
552 # _new_from_uri so we can monitor the result
553 sub my_new_from_uri {
554 my ($uri, $cc, $name) = @_;
555 return $uri if (ref $uri and $uri->isa("Amanda::Changer::Error"));
556 return [ $uri, $cc? "cc" : undef ];
558 *saved_new_from_uri = *Amanda::Changer::_new_from_uri;
559 *Amanda::Changer::_new_from_uri = *my_new_from_uri;
562 my ($global_tapedev, $global_tpchanger, $defn_tpchanger, $custom_defn) = @_;
564 $testconf = Installcheck::Config->new();
566 if (defined($global_tapedev)) {
567 $testconf->add_param('tapedev', "\"$global_tapedev\"")
570 if (defined($global_tpchanger)) {
571 $testconf->add_param('tpchanger', "\"$global_tpchanger\"")
574 if (defined($defn_tpchanger)) {
575 $testconf->add_changer("mychanger", [
576 'tpchanger' => "\"$defn_tpchanger\"",
580 if (defined($custom_defn)) {
581 $testconf->add_changer("customchanger", $custom_defn);
582 $testconf->add_param('tpchanger', '"customchanger"');
587 my $cfg_result = config_init($CONFIG_INIT_EXPLICIT_NAME, 'TESTCONF');
588 if ($cfg_result != $CFGERR_OK) {
589 my ($level, @errors) = Amanda::Config::config_errors();
590 die(join "\n", @errors);
595 my ($global_tapedev, $global_tpchanger, $defn_tpchanger, $custom_defn,
596 $name, $regexp, $msg) = @_;
597 loadconfig($global_tapedev, $global_tpchanger, $defn_tpchanger, $custom_defn);
598 my $err = Amanda::Changer->new($name);
599 if ($err->isa("Amanda::Changer::Error")) {
600 like($err->{'message'}, $regexp, $msg);
602 diag("Amanda::Changer->new did not return an Error object:");
603 diag("".Dumper($err));
608 assert_invalid(undef, undef, undef, undef, undef,
609 qr/You must specify one of 'tapedev' or 'tpchanger'/,
610 "supplying a nothing is invalid");
612 loadconfig(undef, "file:/foo", undef, undef);
613 is_deeply( Amanda::Changer->new(), [ "chg-single:file:/foo", undef ],
614 "default changer with global tpchanger naming a device");
616 loadconfig(undef, "chg-disk:/foo", undef, undef);
617 is_deeply( Amanda::Changer->new(), [ "chg-disk:/foo", undef ],
618 "default changer with global tpchanger naming a changer");
620 loadconfig(undef, "mychanger", "chg-disk:/bar", undef);
621 is_deeply( Amanda::Changer->new(), [ "chg-disk:/bar", "cc" ],
622 "default changer with global tpchanger naming a defined changer with a uri");
624 loadconfig(undef, "mychanger", "chg-zd-mtx", undef);
625 is_deeply( Amanda::Changer->new(), [ "chg-compat:chg-zd-mtx", "cc" ],
626 "default changer with global tpchanger naming a defined changer with a compat script");
628 loadconfig(undef, "chg-zd-mtx", undef, undef);
629 is_deeply( Amanda::Changer->new(), [ "chg-compat:chg-zd-mtx", undef ],
630 "default changer with global tpchanger naming a compat script");
632 loadconfig("tape:/dev/foo", undef, undef, undef);
633 is_deeply( Amanda::Changer->new(), [ "chg-single:tape:/dev/foo", undef ],
634 "default changer with global tapedev naming a device and no tpchanger");
636 assert_invalid("tape:/dev/foo", "tape:/dev/foo", undef, undef, undef,
637 qr/Cannot specify both 'tapedev' and 'tpchanger'/,
638 "supplying a device for both tpchanger and tapedev is invalid");
640 assert_invalid("tape:/dev/foo", "chg-disk:/foo", undef, undef, undef,
641 qr/Cannot specify both 'tapedev' and 'tpchanger'/,
642 "supplying a device for tapedev and a changer for tpchanger is invalid");
644 loadconfig("tape:/dev/foo", 'chg-zd-mtx', undef, undef);
645 is_deeply( Amanda::Changer->new(), [ "chg-compat:chg-zd-mtx", undef ],
646 "default changer with global tapedev naming a device and a global tpchanger naming a compat script");
648 assert_invalid("chg-disk:/foo", "tape:/dev/foo", undef, undef, undef,
649 qr/Cannot specify both 'tapedev' and 'tpchanger'/,
650 "supplying a changer for tapedev and a device for tpchanger is invalid");
652 loadconfig("chg-disk:/foo", undef, undef, undef);
653 is_deeply( Amanda::Changer->new(), [ "chg-disk:/foo", undef ],
654 "default changer with global tapedev naming a device");
656 loadconfig("mychanger", undef, "chg-disk:/bar", undef);
657 is_deeply( Amanda::Changer->new(), [ "chg-disk:/bar", "cc" ],
658 "default changer with global tapedev naming a defined changer with a uri");
660 loadconfig("mychanger", undef, "chg-zd-mtx", undef);
661 is_deeply( Amanda::Changer->new(), [ "chg-compat:chg-zd-mtx", "cc" ],
662 "default changer with global tapedev naming a defined changer with a compat script");
664 loadconfig(undef, undef, "chg-disk:/foo", undef);
665 is_deeply( Amanda::Changer->new("mychanger"), [ "chg-disk:/foo", "cc" ],
666 "named changer loads the proper definition");
668 loadconfig(undef, undef, undef, [
669 tapedev => '"chg-disk:/foo"',
671 is_deeply( Amanda::Changer->new(), [ "chg-disk:/foo", "cc" ],
672 "defined changer with tapedev loads the proper definition");
674 loadconfig(undef, undef, undef, [
675 tpchanger => '"chg-disk:/bar"',
677 is_deeply( Amanda::Changer->new(), [ "chg-disk:/bar", "cc" ],
678 "defined changer with tpchanger loads the proper definition");
680 assert_invalid(undef, undef, undef, [
681 tpchanger => '"chg-disk:/bar"',
682 tapedev => '"file:/bar"',
684 qr/Cannot specify both 'tapedev' and 'tpchanger'/,
685 "supplying both a new tpchanger and tapedev in a definition is invalid");
687 assert_invalid(undef, undef, undef, [
688 property => '"this" "will not work"',
690 qr/You must specify one of 'tapedev' or 'tpchanger'/,
691 "supplying neither a tpchanger nor tapedev in a definition is invalid");
693 *Amanda::Changer::_new_from_uri = *saved_new_from_uri;
695 # test with_locked_state *within* a process
697 sub test_locked_state {
698 my ($finished_cb) = @_;
700 my $stfile = "$Installcheck::TMP/test-statefile";
701 my $num_outstanding = 0;
703 my $steps = define_steps
704 cb_ref => \$finished_cb,
705 finalize => sub { $chg->quit() if defined $chg };
708 $chg = Amanda::Changer->new("chg-null:");
710 for my $num (qw( one two three )) {
712 $chg->with_locked_state($stfile, $steps->{'maybe_done'}, sub {
713 my ($state, $maybe_done) = @_;
715 $state->{$num} = $num;
718 Amanda::MainLoop::call_after(50, $maybe_done, undef, $state);
723 step maybe_done => sub {
724 my ($err, $state) = @_;
727 return if (--$num_outstanding);
734 }, "state is maintained correctly (within a process)");
736 unlink($stfile) if -f $stfile;
741 test_locked_state(\&Amanda::MainLoop::quit);
742 Amanda::MainLoop::run();