[check_postgres] [commit] Test for the 'version' action, plus some fixes revealed by same.

check_postgres at bucardo.org check_postgres at bucardo.org
Fri Feb 27 03:57:17 UTC 2009


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

Test for the 'version' action, plus some fixes revealed by same.

---
 check_postgres.pl |   18 ++++----
 t/02_version.t    |  113 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 t/CP_Testing.pm   |   38 ++++++++++++++++--
 3 files changed, 156 insertions(+), 13 deletions(-)

diff --git a/check_postgres.pl b/check_postgres.pl
index 015f89e..8c8176b 100755
--- a/check_postgres.pl
+++ b/check_postgres.pl
@@ -153,7 +153,7 @@ our %msg = (
 	'range-badpercsize'  => q{Invalid '$1' option: must be a size or a percentage},
 	'range-badsize'      => q{Invalid size for '$1' option},
 	'range-badtype'      => q{validate_range called with unknown type '$1'},
-	'range-badversion'   => q{Invalid string for '1' option: $2},
+	'range-badversion'   => q{Invalid string for '$1' option: $2},
 	'range-cactionly'    => q{This action is for cacti use only and takes not warning or critical arguments},
 	'range-int'          => q{Invalid argument for '$1' option: must be an integer},
 	'range-int-pos'      => q{Invalid argument for '$1' option: must be a positive integer},
@@ -244,8 +244,8 @@ our %msg = (
 	'vac-nomatch-a'      => q{No matching tables have ever been analyzed},
 	'vac-nomatch-v'      => q{No matching tables have ever been vacuumed},
 	'version'            => q{version $1},
-	'version-fail'       => q{version $1, but expected $1},
-	'version-madmrtg'    => q{Invalid mrtg version argument},
+	'version-fail'       => q{version $1, but expected $2},
+	'version-badmrtg'    => q{Invalid mrtg version argument},
 	'version-ok'         => q{version $1},
 },
 'fr' => {
@@ -280,7 +280,7 @@ our %msg = (
 	'die-noset'          => q{Ne peut pas exécuter « $1 » $2 n'est pas activé},
 	'die-nosetting'      => q{N'a pas pu récupérer le paramètre « $1 »},
 	'diskspace-df'       => q{N'a pas pu trouver l'exécutable /bin/df},
-	'diskspace-fail'     => q{Résultat invalide pour la commande « $1 » : $1},
+	'diskspace-fail'     => q{Résultat invalide pour la commande « $1 » : $2},
 	'diskspace-msg'      => q{Le système de fichiers $1 monté sur $2 utilise $3 sur $4 ($5%)},
 	'diskspace-nodata'   => q{N'a pas pu déterminer data_directory : êtes-vous connecté en tant que super-utilisateur ?},
 	'diskspace-nodir'    => q{N'a pas pu trouver le répertoire des données « $1 »},
@@ -328,7 +328,7 @@ our %msg = (
 	'range-badpercsize'  => q{Option « $1 » invalide : doit être une taille ou un pourcentage},
 	'range-badsize'      => q{Taille invalide pour l'option « $1 »},
 	'range-badtype'      => q{validate_range appelé avec un type inconnu « $1 »},
-	'range-badversion'   => q{Chaîne invalide pour l'option « 1 » : $2},
+	'range-badversion'   => q{Chaîne invalide pour l'option « $1 » : $2},
 	'range-cactionly'    => q{Cette action est pour cacti seulement et ne prend pas les arguments warning et critical},
 	'range-int'          => q{Argument invalide pour l'option « $1 » : doit être un entier},
 	'range-int-pos'      => q{Argument invalide pour l'option « $1 » : doit être un entier positif},
@@ -418,8 +418,8 @@ our %msg = (
 	'vac-nomatch-a'      => q{Aucune des tables correspondantes n'a eu d'opération ANALYZE},
 	'vac-nomatch-v'      => q{Aucune des tables correspondantes n'a eu d'opération VACUUM},
 	'version'            => q{version $1},
-	'version-fail'       => q{version $1, alors que la version attendue est $1},
-	'version-madmrtg'    => q{Argument invalide pour la version de mrtg},
+	'version-fail'       => q{version $1, alors que la version attendue est $2},
+	'version-badmrtg'    => q{Argument invalide pour la version de mrtg},
 	'version-ok'         => q{version $1},
 },
 'de' => {
@@ -1826,10 +1826,10 @@ sub validate_range {
 	}
 	elsif ('version' eq $type) {
 		my $msg = msg('range-version');
-		if (length $warning and $warning !~ /^\d+\.\d\.?[\d\w]*$/) {
+		if (length $warning and $warning !~ /^\d+\.\d+\.?[\d\w]*$/) {
 			ndie msg('range-badversion', 'warning', $msg);
 		}
-		if (length $critical and $critical !~ /^\d+\.\d\.?[\d\w]*$/) {
+		if (length $critical and $critical !~ /^\d+\.\d+\.?[\d\w]*$/) {
 			ndie msg('range-badversion', 'critical', $msg);
 		}
 		if (! length $critical and ! length $warning) {
diff --git a/t/02_version.t b/t/02_version.t
new file mode 100644
index 0000000..e5f2e10
--- /dev/null
+++ b/t/02_version.t
@@ -0,0 +1,113 @@
+#!perl
+
+## Test the "version" action
+
+use strict;
+use warnings;
+use Data::Dumper;
+use DBI;
+use Cwd;
+use Test::More tests => 28;
+use lib 't','.';
+use CP_Testing;
+
+use vars qw/$dbh $dbh2 $SQL $count $host $t $result $info/;
+
+my $cp = CP_Testing->new();
+
+$dbh = $cp->test_database_handle();
+
+my $S = q{Action 'version'};
+
+$t=qq{$S fails when called with an invalid option};
+like ($cp->run('version', 'foobar=12'), qr{^\s*Usage:}, $t);
+
+$t=qq{$S fails when called with MRTG but no option};
+like ($cp->run('version', '--output=mrtg'), qr{ERROR: Invalid mrtg}, $t);
+
+$t=qq{$S fails when called with MRTG and a bad argument};
+like ($cp->run('version', '--output=mrtg --mrtg=foobar'), qr{ERROR: Invalid mrtg}, $t);
+
+$t=qq{$S fails when called without warning or critical};
+like ($cp->run('version', ''), qr{Must provide}, $t);
+
+$t=qq{$S fails when called without invalid warning};
+like ($cp->run('version', '-w foo'), qr{ERROR: Invalid string}, $t);
+
+$t=qq{$S fails when called without invalid critical};
+like ($cp->run('version', '-c foo'), qr{ERROR: Invalid string}, $t);
+
+$t=qq{$S gives correct output for warning on two-part version};
+like ($cp->run('version', '-w 5.2'), qr{POSTGRES_VERSION WARNING: .+expected 5.2}, $t);
+
+$t=qq{$S gives correct output for warning on three-part version};
+like ($cp->run('version', '-w 5.2.1'), qr{POSTGRES_VERSION WARNING: .+expected 5.2.1}, $t);
+
+$t=qq{$S gives correct output for critical on two-part version};
+like ($cp->run('version', '-c 6.10'), qr{POSTGRES_VERSION CRITICAL: .+expected 6.10}, $t);
+
+$t=qq{$S gives correct output for critical on three-part version};
+like ($cp->run('version', '-c 6.10.33'), qr{POSTGRES_VERSION CRITICAL: .+expected 6.10.33}, $t);
+
+## Now to pull some trickery
+$cp->fake_version('foobar');
+
+$t=qq{$S gives correct output on invalid version() parse};
+like ($cp->run('version', '-c 8.7'), qr{POSTGRES_VERSION UNKNOWN: .+Invalid query returned}, $t);
+
+$cp->fake_version('7.8.12');
+
+$t=qq{$S gives correct output for two-part version warning};
+like ($cp->run('version', '-w 7.8'), qr{POSTGRES_VERSION OK: .+version 7.8.12}, $t);
+
+$t=qq{$S gives correct output for two-part version warning};
+like ($cp->run('version', '-w 5.8'), qr{POSTGRES_VERSION WARNING: .+version 7.8.12}, $t);
+
+$t=qq{$S gives correct output for two-part version warning};
+like ($cp->run('version', '-w 7.9'), qr{POSTGRES_VERSION WARNING: .+version 7.8.12}, $t);
+
+$t=qq{$S gives correct output for three-part version warning};
+like ($cp->run('version', '-w 7.8.12'), qr{POSTGRES_VERSION OK: .+version 7.8.12}, $t);
+
+$t=qq{$S gives correct output for three-part version warning};
+like ($cp->run('version', '-w 7.8.11'), qr{POSTGRES_VERSION WARNING: .+version 7.8.12}, $t);
+
+$t=qq{$S gives correct output for matching three-part version warning};
+like ($cp->run('version', '-w 7.8.13'), qr{POSTGRES_VERSION WARNING: .+version 7.8.12}, $t);
+
+$t=qq{$S gives correct output for matching three-part version warning};
+like ($cp->run('version', '-w 7.9.13'), qr{POSTGRES_VERSION WARNING: .+version 7.8.12}, $t);
+
+$t=qq{$S gives correct output for two-part version critical};
+like ($cp->run('version', '-c 7.8'), qr{POSTGRES_VERSION OK: .+version 7.8.12}, $t);
+
+$t=qq{$S gives correct output for two-part version critical};
+like ($cp->run('version', '-c 5.8'), qr{POSTGRES_VERSION CRITICAL: .+version 7.8.12}, $t);
+
+$t=qq{$S gives correct output for two-part version critical};
+like ($cp->run('version', '-c 7.9'), qr{POSTGRES_VERSION CRITICAL: .+version 7.8.12}, $t);
+
+$t=qq{$S gives correct output for three-part version critical};
+like ($cp->run('version', '-c 7.8.12'), qr{POSTGRES_VERSION OK: .+version 7.8.12}, $t);
+
+$t=qq{$S gives correct output for three-part version critical};
+like ($cp->run('version', '-c 7.8.11'), qr{POSTGRES_VERSION CRITICAL: .+version 7.8.12}, $t);
+
+$t=qq{$S gives correct output for matching three-part version critical};
+like ($cp->run('version', '-c 7.8.13'), qr{POSTGRES_VERSION CRITICAL: .+version 7.8.12}, $t);
+
+$t=qq{$S gives correct output for matching three-part version critical};
+like ($cp->run('version', '-c 7.9.13'), qr{POSTGRES_VERSION CRITICAL: .+version 7.8.12}, $t);
+
+$t=qq{$S gives correct output for MRTG output};
+like ($cp->run('version', '--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('version', '--output=MRTG --mrtg=7.8'), qr{^1\n0\n\n7.8.12\n}, $t);
+
+$t=qq{$S gives correct output for MRTG output};
+like ($cp->run('version', '--output=MRTG --mrtg=7.8.12'), qr{^1\n0\n\n7.8.12\n}, $t);
+
+$cp->reset_path();
+
+exit;
diff --git a/t/CP_Testing.pm b/t/CP_Testing.pm
index 1bf8c45..af1fa2f 100644
--- a/t/CP_Testing.pm
+++ b/t/CP_Testing.pm
@@ -218,8 +218,8 @@ sub create_fake_pg_table {
 	## Dangerous: do not try this at home!
 
 	my $self = shift;
-	my $name = shift;
-	my $dbh = $self->{dbh};
+	my $name = shift || die;
+	my $dbh = $self->{dbh} || die;
 	my $dbuser = $self->{testuser} || die;
 	{
 		local $dbh->{Warn};
@@ -235,8 +235,8 @@ sub create_fake_pg_table {
 sub remove_fake_pg_table {
 
 	my $self = shift;
-	my $name = shift;
-	my $dbh = $self->{dbh};
+	my $name = shift || die;
+	my $dbh = $self->{dbh} || die;
 	my $dbuser = $self->{testuser} || die;
 	{
 		local $dbh->{Warn};
@@ -259,4 +259,34 @@ sub table_exists {
 
 } ## end of table_exists
 
+sub 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 'PostgreSQL $version on fakefunction for check_postgres.pl testing'::text;
+\$\$
+});
+	$dbh->do("ALTER USER $dbuser SET search_path = public, pg_catalog");
+	$dbh->commit();
+
+} ## end of fake version
+
+sub reset_path {
+
+	my $self = shift;
+	my $dbh = $self->{dbh} || die;
+	my $dbuser = $self->{testuser} || die;
+	$dbh->do("ALTER USER $dbuser SET search_path = public");
+	$dbh->commit();
+
+} ## end of reset_path
+
 1;
-- 
1.6.0.5



More information about the Check_postgres mailing list