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