Imported Upstream version 3.3.0
[debian/amanda] / installcheck / Amanda_Changer_ndmp.pl
1 # Copyright (c) 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 => 171;
20 use File::Path;
21 use Data::Dumper;
22 use strict;
23 use warnings;
24
25 use lib "@amperldir@";
26 use Installcheck;
27 use Installcheck::Config;
28 use Installcheck::Changer;
29 use Installcheck::Mock qw( setup_mock_mtx $mock_mtx_path );
30 use Amanda::Device qw( :constants );
31 use Amanda::Debug;
32 use Amanda::Paths;
33 use Amanda::MainLoop;
34 use Amanda::Config qw( :init :getconf config_dir_relative );
35 use Amanda::Changer;
36
37 # set up debugging so debug output doesn't interfere with test results
38 Amanda::Debug::dbopen("installcheck");
39
40 my $ndmp = Installcheck::Mock::NdmpServer->new();
41
42 # and disable Debug's die() and warn() overrides
43 Amanda::Debug::disable_die_override();
44
45 my $chg_state_file = "$Installcheck::TMP/chg-ndmp-state";
46 unlink($chg_state_file) if -f $chg_state_file;
47
48 sub check_inventory {
49     my ($chg, $barcodes, $next_step, $expected, $msg) = @_;
50
51     $chg->inventory(inventory_cb => make_cb(sub {
52         my ($err, $inv) = @_;
53         die $err if $err;
54
55         # strip barcodes from both $expected and $inv
56         if (!$barcodes) {
57             for (@$expected, @$inv) {
58                 delete $_->{'barcode'};
59             }
60         }
61
62         is_deeply($inv, $expected, $msg)
63             or diag("Got:\n" . Dumper($inv));
64
65         $next_step->();
66     }));
67 }
68
69 ##
70 # test the "interface" package
71
72 sub test_interface {
73     my ($finished_cb) = @_;
74     my ($interface, $chg);
75
76     my $steps = define_steps
77         cb_ref => \$finished_cb,
78         finalize => sub { $chg->quit() if defined $chg};
79
80     step start => sub {
81         my $testconf = Installcheck::Config->new();
82         $testconf->add_changer('robo', [
83             tpchanger => "\"chg-ndmp:127.0.0.1:$ndmp->{port}\@$ndmp->{changer}\"",
84             changerfile => "\"$chg_state_file\"",
85
86             property => "       \"tape-device\" \"0=ndmp:127.0.0.1:$ndmp->{port}\@$ndmp->{drive0}\"",
87             property => "append \"tape-device\" \"1=ndmp:127.0.0.1:$ndmp->{port}\@$ndmp->{drive1}\"",
88         ]);
89         $testconf->write();
90
91         my $cfg_result = config_init($CONFIG_INIT_EXPLICIT_NAME, 'TESTCONF');
92         if ($cfg_result != $CFGERR_OK) {
93             my ($level, @errors) = Amanda::Config::config_errors();
94             die(join "\n", @errors);
95         }
96
97         $chg = Amanda::Changer->new("robo");
98         die "$chg" if $chg->isa("Amanda::Changer::Error");
99         is($chg->have_inventory(), '1', "changer have inventory");
100
101         $interface = $chg->{'interface'};
102         $interface->inquiry($steps->{'inquiry_cb'});
103     };
104
105     step inquiry_cb => sub {
106         my ($error, $info) = @_;
107
108         die $error if $error;
109
110         is_deeply($info, {
111             'revision' => '1.0',
112             'product id' => 'FakeRobot',
113             'vendor id' => 'NDMJOB',
114             'product type' => 'Medium Changer'
115             }, "robot::Interface inquiry() info is correct");
116
117         $steps->{'status1'}->();
118     };
119
120     step status1 => sub {
121         $interface->status(sub {
122             my ($error, $status) = @_;
123
124             die $error if $error;
125
126             is_deeply($status, {
127                 'drives' => {
128                     '0' => undef,
129                     '1' => undef,
130                 },
131                 'slots' => {
132                     1 => { ie => 1, empty => 1 },
133                     2 => { ie => 1, empty => 1 },
134                     3 => { barcode => 'PTAG00XX', },
135                     4 => { barcode => 'PTAG01XX', },
136                     5 => { barcode => 'PTAG02XX', },
137                     6 => { barcode => 'PTAG03XX', },
138                     7 => { barcode => 'PTAG04XX', },
139                     8 => { barcode => 'PTAG05XX', },
140                     9 => { barcode => 'PTAG06XX', },
141                     10 => { barcode => 'PTAG07XX', },
142                     11 => { barcode => 'PTAG08XX', },
143                     12 => { barcode => 'PTAG09XX', },
144                 },
145             }, "robot::Interface status() output is correct (no drives loaded)")
146                 or die("robot does not look like I expect it to");
147
148             $steps->{'load0'}->();
149         });
150     };
151
152     step load0 => sub {
153         $interface->load(3, 0, sub {
154             my ($error) = @_;
155
156             die $error if $error;
157
158             pass("load");
159             $steps->{'status2'}->();
160         });
161     };
162
163     step status2 => sub {
164         $interface->status(sub {
165             my ($error, $status) = @_;
166
167             die $error if $error;
168
169             is_deeply($status, {
170                 'drives' => {
171                     '0' => { barcode => 'PTAG00XX', orig_slot => 3 },
172                     '1' => undef,
173                 },
174                 'slots' => {
175                     1 => { ie => 1, empty => 1 },
176                     2 => { ie => 1, empty => 1 },
177                     3 => { empty => 1 },
178                     4 => { barcode => 'PTAG01XX', },
179                     5 => { barcode => 'PTAG02XX', },
180                     6 => { barcode => 'PTAG03XX', },
181                     7 => { barcode => 'PTAG04XX', },
182                     8 => { barcode => 'PTAG05XX', },
183                     9 => { barcode => 'PTAG06XX', },
184                     10 => { barcode => 'PTAG07XX', },
185                     11 => { barcode => 'PTAG08XX', },
186                     12 => { barcode => 'PTAG09XX', },
187                 },
188             }, "robot::Interface status() output is correct (one drive loaded)")
189                 or die("robot does not look like I expect it to");
190
191             $steps->{'load1'}->();
192         });
193     };
194
195     step load1 => sub {
196         $interface->load(12, 1, sub {
197             my ($error) = @_;
198
199             die $error if $error;
200
201             pass("load");
202             $steps->{'status3'}->();
203         });
204     };
205
206     step status3 => sub {
207         $interface->status(sub {
208             my ($error, $status) = @_;
209
210             die $error if $error;
211
212             is_deeply($status, {
213                 'drives' => {
214                     '0' => { barcode => 'PTAG00XX', orig_slot => 3 },
215                     '1' => { barcode => 'PTAG09XX', orig_slot => 12 },
216                 },
217                 'slots' => {
218                     1 => { ie => 1, empty => 1 },
219                     2 => { ie => 1, empty => 1 },
220                     3 => { empty => 1 },
221                     4 => { barcode => 'PTAG01XX', },
222                     5 => { barcode => 'PTAG02XX', },
223                     6 => { barcode => 'PTAG03XX', },
224                     7 => { barcode => 'PTAG04XX', },
225                     8 => { barcode => 'PTAG05XX', },
226                     9 => { barcode => 'PTAG06XX', },
227                     10 => { barcode => 'PTAG07XX', },
228                     11 => { barcode => 'PTAG08XX', },
229                     12 => { empty => 1 },
230                 },
231             }, "robot::Interface status() output is correct (two drives loaded)")
232                 or die("robot does not look like I expect it to");
233
234             $steps->{'transfer'}->();
235         });
236     };
237
238     step transfer => sub {
239         $interface->transfer(5, 2, sub {
240             my ($error) = @_;
241
242             die $error if $error;
243
244             pass("transfer");
245             $steps->{'status4'}->();
246         });
247     };
248
249     step status4 => sub {
250         $interface->status(sub {
251             my ($error, $status) = @_;
252
253             die $error if $error;
254
255             is_deeply($status, {
256                 'drives' => {
257                     '0' => { barcode => 'PTAG00XX', orig_slot => 3 },
258                     '1' => { barcode => 'PTAG09XX', orig_slot => 12 },
259                 },
260                 'slots' => {
261                     1 => { ie => 1, empty => 1 },
262                     2 => { ie => 1, barcode => 'PTAG02XX', },
263                     3 => { empty => 1 },
264                     4 => { barcode => 'PTAG01XX', },
265                     5 => { empty => 1 },
266                     6 => { barcode => 'PTAG03XX', },
267                     7 => { barcode => 'PTAG04XX', },
268                     8 => { barcode => 'PTAG05XX', },
269                     9 => { barcode => 'PTAG06XX', },
270                     10 => { barcode => 'PTAG07XX', },
271                     11 => { barcode => 'PTAG08XX', },
272                     12 => { empty => 1 },
273                 },
274             }, "robot::Interface status() output is correct after transfer")
275                 or die("robot does not look like I expect it to");
276
277             $finished_cb->();
278         });
279     };
280 }
281 test_interface(\&Amanda::MainLoop::quit);
282 Amanda::MainLoop::run();
283
284 ##
285 # Test the real deal
286
287 sub test_changer {
288     my ($mtx_config, $finished_cb) = @_;
289     my $chg;
290     my ($res1, $res2);
291     my ($drive0_name, $drive1_name);
292     my $pfx = "BC=$mtx_config->{barcodes}; TORIG=$mtx_config->{track_orig}";
293
294     my $steps = define_steps
295         cb_ref => \$finished_cb,
296         finalize => sub { $chg->quit() if defined $chg};
297
298     # clean up
299     step setup => sub {
300         unlink($chg_state_file) if -f $chg_state_file;
301
302         my @ignore_barcodes = ( property => "\"ignore-barcodes\" \"y\"")
303             if ($mtx_config->{'barcodes'} == -1);
304
305         $drive0_name = "ndmp:127.0.0.1:$ndmp->{port}\@$ndmp->{drive0}";
306         $drive1_name = "ndmp:127.0.0.1:$ndmp->{port}\@$ndmp->{drive1}";
307
308         my $testconf = Installcheck::Config->new();
309         $testconf->add_changer('robo', [
310             tpchanger => "\"chg-ndmp:127.0.0.1:$ndmp->{port}\@$ndmp->{changer}\"",
311             changerfile => "\"$chg_state_file\"",
312
313             property => "       \"tape-device\" \"0=$drive0_name\"",
314             property => "append \"tape-device\" \"1=$drive1_name\"",
315             property => "\"use-slots\" \"1-5\"",
316             property => "\"verbose\" \"1\"",
317             @ignore_barcodes,
318         ]);
319         $testconf->write();
320
321         config_uninit();
322         my $cfg_result = config_init($CONFIG_INIT_EXPLICIT_NAME, 'TESTCONF');
323         if ($cfg_result != $CFGERR_OK) {
324             my ($level, @errors) = Amanda::Config::config_errors();
325             die(join "\n", @errors);
326         }
327
328         # reset the changer to its base state
329         $ndmp->reset();
330
331         $steps->{'start'}->();
332     };
333
334     step start => sub {
335         $chg = Amanda::Changer->new("robo");
336         ok(!$chg->isa("Amanda::Changer::Error"),
337             "$pfx: Create working chg-robot instance: $chg")
338             or die("no sense going on");
339
340         is($chg->have_inventory(), '1', "changer have inventory");
341         $chg->info(info => [qw(vendor_string num_slots fast_search)],
342                     info_cb => $steps->{'info_cb'});
343     };
344
345     step info_cb => sub {
346         my ($err, %info) = @_;
347         die $err if $err;
348
349         is_deeply({ %info }, {
350             num_slots => 5,
351             fast_search => 1,
352             vendor_string => "NDMJOB FakeRobot",
353         }, "$pfx: info keys num_slots, fast_search, vendor_string are correct");
354
355         $steps->{'inventory1'}->();
356     };
357
358     step inventory1 => sub {
359         check_inventory($chg, $mtx_config->{'barcodes'} > 0, $steps->{'load_slot_1'}, [
360             { slot => 1, state => Amanda::Changer::SLOT_EMPTY,
361               import_export => 1,
362               device_status => undef, f_type => undef, label => undef },
363             { slot => 2, state => Amanda::Changer::SLOT_EMPTY,
364               import_export => 1,
365               device_status => undef, f_type => undef, label => undef },
366             { slot => 3, state => Amanda::Changer::SLOT_FULL,
367               barcode => 'PTAG00XX', current => 1,
368               device_status => undef, f_type => undef, label => undef },
369             { slot => 4, state => Amanda::Changer::SLOT_FULL,
370               barcode => 'PTAG01XX',
371               device_status => undef, f_type => undef, label => undef },
372             { slot => 5, state => Amanda::Changer::SLOT_FULL,
373               barcode => 'PTAG02XX',
374               device_status => undef, f_type => undef, label => undef },
375         ], "$pfx: inventory is correct on start-up");
376     };
377
378     step load_slot_1 => sub {
379         $chg->load(slot => 3, res_cb => $steps->{'loaded_slot_1'});
380     };
381
382     step loaded_slot_1 => sub {
383         (my $err, $res1) = @_;
384         die $err if $err;
385
386         is($res1->{'device'}->device_name, $drive0_name,
387             "$pfx: first load returns drive-0 device");
388
389         is_deeply({
390                 loaded_in => $chg->{'__last_state'}->{'slots'}->{3}->{'loaded_in'},
391                 orig_slot => $chg->{'__last_state'}->{'drives'}->{0}->{'orig_slot'},
392             }, {
393                 loaded_in => 0,
394                 orig_slot => 3,
395             }, "$pfx: slot 3 'loaded_in' and drive 0 'orig_slot' are correct");
396
397         $steps->{'load_slot_2'}->();
398     };
399
400     step load_slot_2 => sub {
401         $chg->load(slot => 4, res_cb => $steps->{'loaded_slot_2'});
402     };
403
404     step loaded_slot_2 => sub {
405         (my $err, $res2) = @_;
406         die $err if $err;
407
408         is($res2->{'device'}->device_name, $drive1_name,
409             "$pfx: second load returns drive-1 device");
410
411         is_deeply({
412                 loaded_in => $chg->{'__last_state'}->{'slots'}->{3}->{'loaded_in'},
413                 orig_slot => $chg->{'__last_state'}->{'drives'}->{0}->{'orig_slot'},
414             }, {
415                 loaded_in => 0,
416                 orig_slot => 3,
417             }, "$pfx: slot 3 'loaded_in' and drive 0 'orig_slot' are still correct");
418
419         is_deeply({
420                 loaded_in => $chg->{'__last_state'}->{'slots'}->{4}->{'loaded_in'},
421                 orig_slot => $chg->{'__last_state'}->{'drives'}->{1}->{'orig_slot'},
422             }, {
423                 loaded_in => 1,
424                 orig_slot => 4,
425             }, "$pfx: slot 4 'loaded_in' and drive 1 'orig_slot' are correct");
426
427         $steps->{'check_loads'}->();
428     };
429
430     step check_loads => sub {
431         # peek into the interface to check that things are loaded correctly
432         $chg->{'interface'}->status(sub {
433             my ($error, $status) = @_;
434
435             die $error if $error;
436
437             # only perform these checks when barcodes are enabled
438             if ($mtx_config->{'barcodes'} > 0) {
439                 is_deeply($status->{'drives'}, {
440                     0 => { barcode => 'PTAG00XX', 'orig_slot' => 3 },
441                     1 => { barcode => 'PTAG01XX', 'orig_slot' => 4 },
442                 }, "$pfx: double-check: loading drives with the changer gets the right drives loaded");
443             }
444
445             $steps->{'inventory2'}->();
446         });
447     };
448
449     step inventory2 => sub {
450         check_inventory($chg, $mtx_config->{'barcodes'} > 0, $steps->{'load_slot_3'}, [
451             { slot => 1, state => Amanda::Changer::SLOT_EMPTY,
452               import_export => 1,
453               device_status => undef, f_type => undef, label => undef },
454             { slot => 2, state => Amanda::Changer::SLOT_EMPTY,
455               import_export => 1,
456               device_status => undef, f_type => undef, label => undef },
457             { slot => 3, state => Amanda::Changer::SLOT_FULL,
458               barcode => 'PTAG00XX', reserved => 1, loaded_in => 0,
459               current => 1,
460               device_status => undef, f_type => undef, label => undef },
461             { slot => 4, state => Amanda::Changer::SLOT_FULL,
462               barcode => 'PTAG01XX', reserved => 1, loaded_in => 1,
463               device_status => undef, f_type => undef, label => undef },
464             { slot => 5, state => Amanda::Changer::SLOT_FULL,
465               barcode => 'PTAG02XX',
466               device_status => undef, f_type => undef, label => undef },
467         ], "$pfx: inventory is updated when slots are loaded");
468     };
469
470     step load_slot_3 => sub {
471         $chg->load(slot => 5, res_cb => $steps->{'loaded_slot_3'});
472     };
473
474     step loaded_slot_3 => sub {
475         my ($err, $no_res) = @_;
476
477         chg_err_like($err,
478             { message => "no drives available",
479               reason => 'driveinuse',
480               type => 'failed' },
481             "$pfx: trying to load a third slot fails with 'no drives available'");
482
483         $steps->{'label_tape_1'}->();
484     };
485
486     step label_tape_1 => sub {
487         $res1->{'device'}->start($Amanda::Device::ACCESS_WRITE, "TAPE-1", undef);
488         $res1->{'device'}->finish();
489
490         $res1->set_label(label => "TAPE-1", finished_cb => $steps->{'label_tape_2'});
491     };
492
493     step label_tape_2 => sub {
494         my ($err) = @_;
495         die $err if $err;
496
497         pass("$pfx: labeled TAPE-1 in drive 0");
498
499         is_deeply({
500                 loaded_in => $chg->{'__last_state'}->{'slots'}->{3}->{'loaded_in'},
501                 orig_slot => $chg->{'__last_state'}->{'drives'}->{0}->{'orig_slot'},
502                 slot_label => $chg->{'__last_state'}->{'slots'}->{3}->{'label'},
503                 drive_label => $chg->{'__last_state'}->{'drives'}->{0}->{'label'},
504             }, {
505                 loaded_in => 0,
506                 orig_slot => 3,
507                 slot_label => 'TAPE-1',
508                 drive_label => 'TAPE-1',
509             }, "$pfx: label is correctly reflected in changer state");
510
511         is_deeply({
512                 slot_2_loaded_in => $chg->{'__last_state'}->{'slots'}->{4}->{'loaded_in'},
513                 slot_1_loaded_in => $chg->{'__last_state'}->{'drives'}->{1}->{'orig_slot'},
514             }, {
515                 slot_2_loaded_in => 1,
516                 slot_1_loaded_in => 4,
517             },
518             "$pfx: slot 2 'loaded_in' and drive 1 'orig_slot' are correct");
519
520         $res2->{'device'}->start($Amanda::Device::ACCESS_WRITE, "TAPE-2", undef);
521         $res2->{'device'}->finish();
522
523         $res2->set_label(label => "TAPE-2", finished_cb => $steps->{'release1'});
524     };
525
526     step release1 => sub {
527         my ($err) = @_;
528         die $err if $err;
529
530         pass("$pfx: labeled TAPE-2 in drive 1");
531
532         is_deeply({
533                 loaded_in => $chg->{'__last_state'}->{'slots'}->{4}->{'loaded_in'},
534                 orig_slot => $chg->{'__last_state'}->{'drives'}->{1}->{'orig_slot'},
535                 slot_label => $chg->{'__last_state'}->{'slots'}->{4}->{'label'},
536                 drive_label => $chg->{'__last_state'}->{'drives'}->{1}->{'label'},
537             }, {
538                 loaded_in => 1,
539                 orig_slot => 4,
540                 slot_label => 'TAPE-2',
541                 drive_label => 'TAPE-2',
542             }, "$pfx: label is correctly reflected in changer state");
543
544         $res2->release(finished_cb => $steps->{'inventory3'});
545     };
546
547     step inventory3 => sub {
548         my ($err) = @_;
549         die "$err" if $err;
550         pass("$pfx: slot 4/drive 1 released");
551
552         check_inventory($chg, $mtx_config->{'barcodes'} > 0, $steps->{'check_state_after_release1'}, [
553             { slot => 1, state => Amanda::Changer::SLOT_EMPTY,
554               import_export => 1,
555               device_status => undef, f_type => undef, label => undef },
556             { slot => 2, state => Amanda::Changer::SLOT_EMPTY,
557               import_export => 1,
558               device_status => undef, f_type => undef, label => undef },
559             { slot => 3, state => Amanda::Changer::SLOT_FULL,
560               barcode => 'PTAG00XX', reserved => 1, loaded_in => 0,
561               current => 1,
562               device_status => $DEVICE_STATUS_SUCCESS,
563               f_type => $Amanda::Header::F_TAPESTART, label => 'TAPE-1' },
564             { slot => 4, state => Amanda::Changer::SLOT_FULL,
565               barcode => 'PTAG01XX', loaded_in => 1,
566               device_status => $DEVICE_STATUS_SUCCESS,
567               f_type => $Amanda::Header::F_TAPESTART, label => 'TAPE-2' },
568             { slot => 5, state => Amanda::Changer::SLOT_FULL,
569               barcode => 'PTAG02XX',
570               device_status => undef, f_type => undef, label => undef },
571         ], "$pfx: inventory is still up to date");
572     };
573
574     step check_state_after_release1 => sub {
575         is($chg->{'__last_state'}->{'drives'}->{1}->{'res_info'}, undef,
576                 "$pfx: drive is not reserved");
577         is($chg->{'__last_state'}->{'drives'}->{1}->{'label'}, 'TAPE-2',
578                 "$pfx: tape is still in drive");
579
580         $steps->{'load_current_1'}->();
581     };
582
583     step load_current_1 => sub {
584         $chg->load(relative_slot => "current", res_cb => $steps->{'loaded_current_1'});
585     };
586
587     step loaded_current_1 => sub {
588         my ($err, $res) = @_;
589
590         chg_err_like($err,
591             { message => "the requested volume is in use (drive 0)",
592               reason => 'volinuse',
593               type => 'failed' },
594             "$pfx: loading 'current' when set_current hasn't been used yet gets slot 1 (which is in use)");
595
596         $steps->{'load_slot_4'}->();
597     };
598
599     # this should unload what's in drive 1 and load the empty volume in slot 4
600     step load_slot_4 => sub {
601         $chg->load(slot => 5, set_current => 1, res_cb => $steps->{'loaded_slot_4'});
602     };
603
604     step loaded_slot_4 => sub {
605         (my $err, $res2) = @_;
606         die "$err" if $err;
607
608         is($res2->{'device'}->device_name, $drive1_name,
609             "$pfx: loaded slot 5 into drive 1 (and set current to slot 5)");
610
611         is_deeply({
612                 loaded_in => $chg->{'__last_state'}->{'slots'}->{4}->{'loaded_in'},
613                 slot_label => $chg->{'__last_state'}->{'slots'}->{4}->{'label'},
614             }, {
615                 loaded_in => undef,
616                 slot_label => 'TAPE-2',
617             }, "$pfx: slot 4 (which was just unloaded) still tracked correctly");
618
619         is_deeply({
620                 loaded_in => $chg->{'__last_state'}->{'slots'}->{3}->{'loaded_in'},
621                 orig_slot => $chg->{'__last_state'}->{'drives'}->{0}->{'orig_slot'},
622             }, {
623                 loaded_in => 0,
624                 orig_slot => 3,
625             }, "$pfx: slot 1 'loaded_in' and drive 0 'orig_slot' are *still* correct");
626
627         is_deeply({
628                 loaded_in => $chg->{'__last_state'}->{'slots'}->{5}->{'loaded_in'},
629                 orig_slot => $chg->{'__last_state'}->{'drives'}->{1}->{'orig_slot'},
630             }, {
631                 loaded_in => 1,
632                 orig_slot => 5,
633             }, "$pfx: slot 5 'loaded_in' and drive 1 'orig_slot' are correct");
634
635         $steps->{'label_tape_4'}->();
636     };
637
638     step label_tape_4 => sub {
639         $res2->{'device'}->start($Amanda::Device::ACCESS_WRITE, "TAPE-4", undef);
640         $res2->{'device'}->finish();
641
642         $res2->set_label(label => "TAPE-4", finished_cb => $steps->{'inventory4'});
643     };
644
645     step inventory4 => sub {
646         my ($err) = @_;
647         die "$err" if $err;
648         pass("$pfx: labeled TAPE-4 in drive 1");
649
650         check_inventory($chg, $mtx_config->{'barcodes'} > 0, $steps->{'release2'}, [
651             { slot => 1, state => Amanda::Changer::SLOT_EMPTY,
652               import_export => 1,
653               device_status => undef, f_type => undef, label => undef },
654             { slot => 2, state => Amanda::Changer::SLOT_EMPTY,
655               import_export => 1,
656               device_status => undef, f_type => undef, label => undef },
657             { slot => 3, state => Amanda::Changer::SLOT_FULL,
658               barcode => 'PTAG00XX', reserved => 1, loaded_in => 0,
659               device_status => $DEVICE_STATUS_SUCCESS,
660               f_type => $Amanda::Header::F_TAPESTART, label => 'TAPE-1' },
661             { slot => 4, state => Amanda::Changer::SLOT_FULL,
662               barcode => 'PTAG01XX',
663               device_status => $DEVICE_STATUS_SUCCESS,
664               f_type => $Amanda::Header::F_TAPESTART, label => 'TAPE-2' },
665             { slot => 5, state => Amanda::Changer::SLOT_FULL,
666               barcode => 'PTAG02XX', reserved => 1, loaded_in => 1,
667               current => 1,
668               device_status => $DEVICE_STATUS_SUCCESS,
669               f_type => $Amanda::Header::F_TAPESTART, label => 'TAPE-4' },
670         ], "$pfx: inventory is up to date after more labelings");
671     };
672
673     step release2 => sub {
674         is_deeply({
675                 loaded_in => $chg->{'__last_state'}->{'slots'}->{5}->{'loaded_in'},
676                 orig_slot => $chg->{'__last_state'}->{'drives'}->{1}->{'orig_slot'},
677                 slot_label => $chg->{'__last_state'}->{'slots'}->{5}->{'label'},
678                 drive_label => $chg->{'__last_state'}->{'drives'}->{1}->{'label'},
679             }, {
680                 loaded_in => 1,
681                 orig_slot => 5,
682                 slot_label => 'TAPE-4',
683                 drive_label => 'TAPE-4',
684             }, "$pfx: label is correctly reflected in changer state");
685
686         $res1->release(finished_cb => $steps->{'release2_done'});
687     };
688
689     step release2_done => sub {
690         my ($err) = @_;
691         die $err if $err;
692
693         pass("$pfx: slot 1/drive 0 released");
694
695         is($chg->{'__last_state'}->{'drives'}->{0}->{'label'}, 'TAPE-1',
696                 "$pfx: tape is still in drive");
697
698         $steps->{'release3'}->();
699     };
700
701     step release3 => sub {
702         my ($err) = @_;
703         die $err if $err;
704
705         $res2->release(finished_cb => $steps->{'release3_done'});
706     };
707
708     step release3_done => sub {
709         my ($err) = @_;
710         die $err if $err;
711
712         pass("$pfx: slot 4/drive 0 released");
713
714         is($chg->{'__last_state'}->{'drives'}->{1}->{'label'},
715                 'TAPE-4', "$pfx: tape is still in drive");
716
717         $steps->{'quit'}->();
718     };
719
720     # note that Amanda_Changer_robot performs a *lot* more tests; they're
721     # duplicative for this changer, so they are omitted
722
723     step quit => sub {
724         unlink($chg_state_file) if -f $chg_state_file;
725         $finished_cb->();
726     };
727     # ^^ remove final call to first sub XXX
728 }
729
730 # These tests are run over a number of different mtx configurations, to ensure
731 # that the behavior is identical regardless of the changer/mtx characteristics
732 for my $mtx_config (
733     { barcodes => 1, track_orig => 1, },
734     { barcodes => 0, track_orig => 1, },
735     { barcodes => 1, track_orig => -1, },
736     { barcodes => 0, track_orig => 0, },
737     { barcodes => -1, track_orig => 0, },
738     ) {
739     test_changer($mtx_config, \&Amanda::MainLoop::quit);
740     Amanda::MainLoop::run();
741 }
742
743 $ndmp->cleanup();