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