1 # Copyright (c) 2010-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 => 60;
25 use lib '@amperldir@';
26 use Installcheck::Run;
27 use Installcheck::Mock;
32 use POSIX ":sys_wait_h";
37 use Amanda::Header qw( :constants );
42 # put the debug messages somewhere
43 Amanda::Debug::dbopen("installcheck");
44 Installcheck::log_test_output();
46 my $test_hdir = "$Installcheck::TMP/chunker-holding";
47 my $test_hfile = "$test_hdir/holder";
48 my $chunker_stderr_file = "$Installcheck::TMP/chunker-stderr";
49 my $debug = !exists $ENV{'HARNESS_ACTIVE'};
51 # information on the current run
52 my ($datestamp, $handle);
53 my ($chunker_pid, $chunker_in, $chunker_out, $last_chunker_reply, $chunker_reply_timeout);
57 my ($description, %params) = @_;
61 diag("******** $description") if $debug;
63 my $testconf = Installcheck::Run::setup();
64 $testconf->add_param('debug_chunker', 9);
67 if (exists $params{'ENOSPC_at'}) {
68 diag("setting CHUNKER_FAKE_ENOSPC_AT=$params{ENOSPC_at}") if $debug;
69 $ENV{'CHUNKER_FAKE_ENOSPC_AT'} = $params{'ENOSPC_at'};
71 delete $ENV{'CHUNKER_FAKE_ENOSPC'};
74 open(CHUNKER_ERR, ">", $chunker_stderr_file);
75 $chunker_in = $chunker_out = '';
76 $chunker_pid = open3($chunker_in, $chunker_out, ">&CHUNKER_ERR",
77 "$amlibexecdir/chunker", "TESTCONF");
79 $chunker_in->blocking(1);
80 $chunker_out->autoflush();
82 pass("spawned new chunker for 'test $description'");
84 # define this to get the installcheck to wait and allow you to attach
85 # a gdb instance to the chunker
86 if ($params{'use_gdb'}) {
87 $chunker_reply_timeout = 0; # no timeouts while debugging
88 diag("attach debugger to pid $chunker_pid and press ENTER");
91 $chunker_reply_timeout = 120;
94 chunker_cmd("START $datestamp");
99 waitpid($chunker_pid, 0);
100 $chunker_pid = undef;
104 sub cleanup_chunker {
105 -d $test_hdir and rmtree($test_hdir);
108 # make a small effort to collect zombies
110 if (waitpid($chunker_pid, WNOHANG) == $chunker_pid) {
111 $chunker_pid = undef;
115 if (waitpid($writer_pid, WNOHANG) == $writer_pid) {
121 sub wait_for_writer {
123 if (waitpid($writer_pid, 0) == $writer_pid) {
132 diag(">>> $cmd") if $debug;
133 print $chunker_in "$cmd\n";
137 local $SIG{ALRM} = sub { die "Timeout while waiting for reply\n" };
138 alarm($chunker_reply_timeout);
139 $last_chunker_reply = $chunker_out->getline();
142 if (!$last_chunker_reply) {
143 die("wrong pid") unless ($chunker_pid == waitpid($chunker_pid, 0));
144 my $exit_status = $?;
146 open(my $fh, "<", $chunker_stderr_file) or die("open $chunker_stderr_file: $!");
147 my $stderr = do { local $/; <$fh> };
150 diag("chunker stderr:\n$stderr") if $stderr;
151 die("chunker (pid $chunker_pid) died unexpectedly with status $exit_status");
154 # trim trailing whitespace -- C chunker outputs an extra ' ' after
155 # single-word replies
156 $last_chunker_reply =~ s/\s*$//;
157 diag("<<< $last_chunker_reply") if $debug;
159 return $last_chunker_reply;
163 my ($expected, $msg) = @_;
167 # must contain a pid line at the beginning and end
168 unshift @$expected, qr/^INFO chunker chunker pid \d+$/;
169 push @$expected, qr/^INFO chunker pid-done \d+$/;
171 open(my $logfile, "<", "$CONFIG_DIR/TESTCONF/log/log")
172 or die("opening log: $!");
173 my @logfile = grep(/^\S+ chunker /, <$logfile>);
176 while (@logfile and @$expected) {
177 my $logline = shift @logfile;
178 my $expline = shift @$expected;
180 if ($logline !~ $expline) {
181 like($logline, $expline, $msg);
186 fail("$msg (extra trailing log lines)");
190 fail("$msg (logfile ends early)");
191 diag("first missing line should match ");
192 diag("".$expected->[0]);
199 sub check_holding_chunks {
200 my ($filename, $chunks, $host, $disk, $datestamp, $level) = @_;
202 my $msg = ".tmp holding chunk files";
203 my $exp_nchunks = @$chunks;
208 my $filename_tmp = "$filename.tmp";
209 if (!-f $filename_tmp) {
211 diag("file $filename_tmp doesn't exist");
212 diag(`ls -1l $test_hdir`);
217 open($fh, "<", $filename_tmp) or die("opening $filename_tmp: $!");
218 my $hdr_str = Amanda::Util::full_read(fileno($fh), Amanda::Holding::DISK_BLOCK_BYTES);
221 my $hdr = Amanda::Header->from_string($hdr_str);
222 my $exp_type = ($nchunks == 1)? $F_DUMPFILE : $F_CONT_DUMPFILE;
223 if ($hdr->{'type'} != $exp_type) {
224 my ($exp, $got) = (Amanda::Header::filetype_t_to_string($exp_type),
225 Amanda::Header::filetype_t_to_string($hdr->{'type'}));
227 diag("file $filename_tmp has header type $got; expected $exp");
232 $ok &&= $hdr->{'name'} eq $host;
233 $ok &&= $hdr->{'disk'} eq $disk;
234 $ok &&= $hdr->{'datestamp'} eq $datestamp;
235 $ok &&= $hdr->{'dumplevel'} eq $level;
238 diag("file $filename_tmp header has unexpected values:\n" . $hdr->summary());
242 my $data_size = (stat($filename_tmp))[7] - Amanda::Holding::DISK_BLOCK_BYTES;
243 my $exp_size = (shift @$chunks) * 1024;
244 if (defined $exp_size and $exp_size != $data_size) {
246 diag("file $filename_tmp: expected $exp_size bytes, got $data_size");
248 } # note: if @$exp_chunks is empty, the final is() will catch it
250 my $last_filename = $filename;
251 $filename = $hdr->{'cont_filename'};
252 die("header loop!") if $last_filename eq $filename;
255 return is($nchunks, $exp_nchunks, $msg);
259 my $logfile = "$CONFIG_DIR/TESTCONF/log/log";
260 -f $logfile and unlink($logfile);
263 # functions to create dumpfiles
265 sub write_dumpfile_header_to {
266 my ($fh, $size, $hostname, $disk, $expect_failure) = @_;
268 my $hdr = Amanda::Header->new();
269 $hdr->{'type'} = $Amanda::Header::F_DUMPFILE;
270 $hdr->{'datestamp'} = $datestamp;
271 $hdr->{'dumplevel'} = 0;
272 $hdr->{'compressed'} = 0;
273 $hdr->{'comp_suffix'} = ".foo";
274 $hdr->{'name'} = $hostname;
275 $hdr->{'disk'} = $disk;
276 $hdr->{'program'} = "INSTALLCHECK";
277 $hdr = $hdr->to_string(Amanda::Holding::DISK_BLOCK_BYTES,
278 Amanda::Holding::DISK_BLOCK_BYTES);
283 sub write_dumpfile_data_to {
284 my ($fh, $size, $hostname, $disk, $expect_failure) = @_;
286 my $bytes_to_write = $size;
287 my $bufbase = substr((('='x127)."\n".('-'x127)."\n") x 4, 8, -3) . "1K\n";
288 die length($bufbase) unless length($bufbase) == 1024-8;
290 while ($bytes_to_write > 0) {
291 my $buf = sprintf("%08x", $k++).$bufbase;
292 my $written = $fh->syswrite($buf, $bytes_to_write);
293 if (!defined($written)) {
294 die "writing: $!" unless $expect_failure;
297 $bytes_to_write -= $written;
301 # connect to the given port and write a dumpfile; this *will* create
302 # zombies, but it's OK -- installchecks aren't daemons.
304 my ($port_cmd, $size, $hostname, $disk, $expect_error) = @_;
306 my ($header_port, $data_addr) =
307 ($last_chunker_reply =~ /^PORT (\d+) "?(\d+\.\d+\.\d+\.\d+:\d+)/);
309 # just run this in the child
310 $writer_pid = fork();
311 return unless $writer_pid == 0;
313 my $sock = IO::Socket::INET->new(
314 PeerAddr => "127.0.0.1:$header_port",
319 write_dumpfile_header_to($sock, $size, $hostname, $disk, $expect_error);
322 $sock = IO::Socket::INET->new(
323 PeerAddr => $data_addr,
328 write_dumpfile_data_to($sock, $size, $hostname, $disk, $expect_error);
335 # A simple, two-chunk PORT-WRITE
337 $handle = "11-11111";
338 $datestamp = "20070102030405";
339 run_chunker("simple");
340 # note that features (ffff here) and options (ops) are ignored by the chunker
341 chunker_cmd("PORT-WRITE $handle \"$test_hfile\" ghost ffff /boot 0 $datestamp 512 INSTALLCHECK 10240 ops");
342 like(chunker_reply, qr/^PORT (\d+) "?(\d+\.\d+\.\d+\.\d+:\d+;?)+"?$/,
343 "got PORT with data address");
344 write_to_port($last_chunker_reply, 700*1024, "ghost", "/boot", 0);
346 chunker_cmd("DONE $handle");
347 like(chunker_reply, qr/^DONE $handle 700 "\[sec [\d.]+ kb 700 kps [\d.]+\]"$/,
352 qr(^SUCCESS chunker ghost /boot $datestamp 0 \[sec [\d.]+ kb 700 kps [\d.]+\]$),
355 check_holding_chunks($test_hfile, [ 480, 220 ], "ghost", "/boot", $datestamp, 0);
358 # A two-chunk PORT-WRITE that the dumper flags as a failure, but chunker as PARTIAL
360 $handle = "22-11111";
361 $datestamp = "20080808080808";
362 run_chunker("partial");
363 chunker_cmd("PORT-WRITE $handle \"$test_hfile\" ghost ffff /root 0 $datestamp 512 INSTALLCHECK 10240 ops");
364 like(chunker_reply, qr/^PORT (\d+) "?(\d+\.\d+\.\d+\.\d+:\d+;?)+"?$/,
365 "got PORT with data address");
366 write_to_port($last_chunker_reply, 768*1024, "ghost", "/root", 0);
368 chunker_cmd("FAILED $handle");
369 like(chunker_reply, qr/^PARTIAL $handle 768 "\[sec [\d.]+ kb 768 kps [\d.]+\]"$/,
370 "got PARTIAL") or die;
374 qr(^PARTIAL chunker ghost /root $datestamp 0 \[sec [\d.]+ kb 768 kps [\d.]+\]$),
377 check_holding_chunks($test_hfile, [ 480, 288 ], "ghost", "/root", $datestamp, 0);
380 # A two-chunk PORT-WRITE that the dumper flags as a failure and chunker
381 # does too, since no appreciatble bytes were transferred
383 $handle = "33-11111";
384 $datestamp = "20070202020202";
385 run_chunker("failed");
386 chunker_cmd("PORT-WRITE $handle \"$test_hfile\" ghost ffff /usr 0 $datestamp 512 INSTALLCHECK 10240 ops");
387 like(chunker_reply, qr/^PORT (\d+) "?(\d+\.\d+\.\d+\.\d+:\d+;?)+"?$/,
388 "got PORT with data address");
389 write_to_port($last_chunker_reply, 0, "ghost", "/usr", 0);
391 chunker_cmd("FAILED $handle");
392 like(chunker_reply, qr/^FAILED $handle "\[dumper returned FAILED\]"$/,
393 "got FAILED") or die;
397 qr(^FAIL chunker ghost /usr $datestamp 0 \[dumper returned FAILED\]$),
400 check_holding_chunks($test_hfile, [ 0 ], "ghost", "/usr", $datestamp, 0);
405 # A PORT-WRITE with a USE value smaller than the dump size, but an overly large
408 $handle = "44-11111";
409 $datestamp = "20040404040404";
410 run_chunker("more-than-use");
411 chunker_cmd("PORT-WRITE $handle \"$test_hfile\" ghost ffff /var 0 $datestamp 10240 INSTALLCHECK 512 ops");
412 like(chunker_reply, qr/^PORT (\d+) "?(\d+\.\d+\.\d+\.\d+:\d+;?)+"?$/,
413 "got PORT with data address");
414 write_to_port($last_chunker_reply, 700*1024, "ghost", "/var", 1);
415 like(chunker_reply, qr/^RQ-MORE-DISK $handle$/,
416 "got RQ-MORE-DISK") or die;
417 chunker_cmd("CONTINUE $handle $test_hfile-u2 10240 512");
419 chunker_cmd("DONE $handle");
420 like(chunker_reply, qr/^DONE $handle 700 "\[sec [\d.]+ kb 700 kps [\d.]+\]"$/,
425 qr(^SUCCESS chunker ghost /var $datestamp 0 \[sec [\d.]+ kb 700 kps [\d.]+\]$),
428 check_holding_chunks($test_hfile, [ 480, 220 ], "ghost", "/var", $datestamp, 0);
431 # A PORT-WRITE with a USE value smaller than the dump size, and an even smaller
432 # chunksize, with a different chunksize on the second holding disk
434 $handle = "55-11111";
435 $datestamp = "20050505050505";
436 run_chunker("more-than-use-and-chunks");
437 chunker_cmd("PORT-WRITE $handle \"$test_hfile\" ghost ffff /var 0 $datestamp 96 INSTALLCHECK 160 ops");
438 like(chunker_reply, qr/^PORT (\d+) "?(\d+\.\d+\.\d+\.\d+:\d+;?)+"?$/,
439 "got PORT with data address");
440 write_to_port($last_chunker_reply, 400*1024, "ghost", "/var", 1);
441 like(chunker_reply, qr/^RQ-MORE-DISK $handle$/,
442 "got RQ-MORE-DISK") or die;
443 chunker_cmd("CONTINUE $handle $test_hfile-u2 128 10240");
445 chunker_cmd("DONE $handle");
446 like(chunker_reply, qr/^DONE $handle 400 "\[sec [\d.]+ kb 400 kps [\d.]+\]"$/,
451 qr(^SUCCESS chunker ghost /var $datestamp 0 \[sec [\d.]+ kb 400 kps [\d.]+\]$),
454 check_holding_chunks($test_hfile, [ 64, 32, 96, 96, 96, 16 ],
455 "ghost", "/var", $datestamp, 0);
460 # A PORT-WRITE with a USE value smaller than the dump size, but with the CONTINUE
461 # giving the same filename, so that the dump continues in the same file
463 $handle = "55-22222";
464 $datestamp = "20050505050505";
465 run_chunker("use, continue on same file");
466 chunker_cmd("PORT-WRITE $handle \"$test_hfile\" ghost ffff /var/lib 0 $datestamp 10240 INSTALLCHECK 64 ops");
467 like(chunker_reply, qr/^PORT (\d+) "?(\d+\.\d+\.\d+\.\d+:\d+;?)+"?$/,
468 "got PORT with data address");
469 write_to_port($last_chunker_reply, 70*1024, "ghost", "/var/lib", 1);
470 like(chunker_reply, qr/^RQ-MORE-DISK $handle$/,
471 "got RQ-MORE-DISK") or die;
472 chunker_cmd("CONTINUE $handle $test_hfile 10240 10240");
474 chunker_cmd("DONE $handle");
475 like(chunker_reply, qr/^DONE $handle 70 "\[sec [\d.]+ kb 70 kps [\d.]+\]"$/,
480 qr(^SUCCESS chunker ghost /var/lib $datestamp 0 \[sec [\d.]+ kb 70 kps [\d.]+\]$),
483 check_holding_chunks($test_hfile, [ 70 ],
484 "ghost", "/var/lib", $datestamp, 0);
489 # A PORT-WRITE with a USE value that will trigger in the midst of a header
490 # on the second chunk
492 $handle = "66-11111";
493 $datestamp = "20060606060606";
494 run_chunker("out-of-use-during-header");
495 chunker_cmd("PORT-WRITE $handle \"$test_hfile\" ghost ffff /u01 0 $datestamp 96 INSTALLCHECK 120 ops");
496 like(chunker_reply, qr/^PORT (\d+) "?(\d+\.\d+\.\d+\.\d+:\d+;?)+"?$/,
497 "got PORT with data address");
498 write_to_port($last_chunker_reply, 400*1024, "ghost", "/u01", 1);
499 like(chunker_reply, qr/^RQ-MORE-DISK $handle$/,
500 "got RQ-MORE-DISK") or die;
501 chunker_cmd("CONTINUE $handle $test_hfile-u2 128 10240");
503 chunker_cmd("DONE $handle");
504 like(chunker_reply, qr/^DONE $handle 400 "\[sec [\d.]+ kb 400 kps [\d.]+\]"$/,
509 qr(^SUCCESS chunker ghost /u01 $datestamp 0 \[sec [\d.]+ kb 400 kps [\d.]+\]$),
510 ], "logs for more-than-use-and-chunks PORT-WRITE");
512 check_holding_chunks($test_hfile, [ 64, 96, 96, 96, 48 ],
513 "ghost", "/u01", $datestamp, 0);
516 # A two-disk PORT-WRITE, but with the DONE sent before the first byte of data
517 # arrives, to test the ability of the chunker to defer the DONE until it gets
520 $handle = "77-11111";
521 $datestamp = "20070707070707";
522 run_chunker("early-DONE");
523 chunker_cmd("PORT-WRITE $handle \"$test_hfile\" roast ffff /boot 0 $datestamp 10240 INSTALLCHECK 128 ops");
524 like(chunker_reply, qr/^PORT (\d+) "?(\d+\.\d+\.\d+\.\d+:\d+;?)+"?$/,
525 "got PORT with data address");
526 chunker_cmd("DONE $handle");
527 write_to_port($last_chunker_reply, 180*1024, "roast", "/boot", 0);
528 like(chunker_reply, qr/^RQ-MORE-DISK $handle$/,
529 "got RQ-MORE-DISK") or die;
530 chunker_cmd("CONTINUE $handle $test_hfile-u2 10240 10240");
532 like(chunker_reply, qr/^DONE $handle 180 "\[sec [\d.]+ kb 180 kps [\d.]+\]"$/,
537 qr(^SUCCESS chunker roast /boot $datestamp 0 \[sec [\d.]+ kb 180 kps [\d.]+\]$),
538 ], "logs for simple PORT-WRITE");
540 check_holding_chunks($test_hfile, [ 96, 84 ], "roast", "/boot", $datestamp, 0);
543 # A two-disk PORT-WRITE, where the first disk runs out of space before it hits
546 $handle = "88-11111";
547 $datestamp = "20080808080808";
548 run_chunker("ENOSPC-1", ENOSPC_at => 90*1024);
549 chunker_cmd("PORT-WRITE $handle \"$test_hfile\" roast ffff /boot 0 $datestamp 10240 INSTALLCHECK 10240 ops");
550 like(chunker_reply, qr/^PORT (\d+) "?(\d+\.\d+\.\d+\.\d+:\d+;?)+"?$/,
551 "got PORT with data address");
552 write_to_port($last_chunker_reply, 100*1024, "roast", "/boot", 0);
553 like(chunker_reply, qr/^NO-ROOM $handle 10150$/, # == 10240-90
554 "got NO-ROOM") or die;
555 like(chunker_reply, qr/^RQ-MORE-DISK $handle$/,
556 "got RQ-MORE-DISK") or die;
557 chunker_cmd("CONTINUE $handle $test_hfile-u2 10240 10240");
559 chunker_cmd("DONE $handle");
560 like(chunker_reply, qr/^DONE $handle 100 "\[sec [\d.]+ kb 100 kps [\d.]+\]"$/,
565 qr(^SUCCESS chunker roast /boot $datestamp 0 \[sec [\d.]+ kb 100 kps [\d.]+\]$),
566 ], "logs for simple PORT-WRITE");
568 check_holding_chunks($test_hfile, [ 58, 42 ], "roast", "/boot", $datestamp, 0);
571 # A two-chunk PORT-WRITE, where the second chunk gets ENOSPC in the header. This
572 # also checks the behavior of rounding down the use value to the nearest multiple
573 # of 32k (with am_floor)
575 $handle = "88-22222";
576 $datestamp = "20080808080808";
577 run_chunker("ENOSPC-2", ENOSPC_at => 130*1024);
578 chunker_cmd("PORT-WRITE $handle \"$test_hfile\" roast ffff /boot 0 $datestamp 128 INSTALLCHECK 1000 ops");
579 like(chunker_reply, qr/^PORT (\d+) "?(\d+\.\d+\.\d+\.\d+:\d+;?)+"?$/,
580 "got PORT with data address");
581 write_to_port($last_chunker_reply, 128*1024, "roast", "/boot", 0);
582 like(chunker_reply, qr/^NO-ROOM $handle 864$/, # == am_floor(1000)-128
583 "got NO-ROOM") or die;
584 like(chunker_reply, qr/^RQ-MORE-DISK $handle$/,
585 "got RQ-MORE-DISK") or die;
586 chunker_cmd("CONTINUE $handle $test_hfile-u2 300 128");
588 chunker_cmd("DONE $handle");
589 like(chunker_reply, qr/^DONE $handle 128 "\[sec [\d.]+ kb 128 kps [\d.]+\]"$/,
594 qr(^SUCCESS chunker roast /boot $datestamp 0 \[sec [\d.]+ kb 128 kps [\d.]+\]$),
595 ], "logs for simple PORT-WRITE");
597 check_holding_chunks($test_hfile, [ 96, 32 ], "roast", "/boot", $datestamp, 0);
598 ok(!-f "$test_hfile.1.tmp",
599 "half-written header is deleted");