Imported Upstream version 3.2.0
[debian/amanda] / installcheck / Amanda_Changer_single.pl
1 # Copyright (c) 2008, 2009, 2010 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. Mathilda Ave., Suite 300
17 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
18
19 use Test::More tests => 6;
20 use File::Path;
21 use strict;
22 use warnings;
23
24 use lib "@amperldir@";
25 use Installcheck::Config;
26 use Installcheck::Changer;
27 use Amanda::Paths;
28 use Amanda::Device;
29 use Amanda::Debug;
30 use Amanda::MainLoop;
31 use Amanda::Config qw( :init :getconf config_dir_relative );
32 use Amanda::Changer;
33
34 # set up debugging so debug output doesn't interfere with test results
35 Amanda::Debug::dbopen("installcheck");
36 Installcheck::log_test_output();
37
38 # and disable Debug's die() and warn() overrides
39 Amanda::Debug::disable_die_override();
40
41 my $testconf = Installcheck::Config->new();
42 $testconf->write();
43
44 my $cfg_result = config_init($CONFIG_INIT_EXPLICIT_NAME, 'TESTCONF');
45 if ($cfg_result != $CFGERR_OK) {
46     my ($level, @errors) = Amanda::Config::config_errors();
47     die(join "\n", @errors);
48 }
49
50 my $chg = Amanda::Changer->new("chg-single:null:/foo");
51 die "$chg" if $chg->isa("Amanda::Changer::Error");
52
53 {
54     my ($held_res);
55     my ($get_info, $get_res, $got_res, $got_second_res);
56
57     $get_info = make_cb('get_info' => sub {
58         $chg->info(info_cb => $get_res, info => [ 'num_slots', 'fast_search' ]);
59     });
60
61     $get_res = make_cb('get_res' => sub {
62         my $err = shift;
63         my %results = @_;
64         die($err) if defined($err);
65
66         is($results{'num_slots'}, 1, "info() returns the correct num_slots");
67         is($results{'fast_search'}, 0, "info() returns the correct fast_slots");
68
69         $chg->load(relative_slot => "current",
70                    res_cb => $got_res);
71     });
72
73     $got_res = make_cb('got_res' => sub {
74         my ($err, $res) = @_;
75         ok(!$err, "no error loading relative slot 'current'")
76             or diag($err);
77         is($res->{'device'}->device_name(), 'null:/foo',
78             "returns correct device name");
79
80         $held_res = $res; # hang onto it while loading another slot
81
82         $chg->load(label => "FOO!",
83                    res_cb => $got_second_res);
84     });
85
86     $got_second_res = make_cb('got_second_res' => sub {
87         my ($err, $res) = @_;
88         chg_err_like($err,
89             { message => qr{'null:/foo' is already reserved},
90               type => 'failed',
91               reason => 'volinuse' },
92             "second simultaneous reservation rejected");
93
94         # release the first reservation
95         $held_res->release(finished_cb => sub {
96             my ($err) = @_;
97             die $err if $err;
98
99             Amanda::MainLoop::quit();
100         });
101     });
102
103     # start the loop
104     $get_info->();
105     Amanda::MainLoop::run();
106 }
107
108 $chg = Amanda::Changer->new("chg-single:bogus:device");
109 is("$chg",
110     "chg-single: error opening device 'bogus:device': Device type bogus is not known.",
111     "bogus device name detected early");