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 => 18;
24 use lib "@amperldir@";
26 use Installcheck::Config;
27 use Installcheck::Changer;
29 use Amanda::Device qw( :constants );
32 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();
42 my $taperoot = "$Installcheck::TMP/Amanda_Changer_Disk_test";
52 for my $slot (1 .. $nslots) {
53 mkdir("$taperoot/slot$slot")
54 or die("Could not mkdir: $!");
59 my ($res, $slot, $msg) = @_;
61 my ($datalink) = ($res->{'device'}->device_name =~ /file:(.*)/);
63 is(readlink($datalink), "../slot$slot", $msg);
66 # Build a configuration that specifies Amanda::Changer::Disk
67 my $testconf = Installcheck::Config->new();
70 my $cfg_result = config_init($CONFIG_INIT_EXPLICIT_NAME, 'TESTCONF');
71 if ($cfg_result != $CFGERR_OK) {
72 my ($level, @errors) = Amanda::Config::config_errors();
73 die(join "\n", @errors);
79 my $chg = Amanda::Changer->new("chg-disk:$taperoot/foo");
81 { message => qr/directory '.*' does not exist/,
83 "detects nonexistent directory");
85 $chg = Amanda::Changer->new("chg-disk:$taperoot");
86 die($chg) if $chg->isa("Amanda::Changer::Error");
89 my ($finished_cb, @slots) = @_;
90 my @reservations = ();
93 my $steps = define_steps
94 cb_ref => \$finished_cb;
99 return $steps->{'tryreserved'}->();
102 $chg->load(slot => $slot,
103 set_current => ($slot == 5),
104 res_cb => make_cb($steps->{'loaded'}));
108 my ($err, $reservation) = @_;
109 ok(!$err, "no error loading slot $slot")
112 # keep this reservation
114 push @reservations, $reservation;
117 # and start on the next
118 $steps->{'getres'}->();
121 step tryreserved => sub {
122 # try to load an already-reserved slot
123 $chg->load(slot => 3,
125 my ($err, $reservation) = @_;
127 { message => qr/Slot 3 is already in use by drive/,
129 reason => 'volinuse' },
130 "error when requesting already-reserved slot");
131 $steps->{'release'}->();
135 step release => sub {
136 my $res = pop @reservations;
138 return Amanda::MainLoop::quit();
141 $res->release(finished_cb => sub {
144 $steps->{'release'}->();
150 test_reserved(\&Amanda::MainLoop::quit, 1, 3, 5);
151 Amanda::MainLoop::run();
153 # and try it with some different slots, just to see
154 test_reserved(\&Amanda::MainLoop::quit, 4, 2, 3);
155 Amanda::MainLoop::run();
157 # check relative slot ("current" and "next") functionality
158 sub test_relative_slot {
159 my ($finished_cb) = @_;
162 my $steps = define_steps
163 cb_ref => \$finished_cb;
165 # load the "current" slot, which should be 3
166 step load_current => sub {
167 $chg->load(relative_slot => "current", res_cb => $steps->{'check_current_cb'});
170 step check_current_cb => sub {
171 my ($err, $res) = @_;
174 is_pointing_to($res, 5, "'current' is slot 5");
175 $slot = $res->{'this_slot'};
177 $res->release(finished_cb => $steps->{'released1'});
180 step released1 => sub {
184 $chg->load(relative_slot => 'next', slot => $slot,
185 res_cb => $steps->{'check_next_cb'});
188 step check_next_cb => sub {
189 my ($err, $res) = @_;
192 is_pointing_to($res, 1, "'next' from there is slot 1");
194 $res->release(finished_cb => $steps->{'released2'});
197 step released2 => sub {
201 $chg->reset(finished_cb => $steps->{'reset_finished_cb'});
204 step reset_finished_cb => sub {
208 $chg->load(relative_slot => "current", res_cb => $steps->{'check_reset_cb'});
211 step check_reset_cb => sub {
212 my ($err, $res) = @_;
215 is_pointing_to($res, 1, "after reset, 'current' is slot 1");
217 $res->release(finished_cb => $steps->{'released3'});
220 step released3 => sub {
227 test_relative_slot(\&Amanda::MainLoop::quit);
228 Amanda::MainLoop::run();
230 # test loading relative_slot "next"
231 sub test_relative_next {
232 my ($finished_cb) = @_;
234 my $steps = define_steps
235 cb_ref => \$finished_cb;
237 step load_next => sub {
238 $chg->load(relative_slot => "next",
240 my ($err, $res) = @_;
243 is_pointing_to($res, 2, "loading relative slot 'next' loads the correct slot");
245 $steps->{'release'}->($res);
250 step release => sub {
253 $res->release(finished_cb => sub {
261 test_relative_next(\&Amanda::MainLoop::quit);
262 Amanda::MainLoop::run();
264 # scan the changer using except_slots
265 sub test_except_slots {
266 my ($finished_cb) = @_;
270 my $steps = define_steps
271 cb_ref => \$finished_cb;
274 $chg->load(slot => "5", except_slots => { %except_slots },
275 res_cb => $steps->{'loaded'});
279 my ($err, $res) = @_;
281 if ($err->notfound) {
282 # this means the scan is done
283 return $steps->{'quit'}->();
284 } elsif ($err->volinuse and defined $err->{'slot'}) {
285 $slot = $err->{'slot'};
290 $slot = $res->{'this_slot'};
293 $except_slots{$slot} = 1;
296 $res->release(finished_cb => $steps->{'released'});
298 $steps->{'released'}->();
302 step released => sub {
306 $chg->load(relative_slot => 'next', slot => $slot,
307 except_slots => { %except_slots },
308 res_cb => $steps->{'loaded'});
312 is_deeply({ %except_slots }, { 5=>1, 1=>1, 2=>1, 3=>1, 4=>1 },
313 "scanning with except_slots works");
317 test_except_slots(\&Amanda::MainLoop::quit);
318 Amanda::MainLoop::run();
320 # eject is not implemented
322 my $try_eject = make_cb('try_eject' => sub {
323 $chg->eject(finished_cb => make_cb(sub {
324 my ($err, $res) = @_;
326 { type => 'failed', reason => 'notimpl' },
327 "eject returns a failed/notimpl error");
329 Amanda::MainLoop::quit();
334 Amanda::MainLoop::run();
337 # check num_slots and loading by label
339 my ($get_info, $load_label, $check_load_cb) = @_;
341 $get_info = make_cb('get_info' => sub {
342 $chg->info(info_cb => $load_label, info => [ 'num_slots', 'fast_search' ]);
345 $load_label = make_cb('load_label' => sub {
348 die($err) if defined($err);
350 is_deeply({ %results },
351 { num_slots => 5, fast_search => 1 },
352 "info() returns the correct num_slots and fast_search");
354 # note use of a glob metacharacter in the label name
355 $chg->load(label => "FOO?BAR", res_cb => $check_load_cb);
358 $check_load_cb = make_cb('check_load_cb' => sub {
359 my ($err, $res) = @_;
362 is_pointing_to($res, 4, "labeled volume found in slot 4");
364 $res->release(finished_cb => sub {
368 Amanda::MainLoop::quit();
372 # label slot 4, using our own symlink
373 mkpath("$taperoot/tmp");
374 symlink("../slot4", "$taperoot/tmp/data") or die "While symlinking: $!";
375 my $dev = Amanda::Device->new("file:$taperoot/tmp");
376 $dev->start($Amanda::Device::ACCESS_WRITE, "FOO?BAR", undef)
377 or die $dev->error_or_status();
379 or die $dev->error_or_status();
380 rmtree("$taperoot/tmp");
383 Amanda::MainLoop::run();
386 # inventory is pretty cool
388 my $try_inventory = make_cb('try_inventory' => sub {
389 $chg->inventory(inventory_cb => make_cb(sub {
390 my ($err, $inv) = @_;
394 { slot => 1, state => Amanda::Changer::SLOT_FULL,
395 device_status => $DEVICE_STATUS_SUCCESS,
396 f_type => $Amanda::Header::F_EMPTY, label => undef,
397 reserved => 0, current => 1},
398 { slot => 2, state => Amanda::Changer::SLOT_FULL,
399 device_status => $DEVICE_STATUS_SUCCESS,
400 f_type => $Amanda::Header::F_EMPTY, label => undef,
402 { slot => 3, state => Amanda::Changer::SLOT_FULL,
403 device_status => $DEVICE_STATUS_SUCCESS,
404 f_type => $Amanda::Header::F_EMPTY, label => undef,
406 { slot => 4, state => Amanda::Changer::SLOT_FULL,
407 device_status => $DEVICE_STATUS_SUCCESS,
408 f_type => $Amanda::Header::F_TAPESTART, label => "FOO?BAR",
410 { slot => 5, state => Amanda::Changer::SLOT_FULL,
411 device_status => $DEVICE_STATUS_SUCCESS,
412 f_type => $Amanda::Header::F_EMPTY, label => undef,
414 ], "inventory finds the labeled tape");
416 Amanda::MainLoop::quit();
421 Amanda::MainLoop::run();