1 # Copyright (c) 2009 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 => 16;
25 use lib "@amperldir@";
27 use Installcheck::Config;
31 use Amanda::Config qw( :init );
34 # put the debug messages somewhere
35 Amanda::Debug::dbopen("installcheck");
36 Installcheck::log_test_output();
38 my $holding1 = "$Installcheck::TMP/holding1";
39 my $holding2 = "$Installcheck::TMP/holding2";
40 my $holding3 = "$Installcheck::TMP/holding3";
42 # set up a demo holding disk
43 sub make_holding_file {
44 my ($hdir, $ts, $host, $disk, $nchunks) = @_;
46 my $dir = "$hdir/$ts";
49 my $safe_disk = $disk;
50 $safe_disk =~ tr{/}{_};
51 my $base_filename = "$dir/$host.$safe_disk";
53 for my $i (0 .. $nchunks-1) {
54 my $chunk_filename = $base_filename;
55 $chunk_filename .= ".$i" if ($i);
57 my $hdr = Amanda::Header->new();
58 $hdr->{'type'} = ($i == 0)? $Amanda::Header::F_DUMPFILE : $Amanda::Header::F_CONT_DUMPFILE;
59 $hdr->{'datestamp'} = $ts;
60 $hdr->{'dumplevel'} = 0;
61 $hdr->{'name'} = $host;
62 $hdr->{'disk'} = $disk;
63 $hdr->{'program'} = "INSTALLCHECK";
64 if ($i != $nchunks-1) {
65 $hdr->{'cont_filename'} = "$base_filename." . ($i+1);
68 open(my $fh, ">", $chunk_filename) or die("opening '$chunk_filename': $!");
69 print $fh $hdr->to_string(32768,32768);
70 print $fh "some data!\n";
80 for my $file (@files) {
81 make_holding_file(@$file);
85 my $testconf = Installcheck::Config->new();
86 $testconf->add_holdingdisk("holding1", [
87 "directory", '"' . $holding1 . '"',
90 $testconf->add_holdingdisk("holding2", [
91 "directory", '"' . $holding2 . '"',
94 # note: this holding disk is not active -- just a definition
95 $testconf->add_holdingdisk_def("holding3", [
96 "directory", '"' . $holding3 . '"',
99 $testconf->add_dle("videoserver /video/a no-compress");
101 $testconf->add_dle("audio /usr no-compress");
102 $testconf->add_dle("audio /var no-compress");
105 my $cfg_result = config_init($CONFIG_INIT_EXPLICIT_NAME, 'TESTCONF');
106 $cfg_result = Amanda::Disklist::read_disklist();
107 if ($cfg_result != $CFGERR_OK) {
108 my ($level, @errors) = Amanda::Config::config_errors();
109 die(join "\n", @errors);
115 [ $holding1, '20070303000000', 'videoserver', '/video/a', 2 ],
116 [ $holding1, '20070306123456', 'videoserver', '/video/a', 1 ],
117 [ $holding1, '20070306123456', 'videoserver', '/video/b', 2 ],
118 [ $holding2, '20070306123456', 'audio', '/var', 3 ],
119 [ $holding2, '20070306123456', 'audio', '/usr', 1 ],
120 [ $holding3, '20070303000000', 'olfactory', '/perfumes', 1 ],
121 [ $holding3, '20070306123456', 'olfactory', '/stinky', 1 ],
124 is_deeply([ sort(+Amanda::Holding::disks()) ],
125 [ sort($holding1, $holding2) ],
126 "all active holding disks, but not inactive (defined but not used) disks");
128 is_deeply([ sort(+Amanda::Holding::files()) ],
130 "$holding1/20070303000000/videoserver._video_a",
131 "$holding1/20070306123456/videoserver._video_a",
132 "$holding1/20070306123456/videoserver._video_b",
133 "$holding2/20070306123456/audio._usr",
134 "$holding2/20070306123456/audio._var",
138 is_deeply([ sort(+Amanda::Holding::file_chunks("$holding2/20070306123456/audio._var")) ],
140 "$holding2/20070306123456/audio._var",
141 "$holding2/20070306123456/audio._var.1",
142 "$holding2/20070306123456/audio._var.2",
144 "chunks for a chunked file");
146 is(Amanda::Holding::file_size("$holding2/20070306123456/audio._usr", 1), 1,
147 "size of a single-chunk file, without headers");
148 is(Amanda::Holding::file_size("$holding2/20070306123456/audio._usr", 0), 32+1,
149 "size of a single-chunk file, with headers");
150 is(Amanda::Holding::file_size("$holding2/20070306123456/audio._var", 1), 3,
151 "size of a chunked file, without headers");
152 is(Amanda::Holding::file_size("$holding2/20070306123456/audio._var", 0), 32*3+1*3,
153 "size of a chunked file, with headers");
155 my $hdr = Amanda::Holding::get_header("$holding2/20070306123456/audio._usr");
156 is_deeply([ $hdr->{'name'}, $hdr->{'disk'} ],
158 "get_header gives a reasonable header");
160 is_deeply([ Amanda::Holding::get_all_datestamps() ],
161 [ sort("20070303000000", "20070306123456") ],
162 "get_all_datestamps");
164 is_deeply([ sort(+Amanda::Holding::get_files_for_flush("023985")) ],
166 "get_files_for_flush with no matching datestamps returns no files");
167 is_deeply([ Amanda::Holding::get_files_for_flush("20070306123456") ],
169 "$holding2/20070306123456/audio._usr",
170 "$holding2/20070306123456/audio._var",
171 "$holding1/20070306123456/videoserver._video_a",
173 "get_files_for_flush gets only files listed in disklist (no _video_b)");
174 is_deeply([ Amanda::Holding::get_files_for_flush() ],
176 "$holding1/20070303000000/videoserver._video_a",
177 "$holding2/20070306123456/audio._usr",
178 "$holding2/20070306123456/audio._var",
179 "$holding1/20070306123456/videoserver._video_a",
181 "get_files_for_flush with no datestamps returns all files");
183 ok(Amanda::Holding::file_unlink("$holding2/20070306123456/audio._var"),
184 "unlink a holding file");
185 ok(!-f "$holding2/20070306123456/audio._var", "..first chunk gone");
186 ok(!-f "$holding2/20070306123456/audio._var.1", "..second chunk gone");
187 ok(!-f "$holding2/20070306123456/audio._var.2", "..third chunk gone");