Better tests for non-superuser in txn_idle and friends.
authorGreg Sabino Mullane <[email protected]>
Mon, 4 Jul 2011 18:37:04 +0000 (14:37 -0400)
committerGreg Sabino Mullane <[email protected]>
Mon, 4 Jul 2011 18:37:04 +0000 (14:37 -0400)
Attempts to fix bug 59.

check_postgres.pl
t/02_txn_idle.t
t/CP_Testing.pm

index 6a8863e7b2901b05b3714a2e83157f69b600aeb3..3be87c1ae7e09aec995dc6bf30c74703809e4dd1 100755 (executable)
@@ -7162,7 +7162,7 @@ sub check_txn_idle {
     my $thing = shift || msg('transactions');
     my $perf  = shift || msg('txn-time');
     my $start = shift || 'query_start';
-    my $clause = shift || q{current_query = '<IDLE> in transaction'};
+    my $clause = shift || q{current_query ~ '^<'};
 
     ## Extract the warning and critical seconds and counts.
     ## If not given, items will be an empty string
@@ -7196,25 +7196,30 @@ sub check_txn_idle {
         ## Skip if we don't care about this database
         next if skip_item($r->{datname});
 
-        ## Detect cases where pg_stat_activity is not fully populated
-        if (length $r->{xact_start} and $r->{xact_start} !~ /\d/o) {
-            ## Perhaps this is a non-superuser?
-            if ($r->{current_query} =~ /insufficient/) {
-                add_unknown msg('psa-nosuper');
-                return;
-            }
+        ## We do a lot of filtering based on the current_query
+        my $cq = $r->{current_query};
 
-            ## Perhaps stats_command_string / track_activities is off?
-            if ($r->{current_query} =~ /disabled/) {
-                add_unknown msg('psa-disabled');
-                return;
-            }
+        ## Return unknown if we cannot see because we are a non-superuser?
+        if ($cq =~ /insufficient/o) {
+            add_unknown msg('psa-nosuper');
+            return;
+        }
+
+        ## Return unknown if stats_command_string / track_activities is off?
+        if ($cq =~ /disabled/o) {
+            add_unknown msg('psa-disabled');
+            return;
+        }
 
-            ## Something else is going on
+        ## Detect other cases where pg_stat_activity is not fully populated
+        if (length $r->{xact_start} and $r->{xact_start} !~ /\d/o) {
             add_unknown msg('psa-noexact');
             return;
         }
 
+        ## Filter out based on the action
+        next if $action eq 'txn_idle' and $cq ne '<IDLE> in transaction';
+
         ## Keep track of the longest overall time
         $maxr = $r if $r->{seconds} >= $maxr->{seconds};
 
index f5a32a89c770a2b85496fd08401e4e9bb60d8f82..598cc561948e7d91161b10e306c3eab9ffb86041 100644 (file)
@@ -6,7 +6,7 @@ use 5.006;
 use strict;
 use warnings;
 use Data::Dumper;
-use Test::More tests => 14;
+use Test::More tests => 15;
 use lib 't','.';
 use CP_Testing;
 
@@ -78,6 +78,11 @@ sleep(1);
 $t = qq{$S identifies idle using '1 for 2s'};
 like ($cp->run(q{-w '1 for 2s'}), qr{1 idle transactions longer than 2s, longest: \d+s}, $t);
 
+$t = qq{$S returns an unknown if running as a non-superuser};
+my $olduser = $cp->{testuser};
+$cp->{testuser} = 'powerless_pete';
+like ($cp->run('-w 0'), qr{^$label UNKNOWN: .+superuser}, $t);
+
 $idle_dbh->commit;
 
 exit;
index ee9e9e68e8e40edd47d2139afeef369515d881bd..f9d71903e9b76c322964c2d5feca2cf2ecabc524 100644 (file)
@@ -24,6 +24,7 @@ sub new {
         started  => time(),
         dbdir    => $arg->{dbdir}    || 'test_database_check_postgres',
         testuser => $arg->{testuser} || 'check_postgres_testing',
+        testuser2 => $arg->{testuser2} || 'powerless_pete',
     };
     if (exists $arg->{default_action}) {
         $self->{action} = $arg->{default_action};
@@ -228,7 +229,18 @@ sub test_database_handle {
             if ($res !~ /$newuser/) {
                 $COM = qq{psql -d template1 -q -h "$host" -c "CREATE USER $newuser"};
                 system $COM;
-                $SQL = q{UPDATE pg_shadow SET usesuper='t' WHERE usename = 'check_postgres_testing'};
+                $SQL = q{UPDATE pg_shadow SET usesuper='t' WHERE usename = '$newuser'};
+                $COM = qq{psql -d postgres -q -h "$host" -c "$SQL"};
+                system $COM;
+            }
+
+            $newuser = $self->{testuser2};
+            $SQL = qq{SELECT * FROM pg_user WHERE usename = '$newuser'};
+            $res = qx{psql -Ax -qt -d template1 -q -h "$host" -c "$SQL"};
+            if ($res !~ /$newuser/) {
+                $COM = qq{psql -d template1 -q -h "$host" -c "CREATE USER $newuser"};
+                system $COM;
+                $SQL = q{UPDATE pg_shadow SET usesuper='t' WHERE usename = '$newuser'};
                 $COM = qq{psql -d postgres -q -h "$host" -c "$SQL"};
                 system $COM;
             }
@@ -308,6 +320,12 @@ sub test_database_handle {
         $count = $sth->fetchall_arrayref()->[0][0];
         if (!$count) {
             $dbh->do("CREATE USER $dbuser SUPERUSER");
+        }
+               my $user2 = $self->{testuser2};
+        $sth->execute($user2);
+        $count = $sth->fetchall_arrayref()->[0][0];
+        if (!$count) {
+            $dbh->do("CREATE USER $user2");
         }
     }
     $dbh->do('CREATE DATABASE beedeebeedee');