Code

`host_name' is a NULL pointer if the "-I" flag is used instead of "-H",
[nagiosplug.git] / NPTest.pm
index 4036a9d0e4a04520c8d42fe66c48849eae430632..d4542ddc2cc5348c4b41a1b13b3e879b6e53be30 100644 (file)
--- a/NPTest.pm
+++ b/NPTest.pm
@@ -46,15 +46,21 @@ default via the C<use NPTest;> statement.
 
 =item getTestParameter( "ENV_VARIABLE", $brief_description, $default )
 
+$default is optional.
+
 This function allows the test harness
 developer to interactively request test parameter information from the
 user. The user can accept the developer's default value or reply "none"
 which will then be returned as "" for the test to skip if appropriate.
 
-Responses are stored in an external, file-based
-cache so subsequent test runs will use these values. The user is able
-to change the values by amending the values in the file /var/tmp/NPTest.pm,
-or by setting the appropriate environment variable before running the test.
+If a parameter needs to be entered and the test is run without a tty 
+attached (such as a cronjob), the parameter will be assigned as if it 
+was "none". Tests can check for the parameter and skip if not set.
+
+Responses are stored in an external, file-based cache so subsequent test
+runs will use these values. The user is able to change the values by
+amending the values in the file /var/tmp/NPTest.cache, or by setting
+the appropriate environment variable before running the test.
 
 The option exists to store parameters in a scoped means, allowing a
 test harness to a localise a parameter should the need arise. This
@@ -302,7 +308,7 @@ sub getTestParameter
 {
   my( $param, $envvar, $default, $brief, $scoped );
   my $new_style;
-  if (scalar @_ == 3) {
+  if (scalar @_ <= 3) {
        ($param, $brief, $default) = @_;
        $envvar = $param;
        $new_style = 1;
@@ -340,6 +346,9 @@ sub getTestParameter
     return $default;
   }
 
+  # Set "none" if no terminal attached (eg, tinderbox build servers when new variables set)
+  return "" unless (-t STDERR);
+
   my $userResponse = "";
 
   while ( $userResponse eq "" )
@@ -559,7 +568,7 @@ sub TestsFrom
 
   closedir( DIR );
 
-  return @tests;
+  return sort @tests;
 }
 
 # All the new object oriented stuff below
@@ -588,16 +597,33 @@ sub output {
        }
 }
 
+sub perf_output {
+       my $self = shift;
+       $_ = $self->{output};
+       /\|(.*)$/;
+       return $1 || "";
+}
+
+sub only_output {
+       my $self = shift;
+       $_ = $self->{output};
+       /(.*?)\|/;
+       return $1 || "";
+}
+
 sub testCmd {
        my $class = shift;
        my $command = shift or die "No command passed to testCmd";
        my $object = $class->new;
        
        my $output = `$command`;
+       $object->return_code($? >> 8);
+       $_ = $? & 127;
+       if ($_) {
+               die "Got signal $_ for command $command";
+       }
        chomp $output;
-       
        $object->output($output);
-       $object->return_code($? >> 8);
 
        if ($ENV{'NPTEST_DEBUG'}) {
                my ($pkg, $file, $line) = caller(0);