Imported Upstream version 3.3.3
[debian/amanda] / installcheck / amrmtape.pl
1 # Copyright (c) 2008-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 => 41;
21 use strict;
22 use warnings;
23
24 use lib "@amperldir@";
25 use File::Find;
26 use Amanda::Config qw( :init :getconf config_dir_relative );
27 use Amanda::Device qw( :constants );
28 use Amanda::Paths;
29 use Amanda::Tapelist;
30 use Installcheck::Config;
31 use Installcheck::Run qw(run run_err $diskname);
32 use Installcheck::Dumpcache;
33
34 Amanda::Debug::dbopen("installcheck");
35 Installcheck::log_test_output();
36
37 sub proc_diag {
38     diag(join("\n", $?,
39         'stdout:', $Installcheck::Run::stdout, '',
40         'stderr:', $Installcheck::Run::stderr));
41 }
42
43 # note: assumes the config is already loaded and takes a config param
44 # to get as a directory and then count all the files in
45 sub dir_file_count {
46     my $conf_param = shift @_;
47     my $dir_name = getconf($conf_param);
48
49     my $num_files = 0;
50     my $opts = {
51         'wanted' => sub {
52             # ignore directories
53             return if -d $File::Find::name;
54             $num_files++;
55         },
56     };
57
58     find($opts, $dir_name);
59     $num_files;
60 }
61
62 my $dev;
63 my ($idx_count_pre, $idx_count_post);
64
65
66 ## test config overrides
67 Installcheck::Dumpcache::load("notimestamps");
68
69 config_init($CONFIG_INIT_EXPLICIT_NAME, 'TESTCONF');
70
71 cmp_ok(
72     run(qw(amrmtape -o tapelist=/this/is/a/fake/tapelist TESTCONF TESTCONF01)),
73     "==", 1, "config override run"
74 ) or proc_diag();
75
76 cmp_ok(
77     $Installcheck::Run::stdout, "=~",
78     qr/label 'TESTCONF01' not found in \/this\/is\/a\/fake\/tapelist/,
79     "config overrides handled correctly"
80 ) or proc_diag();
81
82 ## test
83
84 Installcheck::Dumpcache::load("notimestamps");
85
86 config_init($CONFIG_INIT_EXPLICIT_NAME, 'TESTCONF');
87 my $tapelist = Amanda::Tapelist->new(config_dir_relative("tapelist"));
88 ok($tapelist->lookup_tapelabel('TESTCONF01'), "looked up tape after dump");
89
90 $idx_count_pre = dir_file_count($CNF_INDEXDIR);
91
92 ok(run('amrmtape', 'TESTCONF', 'TESTCONF01'), "amrmtape runs successfully")
93     or proc_diag();
94
95 $idx_count_post = dir_file_count($CNF_INDEXDIR);
96 is($idx_count_post, $idx_count_pre, "number of index files before and after is the same");
97
98 $tapelist->reload();
99 ok(!$tapelist->lookup_tapelabel('TESTCONF01'),
100      "should fail to look up tape that should has been removed");
101
102 $dev = Amanda::Device->new('file:' . Installcheck::Run::vtape_dir());
103
104 ok($dev->start($ACCESS_READ, undef, undef),
105     "start device in read mode")
106     or diag($dev->error_or_status());
107
108 ok($dev->finish(),
109    "finish device after starting")
110     or diag($dev->error_or_status());
111
112 # test --cleanup
113
114 Installcheck::Dumpcache::load("notimestamps");
115
116 $idx_count_pre = dir_file_count($CNF_INDEXDIR);
117
118 ok(run('amrmtape', '--cleanup', 'TESTCONF', 'TESTCONF01'),
119     "amrmtape runs successfully with --cleanup")
120      or proc_diag();
121
122 $idx_count_post = dir_file_count($CNF_INDEXDIR);
123 isnt($idx_count_post, $idx_count_pre, "number of index files before and after is different");
124
125 $tapelist->reload();
126 ok(!$tapelist->lookup_tapelabel('TESTCONF01'),
127      "succesfully looked up tape that should have been removed after --cleanup");
128
129 $dev = Amanda::Device->new('file:' . Installcheck::Run::vtape_dir());
130
131 ok($dev->start($ACCESS_READ, undef, undef),
132     "start device in read mode")
133     or diag($dev->error_or_status());
134
135 ok($dev->finish(),
136    "finish device after starting")
137     or diag($dev->error_or_status());
138
139 # test --erase
140
141 Installcheck::Dumpcache::load("notimestamps");
142
143 $idx_count_pre = dir_file_count($CNF_INDEXDIR);
144
145 ok(run('amrmtape', '--erase', 'TESTCONF', 'TESTCONF01'),
146     "amrmtape runs successfully with --erase")
147     or proc_diag();
148
149 $idx_count_post = dir_file_count($CNF_INDEXDIR);
150 is($idx_count_post, $idx_count_pre, "number of index files before and after is the same");
151
152 $tapelist->reload();
153 ok(!$tapelist->lookup_tapelabel('TESTCONF01'),
154      "succesfully looked up tape that should have been removed after --erase");
155
156 $dev = Amanda::Device->new('file:' . Installcheck::Run::vtape_dir());
157
158 ok(!$dev->start($ACCESS_READ, undef, undef),
159     "start device in read mode fails")
160     or diag($dev->error_or_status());
161
162 # just in case the above does start the device
163 ok($dev->finish(),
164    "finish device (just in case)")
165     or diag($dev->error_or_status());
166
167 # test --keep-label
168
169 Installcheck::Dumpcache::load("notimestamps");
170
171 $idx_count_pre = dir_file_count($CNF_INDEXDIR);
172
173 ok(run('amrmtape', '--keep-label', 'TESTCONF', 'TESTCONF01'),
174    "amrmtape runs successfully with --keep-label")
175     or proc_diag();
176
177 $idx_count_post = dir_file_count($CNF_INDEXDIR);
178 is($idx_count_post, $idx_count_pre, "number of index files before and after is the same");
179
180 $tapelist->reload();
181 my $tape = $tapelist->lookup_tapelabel('TESTCONF01');
182 ok($tape, "succesfully looked up tape that should still be there");
183 is($tape->{'datestamp'}, "0", "datestamp was zeroed");
184
185 $dev = Amanda::Device->new('file:' . Installcheck::Run::vtape_dir());
186
187 ok($dev->start($ACCESS_READ, undef, undef),
188     "start device in read mode")
189     or diag($dev->error_or_status());
190
191 ok($dev->finish(),
192    "finish device after starting")
193     or diag($dev->error_or_status());
194
195 # test --keep-label --erase
196
197 Installcheck::Dumpcache::load("notimestamps");
198
199 $idx_count_pre = dir_file_count($CNF_INDEXDIR);
200
201 ok(run('amrmtape', '--keep-label', '--erase', 'TESTCONF', 'TESTCONF01'),
202    "amrmtape runs successfully with --keep-label")
203     or proc_diag();
204
205 $idx_count_post = dir_file_count($CNF_INDEXDIR);
206 is($idx_count_post, $idx_count_pre, "number of index files before and after is the same");
207
208 $tapelist->reload();
209 $tape = $tapelist->lookup_tapelabel('TESTCONF01');
210 ok($tape, "succesfully looked up tape that should still be there");
211 is($tape->{'datestamp'}, "0", "datestamp was zeroed");
212
213 $dev = Amanda::Device->new('file:' . Installcheck::Run::vtape_dir());
214
215 $dev->read_label();
216 ok(!($dev->status() & $DEVICE_STATUS_VOLUME_UNLABELED),
217     "tape still has label")
218     or diag($dev->error_or_status());
219 is($dev->volume_label, 'TESTCONF01', "label is correct");
220
221 # test --keep-label --erase
222
223 Installcheck::Dumpcache::load("notimestamps");
224
225 $idx_count_pre = dir_file_count($CNF_INDEXDIR);
226
227 ok(run('amrmtape', '--keep-label', '--erase', 'TESTCONF', 'TESTCONF01'),
228    "amrmtape runs successfully with --keep-label")
229     or proc_diag();
230
231 $idx_count_post = dir_file_count($CNF_INDEXDIR);
232 is($idx_count_post, $idx_count_pre, "number of index files before and after is the same");
233
234 $tapelist->reload();
235 $tape = $tapelist->lookup_tapelabel('TESTCONF01');
236 ok($tape, "succesfully looked up tape that should still be there");
237 is($tape->{'datestamp'}, "0", "datestamp was zeroed");
238
239 $dev = Amanda::Device->new('file:' . Installcheck::Run::vtape_dir());
240
241 $dev->read_label();
242 ok(!($dev->status() & $DEVICE_STATUS_VOLUME_UNLABELED),
243     "tape still has label")
244     or diag($dev->error_or_status());
245 is($dev->volume_label, 'TESTCONF01', "label is correct");
246
247 # test --dryrun --erase --cleanup
248
249 Installcheck::Dumpcache::load("notimestamps");
250
251 $idx_count_pre = dir_file_count($CNF_INDEXDIR);
252
253 ok(run('amrmtape', '--dryrun', '--erase', '--cleanup', 'TESTCONF', 'TESTCONF01'),
254     "amrmtape runs successfully with --dryrun --erase --cleanup")
255     or proc_diag();
256
257 $idx_count_post = dir_file_count($CNF_INDEXDIR);
258 is($idx_count_post, $idx_count_pre, "number of index files before and after is the same");
259
260 $tapelist->reload();
261 ok($tapelist->lookup_tapelabel('TESTCONF01'),
262      "succesfully looked up tape that should still be there");
263
264 $dev = Amanda::Device->new('file:' . Installcheck::Run::vtape_dir());
265
266 ok($dev->start($ACCESS_READ, undef, undef),
267     "start device in read mode")
268     or diag($dev->error_or_status());
269
270 ok($dev->finish(),
271    "finish device after starting")
272     or diag($dev->error_or_status());
273
274 Installcheck::Run::cleanup();