Testing tweaks.
authorGreg Sabino Mullane <[email protected]>
Tue, 14 Apr 2009 17:47:26 +0000 (13:47 -0400)
committerGreg Sabino Mullane <[email protected]>
Tue, 14 Apr 2009 17:47:26 +0000 (13:47 -0400)
check_postgres.pl
t/02_version.t
t/CP_Testing.pm

index dcdf7bc315dab818a791030d7ce4feb9c933e2ac..1ebe1b5db7dcb3cd2e0d816a0781f0f29e893446 100755 (executable)
@@ -2785,8 +2785,8 @@ sub check_fsm_pages {
        my $info = run_command($SQL, {regex => qr[\d+] } );
 
        for $db (@{$info->{db}}) {
-         SLURP: while ($db->{slurp} =~ /\s*(\d+) \|\s+(\d+) \|\s+(\d+)$/gsm) {
-                       my ($pages,$max,$percent) = ($1,$2,$3);
+         SLURP: while ($db->{slurp} =~ /\s*(\d*) \|\s+(\d+) \|\s+(\d*)$/gsm) {
+                       my ($pages,$max,$percent) = ($1||0,$2,$3||0);
 
                        if ($MRTG) {
                                do_mrtg({one => $percent, two => $pages});
index 6b337570f3df3b0a1f03dd6573aebb04c1bcb531..ed0f9e9d13eb206810ce05902e183f8ffc118927 100644 (file)
@@ -102,10 +102,10 @@ $t=qq{$S gives correct output for MRTG output};
 like ($cp->run('--output=MRTG --mrtg=7.9.13'), qr{^0\n0\n\n7.8.12\n}, $t);
 
 $t=qq{$S gives correct output for MRTG output};
-like ($cp->run('--output=MRTG --mrtg=7.8'), qr{^1\n0\n\n7.8.12\n}, $t);
+is ($cp->run('--output=MRTG --mrtg=7.8'), qq{1\n0\n\n7.8.12\n}, $t);
 
 $t=qq{$S gives correct output for MRTG output};
-like ($cp->run('--output=MRTG --mrtg=7.8.12'), qr{^1\n0\n\n7.8.12\n}, $t);
+is ($cp->run('--output=MRTG --mrtg=7.8.12'), qq{1\n0\n\n7.8.12\n}, $t);
 
 $cp->reset_path();
 
index ab2e008917fa172dc31fbc30c6fbef6fe78ad50f..6ff30283640f90de2aacaf1942dbb7e025366bdf 100644 (file)
@@ -8,7 +8,7 @@ use Data::Dumper;
 use Time::HiRes qw/sleep/;
 use Cwd;
 
-my $DEBUG = 0;
+our $DEBUG = 0;
 
 use vars qw/$com $info $count/;
 
@@ -19,8 +19,8 @@ sub new {
        my $arg = shift || {};
        my $self = {
                started  => time(),
-               dbdir    => 'test_database_check_postgres',
-               testuser => 'check_postgres_testing',
+               dbdir    => $arg->{dbdir}    || 'test_database_check_postgres',
+               testuser => $arg->{testuser} || 'check_postgres_testing',
        };
        if (exists $arg->{default_action}) {
                $self->{action} = $arg->{default_action};
@@ -34,6 +34,7 @@ sub test_database_handle {
 
        my $self = shift;
        my $arg = shift || {};
+       $arg->{dbname} ||= 'postgres';
 
        ref $arg eq 'HASH' or die qq{Must pass a hashref (or nothing) to test_database_handle\n};
 
@@ -129,7 +130,7 @@ sub test_database_handle {
        my $dbhost = $self->{dbhost} = "$here/$dbdir/data/socket";
        $dbhost =~ s/^ /\\ /;
        $dbhost =~ s/([^\\]) /$1\\ /g;
-       $self->{dbname} = 'postgres';
+       $self->{dbname} ||= 'postgres';
        my $dsn = qq{dbi:Pg:host=$dbhost;dbname=$self->{dbname}};
        my @superdsn = ($dsn, '', '', {AutoCommit=>0,RaiseError=>1,PrintError=>0});
        my $dbh = DBI->connect(@superdsn);
@@ -147,15 +148,30 @@ sub test_database_handle {
        $dbh->{AutoCommit} = 0;
        $dbh->{RaiseError} = 1;
 
-       $self->{dbh} = $dbh;
-       $self->{dsn} = $dsn;
-       $self->{superdsn} = \@superdsn;
-
        if (! exists $self->{keep_old_schema}) {
                local $dbh->{Warn};
                $dbh->do("DROP SCHEMA IF EXISTS $fakeschema CASCADE");
        }
 
+       if ($arg->{dbname} ne $self->{dbname}) {
+               my $tmp_dsn = $dsn;
+               $tmp_dsn =~ s/dbname=\w+/dbname=$arg->{dbname}/;
+               my $tmp_dbh;
+               eval { $tmp_dbh = DBI->connect($tmp_dsn, @superdsn[1..$#superdsn]) };
+               if ($@) {
+                       local($dbh->{AutoCommit}) = 1;
+                       $dbh->do("CREATE DATABASE " . $arg->{dbname});
+                       eval { $tmp_dbh = DBI->connect($tmp_dsn, @superdsn[1..$#superdsn]) };
+                       die $@ if $@;
+               }
+               $dbh->disconnect;
+               $dbh = $tmp_dbh;
+               $self->{dbname} = $arg->{dbname};
+       }
+
+       $self->{dbh} = $dbh;
+       $self->{dsn} = $dsn;
+       $self->{superdsn} = \@superdsn;
 
        ## Sanity check
        $dbh->do("ALTER USER $dbuser SET search_path = public");
@@ -166,10 +182,18 @@ sub test_database_handle {
 
 } ## end of test_database_handle
 
+sub get_command {
+  return run('get_command', @_);
+}
 
 sub run {
 
        my $self = shift;
+       my $get;
+       if ($self eq 'get_command') {
+               $get = $self;
+               $self = shift;
+       }
        my @arg = @_;
        my $extra = pop @arg || '';
        my $action = @arg ? $arg[0] : $self->{action} || die "First arg must be the command\n";
@@ -190,6 +214,7 @@ sub run {
 
        $DEBUG and warn "DEBUG RUN: $com\n";
 
+       return $com if $get;
        my $result;
        eval {
                $result = qx{$com 2>&1};
@@ -202,6 +227,11 @@ sub run {
 
 } ## end of run
 
+sub get_user {
+       my $self = shift;
+       return $self->{testuser};
+}
+
 sub get_host {
        my $self = shift;
        return $self->{dbhost};
@@ -217,11 +247,6 @@ sub get_dbh {
        return $self->{dbh} || die;
 }
 
-sub get_user {
-       my $self = shift;
-       return $self->{testuser} || die;
-}
-
 sub get_fresh_dbh {
 
        my $self = shift;
@@ -275,6 +300,26 @@ sub create_fake_pg_table {
 } ## end of create_fake_pg_table
 
 
+sub get_fake_schema {
+       return $fakeschema;
+}
+
+
+sub set_fake_schema {
+
+       my $self = shift;
+       my $dbh = $self->{dbh} || die;
+       my $dbuser = $self->{testuser} || die;
+       if (!$self->schema_exists($dbh,$fakeschema)) {
+               $dbh->do("CREATE SCHEMA $fakeschema");
+       }
+
+       $dbh->do("ALTER USER $dbuser SET search_path = $fakeschema, public, pg_catalog");
+       $dbh->commit();
+
+} ## end of set_fake_schema
+
+
 sub remove_fake_pg_table {
 
        my $self = shift;
@@ -352,24 +397,4 @@ sub reset_path {
 
 } ## end of reset_path
 
-sub bad_fake_version {
-
-       my $self = shift;
-       my $version = shift || '9.9';
-       my $dbh = $self->{dbh} || die;
-       my $dbuser = $self->{testuser} || die;
-
-       $dbh->do(qq{
-CREATE OR REPLACE FUNCTION public.version()
-RETURNS TEXT
-LANGUAGE SQL
-AS \$\$
-SELECT 'Postgres $version on fakefunction for check_postgres.pl testing'::text;
-\$\$
-});
-       $dbh->do("ALTER USER $dbuser SET search_path = public, pg_catalog");
-       $dbh->commit();
-
-} ## end of bad_fake_version
-
 1;