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_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");
76 my @reservations = ();
77 my ($release, $getres, $tryreserved);
79 $getres = make_cb(getres => sub {
80 my $slot = pop @slots;
82 return $tryreserved->();
85 $chg->load(slot => $slot,
86 set_current => ($slot == 5),
87 res_cb => make_cb(sub {
88 my ($err, $reservation) = @_;
89 ok(!$err, "no error loading slot $slot")
92 # keep this reservation
94 push @reservations, $reservation;
97 # and start on the next
102 $tryreserved = make_cb(tryreserved => sub {
103 # try to load an already-reserved slot
104 $chg->load(slot => 3,
106 my ($err, $reservation) = @_;
108 { message => qr/Slot 3 is already in use by process/,
110 reason => 'volinuse' },
111 "error when requesting already-reserved slot");
116 $release = make_cb(release => sub {
117 my $res = pop @reservations;
119 return Amanda::MainLoop::quit();
122 $res->release(finished_cb => sub {
130 @slots = ( 1, 3, 5 );
132 Amanda::MainLoop::run();
134 # and try it with some different slots, just to see
135 @slots = ( 4, 2, 3 );
137 Amanda::MainLoop::run();
142 # check relative slot ("current" and "next") functionality
143 sub test_relative_slot {
144 my ($finished_cb) = @_;
147 my $steps = define_steps
148 cb_ref => \$finished_cb;
150 step load_current => sub {
151 # load the "current" slot, which should be 4
152 $chg->load(relative_slot => "current", res_cb => $steps->{'check_current_cb'});
155 step check_current_cb => sub {
156 my ($err, $res) = @_;
159 is ($res->{'this_slot'}, 5, "'current' is slot 5");
160 $slot = $res->{'this_slot'};
162 $res->release(finished_cb => $steps->{'released1'});
165 step released1 => sub {
169 $chg->load(relative_slot => 'next', slot => $slot,
170 res_cb => $steps->{'check_next_cb'});
173 step check_next_cb => sub {
174 my ($err, $res) = @_;
177 is ($res->{'this_slot'}, 1, "'next' from there is slot 1");
179 $res->release(finished_cb => $steps->{'released2'});
182 step released2 => sub {
186 $chg->reset(finished_cb => $steps->{'reset_finished_cb'});
189 step reset_finished_cb => sub {
193 $chg->load(relative_slot => "current", res_cb => $steps->{'check_reset_cb'});
196 step check_reset_cb => sub {
197 my ($err, $res) = @_;
200 is ($res->{'this_slot'}, 1, "after reset, 'current' is slot 1");
202 $res->release(finished_cb => $steps->{'released3'});
205 step released3 => sub {
212 test_relative_slot(\&Amanda::MainLoop::quit);
213 Amanda::MainLoop::run();
215 # test loading relative_slot "next"
216 sub test_relative_slot_next {
217 my ($finished_cb) = @_;
219 my $steps = define_steps
220 cb_ref => \$finished_cb;
222 step load_next => sub {
223 $chg->load(relative_slot => "next",
225 my ($err, $res) = @_;
228 is ($res->{'this_slot'}, 2, "loading relative slot 'next' loads the correct slot");
230 $steps->{'release'}->($res);
235 step release => sub {
238 $res->release(finished_cb => sub {
246 test_relative_slot_next(\&Amanda::MainLoop::quit);
247 Amanda::MainLoop::run();
249 # scan the changer using except_slots
250 sub test_except_slots {
251 my ($finished_cb) = @_;
255 my $steps = define_steps
256 cb_ref => \$finished_cb;
259 $chg->load(slot => "5", except_slots => { %except_slots },
260 res_cb => $steps->{'loaded'});
264 my ($err, $res) = @_;
266 if ($err->notfound) {
267 # this means the scan is done
268 return $steps->{'quit'}->();
269 } elsif ($err->volinuse and defined $err->{'slot'}) {
270 $slot = $err->{'slot'};
275 $slot = $res->{'this_slot'};
278 $except_slots{$slot} = 1;
281 $res->release(finished_cb => $steps->{'released'});
283 $steps->{'released'}->();
287 step released => sub {
291 $chg->load(relative_slot => 'next', slot => $slot,
292 except_slots => { %except_slots },
293 res_cb => $steps->{'loaded'});
297 is_deeply({ %except_slots }, { 5=>1, 1=>1, 2=>1, 3=>1, 4=>1 },
298 "scanning with except_slots works");
302 test_except_slots(\&Amanda::MainLoop::quit);
303 Amanda::MainLoop::run();
305 # eject is not implemented
307 my $try_eject = make_cb('try_eject' => sub {
308 $chg->eject(finished_cb => make_cb(sub {
309 my ($err, $res) = @_;
310 is ($err, undef, "eject is implemented");
312 Amanda::MainLoop::quit();
317 Amanda::MainLoop::run();
320 # check num_slots and loading by label
322 my ($update, $get_info, $load_label, $check_load_cb) = @_;
324 $update = make_cb('update' => sub {
325 $chg->update(changed => "4", finished_cb => $get_info);
328 $get_info = make_cb('get_info' => sub {
329 $chg->info(info_cb => $load_label, info => [ 'num_slots', 'fast_search' ]);
332 $load_label = make_cb('load_label' => sub {
335 die($err) if defined($err);
337 is_deeply({ %results },
338 { num_slots => 5, fast_search => 0 },
339 "info() returns the correct num_slots and fast_search");
341 # note use of a glob metacharacter in the label name
342 $chg->load(label => "FOO?BAR", res_cb => $check_load_cb, set_current => 1);
345 $check_load_cb = make_cb('check_load_cb' => sub {
346 my ($err, $res) = @_;
349 is ($res->{'this_slot'}, 4, "labeled volume found in slot 4");
351 $res->release(finished_cb => sub {
355 Amanda::MainLoop::quit();
360 my $dev = Amanda::Device->new("file:$taperoot/slot3");
361 $dev->start($Amanda::Device::ACCESS_WRITE, "FOO?BAR", undef)
362 or die $dev->error_or_status();
364 or die $dev->error_or_status();
367 Amanda::MainLoop::run();
370 # inventory is pretty cool
372 my $try_inventory = make_cb('try_inventory' => sub {
373 $chg->inventory(inventory_cb => make_cb(sub {
374 my ($err, $inv) = @_;
378 { slot => 1, state => Amanda::Changer::SLOT_FULL,
379 device_status => $DEVICE_STATUS_DEVICE_ERROR,
380 f_type => undef, label => undef,
382 { slot => 2, state => Amanda::Changer::SLOT_FULL,
383 device_status => ($DEVICE_STATUS_DEVICE_ERROR |
384 $DEVICE_STATUS_VOLUME_UNLABELED |
385 $DEVICE_STATUS_VOLUME_ERROR),
386 f_type => undef, label => undef,
388 { slot => 3, state => Amanda::Changer::SLOT_FULL,
389 device_status => ($DEVICE_STATUS_DEVICE_ERROR |
390 $DEVICE_STATUS_VOLUME_UNLABELED |
391 $DEVICE_STATUS_VOLUME_ERROR),
392 f_type => undef, label => undef,
394 { slot => 4, state => Amanda::Changer::SLOT_FULL,
395 device_status => $DEVICE_STATUS_SUCCESS,
396 f_type => $Amanda::Header::F_TAPESTART, label => "FOO?BAR",
397 reserved => 0, current => 1 },
398 { slot => 5, state => Amanda::Changer::SLOT_FULL,
399 device_status => ($DEVICE_STATUS_DEVICE_ERROR |
400 $DEVICE_STATUS_VOLUME_UNLABELED |
401 $DEVICE_STATUS_VOLUME_ERROR),
402 f_type => undef, 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();