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