1 # Copyright (c) 2008-2012 Zmanda, Inc. All Rights Reserved.
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful, but
9 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # You should have received a copy of the GNU General Public License along
14 # with this program; if not, write to the Free Software Foundation, Inc.,
15 # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # Contact information: Zmanda Inc, 465 S. Mathilda Ave., Suite 300
18 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
20 use Test::More tests => 19;
25 use lib "@amperldir@";
27 use Installcheck::Config;
28 use Installcheck::Changer;
30 use Amanda::Device qw( :constants );
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 $taperoot = "$Installcheck::TMP/Amanda_Changer_Disk_test";
53 for my $slot (1 .. $nslots) {
54 mkdir("$taperoot/slot$slot")
55 or die("Could not mkdir: $!");
60 my ($res, $slot, $msg) = @_;
62 my ($datalink) = ($res->{'device'}->device_name =~ /file:(.*)/);
64 is(readlink($datalink), "../slot$slot", $msg);
67 # Build a configuration that specifies Amanda::Changer::Disk
68 my $testconf = Installcheck::Config->new();
71 my $cfg_result = config_init($CONFIG_INIT_EXPLICIT_NAME, 'TESTCONF');
72 if ($cfg_result != $CFGERR_OK) {
73 my ($level, @errors) = Amanda::Config::config_errors();
74 die(join "\n", @errors);
80 my $chg = Amanda::Changer->new("chg-disk:$taperoot/foo");
82 { message => qr/directory '.*' does not exist/,
84 "detects nonexistent directory");
86 $chg = Amanda::Changer->new("chg-disk:$taperoot");
87 die($chg) if $chg->isa("Amanda::Changer::Error");
88 is($chg->have_inventory(), '1', "changer have inventory");
91 my ($finished_cb, @slots) = @_;
92 my @reservations = ();
95 my $steps = define_steps
96 cb_ref => \$finished_cb;
100 if (!defined $slot) {
101 return $steps->{'tryreserved'}->();
104 $chg->load(slot => $slot,
105 set_current => ($slot == 5),
106 res_cb => make_cb($steps->{'loaded'}));
110 my ($err, $reservation) = @_;
111 ok(!$err, "no error loading slot $slot")
114 # keep this reservation
116 push @reservations, $reservation;
119 # and start on the next
120 $steps->{'getres'}->();
123 step tryreserved => sub {
124 # try to load an already-reserved slot
125 $chg->load(slot => 3,
127 my ($err, $reservation) = @_;
129 { message => qr/Slot 3 is already in use by drive/,
131 reason => 'volinuse' },
132 "error when requesting already-reserved slot");
133 $steps->{'release'}->();
137 step release => sub {
138 my $res = pop @reservations;
140 return Amanda::MainLoop::quit();
143 $res->release(finished_cb => sub {
146 $steps->{'release'}->();
152 test_reserved(\&Amanda::MainLoop::quit, 1, 3, 5);
153 Amanda::MainLoop::run();
155 # and try it with some different slots, just to see
156 test_reserved(\&Amanda::MainLoop::quit, 4, 2, 3);
157 Amanda::MainLoop::run();
159 # check relative slot ("current" and "next") functionality
160 sub test_relative_slot {
161 my ($finished_cb) = @_;
164 my $steps = define_steps
165 cb_ref => \$finished_cb;
167 # load the "current" slot, which should be 3
168 step load_current => sub {
169 $chg->load(relative_slot => "current", res_cb => $steps->{'check_current_cb'});
172 step check_current_cb => sub {
173 my ($err, $res) = @_;
176 is_pointing_to($res, 5, "'current' is slot 5");
177 $slot = $res->{'this_slot'};
179 $res->release(finished_cb => $steps->{'released1'});
182 step released1 => sub {
186 $chg->load(relative_slot => 'next', slot => $slot,
187 res_cb => $steps->{'check_next_cb'});
190 step check_next_cb => sub {
191 my ($err, $res) = @_;
194 is_pointing_to($res, 1, "'next' from there is slot 1");
196 $res->release(finished_cb => $steps->{'released2'});
199 step released2 => sub {
203 $chg->reset(finished_cb => $steps->{'reset_finished_cb'});
206 step reset_finished_cb => sub {
210 $chg->load(relative_slot => "current", res_cb => $steps->{'check_reset_cb'});
213 step check_reset_cb => sub {
214 my ($err, $res) = @_;
217 is_pointing_to($res, 1, "after reset, 'current' is slot 1");
219 $res->release(finished_cb => $steps->{'released3'});
222 step released3 => sub {
229 test_relative_slot(\&Amanda::MainLoop::quit);
230 Amanda::MainLoop::run();
232 # test loading relative_slot "next"
233 sub test_relative_next {
234 my ($finished_cb) = @_;
236 my $steps = define_steps
237 cb_ref => \$finished_cb;
239 step load_next => sub {
240 $chg->load(relative_slot => "next",
242 my ($err, $res) = @_;
245 is_pointing_to($res, 2, "loading relative slot 'next' loads the correct slot");
247 $steps->{'release'}->($res);
252 step release => sub {
255 $res->release(finished_cb => sub {
263 test_relative_next(\&Amanda::MainLoop::quit);
264 Amanda::MainLoop::run();
266 # scan the changer using except_slots
267 sub test_except_slots {
268 my ($finished_cb) = @_;
272 my $steps = define_steps
273 cb_ref => \$finished_cb;
276 $chg->load(slot => "5", except_slots => { %except_slots },
277 res_cb => $steps->{'loaded'});
281 my ($err, $res) = @_;
283 if ($err->notfound) {
284 # this means the scan is done
285 return $steps->{'quit'}->();
286 } elsif ($err->volinuse and defined $err->{'slot'}) {
287 $slot = $err->{'slot'};
292 $slot = $res->{'this_slot'};
295 $except_slots{$slot} = 1;
298 $res->release(finished_cb => $steps->{'released'});
300 $steps->{'released'}->();
304 step released => sub {
308 $chg->load(relative_slot => 'next', slot => $slot,
309 except_slots => { %except_slots },
310 res_cb => $steps->{'loaded'});
314 is_deeply({ %except_slots }, { 5=>1, 1=>1, 2=>1, 3=>1, 4=>1 },
315 "scanning with except_slots works");
319 test_except_slots(\&Amanda::MainLoop::quit);
320 Amanda::MainLoop::run();
322 # eject is not implemented
324 my $try_eject = make_cb('try_eject' => sub {
325 $chg->eject(finished_cb => make_cb(sub {
326 my ($err, $res) = @_;
328 { type => 'failed', reason => 'notimpl' },
329 "eject returns a failed/notimpl error");
331 Amanda::MainLoop::quit();
336 Amanda::MainLoop::run();
339 # check num_slots and loading by label
341 my ($get_info, $load_label, $check_load_cb) = @_;
343 $get_info = make_cb('get_info' => sub {
344 $chg->info(info_cb => $load_label, info => [ 'num_slots', 'fast_search' ]);
347 $load_label = make_cb('load_label' => sub {
350 die($err) if defined($err);
352 is_deeply({ %results },
353 { num_slots => 5, fast_search => 1 },
354 "info() returns the correct num_slots and fast_search");
356 # note use of a glob metacharacter in the label name
357 $chg->load(label => "FOO?BAR", res_cb => $check_load_cb);
360 $check_load_cb = make_cb('check_load_cb' => sub {
361 my ($err, $res) = @_;
364 is_pointing_to($res, 4, "labeled volume found in slot 4");
366 $res->release(finished_cb => sub {
370 Amanda::MainLoop::quit();
374 # label slot 4, using our own symlink
375 mkpath("$taperoot/tmp");
376 symlink("../slot4", "$taperoot/tmp/data") or die "While symlinking: $!";
377 my $dev = Amanda::Device->new("file:$taperoot/tmp");
378 $dev->start($Amanda::Device::ACCESS_WRITE, "FOO?BAR", undef)
379 or die $dev->error_or_status();
381 or die $dev->error_or_status();
382 rmtree("$taperoot/tmp");
385 Amanda::MainLoop::run();
388 # inventory is pretty cool
390 my $try_inventory = make_cb('try_inventory' => sub {
391 $chg->inventory(inventory_cb => make_cb(sub {
392 my ($err, $inv) = @_;
396 { slot => 1, state => Amanda::Changer::SLOT_FULL,
397 device_status => $DEVICE_STATUS_VOLUME_UNLABELED,
398 f_type => $Amanda::Header::F_EMPTY, label => undef,
399 reserved => 0, current => 1},
400 { slot => 2, state => Amanda::Changer::SLOT_FULL,
401 device_status => $DEVICE_STATUS_VOLUME_UNLABELED,
402 f_type => $Amanda::Header::F_EMPTY, label => undef,
404 { slot => 3, state => Amanda::Changer::SLOT_FULL,
405 device_status => $DEVICE_STATUS_VOLUME_UNLABELED,
406 f_type => $Amanda::Header::F_EMPTY, label => undef,
408 { slot => 4, state => Amanda::Changer::SLOT_FULL,
409 device_status => $DEVICE_STATUS_SUCCESS,
410 f_type => $Amanda::Header::F_TAPESTART, label => "FOO?BAR",
412 { slot => 5, state => Amanda::Changer::SLOT_FULL,
413 device_status => $DEVICE_STATUS_VOLUME_UNLABELED,
414 f_type => $Amanda::Header::F_EMPTY, label => undef,
416 ], "inventory finds the labeled tape");
418 Amanda::MainLoop::quit();
423 Amanda::MainLoop::run();