Imported Upstream version 3.3.3
[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
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_Multi_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         mkdir("$taperoot/slot$slot/data")
57             or die("Could not mkdir: $!");
58     }
59 }
60
61 # Build a configuration that specifies Amanda::Changer::Multi
62 my $testconf = Installcheck::Config->new();
63 $testconf->write();
64
65 my $cfg_result = config_init($CONFIG_INIT_EXPLICIT_NAME, 'TESTCONF');
66 if ($cfg_result != $CFGERR_OK) {
67     my ($level, @errors) = Amanda::Config::config_errors();
68     die(join "\n", @errors);
69 }
70
71 reset_taperoot(5);
72
73 my $chg = Amanda::Changer->new("chg-multi:file:$taperoot/slot{0,1,2,3,4}");
74 die($chg) if $chg->isa("Amanda::Changer::Error");
75 is($chg->have_inventory(), '1', "changer have inventory");
76 {
77     my @slots = ();
78     my @reservations = ();
79     my ($release, $getres, $tryreserved);
80
81     $getres = make_cb(getres => sub {
82         my $slot = pop @slots;
83         if (!defined $slot) {
84             return $tryreserved->();
85         }
86
87         $chg->load(slot => $slot,
88                    set_current => ($slot == 5),
89                    res_cb => make_cb(sub {
90             my ($err, $reservation) = @_;
91             ok(!$err, "no error loading slot $slot")
92                 or diag($err);
93
94             # keep this reservation
95             if ($reservation) {
96                 push @reservations, $reservation;
97             }
98
99             # and start on the next
100             $getres->();
101         }));
102     });
103
104     $tryreserved = make_cb(tryreserved => sub {
105         # try to load an already-reserved slot
106         $chg->load(slot => 3,
107                    res_cb => sub {
108             my ($err, $reservation) = @_;
109             chg_err_like($err,
110                 { message => qr/Slot 3 is already in use by process/,
111                   type => 'failed',
112                   reason => 'volinuse' },
113                 "error when requesting already-reserved slot");
114             $release->();
115         });
116     });
117
118     $release = make_cb(release => sub {
119         my $res = pop @reservations;
120         if (!defined $res) {
121             return Amanda::MainLoop::quit();
122         }
123
124         $res->release(finished_cb => sub {
125             my ($err) = @_;
126             die $err if $err;
127             $release->();
128         });
129     });
130
131     # start the loop
132     @slots = ( 1, 3, 5 );
133     $getres->();
134     Amanda::MainLoop::run();
135
136     # and try it with some different slots, just to see
137     @slots = ( 4, 2, 3 );
138     $getres->();
139     Amanda::MainLoop::run();
140
141     @reservations = ();
142 }
143
144 # check relative slot ("current" and "next") functionality
145 sub test_relative_slot {
146     my ($finished_cb) = @_;
147     my $slot;
148
149     my $steps = define_steps
150         cb_ref => \$finished_cb;
151
152     step load_current => sub {
153         # load the "current" slot, which should be 4
154         $chg->load(relative_slot => "current", res_cb => $steps->{'check_current_cb'});
155     };
156
157     step check_current_cb => sub {
158         my ($err, $res) = @_;
159         die $err if $err;
160
161         is ($res->{'this_slot'}, 5, "'current' is slot 5");
162         $slot = $res->{'this_slot'};
163
164         $res->release(finished_cb => $steps->{'released1'});
165     };
166
167     step released1 => sub {
168         my ($err) = @_;
169         die $err if $err;
170
171         $chg->load(relative_slot => 'next', slot => $slot,
172                    res_cb => $steps->{'check_next_cb'});
173     };
174
175     step check_next_cb => sub {
176         my ($err, $res) = @_;
177         die $err if $err;
178
179         is ($res->{'this_slot'}, 1, "'next' from there is slot 1");
180
181         $res->release(finished_cb => $steps->{'released2'});
182     };
183
184     step released2 => sub {
185         my ($err) = @_;
186         die $err if $err;
187
188         $chg->reset(finished_cb => $steps->{'reset_finished_cb'});
189     };
190
191     step reset_finished_cb => sub {
192         my ($err) = @_;
193         die $err if $err;
194
195         $chg->load(relative_slot => "current", res_cb => $steps->{'check_reset_cb'});
196     };
197
198     step check_reset_cb => sub {
199         my ($err, $res) = @_;
200         die $err if $err;
201
202         is ($res->{'this_slot'}, 1, "after reset, 'current' is slot 1");
203
204         $res->release(finished_cb => $steps->{'released3'});
205     };
206
207     step released3 => sub {
208         my ($err) = @_;
209         die $err if $err;
210
211         $finished_cb->();
212     };
213 }
214 test_relative_slot(\&Amanda::MainLoop::quit);
215 Amanda::MainLoop::run();
216
217 # test loading relative_slot "next"
218 sub test_relative_slot_next {
219     my ($finished_cb) = @_;
220
221     my $steps = define_steps
222         cb_ref => \$finished_cb;
223
224     step load_next => sub {
225         $chg->load(relative_slot => "next",
226             res_cb => sub {
227                 my ($err, $res) = @_;
228                 die $err if $err;
229
230                 is ($res->{'this_slot'}, 2, "loading relative slot 'next' loads the correct slot");
231
232                 $steps->{'release'}->($res);
233             }
234         );
235     };
236
237     step release => sub {
238         my ($res) = @_;
239
240         $res->release(finished_cb => sub {
241             my ($err) = @_;
242             die $err if $err;
243
244             $finished_cb->();
245         });
246     };
247 }
248 test_relative_slot_next(\&Amanda::MainLoop::quit);
249 Amanda::MainLoop::run();
250
251 # scan the changer using except_slots
252 sub test_except_slots {
253     my ($finished_cb) = @_;
254     my $slot;
255     my %except_slots;
256
257     my $steps = define_steps
258         cb_ref => \$finished_cb;
259
260     step start => sub {
261         $chg->load(slot => "5", except_slots => { %except_slots },
262                    res_cb => $steps->{'loaded'});
263     };
264
265     step loaded => sub {
266         my ($err, $res) = @_;
267         if ($err) {
268             if ($err->notfound) {
269                 # this means the scan is done
270                 return $steps->{'quit'}->();
271             } elsif ($err->volinuse and defined $err->{'slot'}) {
272                 $slot = $err->{'slot'};
273             } else {
274                 die $err;
275             }
276         } else {
277             $slot = $res->{'this_slot'};
278         }
279
280         $except_slots{$slot} = 1;
281
282         if ($res) {
283             $res->release(finished_cb => $steps->{'released'});
284         } else {
285             $steps->{'released'}->();
286         }
287     };
288
289     step released => sub {
290         my ($err) = @_;
291         die $err if $err;
292
293         $chg->load(relative_slot => 'next', slot => $slot,
294                    except_slots => { %except_slots },
295                    res_cb => $steps->{'loaded'});
296     };
297
298     step quit => sub {
299         is_deeply({ %except_slots }, { 5=>1, 1=>1, 2=>1, 3=>1, 4=>1 },
300                 "scanning with except_slots works");
301         $finished_cb->();
302     };
303 }
304 test_except_slots(\&Amanda::MainLoop::quit);
305 Amanda::MainLoop::run();
306
307 # eject is not implemented
308 {
309     my $try_eject = make_cb('try_eject' => sub {
310         $chg->eject(finished_cb => make_cb(sub {
311             my ($err, $res) = @_;
312             is ($err, undef, "eject is implemented");
313
314             Amanda::MainLoop::quit();
315         }));
316     });
317
318     $try_eject->();
319     Amanda::MainLoop::run();
320 }
321
322 # check num_slots and loading by label
323 {
324     my ($update, $get_info, $load_label, $check_load_cb) = @_;
325
326     $update = make_cb('update' => sub {
327         $chg->update(changed => "4", finished_cb => $get_info);
328     });
329
330     $get_info = make_cb('get_info' => sub {
331         $chg->info(info_cb => $load_label, info => [ 'num_slots', 'fast_search' ]);
332     });
333
334     $load_label = make_cb('load_label' => sub {
335         my $err = shift;
336         my %results = @_;
337         die($err) if defined($err);
338
339         is_deeply({ %results },
340             { num_slots => 5, fast_search => 0 },
341             "info() returns the correct num_slots and fast_search");
342
343         # note use of a glob metacharacter in the label name
344         $chg->load(label => "FOO?BAR", res_cb => $check_load_cb, set_current => 1);
345     });
346
347     $check_load_cb = make_cb('check_load_cb' => sub {
348         my ($err, $res) = @_;
349         die $err if $err;
350
351         is ($res->{'this_slot'}, 4, "labeled volume found in slot 4");
352
353         $res->release(finished_cb => sub {
354             my ($err) = @_;
355             die $err if $err;
356
357             Amanda::MainLoop::quit();
358         });
359     });
360
361     # label slot 4
362     my $dev = Amanda::Device->new("file:$taperoot/slot3");
363     $dev->start($Amanda::Device::ACCESS_WRITE, "FOO?BAR", undef)
364         or die $dev->error_or_status();
365     $dev->finish()
366         or die $dev->error_or_status();
367
368     $update->();
369     Amanda::MainLoop::run();
370 }
371
372 # inventory is pretty cool
373 {
374     my $try_inventory = make_cb('try_inventory' => sub {
375         $chg->inventory(inventory_cb => make_cb(sub {
376             my ($err, $inv) = @_;
377             die $err if $err;
378
379             is_deeply($inv, [
380               { slot => 1, state => Amanda::Changer::SLOT_FULL,
381                 device_status => $DEVICE_STATUS_DEVICE_ERROR,
382                 device_error  => "Error checking directory $taperoot/slot0/data/: No such file or directory",
383                 f_type => undef, label => undef,
384                 reserved => 0 },
385               { slot => 2, state => Amanda::Changer::SLOT_FULL,
386                 device_status => $DEVICE_STATUS_VOLUME_UNLABELED,
387                 device_error  => "File 0 not found",
388                 f_type => $Amanda::Header::F_EMPTY, label => undef,
389                 reserved => 0 },
390               { slot => 3, state => Amanda::Changer::SLOT_FULL,
391                 device_status => $DEVICE_STATUS_VOLUME_UNLABELED,
392                 device_error  => "File 0 not found",
393                 f_type => $Amanda::Header::F_EMPTY, label => undef,
394                 reserved => 0 },
395               { slot => 4, state => Amanda::Changer::SLOT_FULL,
396                 device_status => $DEVICE_STATUS_SUCCESS,
397                 device_error  => undef,
398                 f_type => $Amanda::Header::F_TAPESTART, label => "FOO?BAR",
399                 reserved => 0, current => 1 },
400               { slot => 5, state => Amanda::Changer::SLOT_FULL,
401                 device_status => $DEVICE_STATUS_VOLUME_UNLABELED,
402                 device_error  => "File 0 not found",
403                 f_type => $Amanda::Header::F_EMPTY, label => undef,
404                 reserved => 0 },
405                 ], "inventory finds the labeled tape");
406
407             Amanda::MainLoop::quit();
408         }));
409     });
410
411     $try_inventory->();
412     Amanda::MainLoop::run();
413 }
414
415 # check loading by label if the state is unknown
416 {
417     my ($update, $load_label, $check_load_cb) = @_;
418
419     $update = make_cb('update' => sub {
420         $chg->update(changed => "4=", finished_cb => $load_label);
421     });
422
423     $load_label = make_cb('load_label' => sub {
424         # note use of a glob metacharacter in the label name
425         $chg->load(label => "FOO?BAR", res_cb => $check_load_cb);
426     });
427
428     $check_load_cb = make_cb('check_load_cb' => sub {
429         my ($err, $res) = @_;
430         die("labeled volume found in slot 4") if $res;
431         die("bad error: $err") if $err && !$err->notfound;
432         
433         ok(1, "Don't found label for unknown state slot");
434         Amanda::MainLoop::quit();
435     });
436
437     $update->();
438     Amanda::MainLoop::run();
439 }
440
441 $chg->quit();
442 rmtree($taperoot);