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_Multi_test";
53 for my $slot (1 .. $nslots) {
54 mkdir("$taperoot/slot$slot")
55 or die("Could not mkdir: $!");
56 mkdir("$taperoot/slot$slot/data")
57 or die("Could not mkdir: $!");
61 # Build a configuration that specifies Amanda::Changer::Multi
62 my $testconf = Installcheck::Config->new();
65 my $cfg_result = config_init($CONFIG_INIT_EXPLICIT_NAME, 'TESTCONF');
66 if ($cfg_result != $CFGERR_OK) {
67 my ($level, @errors) = Amanda::Config::config_errors();
68 die(join "\n", @errors);
73 my $chg = Amanda::Changer->new("chg-multi:file:$taperoot/slot{0,1,2,3,4}");
74 die($chg) if $chg->isa("Amanda::Changer::Error");
75 is($chg->have_inventory(), '1', "changer have inventory");
78 my @reservations = ();
79 my ($release, $getres, $tryreserved);
81 $getres = make_cb(getres => sub {
82 my $slot = pop @slots;
84 return $tryreserved->();
87 $chg->load(slot => $slot,
88 set_current => ($slot == 5),
89 res_cb => make_cb(sub {
90 my ($err, $reservation) = @_;
91 ok(!$err, "no error loading slot $slot")
94 # keep this reservation
96 push @reservations, $reservation;
99 # and start on the next
104 $tryreserved = make_cb(tryreserved => sub {
105 # try to load an already-reserved slot
106 $chg->load(slot => 3,
108 my ($err, $reservation) = @_;
110 { message => qr/Slot 3 is already in use by process/,
112 reason => 'volinuse' },
113 "error when requesting already-reserved slot");
118 $release = make_cb(release => sub {
119 my $res = pop @reservations;
121 return Amanda::MainLoop::quit();
124 $res->release(finished_cb => sub {
132 @slots = ( 1, 3, 5 );
134 Amanda::MainLoop::run();
136 # and try it with some different slots, just to see
137 @slots = ( 4, 2, 3 );
139 Amanda::MainLoop::run();
144 # check relative slot ("current" and "next") functionality
145 sub test_relative_slot {
146 my ($finished_cb) = @_;
149 my $steps = define_steps
150 cb_ref => \$finished_cb;
152 step load_current => sub {
153 # load the "current" slot, which should be 4
154 $chg->load(relative_slot => "current", res_cb => $steps->{'check_current_cb'});
157 step check_current_cb => sub {
158 my ($err, $res) = @_;
161 is ($res->{'this_slot'}, 5, "'current' is slot 5");
162 $slot = $res->{'this_slot'};
164 $res->release(finished_cb => $steps->{'released1'});
167 step released1 => sub {
171 $chg->load(relative_slot => 'next', slot => $slot,
172 res_cb => $steps->{'check_next_cb'});
175 step check_next_cb => sub {
176 my ($err, $res) = @_;
179 is ($res->{'this_slot'}, 1, "'next' from there is slot 1");
181 $res->release(finished_cb => $steps->{'released2'});
184 step released2 => sub {
188 $chg->reset(finished_cb => $steps->{'reset_finished_cb'});
191 step reset_finished_cb => sub {
195 $chg->load(relative_slot => "current", res_cb => $steps->{'check_reset_cb'});
198 step check_reset_cb => sub {
199 my ($err, $res) = @_;
202 is ($res->{'this_slot'}, 1, "after reset, 'current' is slot 1");
204 $res->release(finished_cb => $steps->{'released3'});
207 step released3 => sub {
214 test_relative_slot(\&Amanda::MainLoop::quit);
215 Amanda::MainLoop::run();
217 # test loading relative_slot "next"
218 sub test_relative_slot_next {
219 my ($finished_cb) = @_;
221 my $steps = define_steps
222 cb_ref => \$finished_cb;
224 step load_next => sub {
225 $chg->load(relative_slot => "next",
227 my ($err, $res) = @_;
230 is ($res->{'this_slot'}, 2, "loading relative slot 'next' loads the correct slot");
232 $steps->{'release'}->($res);
237 step release => sub {
240 $res->release(finished_cb => sub {
248 test_relative_slot_next(\&Amanda::MainLoop::quit);
249 Amanda::MainLoop::run();
251 # scan the changer using except_slots
252 sub test_except_slots {
253 my ($finished_cb) = @_;
257 my $steps = define_steps
258 cb_ref => \$finished_cb;
261 $chg->load(slot => "5", except_slots => { %except_slots },
262 res_cb => $steps->{'loaded'});
266 my ($err, $res) = @_;
268 if ($err->notfound) {
269 # this means the scan is done
270 return $steps->{'quit'}->();
271 } elsif ($err->volinuse and defined $err->{'slot'}) {
272 $slot = $err->{'slot'};
277 $slot = $res->{'this_slot'};
280 $except_slots{$slot} = 1;
283 $res->release(finished_cb => $steps->{'released'});
285 $steps->{'released'}->();
289 step released => sub {
293 $chg->load(relative_slot => 'next', slot => $slot,
294 except_slots => { %except_slots },
295 res_cb => $steps->{'loaded'});
299 is_deeply({ %except_slots }, { 5=>1, 1=>1, 2=>1, 3=>1, 4=>1 },
300 "scanning with except_slots works");
304 test_except_slots(\&Amanda::MainLoop::quit);
305 Amanda::MainLoop::run();
307 # eject is not implemented
309 my $try_eject = make_cb('try_eject' => sub {
310 $chg->eject(finished_cb => make_cb(sub {
311 my ($err, $res) = @_;
312 is ($err, undef, "eject is implemented");
314 Amanda::MainLoop::quit();
319 Amanda::MainLoop::run();
322 # check num_slots and loading by label
324 my ($update, $get_info, $load_label, $check_load_cb) = @_;
326 $update = make_cb('update' => sub {
327 $chg->update(changed => "4", finished_cb => $get_info);
330 $get_info = make_cb('get_info' => sub {
331 $chg->info(info_cb => $load_label, info => [ 'num_slots', 'fast_search' ]);
334 $load_label = make_cb('load_label' => sub {
337 die($err) if defined($err);
339 is_deeply({ %results },
340 { num_slots => 5, fast_search => 0 },
341 "info() returns the correct num_slots and fast_search");
343 # note use of a glob metacharacter in the label name
344 $chg->load(label => "FOO?BAR", res_cb => $check_load_cb, set_current => 1);
347 $check_load_cb = make_cb('check_load_cb' => sub {
348 my ($err, $res) = @_;
351 is ($res->{'this_slot'}, 4, "labeled volume found in slot 4");
353 $res->release(finished_cb => sub {
357 Amanda::MainLoop::quit();
362 my $dev = Amanda::Device->new("file:$taperoot/slot3");
363 $dev->start($Amanda::Device::ACCESS_WRITE, "FOO?BAR", undef)
364 or die $dev->error_or_status();
366 or die $dev->error_or_status();
369 Amanda::MainLoop::run();
372 # inventory is pretty cool
374 my $try_inventory = make_cb('try_inventory' => sub {
375 $chg->inventory(inventory_cb => make_cb(sub {
376 my ($err, $inv) = @_;
380 { slot => 1, state => Amanda::Changer::SLOT_FULL,
381 device_status => $DEVICE_STATUS_DEVICE_ERROR,
382 device_error => "Error checking directory $taperoot/slot0/data/: No such file or directory",
383 f_type => undef, label => undef,
385 { slot => 2, state => Amanda::Changer::SLOT_FULL,
386 device_status => $DEVICE_STATUS_VOLUME_UNLABELED,
387 device_error => "File 0 not found",
388 f_type => $Amanda::Header::F_EMPTY, label => undef,
390 { slot => 3, state => Amanda::Changer::SLOT_FULL,
391 device_status => $DEVICE_STATUS_VOLUME_UNLABELED,
392 device_error => "File 0 not found",
393 f_type => $Amanda::Header::F_EMPTY, label => undef,
395 { slot => 4, state => Amanda::Changer::SLOT_FULL,
396 device_status => $DEVICE_STATUS_SUCCESS,
397 device_error => undef,
398 f_type => $Amanda::Header::F_TAPESTART, label => "FOO?BAR",
399 reserved => 0, current => 1 },
400 { slot => 5, state => Amanda::Changer::SLOT_FULL,
401 device_status => $DEVICE_STATUS_VOLUME_UNLABELED,
402 device_error => "File 0 not found",
403 f_type => $Amanda::Header::F_EMPTY, label => undef,
405 ], "inventory finds the labeled tape");
407 Amanda::MainLoop::quit();
412 Amanda::MainLoop::run();
415 # check loading by label if the state is unknown
417 my ($update, $load_label, $check_load_cb) = @_;
419 $update = make_cb('update' => sub {
420 $chg->update(changed => "4=", finished_cb => $load_label);
423 $load_label = make_cb('load_label' => sub {
424 # note use of a glob metacharacter in the label name
425 $chg->load(label => "FOO?BAR", res_cb => $check_load_cb);
428 $check_load_cb = make_cb('check_load_cb' => sub {
429 my ($err, $res) = @_;
430 die("labeled volume found in slot 4") if $res;
431 die("bad error: $err") if $err && !$err->notfound;
433 ok(1, "Don't found label for unknown state slot");
434 Amanda::MainLoop::quit();
438 Amanda::MainLoop::run();