File: Amanda_Changer_compat.pl

package info (click to toggle)
amanda 1%3A3.3.1-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 22,360 kB
  • sloc: ansic: 214,899; perl: 58,075; sh: 16,954; xml: 13,853; makefile: 2,228; awk: 431; lex: 405; yacc: 343; tcl: 118; sql: 19; sed: 16; php: 2
file content (544 lines) | stat: -rw-r--r-- 14,473 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
# Copyright (c) 2008, 2009, 2010 Zmanda, Inc.  All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
#
# Contact information: Zmanda Inc, 465 S. Mathilda Ave., Suite 300
# Sunnyvale, CA 94086, USA, or: http://www.zmanda.com

use Test::More tests => 31;
use File::Path;
use strict;
use warnings;

use lib "@amperldir@";
use Installcheck;
use Installcheck::Config;
use Installcheck::Run;
use Installcheck::Changer;
use Amanda::Paths;
use Amanda::Device;
use Amanda::Debug qw( :logging );
use Amanda::MainLoop;
use Amanda::Config qw( :init :getconf config_dir_relative );
use Amanda::Changer;

# set up debugging so debug output doesn't interfere with test results
Amanda::Debug::dbopen("installcheck");
Installcheck::log_test_output();

# and disable Debug's die() and warn() overrides
Amanda::Debug::disable_die_override();

my $changer_filename = "$Installcheck::TMP/chg-test";
my $result_file = "$Installcheck::TMP/chg-test.result";

# Set up a 'test' changer; several of these are defined below.
sub setup_changer {
    my ($changer_script) = @_;

    open my $chg_test, ">", $changer_filename or die("Could not create test changer");

    $changer_script =~ s/\$Installcheck::TMP/$Installcheck::TMP/g;

    print $chg_test "#! /bin/sh\n";
    print $chg_test $changer_script;

    close $chg_test;
    chmod 0755, $changer_filename;
}

# slurp the $result_file
sub slurp_result {
    return '' unless (-r $result_file);

    open(my $fh, "<", $result_file) or die("open $result_file: $!");
    my $result = do { local $/; <$fh> };
    close($fh);

    return $result;
}

# Functions to invoke the changer and later verify the result
my ($check_res_cb, $check_finished_cb);
{
    my $expected_err_info;
    my $expected_dev;
    my $msg;
    my $quit;

    $check_res_cb = make_cb('check_res_cb' => sub {
	my ($err, $res) = @_;

	if ($err) {
	    if (defined($expected_err_info)) {
		chg_err_like($err, $expected_err_info, $msg);
	    } else {
		fail($msg);
		diag("Unexpected error: $err");
	    }
	} else {
	    if (defined($expected_dev)) {
		is($res->{'device'}->device_name, $expected_dev, $msg);
	    } else {
		fail($msg);
		diag("Unexpected reservation");
	    }
	}

	if ($res) {
	    $res->release(finished_cb => $quit);
	} else {
	    $quit->();
	}
    });

    $check_finished_cb = make_cb('check_finished_cb' => sub {
	my ($err, $res) = @_;

	if ($err) {
	    if (defined($expected_err_info)) {
		chg_err_like($err, $expected_err_info, $msg);
	    } else {
		fail($msg);
		diag("Unexpected error: $err");
	    }
	} else {
	    if (!defined($expected_err_info)) {
		pass($msg);
	    } else {
		fail($msg);
		diag("Unexpected success");
	    }
	}

	if ($res) {
	    $res->release(finished_cb => $quit);
	} else {
	    $quit->();
	}
    });

    $quit = make_cb(quit => sub {
	my ($err) = @_;
	die $err if $err;

	Amanda::MainLoop::quit();
    });

    sub try_run_changer {
	my $sub;
	($sub, $expected_err_info, $expected_dev, $msg) = @_;

	Amanda::MainLoop::call_later($sub);
	Amanda::MainLoop::run();
    }
}

# OK, let's get started with some simple stuff
setup_changer <<'EOC';
case "${1}" in
    -slot)
        case "${2}" in
            1) echo "1 null:fake1"; exit 0;;
            2) echo "<ignored> slot 2 is empty"; exit 1;;
            3) echo "1"; exit 0;; # test missing 'device' portion
	    4) echo "1 bogus:dev"; exit 0;;
	    5) echo "<error> multiline error"; echo "line 2"; exit 1;;
	    current) echo "1 null:current"; exit 0;;
	    next) echo "1 null:next"; exit 0;;
        esac;;
    -reset)
	echo "reset" > $Installcheck::TMP/chg-test.result
	echo "reset ignored";;
    -eject)
	echo "eject" > $Installcheck::TMP/chg-test.result
	echo "eject ignored";;
    -clean)
	echo "clean" > $Installcheck::TMP/chg-test.result
	echo "clean ignored";;
    -label)
        case "${2}" in
            foo?bar) echo "1 ok"; exit 0;;
            *) echo "<error> bad label"; exit 1;;
        esac;;
    -info) echo "7 10 1 1"; exit 0;;
    -search)
        case "${2}" in
            TAPE?01) echo "5 null:fakedev"; exit 0;;
	    fatal) echo "<error> game over"; exit 2;;
            *) echo "<error> not found"; exit 1;;
        esac;;
esac
EOC

# set up a config for this changer, implicitly using Amanda::Changer::Compat
my $testconf;
$testconf = Installcheck::Config->new();
$testconf->add_param("tpchanger", "\"$changer_filename\"");
$testconf->write();

my $cfg_result = config_init($CONFIG_INIT_EXPLICIT_NAME, 'TESTCONF');
if ($cfg_result != $CFGERR_OK) {
    my ($level, @errors) = Amanda::Config::config_errors();
    die(join "\n", @errors);
}

my $chg = Amanda::Changer->new();
die($chg) if $chg->isa("Amanda::Changer::Error");

is($chg->have_inventory(), '', "changer have inventory");

try_run_changer(
    sub { $chg->load(label => 'TAPE-01', res_cb => $check_res_cb); },
    undef,
    "null:fakedev",
    "search by label succeeds");

try_run_changer(
    sub { $chg->load(label => 'TAPE-99', res_cb => $check_res_cb); },
    { message => "not found", type => 'failed', reason => 'notfound' },
    undef,
    "search by label; nonexistent tape");

try_run_changer(
    sub { $chg->load(slot => '1', res_cb => $check_res_cb); },
    undef,
    "null:fake1",
    "search by slot");

try_run_changer(
    sub { $chg->load(slot => '2', res_cb => $check_res_cb); },
    { message => "slot 2 is empty", type => 'failed', reason => 'notfound' },
    undef,
    "search by slot; empty slot");

try_run_changer(
    sub { $chg->load(slot => '3', res_cb => $check_res_cb); },
    { message => "changer script did not provide a device name", type => 'fatal' },
    undef,
    "search by slot; no device in response");

try_run_changer(
    sub { $chg->load(slot => '1', res_cb => $check_res_cb); },
    { message => "changer script did not provide a device name", type => 'fatal' },
    undef,
    "fatal error is sticky");

$chg->{'fatal_error'} = undef; # reset the fatal error

try_run_changer(
    sub { $chg->load(slot => '4', res_cb => $check_res_cb); },
    { message => "opening 'bogus:dev': Device type bogus is not known.",
      type => 'failed',
      reason => 'device' },
    undef,
    "search by slot; bogus device leads to 'failed' error");

$chg->{'fatal_error'} = undef; # reset the fatal error

try_run_changer(
    sub { $chg->load(slot => '5', res_cb => $check_res_cb); },
    { message => "multiline error\nline 2",
      type => 'failed',
      reason => 'notfound' },
    undef,
    "multiline error response captured in its entirety");

$chg->{'fatal_error'} = undef; # reset the fatal error

try_run_changer(
    sub { $chg->load(label => 'fatal', res_cb => $check_res_cb); },
    { message => "game over", type => 'fatal' },
    undef,
    "search by label with fatal error");

# reset the fatal error
$chg->{'fatal_error'} = undef;

try_run_changer(
    sub { $chg->eject(finished_cb => $check_finished_cb); },
    undef, undef, "chg->eject doesn't fail");
like(slurp_result(), qr/eject/, ".. and calls chg-test -eject");

try_run_changer(
    sub { $chg->reset(finished_cb => $check_finished_cb); },
    undef, undef, "chg->reset doesn't fail");
like(slurp_result(), qr/reset/, ".. and calls chg-test -reset");

try_run_changer(
    sub { $chg->clean(finished_cb => $check_finished_cb); },
    undef, undef, "chg->clean doesn't fail");
like(slurp_result(), qr/clean/, ".. and calls chg-test -clean");

try_run_changer(
    sub { $chg->update(finished_cb => $check_finished_cb); },
    undef, undef, "chg->update doesn't fail");

try_run_changer(
    sub { $chg->inventory(inventory_cb => $check_finished_cb); },
    { message => "'chg-compat:' does not support inventory",
	    type => 'failed', reason => 'notimpl' },
    undef,
    "inventory not implemented");


# make sure only one reservation can be held at once
{
    my $first_res;

    my ($load_1, $load_2, $check_load_2, $check_eject);

    $load_1 = make_cb('load_1' => sub {
        $chg->load(slot => 1, res_cb => $load_2);
    });

    $load_2 = make_cb('load_2' => sub {
        my ($err, $res) = @_;
        die $err if ($err);

        # keep this in scope through the next load
        $first_res = $res;

        $chg->load(slot => 2, res_cb => $check_load_2);
    });

    $check_load_2 = make_cb('check_load_2' => sub {
        my ($err, $res) = @_;

        like($err, qr/Changer is already reserved/,
            "mulitple simultaneous reservations not alowed");

        $first_res->release(eject => 1, finished_cb => $check_eject);
    });

    $check_eject = make_cb('check_eject' => sub {
        my ($err) = @_;

        ok(!defined $err, "release with eject succeeds");

	like(slurp_result(), qr/eject/, "..and calls chg-test -eject");

        Amanda::MainLoop::quit();
    });

    $load_1->();
    Amanda::MainLoop::run();
}

## check chg-disk

# Installcheck::Run sets up the whole chg-disk thing for us
$testconf = Installcheck::Run::setup();
$testconf->write();

$cfg_result = config_init($CONFIG_INIT_EXPLICIT_NAME, 'TESTCONF');
if ($cfg_result != $CFGERR_OK) {
    my ($level, @errors) = Amanda::Config::config_errors();
    die(join "\n", @errors);
}

$chg->quit();
$chg = Amanda::Changer->new();
die($chg) if $chg->isa("Amanda::Changer::Error");

{
    my $res;
    my ($get_info, $load_current, $label_current, $load_next,
        $released1, $release_next, $load_by_label, $check_by_label);

    $get_info = make_cb('get_info' => sub {
        $chg->info(info_cb => $load_current, info => [ 'num_slots', 'fast_search' ]);
    });

    $load_current = make_cb('load_current' => sub {
        my $err = shift;
        my %results = @_;
        die($err) if defined($err);

        is_deeply({ %results },
	    { num_slots => 3, fast_search => 0 }, # old chg-disk is not searchable
	    "info() returns the correct num_slots and fast_search");

        $chg->load(slot => "1", res_cb => $label_current);
    });

    $label_current = make_cb('label_current' => sub {
        (my $err, $res) = @_;
        die $err if ($err);

        pass("seek to current slot succeeded");

        my $dev = $res->{'device'};
        $dev->start($Amanda::Device::ACCESS_WRITE, "TESTCONF18", undef)
            or die $dev->error_or_status();
        $dev->finish()
            or die $dev->error_or_status();

        is($res->{'this_slot'}, "1", "this slot is '1'");
        $res->set_label(label => "TESTCONF18", finished_cb => $load_next);
    });

    $load_next = make_cb('load_next' => sub {
        my ($err) = @_;
        die $err if ($err);

        pass("set_label succeeded");

	$res->release(finished_cb => $released1);
    });

    $released1 = make_cb(released1 => sub {
	my ($err) = @_;
	die $err if $err;

        $chg->load(relative_slot => "next", res_cb => $release_next);
    });

    $release_next = make_cb('release_next' => sub {
        (my $err, $res) = @_;
        die $err if ($err);

        pass("load relative slot 'next' succeeded");

        $res->release(finished_cb => $load_by_label);
    });

    $load_by_label = make_cb('load_by_label' => sub {
        my ($err) = @_;
        die $err if ($err);

        pass("release loaded");

        $chg->load(label => "TESTCONF18", res_cb => $check_by_label);
    });

    $check_by_label = make_cb('check_by_label' => sub {
        (my $err, $res) = @_;
        die $err if ($err);

        pass("load by label succeeded");

        my $dev = $res->{'device'};
        $dev->read_label() == 0
            or die $dev->error_or_status();

        is($dev->volume_label(), "TESTCONF18",
            "..and finds the right volume");

	$res->release(finished_cb => sub {
	    my ($err) = @_;
	    die $err if $err;

	    Amanda::MainLoop::quit();
	});
    });

    $get_info->();
    Amanda::MainLoop::run();
}
$chg->quit();

# test two simultaneous invocations of info()

$chg = Amanda::Changer->new();
die($chg) if $chg->isa("Amanda::Changer::Error");

sub test_get_infos {
    my ($finished_cb) = @_;
    my $n_info_results = 0;

    my $steps = define_steps
	cb_ref => \$finished_cb;

    step get_infos => sub {
	# convince the changer that it has not gotten any info yet
	$chg->{'got_info'} = 0;

        $chg->info(info_cb => $steps->{'got_info_result'}, info => [ 'num_slots' ]);
        $chg->info(info_cb => $steps->{'got_info_result'}, info => [ 'fast_search' ]);
    };

    step got_info_result => sub {
	my ($err, %info) = @_;
	die $err if $err;
	++$n_info_results;
	if ($n_info_results >= 2) {
	    pass("two simultaneous info() invocations are successful");
	    $finished_cb->();
	}
    };
}
test_get_infos(\&Amanda::MainLoop::quit);
Amanda::MainLoop::run();

# scan the changer using except_slots
sub test_except_slots {
    my ($finished_cb) = @_;
    my $slot;
    my %except_slots;

    my $steps = define_steps
	cb_ref => \$finished_cb;

    step start => sub {
	$chg->load(relative_slot => "current",
		   except_slots => { %except_slots },
		   res_cb => $steps->{'loaded'});
    };

    step loaded => sub {
        my ($err, $res) = @_;
	if ($err) {
	    if ($err->notfound) {
		# this means the scan is done
		return $steps->{'quit'}->();
	    } elsif ($err->volinuse and defined $err->{'slot'}) {
		$slot = $err->{'slot'};
	    } else {
		die $err;
	    }
	} else {
	    $slot = $res->{'this_slot'};
	}

	$except_slots{$slot} = 1;

	if ($res) {
	    $res->release(finished_cb => $steps->{'released'});
	} else {
	    $steps->{'released'}->();
	}
    };

    step released => sub {
	my ($err) = @_;
	die $err if $err;

        $chg->load(relative_slot => 'next', slot => $slot,
		   except_slots => { %except_slots },
		   res_cb => $steps->{'loaded'});
    };

    step quit => sub {
	is_deeply({ %except_slots }, { 1=>1, 2=>1, 3=>1 },
		"scanning with except_slots works");
	$finished_cb->();
    };
}
test_except_slots(\&Amanda::MainLoop::quit);
Amanda::MainLoop::run();
$chg->quit();

unlink($changer_filename);
unlink($result_file);