Imported Upstream version 3.2.1
[debian/amanda] / installcheck / amlabel.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 => 19;
20 use strict;
21 use warnings;
22
23 use lib "@amperldir@";
24 use Installcheck::Config;
25 use Installcheck::Run qw(run run_err run_get load_vtape vtape_dir);
26 use Amanda::Device qw( :constants );
27 use Amanda::Config qw( :init :getconf );
28 use Amanda::Paths;
29 use Amanda::Debug;
30 use Amanda::Constants;
31 use Amanda::Tapelist;
32
33 my $testconf;
34
35 Amanda::Debug::dbopen("installcheck");
36 Installcheck::log_test_output();
37
38 $testconf = Installcheck::Run::setup();
39 $testconf->write();
40
41 config_init($CONFIG_INIT_EXPLICIT_NAME, "TESTCONF");
42 my ($cfgerr_level, @cfgerr_errors) = config_errors();
43 if ($cfgerr_level >= $CFGERR_WARNINGS) {
44     config_print_errors();
45     BAIL_OUT("config errors");
46 }
47
48 # label slot 2 with "MyTape", slot 3 with "TESTCONF13", and add
49 # the latter to the tapelist
50 my ($devdir, $dev);
51
52 $devdir = load_vtape(2);
53 $dev = Amanda::Device->new("file:$devdir");
54 ($dev && $dev->status == $DEVICE_STATUS_SUCCESS)
55     or BAIL_OUT("device error");
56
57 $dev->start($ACCESS_WRITE, "MyTape", undef)
58     or BAIL_OUT("device error");
59 $dev->finish()
60     or BAIL_OUT("device error");
61
62 my $tlf = Amanda::Config::config_dir_relative(getconf($CNF_TAPELIST));
63 my $tl = Amanda::Tapelist->new($tlf, 1);
64 $tl->add_tapelabel("0", "TESTCONF13", "test tape");
65 $tl->write($tlf);
66
67 like(run_err('amlabel'),
68     qr/^Usage:/,
69     "bare 'amlabel' gives usage message");
70
71 like(run_get('amlabel', '--version'),
72     qr/^amlabel-\Q$Amanda::Constants::VERSION\E/,
73     "'amlabel --version' gives version");
74
75 like(run_get('amlabel', 'TESTCONF', 'TESTCONF92'),
76     qr/Writing label 'TESTCONF92'/,
77     "amlabel labels the current slot by default");
78
79 $tl->reload();
80 is_deeply($tl->{'tles'}->[0], {
81        'reuse' => 1,
82         'barcode' => undef,
83        'comment' => undef,
84        'position' => 1,
85        'label' => 'TESTCONF92',
86        'datestamp' => '0'
87      },
88     "tapelist correctly updated");
89
90 $devdir = load_vtape(1);
91 $dev = Amanda::Device->new("file:$devdir");
92 die "read_label failed" unless $dev->read_label() == $DEVICE_STATUS_SUCCESS;
93 is($dev->volume_label, "TESTCONF92", "volume is actually labeled");
94
95 ok(!run('amlabel', 'TESTCONF', 'TESTCONF93'),
96     "amlabel refuses to re-label a labeled volume");
97 like($Installcheck::Run::stdout,
98     qr/Volume with label 'TESTCONF92' is active and contains data from this configuration/,
99     "with correct message");
100
101 ok(!run('amlabel', 'TESTCONF', 'SomeTape'),
102     "amlabel refuses to write a non-matching label");
103 like($Installcheck::Run::stderr,
104     qr/Label 'SomeTape' doesn't match labelstr '.*'/,
105     "with correct message on stderr");
106
107 ok(!run('amlabel', '-f', 'TESTCONF', 'SomeTape'),
108     "amlabel will not write a non-matching label even with -f");
109
110 ok(!run('amlabel', 'TESTCONF', 'TESTCONF13', 'slot', '3'),
111     "amlabel refuses to write a label already in the tapelist (and recognizes 'slot xx')");
112 like($Installcheck::Run::stderr,
113     qr/Label 'TESTCONF13' already on a volume/,
114     "with correct message on stderr");
115
116 ok(run('amlabel', '-f', 'TESTCONF', 'TESTCONF13', 'slot', '3'),
117     "amlabel will write a label already in the tapelist with -f");
118 like($Installcheck::Run::stdout,
119     qr/Writing label 'TESTCONF13'/,
120     "with correct message on stdout");
121
122 ok(!run('amlabel', 'TESTCONF', 'TESTCONF88', 'slot', '2'),
123     "amlabel refuses to overwrite a non-matching label");
124 like($Installcheck::Run::stdout,
125     qr/Found label 'MyTape', but it is not from configuration 'TESTCONF'\./,
126     "with correct message on stdout");
127
128 ok(run('amlabel', '-f', 'TESTCONF', 'TESTCONF88', 'slot', '2'),
129     "amlabel will overwrite a non-matching label with -f");
130 like($Installcheck::Run::stdout,
131     qr/Found label 'MyTape', but it is not from configuration 'TESTCONF'\.
132 Writing label 'TESTCONF88'/,
133     "with correct message on stdout");
134
135 ok(run('amlabel', 'TESTCONF', 'TESTCONF88', '-f', 'slot', '2'),
136     "-f option doesn't have to follow 'amlabel'");