1 # Copyright (c) 2008-2012 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 => 19;
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_Multi_test";
52 for my $slot (1 .. $nslots) {
53 mkdir("$taperoot/slot$slot")
54 or die("Could not mkdir: $!");
55 mkdir("$taperoot/slot$slot/data")
56 or die("Could not mkdir: $!");
60 # Build a configuration that specifies Amanda::Changer::Multi
61 my $testconf = Installcheck::Config->new();
64 my $cfg_result = config_init($CONFIG_INIT_EXPLICIT_NAME, 'TESTCONF');
65 if ($cfg_result != $CFGERR_OK) {
66 my ($level, @errors) = Amanda::Config::config_errors();
67 die(join "\n", @errors);
72 my $chg = Amanda::Changer->new("chg-multi:file:$taperoot/slot{0,1,2,3,4}");
73 die($chg) if $chg->isa("Amanda::Changer::Error");
74 is($chg->have_inventory(), '1', "changer have inventory");
77 my @reservations = ();
78 my ($release, $getres, $tryreserved);
80 $getres = make_cb(getres => sub {
81 my $slot = pop @slots;
83 return $tryreserved->();
86 $chg->load(slot => $slot,
87 set_current => ($slot == 5),
88 res_cb => make_cb(sub {
89 my ($err, $reservation) = @_;
90 ok(!$err, "no error loading slot $slot")
93 # keep this reservation
95 push @reservations, $reservation;
98 # and start on the next
103 $tryreserved = make_cb(tryreserved => sub {
104 # try to load an already-reserved slot
105 $chg->load(slot => 3,
107 my ($err, $reservation) = @_;
109 { message => qr/Slot 3 is already in use by process/,
111 reason => 'volinuse' },
112 "error when requesting already-reserved slot");
117 $release = make_cb(release => sub {
118 my $res = pop @reservations;
120 return Amanda::MainLoop::quit();
123 $res->release(finished_cb => sub {
131 @slots = ( 1, 3, 5 );
133 Amanda::MainLoop::run();
135 # and try it with some different slots, just to see
136 @slots = ( 4, 2, 3 );
138 Amanda::MainLoop::run();
143 # check relative slot ("current" and "next") functionality
144 sub test_relative_slot {
145 my ($finished_cb) = @_;
148 my $steps = define_steps
149 cb_ref => \$finished_cb;
151 step load_current => sub {
152 # load the "current" slot, which should be 4
153 $chg->load(relative_slot => "current", res_cb => $steps->{'check_current_cb'});
156 step check_current_cb => sub {
157 my ($err, $res) = @_;
160 is ($res->{'this_slot'}, 5, "'current' is slot 5");
161 $slot = $res->{'this_slot'};
163 $res->release(finished_cb => $steps->{'released1'});
166 step released1 => sub {
170 $chg->load(relative_slot => 'next', slot => $slot,
171 res_cb => $steps->{'check_next_cb'});
174 step check_next_cb => sub {
175 my ($err, $res) = @_;
178 is ($res->{'this_slot'}, 1, "'next' from there is slot 1");
180 $res->release(finished_cb => $steps->{'released2'});
183 step released2 => sub {
187 $chg->reset(finished_cb => $steps->{'reset_finished_cb'});
190 step reset_finished_cb => sub {
194 $chg->load(relative_slot => "current", res_cb => $steps->{'check_reset_cb'});
197 step check_reset_cb => sub {
198 my ($err, $res) = @_;
201 is ($res->{'this_slot'}, 1, "after reset, 'current' is slot 1");
203 $res->release(finished_cb => $steps->{'released3'});
206 step released3 => sub {
213 test_relative_slot(\&Amanda::MainLoop::quit);
214 Amanda::MainLoop::run();
216 # test loading relative_slot "next"
217 sub test_relative_slot_next {
218 my ($finished_cb) = @_;
220 my $steps = define_steps
221 cb_ref => \$finished_cb;
223 step load_next => sub {
224 $chg->load(relative_slot => "next",
226 my ($err, $res) = @_;
229 is ($res->{'this_slot'}, 2, "loading relative slot 'next' loads the correct slot");
231 $steps->{'release'}->($res);
236 step release => sub {
239 $res->release(finished_cb => sub {
247 test_relative_slot_next(\&Amanda::MainLoop::quit);
248 Amanda::MainLoop::run();
250 # scan the changer using except_slots
251 sub test_except_slots {
252 my ($finished_cb) = @_;
256 my $steps = define_steps
257 cb_ref => \$finished_cb;
260 $chg->load(slot => "5", except_slots => { %except_slots },
261 res_cb => $steps->{'loaded'});
265 my ($err, $res) = @_;
267 if ($err->notfound) {
268 # this means the scan is done
269 return $steps->{'quit'}->();
270 } elsif ($err->volinuse and defined $err->{'slot'}) {
271 $slot = $err->{'slot'};
276 $slot = $res->{'this_slot'};
279 $except_slots{$slot} = 1;
282 $res->release(finished_cb => $steps->{'released'});
284 $steps->{'released'}->();
288 step released => sub {
292 $chg->load(relative_slot => 'next', slot => $slot,
293 except_slots => { %except_slots },
294 res_cb => $steps->{'loaded'});
298 is_deeply({ %except_slots }, { 5=>1, 1=>1, 2=>1, 3=>1, 4=>1 },
299 "scanning with except_slots works");
303 test_except_slots(\&Amanda::MainLoop::quit);
304 Amanda::MainLoop::run();
306 # eject is not implemented
308 my $try_eject = make_cb('try_eject' => sub {
309 $chg->eject(finished_cb => make_cb(sub {
310 my ($err, $res) = @_;
311 is ($err, undef, "eject is implemented");
313 Amanda::MainLoop::quit();
318 Amanda::MainLoop::run();
321 # check num_slots and loading by label
323 my ($update, $get_info, $load_label, $check_load_cb) = @_;
325 $update = make_cb('update' => sub {
326 $chg->update(changed => "4", finished_cb => $get_info);
329 $get_info = make_cb('get_info' => sub {
330 $chg->info(info_cb => $load_label, info => [ 'num_slots', 'fast_search' ]);
333 $load_label = make_cb('load_label' => sub {
336 die($err) if defined($err);
338 is_deeply({ %results },
339 { num_slots => 5, fast_search => 0 },
340 "info() returns the correct num_slots and fast_search");
342 # note use of a glob metacharacter in the label name
343 $chg->load(label => "FOO?BAR", res_cb => $check_load_cb, set_current => 1);
346 $check_load_cb = make_cb('check_load_cb' => sub {
347 my ($err, $res) = @_;
350 is ($res->{'this_slot'}, 4, "labeled volume found in slot 4");
352 $res->release(finished_cb => sub {
356 Amanda::MainLoop::quit();
361 my $dev = Amanda::Device->new("file:$taperoot/slot3");
362 $dev->start($Amanda::Device::ACCESS_WRITE, "FOO?BAR", undef)
363 or die $dev->error_or_status();
365 or die $dev->error_or_status();
368 Amanda::MainLoop::run();
371 # inventory is pretty cool
373 my $try_inventory = make_cb('try_inventory' => sub {
374 $chg->inventory(inventory_cb => make_cb(sub {
375 my ($err, $inv) = @_;
379 { slot => 1, state => Amanda::Changer::SLOT_FULL,
380 device_status => $DEVICE_STATUS_DEVICE_ERROR,
381 device_error => "Error checking directory $taperoot/slot0/data/: No such file or directory",
382 f_type => undef, label => undef,
384 { slot => 2, state => Amanda::Changer::SLOT_FULL,
385 device_status => $DEVICE_STATUS_VOLUME_UNLABELED,
386 device_error => "File 0 not found",
387 f_type => $Amanda::Header::F_EMPTY, label => undef,
389 { slot => 3, state => Amanda::Changer::SLOT_FULL,
390 device_status => $DEVICE_STATUS_VOLUME_UNLABELED,
391 device_error => "File 0 not found",
392 f_type => $Amanda::Header::F_EMPTY, label => undef,
394 { slot => 4, state => Amanda::Changer::SLOT_FULL,
395 device_status => $DEVICE_STATUS_SUCCESS,
396 device_error => undef,
397 f_type => $Amanda::Header::F_TAPESTART, label => "FOO?BAR",
398 reserved => 0, current => 1 },
399 { slot => 5, state => Amanda::Changer::SLOT_FULL,
400 device_status => $DEVICE_STATUS_VOLUME_UNLABELED,
401 device_error => "File 0 not found",
402 f_type => $Amanda::Header::F_EMPTY, label => undef,
404 ], "inventory finds the labeled tape");
406 Amanda::MainLoop::quit();
411 Amanda::MainLoop::run();
414 # check loading by label if the state is unknown
416 my ($update, $load_label, $check_load_cb) = @_;
418 $update = make_cb('update' => sub {
419 $chg->update(changed => "4=", finished_cb => $load_label);
422 $load_label = make_cb('load_label' => sub {
423 # note use of a glob metacharacter in the label name
424 $chg->load(label => "FOO?BAR", res_cb => $check_load_cb);
427 $check_load_cb = make_cb('check_load_cb' => sub {
428 my ($err, $res) = @_;
429 die("labeled volume found in slot 4") if $res;
430 die("bad error: $err") if $err && !$err->notfound;
432 ok(1, "Don't found label for unknown state slot");
433 Amanda::MainLoop::quit();
437 Amanda::MainLoop::run();