1 # Copyright (c) 2009-2012 Zmanda, Inc. All Rights Reserved.
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful, but
9 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # You should have received a copy of the GNU General Public License along
14 # with this program; if not, write to the Free Software Foundation, Inc.,
15 # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # Contact information: Zmanda Inc, 465 S. Mathilda Ave., Suite 300
18 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
20 use Test::More tests => 16;
26 use lib "@amperldir@";
28 use Installcheck::Config;
32 use Amanda::Config qw( :init );
35 # put the debug messages somewhere
36 Amanda::Debug::dbopen("installcheck");
37 Installcheck::log_test_output();
39 my $holding1 = "$Installcheck::TMP/holding1";
40 my $holding2 = "$Installcheck::TMP/holding2";
41 my $holding3 = "$Installcheck::TMP/holding3";
43 # set up a demo holding disk
44 sub make_holding_file {
45 my ($hdir, $ts, $host, $disk, $nchunks) = @_;
47 my $dir = "$hdir/$ts";
50 my $safe_disk = $disk;
51 $safe_disk =~ tr{/}{_};
52 my $base_filename = "$dir/$host.$safe_disk";
54 for my $i (0 .. $nchunks-1) {
55 my $chunk_filename = $base_filename;
56 $chunk_filename .= ".$i" if ($i);
58 my $hdr = Amanda::Header->new();
59 $hdr->{'type'} = ($i == 0)? $Amanda::Header::F_DUMPFILE : $Amanda::Header::F_CONT_DUMPFILE;
60 $hdr->{'datestamp'} = $ts;
61 $hdr->{'dumplevel'} = 0;
62 $hdr->{'name'} = $host;
63 $hdr->{'disk'} = $disk;
64 $hdr->{'program'} = "INSTALLCHECK";
65 if ($i != $nchunks-1) {
66 $hdr->{'cont_filename'} = "$base_filename." . ($i+1);
69 open(my $fh, ">", $chunk_filename) or die("opening '$chunk_filename': $!");
70 print $fh $hdr->to_string(32768,32768);
71 print $fh "some data!\n";
81 for my $file (@files) {
82 make_holding_file(@$file);
86 my $testconf = Installcheck::Config->new();
87 $testconf->add_holdingdisk("holding1", [
88 "directory", '"' . $holding1 . '"',
91 $testconf->add_holdingdisk("holding2", [
92 "directory", '"' . $holding2 . '"',
95 # note: this holding disk is not active -- just a definition
96 $testconf->add_holdingdisk_def("holding3", [
97 "directory", '"' . $holding3 . '"',
100 $testconf->add_dle("videoserver /video/a no-compress");
102 $testconf->add_dle("audio /usr no-compress");
103 $testconf->add_dle("audio /var no-compress");
106 my $cfg_result = config_init($CONFIG_INIT_EXPLICIT_NAME, 'TESTCONF');
107 $cfg_result = Amanda::Disklist::read_disklist();
108 if ($cfg_result != $CFGERR_OK) {
109 my ($level, @errors) = Amanda::Config::config_errors();
110 die(join "\n", @errors);
116 [ $holding1, '20070303000000', 'videoserver', '/video/a', 2 ],
117 [ $holding1, '20070306123456', 'videoserver', '/video/a', 1 ],
118 [ $holding1, '20070306123456', 'videoserver', '/video/b', 2 ],
119 [ $holding2, '20070306123456', 'audio', '/var', 3 ],
120 [ $holding2, '20070306123456', 'audio', '/usr', 1 ],
121 [ $holding3, '20070303000000', 'olfactory', '/perfumes', 1 ],
122 [ $holding3, '20070306123456', 'olfactory', '/stinky', 1 ],
125 is_deeply([ sort(+Amanda::Holding::disks()) ],
126 [ sort($holding1, $holding2) ],
127 "all active holding disks, but not inactive (defined but not used) disks");
129 is_deeply([ sort(+Amanda::Holding::files()) ],
131 "$holding1/20070303000000/videoserver._video_a",
132 "$holding1/20070306123456/videoserver._video_a",
133 "$holding1/20070306123456/videoserver._video_b",
134 "$holding2/20070306123456/audio._usr",
135 "$holding2/20070306123456/audio._var",
139 is_deeply([ sort(+Amanda::Holding::file_chunks("$holding2/20070306123456/audio._var")) ],
141 "$holding2/20070306123456/audio._var",
142 "$holding2/20070306123456/audio._var.1",
143 "$holding2/20070306123456/audio._var.2",
145 "chunks for a chunked file");
147 is(Amanda::Holding::file_size("$holding2/20070306123456/audio._usr", 1), 1,
148 "size of a single-chunk file, without headers");
149 is(Amanda::Holding::file_size("$holding2/20070306123456/audio._usr", 0), 32+1,
150 "size of a single-chunk file, with headers");
151 is(Amanda::Holding::file_size("$holding2/20070306123456/audio._var", 1), 3,
152 "size of a chunked file, without headers");
153 is(Amanda::Holding::file_size("$holding2/20070306123456/audio._var", 0), 32*3+1*3,
154 "size of a chunked file, with headers");
156 my $hdr = Amanda::Holding::get_header("$holding2/20070306123456/audio._usr");
157 is_deeply([ $hdr->{'name'}, $hdr->{'disk'} ],
159 "get_header gives a reasonable header");
161 is_deeply([ Amanda::Holding::get_all_datestamps() ],
162 [ sort("20070303000000", "20070306123456") ],
163 "get_all_datestamps");
165 is_deeply([ sort(+Amanda::Holding::get_files_for_flush("023985")) ],
167 "get_files_for_flush with no matching datestamps returns no files");
168 is_deeply([ Amanda::Holding::get_files_for_flush("20070306123456") ],
170 "$holding2/20070306123456/audio._usr",
171 "$holding2/20070306123456/audio._var",
172 "$holding1/20070306123456/videoserver._video_a",
174 "get_files_for_flush gets only files listed in disklist (no _video_b)");
175 is_deeply([ Amanda::Holding::get_files_for_flush() ],
177 "$holding1/20070303000000/videoserver._video_a",
178 "$holding2/20070306123456/audio._usr",
179 "$holding2/20070306123456/audio._var",
180 "$holding1/20070306123456/videoserver._video_a",
182 "get_files_for_flush with no datestamps returns all files");
184 ok(Amanda::Holding::file_unlink("$holding2/20070306123456/audio._var"),
185 "unlink a holding file");
186 ok(!-f "$holding2/20070306123456/audio._var", "..first chunk gone");
187 ok(!-f "$holding2/20070306123456/audio._var.1", "..second chunk gone");
188 ok(!-f "$holding2/20070306123456/audio._var.2", "..third chunk gone");