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