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