2005-12-05 08:35:07 -05:00
|
|
|
#!/usr/bin/perl -w
|
|
|
|
|
|
|
|
package Test;
|
|
|
|
$VERSION = 0.01;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
|
2006-01-24 07:15:58 -05:00
|
|
|
use Cwd;
|
|
|
|
use File::Path;
|
|
|
|
|
|
|
|
my @unexpected_downloads = ();
|
2005-12-05 08:35:07 -05:00
|
|
|
|
|
|
|
{
|
|
|
|
my %_attr_data = ( # DEFAULT
|
2006-01-24 07:15:58 -05:00
|
|
|
_cmdline => "",
|
2006-04-27 09:27:17 -04:00
|
|
|
_workdir => Cwd::getcwd(),
|
2006-01-24 07:15:58 -05:00
|
|
|
_errcode => 0,
|
2006-04-27 09:27:17 -04:00
|
|
|
_existing => {},
|
2006-01-24 07:15:58 -05:00
|
|
|
_input => {},
|
|
|
|
_name => "",
|
|
|
|
_output => {},
|
2005-12-05 08:35:07 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
sub _default_for
|
|
|
|
{
|
|
|
|
my ($self, $attr) = @_;
|
|
|
|
$_attr_data{$attr};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub _standard_keys
|
|
|
|
{
|
|
|
|
keys %_attr_data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-24 07:15:58 -05:00
|
|
|
|
2005-12-05 08:35:07 -05:00
|
|
|
sub new {
|
|
|
|
my ($caller, %args) = @_;
|
|
|
|
my $caller_is_obj = ref($caller);
|
|
|
|
my $class = $caller_is_obj || $caller;
|
2006-01-24 07:15:58 -05:00
|
|
|
#print STDERR "class = ", $class, "\n";
|
2006-04-27 09:27:17 -04:00
|
|
|
#print STDERR "_attr_data {workdir} = ", $Test::_attr_data{_workdir}, "\n";
|
2005-12-05 08:35:07 -05:00
|
|
|
my $self = bless {}, $class;
|
|
|
|
foreach my $attrname ($self->_standard_keys()) {
|
2006-01-24 07:15:58 -05:00
|
|
|
#print STDERR "attrname = ", $attrname, " value = ";
|
2005-12-05 08:35:07 -05:00
|
|
|
my ($argname) = ($attrname =~ /^_(.*)/);
|
|
|
|
if (exists $args{$argname}) {
|
|
|
|
#printf STDERR "Setting up $attrname\n";
|
|
|
|
$self->{$attrname} = $args{$argname};
|
|
|
|
} elsif ($caller_is_obj) {
|
|
|
|
#printf STDERR "Copying $attrname\n";
|
2006-01-24 07:15:58 -05:00
|
|
|
$self->{$attrname} = $caller->{$attrname};
|
2005-12-05 08:35:07 -05:00
|
|
|
} else {
|
|
|
|
#printf STDERR "Using default for $attrname\n";
|
2006-01-24 07:15:58 -05:00
|
|
|
$self->{$attrname} = $self->_default_for($attrname);
|
2005-12-05 08:35:07 -05:00
|
|
|
}
|
2006-01-24 07:15:58 -05:00
|
|
|
#print STDERR $attrname, '=', $self->{$attrname}, "\n";
|
2005-12-05 08:35:07 -05:00
|
|
|
}
|
2006-04-27 09:27:17 -04:00
|
|
|
#printf STDERR "_workdir default = ", $self->_default_for("_workdir");
|
2005-12-05 08:35:07 -05:00
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
2006-01-24 07:15:58 -05:00
|
|
|
|
|
|
|
sub run {
|
|
|
|
my $self = shift;
|
|
|
|
my $result_message = "Test successful.\n";
|
|
|
|
|
|
|
|
printf "Running test $self->{_name}\n";
|
|
|
|
|
|
|
|
# Setup
|
|
|
|
$self->_setup();
|
2006-04-27 09:27:17 -04:00
|
|
|
chdir ("$self->{_workdir}/$self->{_name}/input");
|
2006-01-24 07:15:58 -05:00
|
|
|
|
|
|
|
# Launch server
|
2006-05-29 05:15:06 -04:00
|
|
|
my $pid = $self->_fork_and_launch_server();
|
2006-01-24 07:15:58 -05:00
|
|
|
|
|
|
|
# Call wget
|
2006-04-27 09:27:17 -04:00
|
|
|
chdir ("$self->{_workdir}/$self->{_name}/output");
|
2006-01-24 07:15:58 -05:00
|
|
|
# print "Calling $self->{_cmdline}\n";
|
2006-05-26 08:55:34 -04:00
|
|
|
my $errcode =
|
|
|
|
($self->{_cmdline} =~ m{^/.*})
|
|
|
|
? system ($self->{_cmdline})
|
|
|
|
: system ("$self->{_workdir}/../src/$self->{_cmdline}");
|
2006-01-24 07:15:58 -05:00
|
|
|
|
|
|
|
# Shutdown server
|
2006-05-29 05:15:06 -04:00
|
|
|
# if we didn't explicitely kill the server, we would have to call
|
|
|
|
# waitpid ($pid, 0) here in order to wait for the child process to
|
|
|
|
# terminate
|
2006-01-24 07:15:58 -05:00
|
|
|
kill ('TERM', $pid);
|
|
|
|
|
|
|
|
# Verify download
|
|
|
|
unless ($errcode == $self->{_errcode}) {
|
|
|
|
$result_message = "Test failed: wrong code returned (was: $errcode, expected: $self->{_errcode})\n";
|
|
|
|
}
|
|
|
|
if (my $error_str = $self->_verify_download()) {
|
|
|
|
$result_message = $error_str;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Cleanup
|
|
|
|
$self->_cleanup();
|
|
|
|
|
|
|
|
print $result_message;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub _setup {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
#print $self->{_name}, "\n";
|
2006-04-27 09:27:17 -04:00
|
|
|
chdir ($self->{_workdir});
|
2006-01-24 07:15:58 -05:00
|
|
|
|
|
|
|
# Create temporary directory
|
|
|
|
mkdir ($self->{_name});
|
|
|
|
chdir ($self->{_name});
|
|
|
|
mkdir ("input");
|
|
|
|
mkdir ("output");
|
2006-04-27 09:27:17 -04:00
|
|
|
|
|
|
|
# Setup existing files
|
|
|
|
chdir ("output");
|
|
|
|
foreach my $filename (keys %{$self->{_existing}}) {
|
|
|
|
open (FILE, ">$filename")
|
|
|
|
or return "Test failed: cannot open pre-existing file $filename\n";
|
|
|
|
|
|
|
|
print FILE $self->{_existing}->{$filename}->{content}
|
|
|
|
or return "Test failed: cannot write pre-existing file $filename\n";
|
|
|
|
|
|
|
|
close (FILE);
|
|
|
|
}
|
|
|
|
|
2006-01-24 07:15:58 -05:00
|
|
|
chdir ("input");
|
|
|
|
$self->_setup_server();
|
|
|
|
|
2006-04-27 09:27:17 -04:00
|
|
|
chdir ($self->{_workdir});
|
2006-01-24 07:15:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub _cleanup {
|
|
|
|
my $self = shift;
|
|
|
|
|
2006-04-27 09:27:17 -04:00
|
|
|
chdir ($self->{_workdir});
|
2006-01-24 07:15:58 -05:00
|
|
|
File::Path::rmtree ($self->{_name});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub _verify_download {
|
|
|
|
my $self = shift;
|
|
|
|
|
2006-04-27 09:27:17 -04:00
|
|
|
chdir ("$self->{_workdir}/$self->{_name}/output");
|
2006-01-24 07:15:58 -05:00
|
|
|
|
|
|
|
# use slurp mode to read file content
|
|
|
|
my $old_input_record_separator = $/;
|
|
|
|
undef $/;
|
|
|
|
|
|
|
|
while (my ($filename, $filedata) = each %{$self->{_output}}) {
|
|
|
|
open (FILE, $filename)
|
|
|
|
or return "Test failed: file $filename not downloaded\n";
|
|
|
|
|
|
|
|
my $content = <FILE>;
|
|
|
|
$content eq $filedata->{'content'}
|
|
|
|
or return "Test failed: wrong content for file $filename\n";
|
|
|
|
|
|
|
|
if (exists($filedata->{'timestamp'})) {
|
|
|
|
my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
|
|
|
|
$atime, $mtime, $ctime, $blksize, $blocks) = stat FILE;
|
|
|
|
|
|
|
|
$mtime == $filedata->{'timestamp'}
|
|
|
|
or return "Test failed: wrong timestamp for file $filename\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
close (FILE);
|
|
|
|
}
|
|
|
|
|
|
|
|
$/ = $old_input_record_separator;
|
|
|
|
|
|
|
|
# make sure no unexpected files were downloaded
|
2006-04-27 09:27:17 -04:00
|
|
|
chdir ("$self->{_workdir}/$self->{_name}/output");
|
2006-01-24 07:15:58 -05:00
|
|
|
|
|
|
|
__dir_walk('.', sub { push @unexpected_downloads, $_[0] unless (exists $self->{_output}{$_[0]}) }, sub { shift; return @_ } );
|
|
|
|
if (@unexpected_downloads) {
|
|
|
|
return "Test failed: unexpected downloaded files [" . join(', ', @unexpected_downloads) . "]\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub __dir_walk {
|
|
|
|
my ($top, $filefunc, $dirfunc) = @_;
|
|
|
|
|
|
|
|
my $DIR;
|
|
|
|
|
|
|
|
if (-d $top) {
|
|
|
|
my $file;
|
|
|
|
unless (opendir $DIR, $top) {
|
|
|
|
warn "Couldn't open directory $DIR: $!; skipping.\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
my @results;
|
|
|
|
while ($file = readdir $DIR) {
|
|
|
|
next if $file eq '.' || $file eq '..';
|
|
|
|
my $nextdir = $top eq '.' ? $file : "$top/$file";
|
|
|
|
push @results, __dir_walk($nextdir, $filefunc, $dirfunc);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $dirfunc ? $dirfunc->($top, @results) : () ;
|
|
|
|
} else {
|
|
|
|
return $filefunc ? $filefunc->($top) : () ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-29 05:15:06 -04:00
|
|
|
|
|
|
|
sub _fork_and_launch_server
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
pipe(FROM_CHILD, TO_PARENT) or die "Cannot create pipe!";
|
|
|
|
select((select(TO_PARENT), $| = 1)[0]);
|
|
|
|
|
|
|
|
my $pid = fork();
|
|
|
|
if ($pid < 0) {
|
|
|
|
die "Cannot fork";
|
|
|
|
} elsif ($pid == 0) {
|
|
|
|
# child
|
|
|
|
close FROM_CHILD;
|
|
|
|
$self->_launch_server(sub { print TO_PARENT "SYNC\n"; close TO_PARENT });
|
|
|
|
} else {
|
|
|
|
# father
|
|
|
|
close TO_PARENT;
|
|
|
|
chomp(my $line = <FROM_CHILD>);
|
|
|
|
close FROM_CHILD;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $pid;
|
|
|
|
}
|
|
|
|
|
2005-12-05 08:35:07 -05:00
|
|
|
1;
|
|
|
|
|
|
|
|
# vim: et ts=4 sw=4
|
|
|
|
|