[check_postgres] [commit] Testing tweaks.

check_postgres at bucardo.org check_postgres at bucardo.org
Wed Apr 22 20:33:15 UTC 2009


Committed by Greg Sabino Mullane <greg at endpoint.com>

Testing tweaks.

---
 check_postgres.pl |    4 +-
 t/02_version.t    |    4 +-
 t/CP_Testing.pm   |   91 +++++++++++++++++++++++++++++++++-------------------
 3 files changed, 62 insertions(+), 37 deletions(-)

diff --git a/check_postgres.pl b/check_postgres.pl
index dcdf7bc..1ebe1b5 100755
--- a/check_postgres.pl
+++ b/check_postgres.pl
@@ -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});
diff --git a/t/02_version.t b/t/02_version.t
index 6b33757..ed0f9e9 100644
--- a/t/02_version.t
+++ b/t/02_version.t
@@ -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();
 
diff --git a/t/CP_Testing.pm b/t/CP_Testing.pm
index ab2e008..6ff3028 100644
--- a/t/CP_Testing.pm
+++ b/t/CP_Testing.pm
@@ -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;
-- 
1.6.0.5



More information about the Check_postgres mailing list