Imported Upstream version 3.3.3
[debian/amanda] / installcheck / Amanda_Changer_disk.pl
1 # Copyright (c) 2008-2012 Zmanda, Inc.  All Rights Reserved.
2 #
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.
7 #
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
11 # for more details.
12 #
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
16 #
17 # Contact information: Zmanda Inc, 465 S. Mathilda Ave., Suite 300
18 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
19
20 use Test::More tests => 19;
21 use File::Path;
22 use strict;
23 use warnings;
24
25 use lib "@amperldir@";
26 use Installcheck;
27 use Installcheck::Config;
28 use Installcheck::Changer;
29 use Amanda::Paths;
30 use Amanda::Device qw( :constants );
31 use Amanda::Debug;
32 use Amanda::MainLoop;
33 use Amanda::Config qw( :init :getconf config_dir_relative );
34 use Amanda::Changer;
35
36 # set up debugging so debug output doesn't interfere with test results
37 Amanda::Debug::dbopen("installcheck");
38 Installcheck::log_test_output();
39
40 # and disable Debug's die() and warn() overrides
41 Amanda::Debug::disable_die_override();
42
43 my $taperoot = "$Installcheck::TMP/Amanda_Changer_Disk_test";
44
45 sub reset_taperoot {
46     my ($nslots) = @_;
47
48     if (-d $taperoot) {
49         rmtree($taperoot);
50     }
51     mkpath($taperoot);
52
53     for my $slot (1 .. $nslots) {
54         mkdir("$taperoot/slot$slot")
55             or die("Could not mkdir: $!");
56     }
57 }
58
59 sub is_pointing_to {
60     my ($res, $slot, $msg) = @_;
61
62     my ($datalink) = ($res->{'device'}->device_name =~ /file:(.*)/);
63     $datalink .= "/data";
64     is(readlink($datalink), "../slot$slot", $msg);
65 }
66
67 # Build a configuration that specifies Amanda::Changer::Disk
68 my $testconf = Installcheck::Config->new();
69 $testconf->write();
70
71 my $cfg_result = config_init($CONFIG_INIT_EXPLICIT_NAME, 'TESTCONF');
72 if ($cfg_result != $CFGERR_OK) {
73     my ($level, @errors) = Amanda::Config::config_errors();
74     die(join "\n", @errors);
75 }
76
77 reset_taperoot(5);
78
79 # first try an error
80 my $chg = Amanda::Changer->new("chg-disk:$taperoot/foo");
81 chg_err_like($chg,
82     { message => qr/directory '.*' does not exist/,
83       type => 'fatal' },
84     "detects nonexistent directory");
85
86 $chg = Amanda::Changer->new("chg-disk:$taperoot");
87 die($chg) if $chg->isa("Amanda::Changer::Error");
88 is($chg->have_inventory(), '1', "changer have inventory");
89
90 sub test_reserved {
91     my ($finished_cb, @slots) = @_;
92     my @reservations = ();
93     my $slot;
94
95     my $steps = define_steps
96         cb_ref => \$finished_cb;
97
98     step getres => sub {
99         $slot = pop @slots;
100         if (!defined $slot) {
101             return $steps->{'tryreserved'}->();
102         }
103
104         $chg->load(slot => $slot,
105                    set_current => ($slot == 5),
106                    res_cb => make_cb($steps->{'loaded'}));
107     };
108
109     step loaded => sub {
110         my ($err, $reservation) = @_;
111         ok(!$err, "no error loading slot $slot")
112             or diag($err);
113
114         # keep this reservation
115         if ($reservation) {
116             push @reservations, $reservation;
117         }
118
119         # and start on the next
120         $steps->{'getres'}->();
121     };
122
123     step tryreserved => sub {
124         # try to load an already-reserved slot
125         $chg->load(slot => 3,
126                    res_cb => sub {
127             my ($err, $reservation) = @_;
128             chg_err_like($err,
129                 { message => qr/Slot 3 is already in use by drive/,
130                   type => 'failed',
131                   reason => 'volinuse' },
132                 "error when requesting already-reserved slot");
133             $steps->{'release'}->();
134         });
135     };
136
137     step release => sub {
138         my $res = pop @reservations;
139         if (!defined $res) {
140             return Amanda::MainLoop::quit();
141         }
142
143         $res->release(finished_cb => sub {
144             my ($err) = @_;
145             die $err if $err;
146             $steps->{'release'}->();
147         });
148     };
149 }
150
151 # start the loop
152 test_reserved(\&Amanda::MainLoop::quit, 1, 3, 5);
153 Amanda::MainLoop::run();
154
155 # and try it with some different slots, just to see
156 test_reserved(\&Amanda::MainLoop::quit, 4, 2, 3);
157 Amanda::MainLoop::run();
158
159 # check relative slot ("current" and "next") functionality
160 sub test_relative_slot {
161     my ($finished_cb) = @_;
162     my $slot;
163
164     my $steps = define_steps
165         cb_ref => \$finished_cb;
166
167     # load the "current" slot, which should be 3
168     step load_current => sub {
169         $chg->load(relative_slot => "current", res_cb => $steps->{'check_current_cb'});
170     };
171
172     step check_current_cb => sub {
173         my ($err, $res) = @_;
174         die $err if $err;
175
176         is_pointing_to($res, 5, "'current' is slot 5");
177         $slot = $res->{'this_slot'};
178
179         $res->release(finished_cb => $steps->{'released1'});
180     };
181
182     step released1 => sub {
183         my ($err) = @_;
184         die $err if $err;
185
186         $chg->load(relative_slot => 'next', slot => $slot,
187                    res_cb => $steps->{'check_next_cb'});
188     };
189
190     step check_next_cb => sub {
191         my ($err, $res) = @_;
192         die $err if $err;
193
194         is_pointing_to($res, 1, "'next' from there is slot 1");
195
196         $res->release(finished_cb => $steps->{'released2'});
197     };
198
199     step released2 => sub {
200         my ($err) = @_;
201         die $err if $err;
202
203         $chg->reset(finished_cb => $steps->{'reset_finished_cb'});
204     };
205
206     step reset_finished_cb => sub {
207         my ($err) = @_;
208         die $err if $err;
209
210         $chg->load(relative_slot => "current", res_cb => $steps->{'check_reset_cb'});
211     };
212
213     step check_reset_cb => sub {
214         my ($err, $res) = @_;
215         die $err if $err;
216
217         is_pointing_to($res, 1, "after reset, 'current' is slot 1");
218
219         $res->release(finished_cb => $steps->{'released3'});
220     };
221
222     step released3 => sub {
223         my ($err) = @_;
224         die $err if $err;
225
226         $finished_cb->();
227     };
228 }
229 test_relative_slot(\&Amanda::MainLoop::quit);
230 Amanda::MainLoop::run();
231
232 # test loading relative_slot "next"
233 sub test_relative_next {
234     my ($finished_cb) = @_;
235
236     my $steps = define_steps
237         cb_ref => \$finished_cb;
238
239     step load_next => sub {
240         $chg->load(relative_slot => "next",
241             res_cb => sub {
242                 my ($err, $res) = @_;
243                 die $err if $err;
244
245                 is_pointing_to($res, 2, "loading relative slot 'next' loads the correct slot");
246
247                 $steps->{'release'}->($res);
248             }
249         );
250     };
251
252     step release => sub {
253         my ($res) = @_;
254
255         $res->release(finished_cb => sub {
256             my ($err) = @_;
257             die $err if $err;
258
259             $finished_cb->();
260         });
261     };
262 }
263 test_relative_next(\&Amanda::MainLoop::quit);
264 Amanda::MainLoop::run();
265
266 # scan the changer using except_slots
267 sub test_except_slots {
268     my ($finished_cb) = @_;
269     my $slot;
270     my %except_slots;
271
272     my $steps = define_steps
273         cb_ref => \$finished_cb;
274
275     step start => sub {
276         $chg->load(slot => "5", except_slots => { %except_slots },
277                    res_cb => $steps->{'loaded'});
278     };
279
280     step loaded => sub {
281         my ($err, $res) = @_;
282         if ($err) {
283             if ($err->notfound) {
284                 # this means the scan is done
285                 return $steps->{'quit'}->();
286             } elsif ($err->volinuse and defined $err->{'slot'}) {
287                 $slot = $err->{'slot'};
288             } else {
289                 die $err;
290             }
291         } else {
292             $slot = $res->{'this_slot'};
293         }
294
295         $except_slots{$slot} = 1;
296
297         if ($res) {
298             $res->release(finished_cb => $steps->{'released'});
299         } else {
300             $steps->{'released'}->();
301         }
302     };
303
304     step released => sub {
305         my ($err) = @_;
306         die $err if $err;
307
308         $chg->load(relative_slot => 'next', slot => $slot,
309                    except_slots => { %except_slots },
310                    res_cb => $steps->{'loaded'});
311     };
312
313     step quit => sub {
314         is_deeply({ %except_slots }, { 5=>1, 1=>1, 2=>1, 3=>1, 4=>1 },
315                 "scanning with except_slots works");
316         $finished_cb->();
317     };
318 }
319 test_except_slots(\&Amanda::MainLoop::quit);
320 Amanda::MainLoop::run();
321
322 # eject is not implemented
323 {
324     my $try_eject = make_cb('try_eject' => sub {
325         $chg->eject(finished_cb => make_cb(sub {
326             my ($err, $res) = @_;
327             chg_err_like($err,
328                 { type => 'failed', reason => 'notimpl' },
329                 "eject returns a failed/notimpl error");
330
331             Amanda::MainLoop::quit();
332         }));
333     });
334
335     $try_eject->();
336     Amanda::MainLoop::run();
337 }
338
339 # check num_slots and loading by label
340 {
341     my ($get_info, $load_label, $check_load_cb) = @_;
342
343     $get_info = make_cb('get_info' => sub {
344         $chg->info(info_cb => $load_label, info => [ 'num_slots', 'fast_search' ]);
345     });
346
347     $load_label = make_cb('load_label' => sub {
348         my $err = shift;
349         my %results = @_;
350         die($err) if defined($err);
351
352         is_deeply({ %results },
353             { num_slots => 5, fast_search => 1 },
354             "info() returns the correct num_slots and fast_search");
355
356         # note use of a glob metacharacter in the label name
357         $chg->load(label => "FOO?BAR", res_cb => $check_load_cb);
358     });
359
360     $check_load_cb = make_cb('check_load_cb' => sub {
361         my ($err, $res) = @_;
362         die $err if $err;
363
364         is_pointing_to($res, 4, "labeled volume found in slot 4");
365
366         $res->release(finished_cb => sub {
367             my ($err) = @_;
368             die $err if $err;
369
370             Amanda::MainLoop::quit();
371         });
372     });
373
374     # label slot 4, using our own symlink
375     mkpath("$taperoot/tmp");
376     symlink("../slot4", "$taperoot/tmp/data") or die "While symlinking: $!";
377     my $dev = Amanda::Device->new("file:$taperoot/tmp");
378     $dev->start($Amanda::Device::ACCESS_WRITE, "FOO?BAR", undef)
379         or die $dev->error_or_status();
380     $dev->finish()
381         or die $dev->error_or_status();
382     rmtree("$taperoot/tmp");
383
384     $get_info->();
385     Amanda::MainLoop::run();
386 }
387
388 # inventory is pretty cool
389 {
390     my $try_inventory = make_cb('try_inventory' => sub {
391         $chg->inventory(inventory_cb => make_cb(sub {
392             my ($err, $inv) = @_;
393             die $err if $err;
394
395             is_deeply($inv, [
396               { slot => 1, state => Amanda::Changer::SLOT_FULL,
397                 device_status => $DEVICE_STATUS_VOLUME_UNLABELED,
398                 f_type => $Amanda::Header::F_EMPTY, label => undef,
399                 reserved => 0,  current => 1},
400               { slot => 2, state => Amanda::Changer::SLOT_FULL,
401                 device_status => $DEVICE_STATUS_VOLUME_UNLABELED,
402                 f_type => $Amanda::Header::F_EMPTY, label => undef,
403                 reserved => 0 },
404               { slot => 3, state => Amanda::Changer::SLOT_FULL,
405                 device_status => $DEVICE_STATUS_VOLUME_UNLABELED,
406                 f_type => $Amanda::Header::F_EMPTY, label => undef,
407                 reserved => 0 },
408               { slot => 4, state => Amanda::Changer::SLOT_FULL,
409                 device_status => $DEVICE_STATUS_SUCCESS,
410                 f_type => $Amanda::Header::F_TAPESTART, label => "FOO?BAR",
411                 reserved => 0 },
412               { slot => 5, state => Amanda::Changer::SLOT_FULL,
413                 device_status => $DEVICE_STATUS_VOLUME_UNLABELED,
414                 f_type => $Amanda::Header::F_EMPTY, label => undef,
415                 reserved => 0 },
416                 ], "inventory finds the labeled tape");
417
418             Amanda::MainLoop::quit();
419         }));
420     });
421
422     $try_inventory->();
423     Amanda::MainLoop::run();
424 }
425
426 $chg->quit();
427 rmtree($taperoot);