diff options
author | Greg Sabino Mullane | 2009-04-14 17:47:26 +0000 |
---|---|---|
committer | Greg Sabino Mullane | 2009-04-14 17:47:26 +0000 |
commit | 5c83036f84f8a3579cadc3e3080142918a500105 (patch) | |
tree | 6303c20c4f5c667d8c44455e84a9d447ecb5730c | |
parent | b584b2b062be8f94f4321f142ff8b4cee24ae18e (diff) |
Testing tweaks.
-rwxr-xr-x | check_postgres.pl | 4 | ||||
-rw-r--r-- | t/02_version.t | 4 | ||||
-rw-r--r-- | t/CP_Testing.pm | 91 |
3 files changed, 62 insertions, 37 deletions
diff --git a/check_postgres.pl b/check_postgres.pl index dcdf7bc31..1ebe1b5db 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 6b337570f..ed0f9e9d1 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 ab2e00891..6ff302836 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; |