/usr/share/perl5/Lire/Test/FunctionalTestCase.pm is in lire 2:2.1.1-2.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 | package Lire::Test::FunctionalTestCase;
use strict;
use base qw/ Test::Unit::TestCase /;
use Carp;
use File::Path qw/ mkpath rmtree /;
use File::Copy;
use File::Basename;
use File::Path qw/rmtree/;
use IO::Dir;
use Lire::Config;
use Lire::PluginManager;
use Lire::Config::SpecParser;
use Lire::Config::Value;
use Lire::Config::Build qw/ ac_info ac_path /;
use Lire::Test::CommandResult;
use Lire::Utils qw/ tempdir create_file file_content check_param /;
=pod
=head1 NAME
Lire::Test::FunctionalTestCase - Base class for writing functional unit tests
=head1 SYNOPSIS
use base qw/ Lire::Test::FunctionalTestCase/;
=head1 DESCRIPTION
This is a Test::Unit::TestCase subclass which can be used to make it
easier to write functional tests.
=head2 WHAT ARE FUNCTIONAL TESTS
Unlike unit tests that test the semantics of the API and document the
internals assumptions, functional tests are tests that tests the
'functionality' of the overall software. They kind of document the
expected functionality of the software.
=head2 Lire::Test::FunctionalTestCase
In Lire, the "expected functionality" is exercised by running
commands, so this module provides convenient methods to run Lire
commands and tests their expected output.
It also setup a mock sendmail which can be used to analyze the email
that the Lire system should send.
Some of Lire functionality cannot be tested anymore simply by running
command (for example the Curses::UI based user interface cannot be
tested that way but throught the use of Lire::Test::CursesUIDriver).
For these kind of tests, this base class will set up a new
Lire::Config and Lire::PluginManager instance in the set_up
method. Tests that requires these functionality can simply call
Lire::Config->init() and
Lire::PluginManager->instance()->register_default_converters()
without fear of messing the test environement.
=cut
sub new {
my $self = shift->SUPER::new( @_ );
$self->{'_test_files'} = [];
$self->{'_locale'} = 'C';
$self->_check_lire_installation();
$self->_load_config_spec();
return $self;
}
sub set_up {
my $self = $_[0];
$self->{'_config_files'} = {};
$self->{'_rundir'} = tempdir( "FunctionalTest_XXXXXX" );
$self->{'_homedir'} = $self->{'_rundir'} . "/home";
mkdir $self->{'_homedir'}
or croak "failed to create $self->{'_homedir'}";
$self->_create_lire_home_directories();
$self->_set_up_fake_sendmail();
$self->_set_up_lire_globals();
$self->{'_stdout_file'} = $self->{'_rundir'} . "/stdout.log";
$self->{'_stderr_file'} = $self->{'_rundir'} . "/stderr.log";
unshift @INC, ac_info( 'LR_PERL5LIB' );
return;
}
sub tear_down {
my $self = $_[0];
foreach my $f ( @{$self->{'_test_files'}}) {
unlink $f
or croak "failed to delete $f";
}
rmtree( $self->{'_rundir'} );
$self->_tear_down_lire_globals();
shift @INC if $INC[0] eq ac_info( 'LR_PERL5LIB' );
return;
}
sub _check_lire_installation {
my $self = $_[0];
my $lr_xml2report = ac_info( 'bindir' ) . "/lr_xml2report";
my $defaults = ac_path( 'sysconfdir', 'PACKAGE' ) . "/defaults";
my $sysconfdir = ac_path( 'sysconfdir', 'PACKAGE' );
my $datadir = ac_path( 'datadir', 'PACKAGE' );
croak "$lr_xml2report isn't an executable. Was Lire installed?"
unless -f $lr_xml2report && -x $lr_xml2report;
croak "$defaults isn't present. Was Lire installed?"
unless -f $defaults && -r $defaults;
croak "you need write access to Lire's sysconfdir ($sysconfdir) to use FunctionalTestCase"
unless -w $sysconfdir;
croak "you need write access to Lire's datadir ($datadir) to use FunctionalTestCase"
unless -w $datadir;
}
sub _create_lire_home_directories {
my $self = $_[0];
mkpath( [ $self->{'_homedir'} . "/.lire",
$self->{'_homedir'} . "/.lire/config",
$self->{'_homedir'} . "/.lire/converters",
$self->{'_homedir'} . "/.lire/plugins",
$self->{'_homedir'} . "/.lire/schemas",
$self->{'_homedir'} . "/.lire/filters",
$self->{'_homedir'} . "/.lire/reports",
], 0, 0755 ) == 7
or croak "mkpath() failed";
return;
}
sub _set_up_lire_globals {
my $self = $_[0];
$self->{'_old_homedir'} = $ENV{'HOME'};
$self->{'_old_config'} = $Lire::Config::SINGLETON;
$self->{'_old_mgr'} = $Lire::PluginManager::instance;
$ENV{'HOME'} = $self->{'_homedir'};
$Lire::Config::SINGLETON = new Lire::Config::XMLFilesConfig();
$Lire::PluginManager::instance = new Lire::PluginManager();
return;
}
sub _tear_down_lire_globals {
my $self = $_[0];
$ENV{'HOME'} = $self->{'_old_homedir'};
$Lire::Config::SINGLETON = $self->{'_old_config'};
$Lire::PluginManager::instance = $self->{'_old_mgr'};
return;
}
sub _load_config_spec {
my $self = $_[0];
my $parser = new Lire::Config::SpecParser();
$parser->merge_specifications_dir( ac_path( 'datadir', 'PACKAGE') . "/config-spec" );
$self->{'_spec'} = $parser->configspec();
return;
}
sub _set_up_sendmail_config {
my $self = $_[0];
my $cfg = $self->create_test_cfg_file( "func_test_sendmail" );
$cfg->global->get( "sendmail_path" )->set( $self->{'_sendmail_path'} );
$cfg->save();
return;
}
sub _set_up_fake_sendmail {
my $self = $_[0];
$self->{'_maildir'} = "$self->{'_rundir'}/.func_test_mail";
mkdir $self->{'_maildir'}, 0770
or croak "mkdir $self->{'_maildir'} failed: $!";
$self->{'_sendmail_path'} = "$self->{'_rundir'}/func_test_sendmail";
open my $fh, "> $self->{'_sendmail_path'}"
or croak "open $self->{'_sendmail_path'} failed: $!";
my $script = <<'EOF';
#! PERL -w
use strict;
use File::Copy;
use Carp;
my $file;
my $idx = 0;
while (1) {
$file = sprintf( 'MAILDIR/%02x', $idx);
last unless -f "$file.recipients";
$idx++;
}
open RECIPIENTS, "> $file.recipients"
or croak "open $file.recipients failed: $!\n";
print RECIPIENTS join( "\n", @ARGV ), "\n";
close RECIPIENTS;
copy( \*STDIN, $file . ".message" );
exit 0
EOF
my $perl = ac_info( 'PERL' );
$script =~ s/PERL/$perl/;
$script =~ s/MAILDIR/$self->{'_maildir'}/g;
print $fh $script;
close $fh;
chmod 0775, $self->{'_sendmail_path'}
or croak "failed to chmod $self->{'_sendmail_path'}: $!\n";
$self->_set_up_sendmail_config();
return;
}
=pod
=head2 homedir()
Returns the directory which will be assigned to ENV{'HOME'} when commands
are run. This directory is cleaned up after every test.
=cut
sub homedir {
return $_[0]{'_homedir'};
}
=pod
=head2 rundir()
Returns the directory which will be the working directory when the
commands are run. This directory is cleaned up after every test.
=cut
sub rundir {
return $_[0]{'_rundir'};
}
=pod
=head2 config_spec()
Returns the Lire::Config::TypeSpec object which will be used at runtime by
the functional tests. It can be used to setup configuration files for
the test.
=cut
sub config_spec {
return $_[0]{'_spec'};
}
=pod
=head2 locale( [new_locale] )
Changes the locale under which the functional test is going to be run. By
default, all tests are run under the 'C' locale ignoring all user's
environment variables.
Returns the previous locale set.
=cut
sub locale {
my ( $self, $locale ) = @_;
my $old = $self->{'_locale'};
$self->{'_locale'} = $locale || 'C';
return $old;
}
=pod
=head2 lire_run( $shell_cmd )
This method runs the command $shell_cmd through the shell and returns
a Lire::Test::CommandResult object which can be used to run tests on
this command. The Lire environment will be set up before invoking the
shell.
=cut
sub lire_run {
my ($self, $cmd) = @_;
check_param( $cmd, 'cmd' );
rmtree( $self->{'_maildir'} );
mkdir $self->{'_maildir'}, 0770
or croak "mkdir $self->{'_maildir'} failed: $!";
$self->_create_cfg_files();
my $pid = fork;
croak "fork failed: $!" unless defined $pid;
if ( !$pid ) {
$self->_run_command( $cmd );
}
return $self->_make_cmd_result( $cmd, $pid );
}
sub _run_command {
my ( $self, $cmd ) = @_;
local $ENV{'HOME'} = $self->homedir();
local $ENV{'PATH'} = join( ":", ac_info( "bindir"), $ENV{'PATH'} );
delete $ENV{'LANGUAGE'};
delete $ENV{'LANG'};
local $ENV{'LC_ALL'} = $self->{'_locale'};
chdir $self->{'_rundir'}
or croak "chdir failed; $!\n";
open STDOUT, "> $self->{'_stdout_file'}"
or croak "failed to redirect STDOUT: $!\n";
open STDERR, "> $self->{'_stderr_file'}"
or croak "failed to redirect STDERR: $!\n";
exec( "/bin/sh", "-c", $cmd )
or croak "exec failed: $!\n";
}
sub _make_cmd_result {
my ( $self, $cmd, $pid ) = @_;
waitpid $pid, 0
or croak "waitpid failed: $!\n";
my $result = new Lire::Test::CommandResult( '_command' => $cmd,
'_status' => $?,
);
$result->{'_stdout'} = file_content( $self->{'_stdout_file'} );
$result->{'_stderr'} = file_content( $self->{'_stderr_file'} );
$result->{'_sent_mail'} = $self->sent_mail();
return $result;
}
=pod
=head2 sent_mail()
Returns in an array reference the message that were sent out during
the test. The element of this array are hash reference containing two
keys:
=over
=item recipients
An array reference containing the recipients as passed on the command
line to sendmail.
=item message
The email which was sent. This is the complete text of the email. This
is what was passed to sendmail via STDIN.
=back
=cut
sub sent_mail {
my $self = $_[0];
my $sent_mail = [];
my $dir = new IO::Dir( $self->{'_maildir'} );
my @emails = map { /(.*)\.recipients/ } grep { $_ =~ /\.recipients$/ } $dir->read;
$dir->close;
foreach my $base ( @emails ) {
my $mail = { 'recipients' => [],
'message' => undef,
};
$mail->{'recipients'} =
[split ' ', file_content( "$self->{'_maildir'}/$base.recipients" )];
$mail->{'message'} = file_content( "$self->{'_maildir'}/$base.message" );
push @$sent_mail, $mail;
}
return $sent_mail;
}
=pod
=head2 create_test_file( $filename, [$content] )
Create a file $filename with the content $content. This file will be
removed after the test is run during the tear_down() method. An empty
file will be created if the $content parameter is omitted.
=cut
sub create_test_file {
my ( $self, $filename, $content ) = @_;
check_param( $filename, 'filename',
sub { ! -f $_[0] },
'file already exists' );
$content = "" unless defined $content;
create_file( $filename, $content );
push @{$self->{'_test_files'}}, $filename;
return;
}
=pod
=head2 install_xml_spec( $spec_type, $superservice, $file )
Install the XML specification in $file of base schema $superservice.
The $spec_type parameter specify where the file will be installed:
=over
=item site_filter
The file will be installed as a filter specification in the system
directory.
=item site_report
The file will be installed as a report specification in the system
directory.
=item site_schema
The file will be installed as a schema specification in the system
directory.
=item filter
The file will be installed as a filter specification in the HOME
directory.
=item report
The file will be installed as a report specification in the HOME
directory.
=item schema
The file will be installed as a schema specification in the HOME
directory.
=back
These files will be removed after the test.
=cut
sub install_xml_spec {
my ( $self, $spec_type, $superservice, $spec_file ) = @_;
check_param( $spec_type, 'spec_type',
qr/^(site_filter|site_schema|site_report|filter|report||schema)$/,
"invalid spec_type parameter" );
check_param( $superservice, 'superservice' );
check_param( $spec_file, 'spec_file',
sub { -f $_[0] && -r $_[0] },
"file doesn't exists" );
my $tree_root = $self->_find_tree_root( $spec_type );
my $spec_dir = $self->_find_spec_dir( $spec_type, $superservice );
unless (-d "$tree_root/$spec_dir" ) {
mkdir "$tree_root/$spec_dir", 0775
or croak "can't create directory $tree_root/$spec_dir";
}
my $filename = basename( $spec_file );
copy( $spec_file, "$tree_root/$spec_dir/$filename" )
or croak "copy failed: $!";
push @{$self->{'_test_files'}}, "$tree_root/$spec_dir/$filename";
return;
}
sub _find_tree_root {
my ( $self, $spec_type) = @_;
return $spec_type =~ /^site_/ ? ac_path( "datadir", "PACKAGE" ) : $self->{'_homedir'} . "/.lire";
}
sub _find_spec_dir {
my ( $self, $spec_type, $superservice ) = @_;
if ( $spec_type =~ /filter/ ) {
return "filters/$superservice";
} elsif ( $spec_type =~ /schema/ ) {
return "schemas";
} else {
return "reports/$superservice";
}
}
=pod
=head2 create_test_cfg_file( $name )
Returns a Lire::Config::ConfigFile object initialized with the
appropriate config specification. This configuration file will be
created under $HOME/.lire/config/$name.xml when lire_run() will be
used. It can be use to set config option which will be used when the
command is run:
my $cfg = $self->create_test_cfg_file( "test" );
$cfg->global->get( "lr_from" )->set( "flacoste\@logreport.org" );
If a configuration file $name was already created, it returns the
associated ConfigFile object.
Note that if you want these configurations variable to have effect
when running test in-process ( not via lire_run() ), you have to call
save() on the object before calling Lire::Config->init(). Otherwise,
you better set the configuration option directly using the
Lire::Config API.
=cut
sub create_test_cfg_file {
my ($self, $name) = @_;
check_param( $name, 'name',
qr/^[a-zA-Z0-9_-]+$/,
"'name' parameter should only contains digits, letters, '-' and '_'" );
return $self->{'_config_files'}{$name}
if exists $self->{'_config_files'}{$name};
my $config_file = $self->homedir . "/.lire/config/$name.xml";
my $cfg = new Lire::Config::ConfigFile( 'filename' => $config_file,
'spec' => $self->{'_spec'} );
$cfg->global( $self->{'_spec'}->instance );
$self->{'_config_files'}{$name} = $cfg;
return $cfg;
}
sub _create_cfg_files {
my $self = $_[0];
foreach my $cfg_file ( values %{$self->{'_config_files'}}) {
$cfg_file->save();
}
}
# keep perl happy
1;
__END__
=pod
=head1 SEE ALSO
Test::Unit::TestCase(3pm)
=head1 VERSION
$Id: FunctionalTestCase.pm,v 1.27 2006/07/23 13:16:31 vanbaal Exp $
=head1 AUTHOR
Francis J. Lacoste <flacoste@logreport.org>
=head1 COPYRIGHT
Copyright (C) 2003 Stichting LogReport Foundation LogReport@LogReport.org
This file is part of Lire.
Lire is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program (see COPYING); if not, check with
http://www.gnu.org/copyleft/gpl.html.
=cut
|