Imported Upstream version 3.3.2
[debian/amanda] / installcheck / Amanda_Changer_multi.pl
1 # Copyright (c) 2008-2012 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 => 19;
20 use File::Path;
21 use strict;
22 use warnings;
23
24 use lib "@amperldir@";
25 use Installcheck;
26 use Installcheck::Config;
27 use Installcheck::Changer;
28 use Amanda::Paths;
29 use Amanda::Device qw( :constants );
30 use Amanda::Debug;
31 use Amanda::MainLoop;
32 use Amanda::Config qw( :init :getconf config_dir_relative );
33 use Amanda::Changer;
34
35 # set up debugging so debug output doesn't interfere with test results
36 Amanda::Debug::dbopen("installcheck");
37 Installcheck::log_test_output();
38
39 # and disable Debug's die() and warn() overrides
40 Amanda::Debug::disable_die_override();
41
42 my $taperoot = "$Installcheck::TMP/Amanda_Changer_Multi_test";
43
44 sub reset_taperoot {
45     my ($nslots) = @_;
46
47     if (-d $taperoot) {
48         rmtree($taperoot);
49     }
50     mkpath($taperoot);
51
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: $!");
57     }
58 }
59
60 # Build a configuration that specifies Amanda::Changer::Multi
61 my $testconf = Installcheck::Config->new();
62 $testconf->write();
63
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);
68 }
69
70 reset_taperoot(5);
71
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");
75 {
76     my @slots = ();
77     my @reservations = ();
78     my ($release, $getres, $tryreserved);
79
80     $getres = make_cb(getres => sub {
81         my $slot = pop @slots;
82         if (!defined $slot) {
83             return $tryreserved->();
84         }
85
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")
91                 or diag($err);
92
93             # keep this reservation
94             if ($reservation) {
95                 push @reservations, $reservation;
96             }
97
98             # and start on the next
99             $getres->();
100         }));
101     });
102
103     $tryreserved = make_cb(tryreserved => sub {
104         # try to load an already-reserved slot
105         $chg->load(slot => 3,
106                    res_cb => sub {
107             my ($err, $reservation) = @_;
108             chg_err_like($err,
109                 { message => qr/Slot 3 is already in use by process/,
110                   type => 'failed',
111                   reason => 'volinuse' },
112                 "error when requesting already-reserved slot");
113             $release->();
114         });
115     });
116
117     $release = make_cb(release => sub {
118         my $res = pop @reservations;
119         if (!defined $res) {
120             return Amanda::MainLoop::quit();
121         }
122
123         $res->release(finished_cb => sub {
124             my ($err) = @_;
125             die $err if $err;
126             $release->();
127         });
128     });
129
130     # start the loop
131     @slots = ( 1, 3, 5 );
132     $getres->();
133     Amanda::MainLoop::run();
134
135     # and try it with some different slots, just to see
136     @slots = ( 4, 2, 3 );
137     $getres->();
138     Amanda::MainLoop::run();
139
140     @reservations = ();
141 }
142
143 # check relative slot ("current" and "next") functionality
144 sub test_relative_slot {
145     my ($finished_cb) = @_;
146     my $slot;
147
148     my $steps = define_steps
149         cb_ref => \$finished_cb;
150
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'});
154     };
155
156     step check_current_cb => sub {
157         my ($err, $res) = @_;
158         die $err if $err;
159
160         is ($res->{'this_slot'}, 5, "'current' is slot 5");
161         $slot = $res->{'this_slot'};
162
163         $res->release(finished_cb => $steps->{'released1'});
164     };
165
166     step released1 => sub {
167         my ($err) = @_;
168         die $err if $err;
169
170         $chg->load(relative_slot => 'next', slot => $slot,
171                    res_cb => $steps->{'check_next_cb'});
172     };
173
174     step check_next_cb => sub {
175         my ($err, $res) = @_;
176         die $err if $err;
177
178         is ($res->{'this_slot'}, 1, "'next' from there is slot 1");
179
180         $res->release(finished_cb => $steps->{'released2'});
181     };
182
183     step released2 => sub {
184         my ($err) = @_;
185         die $err if $err;
186
187         $chg->reset(finished_cb => $steps->{'reset_finished_cb'});
188     };
189
190     step reset_finished_cb => sub {
191         my ($err) = @_;
192         die $err if $err;
193
194         $chg->load(relative_slot => "current", res_cb => $steps->{'check_reset_cb'});
195     };
196
197     step check_reset_cb => sub {
198         my ($err, $res) = @_;
199         die $err if $err;
200
201         is ($res->{'this_slot'}, 1, "after reset, 'current' is slot 1");
202
203         $res->release(finished_cb => $steps->{'released3'});
204     };
205
206     step released3 => sub {
207         my ($err) = @_;
208         die $err if $err;
209
210         $finished_cb->();
211     };
212 }
213 test_relative_slot(\&Amanda::MainLoop::quit);
214 Amanda::MainLoop::run();
215
216 # test loading relative_slot "next"
217 sub test_relative_slot_next {
218     my ($finished_cb) = @_;
219
220     my $steps = define_steps
221         cb_ref => \$finished_cb;
222
223     step load_next => sub {
224         $chg->load(relative_slot => "next",
225             res_cb => sub {
226                 my ($err, $res) = @_;
227                 die $err if $err;
228
229                 is ($res->{'this_slot'}, 2, "loading relative slot 'next' loads the correct slot");
230
231                 $steps->{'release'}->($res);
232             }
233         );
234     };
235
236     step release => sub {
237         my ($res) = @_;
238
239         $res->release(finished_cb => sub {
240             my ($err) = @_;
241             die $err if $err;
242
243             $finished_cb->();
244         });
245     };
246 }
247 test_relative_slot_next(\&Amanda::MainLoop::quit);
248 Amanda::MainLoop::run();
249
250 # scan the changer using except_slots
251 sub test_except_slots {
252     my ($finished_cb) = @_;
253     my $slot;
254     my %except_slots;
255
256     my $steps = define_steps
257         cb_ref => \$finished_cb;
258
259     step start => sub {
260         $chg->load(slot => "5", except_slots => { %except_slots },
261                    res_cb => $steps->{'loaded'});
262     };
263
264     step loaded => sub {
265         my ($err, $res) = @_;
266         if ($err) {
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'};
272             } else {
273                 die $err;
274             }
275         } else {
276             $slot = $res->{'this_slot'};
277         }
278
279         $except_slots{$slot} = 1;
280
281         if ($res) {
282             $res->release(finished_cb => $steps->{'released'});
283         } else {
284             $steps->{'released'}->();
285         }
286     };
287
288     step released => sub {
289         my ($err) = @_;
290         die $err if $err;
291
292         $chg->load(relative_slot => 'next', slot => $slot,
293                    except_slots => { %except_slots },
294                    res_cb => $steps->{'loaded'});
295     };
296
297     step quit => sub {
298         is_deeply({ %except_slots }, { 5=>1, 1=>1, 2=>1, 3=>1, 4=>1 },
299                 "scanning with except_slots works");
300         $finished_cb->();
301     };
302 }
303 test_except_slots(\&Amanda::MainLoop::quit);
304 Amanda::MainLoop::run();
305
306 # eject is not implemented
307 {
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");
312
313             Amanda::MainLoop::quit();
314         }));
315     });
316
317     $try_eject->();
318     Amanda::MainLoop::run();
319 }
320
321 # check num_slots and loading by label
322 {
323     my ($update, $get_info, $load_label, $check_load_cb) = @_;
324
325     $update = make_cb('update' => sub {
326         $chg->update(changed => "4", finished_cb => $get_info);
327     });
328
329     $get_info = make_cb('get_info' => sub {
330         $chg->info(info_cb => $load_label, info => [ 'num_slots', 'fast_search' ]);
331     });
332
333     $load_label = make_cb('load_label' => sub {
334         my $err = shift;
335         my %results = @_;
336         die($err) if defined($err);
337
338         is_deeply({ %results },
339             { num_slots => 5, fast_search => 0 },
340             "info() returns the correct num_slots and fast_search");
341
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);
344     });
345
346     $check_load_cb = make_cb('check_load_cb' => sub {
347         my ($err, $res) = @_;
348         die $err if $err;
349
350         is ($res->{'this_slot'}, 4, "labeled volume found in slot 4");
351
352         $res->release(finished_cb => sub {
353             my ($err) = @_;
354             die $err if $err;
355
356             Amanda::MainLoop::quit();
357         });
358     });
359
360     # label slot 4
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();
364     $dev->finish()
365         or die $dev->error_or_status();
366
367     $update->();
368     Amanda::MainLoop::run();
369 }
370
371 # inventory is pretty cool
372 {
373     my $try_inventory = make_cb('try_inventory' => sub {
374         $chg->inventory(inventory_cb => make_cb(sub {
375             my ($err, $inv) = @_;
376             die $err if $err;
377
378             is_deeply($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,
383                 reserved => 0 },
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,
388                 reserved => 0 },
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,
393                 reserved => 0 },
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,
403                 reserved => 0 },
404                 ], "inventory finds the labeled tape");
405
406             Amanda::MainLoop::quit();
407         }));
408     });
409
410     $try_inventory->();
411     Amanda::MainLoop::run();
412 }
413
414 # check loading by label if the state is unknown
415 {
416     my ($update, $load_label, $check_load_cb) = @_;
417
418     $update = make_cb('update' => sub {
419         $chg->update(changed => "4=", finished_cb => $load_label);
420     });
421
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);
425     });
426
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;
431         
432         ok(1, "Don't found label for unknown state slot");
433         Amanda::MainLoop::quit();
434     });
435
436     $update->();
437     Amanda::MainLoop::run();
438 }
439
440 $chg->quit();
441 rmtree($taperoot);