1 # Copyright (c) 2005-2008 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 Mathlida Ave, Suite 300
17 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
19 use Test::More tests => 18;
22 use lib "@amperldir@";
23 use Installcheck::Config;
25 use Amanda::Config qw( :init :getconf config_dir_relative );
32 # First try reading a tapelist
34 my $testconf = Installcheck::Config->new();
37 config_init($CONFIG_INIT_EXPLICIT_NAME, "TESTCONF") == $CFGERR_OK
38 or die("config_init failed");
39 my $tapelist = config_dir_relative("tapelist");
42 my ($filename, @lines) = @_;
43 open(my $fh, ">", $filename) or die("Could not make tapelist '$filename'");
44 for my $line (@lines) {
52 open(my $fh, "<", $filename) or die("Could not read tapelist '$filename'");
53 my @reread_lines = <$fh>;
59 "20071111010002 TESTCONF004 reuse\n",
60 "20071110010002 TESTCONF003 reuse\n",
61 "20071109010002 TESTCONF002 reuse #comment 2\n",
62 "20071108010001 TESTCONF001 no-reuse #comment 1\n",
64 mktapelist($tapelist, @lines);
66 $tl = Amanda::Tapelist::read_tapelist($tapelist);
67 $tl_ok = is_deeply($tl, [
68 { 'datestamp' => '20071111010002', 'label' => 'TESTCONF004',
69 'reuse' => 1, 'position' => 1, 'comment' => undef },
70 { 'datestamp' => '20071110010002', 'label' => 'TESTCONF003',
71 'reuse' => 1, 'position' => 2, 'comment' => undef },
72 { 'datestamp' => '20071109010002', 'label' => 'TESTCONF002',
73 'reuse' => 1, 'position' => 3, 'comment' => 'comment 2' },
74 { 'datestamp' => '20071108010001', 'label' => 'TESTCONF001',
75 'reuse' => '', 'position' => 4, 'comment' => 'comment 1' },
76 ], "A simple tapelist is parsed correctly");
79 skip "Tapelist is parsed incorrectly, so these tests are unlikely to work", 15,
82 # now try writing it out and check that the results are the same
83 $tl->write("$tapelist-new");
84 my @reread_lines = readtapelist("$tapelist-new");
85 is_deeply(\@reread_lines, \@lines, "Lines of freshly written tapelist match the original");
87 is_deeply($tl->lookup_tapelabel('TESTCONF002'),
88 { 'datestamp' => '20071109010002', 'label' => 'TESTCONF002',
89 'reuse' => 1, 'position' => 3, 'comment' => 'comment 2' },
90 "lookup_tapelabel works");
92 is_deeply($tl->lookup_tapelabel('TESTCONF009'), undef,
93 "lookup_tapelabel returns undef on an unknown label");
95 is_deeply($tl->lookup_tapepos(4),
96 { 'datestamp' => '20071108010001', 'label' => 'TESTCONF001',
97 'reuse' => '', 'position' => 4, 'comment' => 'comment 1' },
98 "lookup_tapepos works");
100 is_deeply($tl->lookup_tapepos(9), undef,
101 "lookup_tapepos returns undef on an unknown position");
103 is_deeply($tl->lookup_tapedate('20071110010002'),
104 { 'datestamp' => '20071110010002', 'label' => 'TESTCONF003',
105 'reuse' => 1, 'position' => 2, 'comment' => undef },
106 "lookup_tapedate works");
108 is_deeply($tl->lookup_tapedate('12345678'), undef,
109 "lookup_tapedate returns undef on an unknown datestamp");
112 $tl->add_tapelabel("20080112010203", "TESTCONF007", "seven");
113 is(scalar @$tl, 5, "add_tapelabel adds a new element to the tapelist");
115 is_deeply($tl->lookup_tapepos(5),
116 { 'datestamp' => '20080112010203', 'label' => 'TESTCONF007',
117 'reuse' => 1, 'position' => 5, 'comment' => 'seven' },
118 ".. lookup_tapepos finds it");
120 is_deeply($tl->lookup_tapelabel("TESTCONF007"),
121 { 'datestamp' => '20080112010203', 'label' => 'TESTCONF007',
122 'reuse' => 1, 'position' => 5, 'comment' => 'seven' },
123 ".. lookup_tapelabel finds it");
125 is_deeply($tl->lookup_tapedate("20080112010203"),
126 { 'datestamp' => '20080112010203', 'label' => 'TESTCONF007',
127 'reuse' => 1, 'position' => 5, 'comment' => 'seven' },
128 ".. lookup_tapedate finds it");
130 $tl->remove_tapelabel("TESTCONF002");
131 is(scalar @$tl, 4, "remove_tapelabel removes an element from the tapelist");
133 is_deeply($tl->lookup_tapepos(4), # used to be in position 5
134 { 'datestamp' => '20080112010203', 'label' => 'TESTCONF007',
135 'reuse' => 1, 'position' => 4, 'comment' => 'seven' },
136 ".. tape positions are adjusted correctly");
138 is_deeply($tl->lookup_tapelabel("TESTCONF002"), undef,
139 ".. lookup_tapelabel no longer finds it");
141 is_deeply($tl->lookup_tapedate("20071109010002"), undef,
142 ".. lookup_tapedate no longer finds it");
146 # try parsing various invalid lines
148 "2006123456 FOO reuse\n", # valid
149 "TESTCONF003 290385098 reuse\n", # invalid
150 "20071109010002 TESTCONF002 re-use\n", # invalid
151 "20071108010001 TESTCONF001\n", # invalid
152 "20071108010001 TESTCONF001 #comment\n", # invalid
153 "#comment\n", # invalid
155 mktapelist($tapelist, @lines);
157 $tl = Amanda::Tapelist::read_tapelist($tapelist);
159 { 'datestamp' => '2006123456', 'label' => 'FOO',
160 'reuse' => 1, 'position' => 1, 'comment' => undef },
161 ], "Invalid lines are ignored");
163 # make sure clear_tapelist is empty
164 $tl = Amanda::Tapelist::clear_tapelist();
165 is_deeply($tl, [ ], "clear_tapelist returns an empty tapelist");