f4b8f8329783ee6cf559ab7f2c00924f878b1f3e
[debian/amanda] / installcheck / Amanda_Changer_single.pl
1 # Copyright (c) 2005-2008 Zmanda Inc.  All Rights Reserved.
2 #
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.
6 #
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
10 # for more details.
11 #
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
15 #
16 # Contact information: Zmanda Inc, 465 S Mathlida Ave, Suite 300
17 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
18
19 use Test::More tests => 4;
20 use File::Path;
21 use strict;
22
23 use lib "@amperldir@";
24 use Installcheck::Config;
25 use Amanda::Paths;
26 use Amanda::Device;
27 use Amanda::Debug;
28 use Amanda::MainLoop;
29 use Amanda::Config qw( :init :getconf config_dir_relative );
30 use Amanda::Changer;
31
32 # set up debugging so debug output doesn't interfere with test results
33 Amanda::Debug::dbopen("installcheck");
34
35 # and disable Debug's die() and warn() overrides
36 Amanda::Debug::disable_die_override();
37
38 my $testconf = Installcheck::Config->new();
39 $testconf->write();
40
41 my $cfg_result = config_init($CONFIG_INIT_EXPLICIT_NAME, 'TESTCONF');
42 if ($cfg_result != $CFGERR_OK) {
43     my ($level, @errors) = Amanda::Config::config_errors();
44     die(join "\n", @errors);
45 }
46
47 my $chg = Amanda::Changer->new("chg-single:tape:/foo");
48
49 {
50     my ($held_res);
51     my ($get_info, $get_res, $got_res, $got_second_res);
52
53     $get_info = sub {
54         $chg->info(info_cb => $get_res, info => [ 'num_slots' ]);
55     };
56
57     $get_res = sub {
58         my $err = shift;
59         my %results = @_;
60         die($err) if defined($err);
61
62         is($results{'num_slots'}, 1, "info() returns the correct num_slots");
63
64         $chg->load(slot => "current",
65                    res_cb => $got_res);
66     };
67
68     $got_res = sub {
69         my ($err, $res) = @_;
70         ok(!$err, "no error loading slot 'current'")
71             or diag($err);
72         is($res->{'device_name'}, 'tape:/foo',
73             "returns correct device name");
74
75         $held_res = $res; # hang onto it while loading another slot
76
77         $chg->load(label => "FOO!",
78                    res_cb => $got_second_res);
79     };
80
81     $got_second_res = sub {
82         my ($err, $res) = @_;
83         ok($err, "second simultaneous reservation rejected");
84
85         Amanda::MainLoop::quit();
86     };
87
88     # start the loop
89     Amanda::MainLoop::call_later($get_info);
90     Amanda::MainLoop::run();
91 }