Tweaks for better "fakeschema" in the tests.
authorGreg Sabino Mullane <[email protected]>
Tue, 17 Jan 2012 17:52:43 +0000 (12:52 -0500)
committerGreg Sabino Mullane <[email protected]>
Tue, 17 Jan 2012 17:52:43 +0000 (12:52 -0500)
t/02_backends.t
t/CP_Testing.pm

index 4dd03b67faafa9ffcd7187f23b05ddf8a184ff08..d8e6c50cef20c4ba1c3f67032d71cf9fee954efb 100644 (file)
@@ -6,7 +6,7 @@ use 5.006;
 use strict;
 use warnings;
 use Data::Dumper;
-use Test::More tests => 53;
+use Test::More tests => 52;
 use lib 't','.';
 use CP_Testing;
 
@@ -16,6 +16,9 @@ my $cp = CP_Testing->new( {default_action => 'backends'} );
 
 $dbh = $cp->test_database_handle();
 
+## Remove any old fake schema
+$cp->drop_schema_if_exists();
+
 my $S = q{Action 'backends'};
 my $label = 'POSTGRES_BACKENDS';
 
@@ -129,18 +132,13 @@ $num = $goodver ? 7 : 8;
 like ($cp->run("-c -$num"), qr{^$label CRITICAL}, $t);
 
 $t=qq{$S works when no items caught by pg_stat_activity};
-
-$cp->drop_schema_if_exists();
-$cp->create_fake_pg_table('pg_stat_activity');
-like ($cp->run(), qr{^$label OK: .+No connections}, $t);
-
-$t=qq{$S returns correct MRTG output when no rows};
-is ($cp->run('--output=MRTG'), qq{0\n0\n\nDB=postgres Max connections=10\n}, $t);
+$cp->create_fake_pg_table('pg_stat_activity','', ' WHERE procpid = pg_backend_pid()');
+like ($cp->run(), qr{^$label OK: .+1 of }, $t);
 
 $t=qq{$S fails as expected when max_connections cannot be determined};
 $cp->create_fake_pg_table('pg_settings');
 like ($cp->run(), qr{^$label UNKNOWN: .+max_connections}, $t);
-$cp->drop_schema_if_exists();
+$cp->remove_fake_pg_table('pg_settings');
 
 $t=qq{$S works when include forces no matches};
 like ($cp->run('--include=foobar'), qr{^$label OK: .+No connections}, $t);
@@ -150,10 +148,10 @@ SKIP: {
     $goodver or skip 'Cannot test backends completely with older versions of Postgres', 2;
 
     $t=qq{$S returns correct MRTG output when rows found};
-    is ($cp->run('--output=MRTG'), qq{3\n0\n\nDB=postgres Max connections=10\n}, $t);
+    like ($cp->run('--output=MRTG'), qr{^1\n0\n\nDB=postgres}, $t);
 
     $t=qq{$S works when include has valid database};
-    like ($cp->run('--include=postgres'), qr{^$label OK: .+3 of 10}, $t);
+    like ($cp->run('--include=postgres'), qr{^$label OK: .+1 of 10}, $t);
 }
 
 $t=qq{$S works when exclude forces no matches};
@@ -164,16 +162,16 @@ SKIP: {
     $goodver or skip 'Cannot test backends completely with older versions of Postgres', 4;
 
     $t=qq{$S works when exclude excludes nothing};
-    like ($cp->run('--exclude=foobar'), qr{^$label OK: .+3 of 10}, $t);
+    like ($cp->run('--exclude=foobar'), qr{^$label OK: .+1 of 10}, $t);
 
     $t=qq{$S works when include and exclude make a match};
-    like ($cp->run('--exclude=postgres --include=postgres'), qr{^$label OK: .+3 of 10}, $t);
+    like ($cp->run('--exclude=postgres --include=postgres'), qr{^$label OK: .+1 of 10}, $t);
 
     $t=qq{$S works when include and exclude make a match};
-    like ($cp->run('--include=postgres --exclude=postgres'), qr{^$label OK: .+3 of 10}, $t);
+    like ($cp->run('--include=postgres --exclude=postgres'), qr{^$label OK: .+1 of 10}, $t);
 
     $t=qq{$S returned correct performance data with include};
-    like ($cp->run('--include=postgres'), qr{ \| time=\d\.\d\ds postgres=3;9;9;0;10}, $t);
+    like ($cp->run('--include=postgres'), qr{ \| time=\d\.\d\ds postgres=1}, $t);
 }
 
 my %dbh;
@@ -184,6 +182,4 @@ for my $num (1..8) {
 $t=qq{$S returns critical when too many clients to even connect};
 like ($cp->run('-w -10'), qr{^$label CRITICAL: .+too many connections}, $t);
 
-$cp->drop_schema_if_exists();
-
 exit;
index 530271ce5972372c0f87a13c673b00f65dcd3ae5..a72a957cb5d13dcd26091dd4f860e2f8bd454de1 100644 (file)
@@ -508,6 +508,7 @@ sub create_fake_pg_table {
     my $self = shift;
     my $name = shift || die;
     my $args = shift || '';
+       my $where = shift || '';
     my $dbh = $self->{dbh} || die;
     my $dbuser = $self->{testuser} || die;
     if ($self->schema_exists($dbh,$fakeschema)) {
@@ -523,7 +524,10 @@ sub create_fake_pg_table {
         $funcargs = qq{($funcargs)};
     }
 
-    $dbh->do("CREATE TABLE $fakeschema.$name AS SELECT * FROM $name$funcargs LIMIT 0");
+       my $SQL = "CREATE TABLE $fakeschema.$name AS SELECT * FROM $name$funcargs$where ";
+       $SQL .= $where ? 'LIMIT 1' : 'LIMIT 0';
+
+    $dbh->do($SQL);
 
     if ($args) {
         $self->drop_function_if_exists($fakeschema,$name,$args);
@@ -537,6 +541,28 @@ sub create_fake_pg_table {
 } ## end of create_fake_pg_table
 
 
+sub remove_fake_pg_table {
+
+    my $self = shift;
+    my $name = shift || die;
+    my $dbh = $self->{dbh} || die;
+    my $dbuser = $self->{testuser} || die;
+    if (! $self->schema_exists($dbh,$fakeschema)) {
+               ## No schema means no table!
+               return;
+       }
+
+       my $SQL = "DROP TABLE $fakeschema.$name";
+
+    $dbh->do($SQL);
+
+    $dbh->commit();
+
+    return;
+
+} ## end of remove_fake_pg_table
+
+
 sub get_fake_schema {
     return $fakeschema;
 }
@@ -625,7 +651,8 @@ sub drop_table_if_exists {
     my $count = $dbh->selectall_arrayref($SQL)->[0][0];
     if ($count) {
         $dbh->{Warn} = 0;
-        $dbh->do("DROP TABLE $name CASCADE");
+               my $fullname = $schema ? "$schema.$name" : $name;
+        $dbh->do("DROP TABLE $fullname CASCADE");
         $dbh->{Warn} = 1;
         $dbh->commit();
     }