1 # Copyright (c) 2008, 2010 Zmanda, Inc. All Rights Reserved.
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.
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
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
16 # Contact information: Zmanda Inc, 465 S. Mathilda Ave., Suite 300
17 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
19 use Test::More tests => 26;
23 use lib "@amperldir@";
24 use Installcheck::Config;
26 use Amanda::Config qw( :init :getconf config_dir_relative );
27 use POSIX ":sys_wait_h";
30 # put the debug messages somewhere
31 Amanda::Debug::dbopen("installcheck");
32 Installcheck::log_test_output();
39 # First try reading a tapelist
41 my $testconf = Installcheck::Config->new();
44 config_init($CONFIG_INIT_EXPLICIT_NAME, "TESTCONF") == $CFGERR_OK
45 or die("config_init failed");
46 my $tapelist = config_dir_relative("tapelist");
49 my ($filename, @lines) = @_;
50 open(my $fh, ">", $filename) or die("Could not make tapelist '$filename'");
51 for my $line (@lines) {
59 open(my $fh, "<", $filename) or die("Could not read tapelist '$filename'");
60 my @reread_lines = <$fh>;
66 "20071111010002 TESTCONF004 reuse META:META1\n",
67 "20071110010002 TESTCONF003 reuse BARCODE:BAR-003\n",
68 "20071109010002 TESTCONF002 reuse BARCODE:BAR-002 META:META2 #comment 2\n",
69 "20071108010001 TESTCONF001 no-reuse #comment 1\n",
71 mktapelist($tapelist, @lines);
73 $tl = Amanda::Tapelist->new($tapelist);
74 $tl_ok = is_deeply($tl, {
75 filename => $tapelist,
76 lockname => $tapelist . ".lock",
78 { 'datestamp' => '20071111010002', 'label' => 'TESTCONF004',
79 'reuse' => 1, 'position' => 1,
80 'barcode' => undef, 'meta' => 'META1', 'comment' => undef },
81 { 'datestamp' => '20071110010002', 'label' => 'TESTCONF003',
82 'reuse' => 1, 'position' => 2,
83 'barcode' => 'BAR-003', 'meta' => undef, 'comment' => undef },
84 { 'datestamp' => '20071109010002', 'label' => 'TESTCONF002',
85 'reuse' => 1, 'position' => 3,
86 'barcode' => 'BAR-002', 'meta' => 'META2', 'comment' => 'comment 2' },
87 { 'datestamp' => '20071108010001', 'label' => 'TESTCONF001',
88 'reuse' => '', 'position' => 4,
89 'barcode' => undef, 'meta' => undef, 'comment' => 'comment 1' },
90 ] }, "A simple tapelist is parsed correctly");
93 skip "Tapelist is parsed incorrectly, so these tests are unlikely to work", 15,
96 # now try writing it out and check that the results are the same
97 $tl->write("$tapelist-new");
98 my @reread_lines = readtapelist("$tapelist-new");
99 is_deeply(\@reread_lines, \@lines, "Lines of freshly written tapelist match the original");
101 is_deeply($tl->lookup_tapelabel('TESTCONF002'),
102 { 'datestamp' => '20071109010002', 'label' => 'TESTCONF002',
103 'reuse' => 1, 'position' => 3,
104 'barcode' => 'BAR-002', 'meta' => 'META2', 'comment' => 'comment 2' },
105 "lookup_tapelabel works");
107 is_deeply($tl->lookup_tapelabel('TESTCONF009'), undef,
108 "lookup_tapelabel returns undef on an unknown label");
110 is_deeply($tl->lookup_tapepos(4),
111 { 'datestamp' => '20071108010001', 'label' => 'TESTCONF001',
112 'reuse' => '', 'position' => 4,
113 'barcode' => undef, 'meta' => undef, 'comment' => 'comment 1' },
114 "lookup_tapepos works");
116 is_deeply($tl->lookup_tapepos(9), undef,
117 "lookup_tapepos returns undef on an unknown position");
119 is_deeply($tl->lookup_tapedate('20071110010002'),
120 { 'datestamp' => '20071110010002', 'label' => 'TESTCONF003',
121 'reuse' => 1, 'position' => 2,
122 'barcode' => 'BAR-003', 'meta' => undef, 'comment' => undef },
123 "lookup_tapedate works");
125 is_deeply($tl->lookup_tapedate('12345678'), undef,
126 "lookup_tapedate returns undef on an unknown datestamp");
129 $tl->add_tapelabel("20080112010203", "TESTCONF007", "seven", 1, 'META3', 'BAR-007');
130 is(scalar @{$tl->{'tles'}}, 5, "add_tapelabel adds a new element to the tapelist");
132 is_deeply($tl->lookup_tapepos(1),
133 { 'datestamp' => '20080112010203', 'label' => 'TESTCONF007',
134 'reuse' => 1, 'position' => 1,
135 'barcode' => 'BAR-007', 'meta' => 'META3', 'comment' => 'seven' },
136 ".. lookup_tapepos finds it at the beginning");
138 is_deeply($tl->lookup_tapelabel("TESTCONF007"),
139 { 'datestamp' => '20080112010203', 'label' => 'TESTCONF007',
140 'reuse' => 1, 'position' => 1,
141 'barcode' => 'BAR-007', 'meta' => 'META3' , 'comment' => 'seven' },
142 ".. lookup_tapelabel finds it");
144 is_deeply($tl->lookup_tapedate("20080112010203"),
145 { 'datestamp' => '20080112010203', 'label' => 'TESTCONF007',
146 'reuse' => 1, 'position' => 1,
147 'barcode' => 'BAR-007', 'meta' => 'META3', 'comment' => 'seven' },
148 ".. lookup_tapedate finds it");
151 $tl->add_tapelabel("20080112010204", "TESTCONF008", "eight", 0);
152 is(scalar @{$tl->{'tles'}}, 6, "add_tapelabel adds a new element to the tapelist no-reuse");
154 is_deeply($tl->lookup_tapelabel("TESTCONF008"),
155 { 'datestamp' => '20080112010204', 'label' => 'TESTCONF008',
156 'reuse' => 0, 'position' => 1,
157 'barcode' => undef, 'meta' => undef, 'comment' => 'eight' },
158 ".. lookup_tapelabel finds it no-reuse");
160 $tl->remove_tapelabel("TESTCONF008");
161 is(scalar @{$tl->{'tles'}}, 5, "remove_tapelabel removes an element from the tapelist, no-reuse");
163 $tl->remove_tapelabel("TESTCONF002");
164 is(scalar @{$tl->{'tles'}}, 4, "remove_tapelabel removes an element from the tapelist");
166 is_deeply($tl->lookup_tapepos(4), # used to be in position 5
167 { 'datestamp' => '20071108010001', 'label' => 'TESTCONF001',
168 'reuse' => '', 'position' => 4,
169 'barcode' => undef, 'meta' => undef, 'comment' => 'comment 1' },
170 ".. tape positions are adjusted correctly");
172 is_deeply($tl->lookup_tapelabel("TESTCONF002"), undef,
173 ".. lookup_tapelabel no longer finds it");
175 is_deeply($tl->lookup_tapedate("20071109010002"), undef,
176 ".. lookup_tapedate no longer finds it");
178 # insert in the middle of the list.
179 $tl->add_tapelabel("20071109010204", "TESTCONF009", "nine", 1);
181 is_deeply($tl->lookup_tapepos(4),
182 { 'datestamp' => '20071109010204', 'label' => 'TESTCONF009',
183 'reuse' => '1', 'position' => 4,
184 'barcode' => undef, 'meta' => undef, 'comment' => 'nine' },
185 ".. tape positions are adjusted correctly");
187 is_deeply($tl->lookup_tapelabel('TESTCONF009'),
188 { 'datestamp' => '20071109010204', 'label' => 'TESTCONF009',
189 'reuse' => '1', 'position' => 4,
190 'barcode' => undef, 'meta' => undef, 'comment' => 'nine' },
191 ".. tape positions are adjusted correctly");
193 ## set tapecycle to 0 to perform the next couple tests
195 my $cor = new_config_overrides(1);
196 add_config_override_opt($cor, "tapecycle=1");
197 set_config_overrides($cor);
198 config_init($CONFIG_INIT_EXPLICIT_NAME, "TESTCONF") == $CFGERR_OK
199 or die("config_init failed");
201 is( Amanda::Tapelist::get_last_reusable_tape_label(0),
202 'TESTCONF002', ".. get_last_reusable_tape_labe for skip=0" );
204 is( Amanda::Tapelist::get_last_reusable_tape_label(2),
205 'TESTCONF004', ".. get_last_reusable_tape_labe for skip=2" );
208 # try parsing various invalid lines
210 "2006123456 FOO reuse\n", # valid
211 "TESTCONF003 290385098 reuse\n", # invalid
212 "20071109010002 TESTCONF002 re-use\n", # invalid
213 "20071108010001 TESTCONF001\n", # invalid
214 "20071108010001 TESTCONF001 #comment\n", # invalid
215 "#comment\n", # invalid
217 mktapelist($tapelist, @lines);
219 $tl = Amanda::Tapelist->new($tapelist);
221 filename => $tapelist,
222 lockname => $tapelist . ".lock",
224 { 'datestamp' => '2006123456', 'label' => 'FOO',
225 'reuse' => 1, 'position' => 1,
226 'barcode' => undef, 'meta' => undef, 'comment' => undef },
227 ] }, "Invalid lines are ignored");
229 # make sure clear_tapelist is empty
230 $tl->clear_tapelist();
231 is_deeply($tl, { filename => $tapelist,
232 lockname => $tapelist . ".lock",
233 tles => [] }, "clear_tapelist returns an empty tapelist");
237 filename => $tapelist,
238 lockname => $tapelist . ".lock",
240 { 'datestamp' => '2006123456', 'label' => 'FOO',
241 'reuse' => 1, 'position' => 1,
242 'barcode' => undef, 'meta' => undef, 'comment' => undef },
243 ] }, "reload works");