X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=t%2Flib%2FDebbugsTest.pm;h=e2db327b6ffbc41c7535cab4815fc715ac328073;hb=b03d82dc238cbbf33844976e0df06b2e92ba0341;hp=580c41d5ebaa06c199e52f766d0fae39c064987f;hpb=46fa42780ecb746d7bec2fb01190b05584b9283e;p=debbugs.git diff --git a/t/lib/DebbugsTest.pm b/t/lib/DebbugsTest.pm index 580c41d..e2db327 100644 --- a/t/lib/DebbugsTest.pm +++ b/t/lib/DebbugsTest.pm @@ -31,6 +31,8 @@ use Debbugs::MIME qw(create_mime_message); use File::Basename qw(dirname basename); use IPC::Open3; use IO::Handle; +use Test::More; +use Test::PostgreSQL; use Params::Validate qw(validate_with :types); @@ -40,9 +42,11 @@ BEGIN{ @EXPORT = (); %EXPORT_TAGS = (configuration => [qw(dirsize create_debbugs_configuration send_message)], + mail => [qw(num_messages_sent)], + database => [qw(create_postgresql_database update_postgresql_database)] ); @EXPORT_OK = (); - Exporter::export_ok_tags(qw(configuration)); + Exporter::export_ok_tags(keys %EXPORT_TAGS); $EXPORT_TAGS{all} = [@EXPORT_OK]; } @@ -55,7 +59,8 @@ BEGIN{ sub create_debbugs_configuration { my %param = validate_with(params => \@_, spec => {debug => {type => BOOLEAN, - default => 0, + default => exists $ENV{DEBUG}? + $ENV{DEBUG}:0, }, cleanup => {type => BOOLEAN, optional => 1, @@ -71,6 +76,7 @@ sub create_debbugs_configuration { $ENV{DEBBUGS_CONFIG_FILE} ="$config_dir/debbugs_config"; $ENV{PERL5LIB} = getcwd(); $ENV{SENDMAIL_TESTDIR} = $sendmail_dir; + eval { my $sendmail_tester = getcwd().'/t/sendmail_tester'; unless (-x $sendmail_tester) { die q(t/sendmail_tester doesn't exist or isn't executable. You may be in the wrong directory.); @@ -80,16 +86,19 @@ sub create_debbugs_configuration { \$gSpoolDir='$spool_dir'; \$gLibPath='@{[getcwd()]}/scripts'; \$gTemplateDir='@{[getcwd()]}/templates'; +\$gWebDir='@{[getcwd()]}/html'; \$gWebHost='localhost'; 1; END "$spool_dir/nextnumber" => qq(1\n), - "$config_dir/Maintainers" => qq(foo Blah Bleargh \n), + "$config_dir/Maintainers" => qq(foo Blah Bleargh \nbar Bar Bleargh \n), "$config_dir/Maintainers.override" => qq(), + "$config_dir/Source_maintainers" => qq(foo Blah Bleargh \nbar Bar Bleargh \n), "$config_dir/indices/sources" => < '', + "$config_dir/pseudo-packages.maintainers" => '', ); while (my ($file,$contents) = each %files_to_create) { system('mkdir','-p',dirname($file)); @@ -107,12 +116,24 @@ END "$spool_dir/index.archive"); # create the spool files and sub directories - map {system('mkdir','-p',"$spool_dir/$_"); } - map {('db-h/'.$_,'archive/'.$_)} - map { sprintf "%02d",$_ % 100} 0..99; + for my $dir (0..99) { + for my $archive (qw(db-h archive)) { + system('mkdir','-p',"$spool_dir/$archive/".sprintf('%02d',$dir)); + } + } system('mkdir','-p',"$spool_dir/incoming"); system('mkdir','-p',"$spool_dir/lock"); + eval ' +END{ + if ($ENV{DEBUG}) { + diag("spool_dir: $spool_dir\n"); + diag("config_dir: $config_dir\n",); + diag("sendmail_dir: $sendmail_dir\n"); + } +}'; + }; + BAIL_OUT ($@) if ($@); return (spool_dir => $spool_dir, sendmail_dir => $sendmail_dir, config_dir => $config_dir, @@ -141,6 +162,9 @@ sub send_message{ }, body => {type => SCALAR, }, + attachments => {type => ARRAYREF, + default => [], + }, run_processall =>{type => BOOLEAN, default => 1, }, @@ -149,13 +173,17 @@ sub send_message{ $ENV{LOCAL_PART} = $param{to}; my ($rfd,$wfd); my $output=''; - local $SIG{PIPE} = 'IGNORE'; - local $SIG{CHLD} = sub {}; + my $pipe_handler = $SIG{PIPE}; + $SIG{PIPE} = 'IGNORE'; + $SIG{CHLD} = 'DEFAULT'; my $pid = open3($wfd,$rfd,$rfd,'scripts/receive') or die "Unable to start receive: $!"; print {$wfd} create_mime_message($param{headers}, - $param{body}) or die "Unable to to print to receive"; + $param{body}, + $param{attachments}) or + die "Unable to to print to receive"; close($wfd) or die "Unable to close receive"; + $SIG{PIPE} = $pipe_handler; my $err = $? >> 8; my $childpid = waitpid($pid,0); if ($childpid != -1) { @@ -181,7 +209,7 @@ sub send_message{ { package DebbugsTest::HTTPServer; - use base qw(HTTP::Server::Simple::CGI); + use base qw(HTTP::Server::Simple::CGI HTTP::Server::Simple::CGI::Environment); our $child_pid = undef; our $webserver = undef; @@ -230,6 +258,119 @@ sub send_message{ } } +=head2 num_messages_sent + + $SD_SIZE = num_messages_sent($SD_SIZE,2,$sendmail_dir,'2 messages have been sent properly'); + +Tests to make sure that at least a certain number of messages have +been sent since the last time this command was run. Usefull to test to +make sure that mail has been sent. + +=cut + +sub num_messages_sent { + my ($prev_size,$num_messages,$sendmail_dir,$test_name) = @_; + my $cur_size = dirsize($sendmail_dir); + ## print STDERR "sendmail: $sendmail_dir, want: $num_messages, + ## size: $cur_size, prev_size: $prev_size\n"; + ok($cur_size-$prev_size >= $num_messages, $test_name); + return $cur_size; +} + +=head2 create_postgresql_database + +C + +Create a postgresql database for testing; when the L object it +returns is destroyed (or goes out of scope) the database will be removed. + +=cut + +sub create_postgresql_database { + my $pgsql = Test::PostgreSQL->new(use_socket => 1) or + return undef; + my $installsql = + File::Spec->rel2abs(dirname(__FILE__).'/../..'). + '/bin/debbugs-installsql'; + # create the debversion extension + print STDERR $pgsql->dsn; + print STDERR "\n"; + my $dbh = DBI->connect($pgsql->dsn); + $dbh->do(<1); + system($installsql, + '--dsn',$pgsql->dsn, + '--install', + '--deployment-dir',$dep_dir); + + initialize_postgresql_database($pgsql,@_); + return $pgsql; +} + +=item iniitalize_postgresql_database + +C + +Initialize postgresql database by calling debbugs-loadsql appropriately. + +=cut + +sub initialize_postgresql_database { + my ($pgsql,@options) = @_; + my $loadsql = + File::Spec->rel2abs(dirname(__FILE__).'/../..'). + '/bin/debbugs-loadsql'; + + my $ftpdists = + File::Spec->rel2abs(dirname(__FILE__).'/../debian/dist'); + my $debinfo_dir = + File::Spec->rel2abs(dirname(__FILE__).'/../debian/debinfo'); + my %loadsql_commands = + (configuration => [], + suites => ['--ftpdists',$ftpdists], + debinfo => ['--debinfo-dir',$debinfo_dir], + packages => ['--ftpdists',$ftpdists], + maintainers => [], + ); + for my $command (keys %loadsql_commands) { + system($loadsql,$command, + '--dsn',$pgsql->dsn, + @options, + @{$loadsql_commands{$command}}) == 0 or + die "Unable to load $command"; + } +} + + +=item update_postgresql_database + +C + +Update the postgresql database by calling debbugs-loadsql appropriately. + +=cut +sub update_postgresql_database { + my ($pgsql,@options) = @_; + my $loadsql = + File::Spec->rel2abs(dirname(__FILE__).'/../..'). + '/bin/debbugs-loadsql'; + + my %loadsql_commands = + (bugs_and_logs => [], + ); + for my $command (keys %loadsql_commands) { + system($loadsql,$command, + '--dsn',$pgsql->dsn, + @options, + @{$loadsql_commands{$command}}) == 0 or + die "Unable to load $command"; + } +} + + 1;