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