[monitoring-plugins] tests: testCmd has own timeout which overwrites ...

Sven Nierlein git at monitoring-plugins.org
Fri Jun 13 16:20:09 CEST 2014


 Module: monitoring-plugins
 Branch: master
 Commit: c5a6c5136a2a7e629907b04a63dff059603bdb09
 Author: Sven Nierlein <Sven.Nierlein at consol.de>
   Date: Thu Jun 12 13:56:48 2014 +0200
    URL: https://www.monitoring-plugins.org/repositories/monitoring-plugins/commit/?id=c5a6c51

tests: testCmd has own timeout which overwrites local one

so add configurable/optional timeout to testCmd.

Signed-off-by: Sven Nierlein <Sven.Nierlein at consol.de>

---

 NPTest.pm                  |  5 +++--
 plugins/tests/check_http.t | 24 +++++++++---------------
 2 files changed, 12 insertions(+), 17 deletions(-)

diff --git a/NPTest.pm b/NPTest.pm
index 2baed0b..e04ebba 100644
--- a/NPTest.pm
+++ b/NPTest.pm
@@ -627,12 +627,13 @@ sub only_output {
 }
 
 sub testCmd {
-	my $class = shift;
+	my $class   = shift;
 	my $command = shift or die "No command passed to testCmd";
+	my $timeout = shift || 120;
 	my $object = $class->new;
 
 	local $SIG{'ALRM'} = sub { die("timeout in command: $command"); };
-	alarm(120); # no test should take longer than 120 seconds
+	alarm($timeout); # no test should take longer than 120 seconds
 
 	my $output = `$command`;
 	$object->return_code($? >> 8);
diff --git a/plugins/tests/check_http.t b/plugins/tests/check_http.t
index 2c89beb..c40bb07 100755
--- a/plugins/tests/check_http.t
+++ b/plugins/tests/check_http.t
@@ -392,27 +392,21 @@ sub run_common_tests {
 		skip "This doesn't seems to work all the time", 1 unless ($ENV{HTTP_EXTERNAL});
 		$cmd = "$command -f follow -u /redir_external -t 5";
 		eval {
-			local $SIG{ALRM} = sub { die "alarm\n" };
-			alarm(2);
-			$result = NPTest->testCmd( $cmd );
-			alarm(0); };
-		is( $@, "alarm\n", $cmd );
+			$result = NPTest->testCmd( $cmd, 2 );
+		};
+		like( $@, "/timeout in command: $cmd/", $cmd );
 	}
 
 	$cmd = "$command -u /timeout -t 5";
 	eval {
-		local $SIG{ALRM} = sub { die "alarm\n" };
-		alarm(2);
-		$result = NPTest->testCmd( $cmd );
-		alarm(0); };
-	is( $@, "alarm\n", $cmd );
+		$result = NPTest->testCmd( $cmd, 2 );
+	};
+	like( $@, "/timeout in command: $cmd/", $cmd );
 
 	$cmd = "$command -f follow -u /redir_timeout -t 2";
 	eval {
-		local $SIG{ALRM} = sub { die "alarm\n" };
-		alarm(5);
-		$result = NPTest->testCmd( $cmd );
-		alarm(0); };
-	isnt( $@, "alarm\n", $cmd );
+		$result = NPTest->testCmd( $cmd, 5 );
+	};
+	is( $@, "", $cmd );
 
 }



More information about the Commits mailing list