Imported Upstream version 3.3.3
[debian/amanda] / installcheck / Amanda_Holding.pl
1 # Copyright (c) 2009-2012 Zmanda, Inc.  All Rights Reserved.
2 #
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.
7 #
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
11 # for more details.
12 #
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
16 #
17 # Contact information: Zmanda Inc, 465 S. Mathilda Ave., Suite 300
18 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
19
20 use Test::More tests => 16;
21 use strict;
22 use warnings;
23 use File::Path;
24 use Data::Dumper;
25
26 use lib "@amperldir@";
27 use Installcheck;
28 use Installcheck::Config;
29 use Amanda::Holding;
30 use Amanda::Header;
31 use Amanda::Debug;
32 use Amanda::Config qw( :init );
33 use Amanda::Disklist;
34
35 # put the debug messages somewhere
36 Amanda::Debug::dbopen("installcheck");
37 Installcheck::log_test_output();
38
39 my $holding1 = "$Installcheck::TMP/holding1";
40 my $holding2 = "$Installcheck::TMP/holding2";
41 my $holding3 = "$Installcheck::TMP/holding3";
42
43 # set up a demo holding disk
44 sub make_holding_file {
45     my ($hdir, $ts, $host, $disk, $nchunks) = @_;
46
47     my $dir = "$hdir/$ts";
48     mkpath($dir);
49
50     my $safe_disk = $disk;
51     $safe_disk =~ tr{/}{_};
52     my $base_filename = "$dir/$host.$safe_disk";
53
54     for my $i (0 .. $nchunks-1) {
55         my $chunk_filename = $base_filename;
56         $chunk_filename .= ".$i" if ($i);
57
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);
67         }
68
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";
72         close($fh);
73     }
74 }
75
76 sub make_holding {
77     my @files = @_;
78
79     rmtree($holding1);
80     rmtree($holding2);
81     for my $file (@files) {
82         make_holding_file(@$file);
83     }
84 }
85
86 my $testconf = Installcheck::Config->new();
87 $testconf->add_holdingdisk("holding1", [
88     "directory", '"' . $holding1 . '"',
89     "use", "10m",
90 ]);
91 $testconf->add_holdingdisk("holding2", [
92     "directory", '"' . $holding2 . '"',
93     "use", "10m",
94 ]);
95 # note: this holding disk is not active -- just a definition
96 $testconf->add_holdingdisk_def("holding3", [
97     "directory", '"' . $holding3 . '"',
98     "use", "10m",
99 ]);
100 $testconf->add_dle("videoserver /video/a no-compress");
101 # note no /video/b
102 $testconf->add_dle("audio /usr no-compress");
103 $testconf->add_dle("audio /var no-compress");
104 $testconf->write();
105
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);
111 }
112
113 # Let's get started!
114
115 make_holding(
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 ],
123 );
124
125 is_deeply([ sort(+Amanda::Holding::disks()) ],
126     [ sort($holding1, $holding2) ],
127     "all active holding disks, but not inactive (defined but not used) disks");
128
129 is_deeply([ sort(+Amanda::Holding::files()) ],
130     [ sort(
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",
136     ) ],
137     "all files");
138
139 is_deeply([ sort(+Amanda::Holding::file_chunks("$holding2/20070306123456/audio._var")) ],
140     [ sort(
141         "$holding2/20070306123456/audio._var",
142         "$holding2/20070306123456/audio._var.1",
143         "$holding2/20070306123456/audio._var.2",
144     ) ],
145     "chunks for a chunked file");
146
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");
155
156 my $hdr = Amanda::Holding::get_header("$holding2/20070306123456/audio._usr");
157 is_deeply([ $hdr->{'name'}, $hdr->{'disk'} ],
158           [ 'audio', '/usr' ],
159           "get_header gives a reasonable header");
160
161 is_deeply([ Amanda::Holding::get_all_datestamps() ],
162           [ sort("20070303000000", "20070306123456") ],
163           "get_all_datestamps");
164
165 is_deeply([ sort(+Amanda::Holding::get_files_for_flush("023985")) ],
166           [ sort() ],
167           "get_files_for_flush with no matching datestamps returns no files");
168 is_deeply([ Amanda::Holding::get_files_for_flush("20070306123456") ],
169           [ sort(
170                 "$holding2/20070306123456/audio._usr",
171                 "$holding2/20070306123456/audio._var",
172                 "$holding1/20070306123456/videoserver._video_a",
173           )],
174           "get_files_for_flush gets only files listed in disklist (no _video_b)");
175 is_deeply([ Amanda::Holding::get_files_for_flush() ],
176           [ sort(
177                 "$holding1/20070303000000/videoserver._video_a",
178                 "$holding2/20070306123456/audio._usr",
179                 "$holding2/20070306123456/audio._var",
180                 "$holding1/20070306123456/videoserver._video_a",
181           )],
182           "get_files_for_flush with no datestamps returns all files");
183
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");
189
190 rmtree($holding1);
191 rmtree($holding2);
192 rmtree($holding3);