]> git.donarmstrong.com Git - debbugs.git/blobdiff - t/lib/DebbugsTest.pm
Merge branch 'database'
[debbugs.git] / t / lib / DebbugsTest.pm
index 6e33399d4e0f6ce0bfefd04a7dbdf586c62bd744..a23fdd4d7a0f56dd4e10df34b79ba1ffbb052dbf 100644 (file)
@@ -32,6 +32,7 @@ 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);
 
@@ -42,9 +43,11 @@ BEGIN{
      @EXPORT = ();
      %EXPORT_TAGS = (configuration => [qw(dirsize create_debbugs_configuration send_message)],
                     mail          => [qw(num_messages_sent)],
+                    control       => [qw(test_control_commands)],
+                    database => [qw(create_postgresql_database update_postgresql_database)]
                    );
      @EXPORT_OK = ();
-     Exporter::export_ok_tags(qw(configuration mail));
+     Exporter::export_ok_tags(keys %EXPORT_TAGS);
      $EXPORT_TAGS{all} = [@EXPORT_OK];
 }
 
@@ -205,7 +208,79 @@ sub send_message{
      }
 }
 
-$SIG{CHLD} = sub {};
+=item test_control_commands
+
+ test_control_commands(\%config,
+                       forcemerge => {command => 'forcemerge',
+                                      value   => '1 2',
+                                      status_key => 'mergedwith',
+                                      status_value => '2',
+                                      expect_error => 0,
+                                     });
+
+Test a set of control commands to see if they will fail or not. Takes
+SCALAR/HASHREF pairs, where the scalar should be unique, and the HASHREF
+contains the following keys:
+
+=over
+
+=item command -- control command to issue
+
+=item value -- value to pass to control command
+
+=item status_key -- bug status key to check
+
+=item status_value -- value of status key
+
+=item expect_error -- whether to expect the control command to error or not
+
+=back
+
+=cut
+
+sub test_control_commands {
+    my ($config,@commands) = @_;
+
+    # now we need to check to make sure that the control message actually did anything
+    # This is an eval because $ENV{DEBBUGS_CONFIG_FILE} isn't set at BEGIN{} time
+    eval "use Debbugs::Status qw(read_bug writebug);";
+    while (my ($command,$control_command) = splice(@commands,0,2)) {
+       # just check to see that control doesn't explode
+       $control_command->{value} = " $control_command->{value}" if length $control_command->{value}
+           and $control_command->{value} !~ /^\s/;
+       send_message(to => 'control@bugs.something',
+                    headers => [To   => 'control@bugs.something',
+                                From => 'foo@bugs.something',
+                                Subject => "Munging a bug with $command",
+                               ],
+                    body => <<EOF) or fail 'message to control@bugs.something failed';
+debug 10
+$control_command->{command} $control_command->{value}
+thanks
+EOF
+       ;
+       # now we need to check to make sure the control message was processed without errors
+       if (not ($control_command->{expect_error} // 0)) {
+           ok(system('sh','-c','find '.$config->{sendmail_dir}.
+                     q( -type f | xargs grep -q "Subject: Processed: Munging a bug with $command")
+                    ) == 0,
+              'control@bugs.something'. "$command message was parsed without errors");
+       }
+       # now we need to check to make sure that the control message actually did anything
+       my $status;
+       $status = read_bug(exists $control_command->{bug}?(bug => $control_command->{bug}):(bug=>1),
+                          exists $control_command->{location}?(location => $control_command->{location}):(),
+                         );
+       is_deeply($status->{$control_command->{status_key}},
+                 $control_command->{status_value},
+                 "bug " .
+                 (exists $control_command->{bug}?$control_command->{bug}:1).
+                 " $command"
+                )
+           or fail(Data::Dumper->Dump([$status],[qw(status)]));
+    }
+}
+
 
 {
      package DebbugsTest::HTTPServer;
@@ -277,6 +352,98 @@ sub num_messages_sent {
     return $cur_size;
 }
 
+=head2 create_postgresql_database
+
+C<my $pgsql = create_postgresql_database();>
+
+Create a postgresql database for testing; when the L<Test::PostgreSQL> 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
+    my $dbh = DBI->connect($pgsql->dsn);
+    $dbh->do(<<END) or die "Unable to create extension";
+CREATE EXTENSION IF NOT EXISTS debversion;
+END
+    # create the schema for the bug tracking system
+    my $dep_dir = File::Temp::tempdir(CLEANUP=>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();>
+
+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_postgresql_database();>
+
+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;