summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--NPTest.pm53
-rw-r--r--doc/developer-guidelines.sgml47
-rw-r--r--plugins/t/check_disk.t14
3 files changed, 88 insertions, 26 deletions
diff --git a/NPTest.pm b/NPTest.pm
index 440e734..4036a9d 100644
--- a/NPTest.pm
+++ b/NPTest.pm
@@ -44,23 +44,17 @@ default via the C<use NPTest;> statement.
44 44
45=over 45=over
46 46
47=item C<getTestParameter(...)> 47=item getTestParameter( "ENV_VARIABLE", $brief_description, $default )
48 48
49A flexible and user override-able method of collecting, storing and 49This function allows the test harness
50retrieving test parameters. This function allows the test harness
51developer to interactively request test parameter information from the 50developer to interactively request test parameter information from the
52user, when the no means of obtaining the information automatically has 51user. The user can accept the developer's default value or reply "none"
53been successful. The user is provided with the option of accepting 52which will then be returned as "" for the test to skip if appropriate.
54test harness developer's default value for the parameter, if a suggested 53
55default is provided. 54Responses are stored in an external, file-based
56 55cache so subsequent test runs will use these values. The user is able
57User supplied responses are stored in an external (file-based) 56to change the values by amending the values in the file /var/tmp/NPTest.pm,
58cache. These values are retrieved on subsequent runs alleviating the 57or by setting the appropriate environment variable before running the test.
59user of reconfirming the previous entered responses. The user is able
60to override the value of a parameter on any given run by setting the
61associated environment variable. These environment variable based
62overrides are not stored in the cache, allowing one-time and what-if
63based tests on the command line without polluting the cache.
64 58
65The option exists to store parameters in a scoped means, allowing a 59The option exists to store parameters in a scoped means, allowing a
66test harness to a localise a parameter should the need arise. This 60test harness to a localise a parameter should the need arise. This
@@ -73,7 +67,7 @@ called "check_disk.t" requesting the parameter "mountpoint_valid", the
73cache is first searched for "check_disk"/"mountpoint_valid", if this 67cache is first searched for "check_disk"/"mountpoint_valid", if this
74fails, then a search is conducted for "mountpoint_valid". 68fails, then a search is conducted for "mountpoint_valid".
75 69
76The facilitate quick testing setup, it is possible to accept all the 70To facilitate quick testing setup, it is possible to accept all the
77developer provided defaults by setting the environment variable 71developer provided defaults by setting the environment variable
78"NPTEST_ACCEPTDEFAULT" to "1" (or any other perl truth value). Note 72"NPTEST_ACCEPTDEFAULT" to "1" (or any other perl truth value). Note
79that, such defaults are not stored in the cache, as there is currently 73that, such defaults are not stored in the cache, as there is currently
@@ -306,7 +300,16 @@ sub skipMissingCmd
306 300
307sub getTestParameter 301sub getTestParameter
308{ 302{
309 my( $param, $envvar, $default, $brief, $scoped ) = @_; 303 my( $param, $envvar, $default, $brief, $scoped );
304 my $new_style;
305 if (scalar @_ == 3) {
306 ($param, $brief, $default) = @_;
307 $envvar = $param;
308 $new_style = 1;
309 } else {
310 ( $param, $envvar, $default, $brief, $scoped ) = @_;
311 $new_style = 0;
312 }
310 313
311 # Apply default values for optional arguments 314 # Apply default values for optional arguments
312 $scoped = ( defined( $scoped ) && $scoped ); 315 $scoped = ( defined( $scoped ) && $scoped );
@@ -319,8 +322,13 @@ sub getTestParameter
319 } 322 }
320 323
321 my $cachedValue = SearchCache( $param, $testharness ); 324 my $cachedValue = SearchCache( $param, $testharness );
322 if ( defined( $cachedValue ) && $cachedValue ) 325 if ( defined( $cachedValue ) )
323 { 326 {
327 # This save required to convert to new style because the key required is
328 # changing to the environment variable
329 if ($new_style == 0) {
330 SetCacheParameter( $envvar, undef, $cachedValue );
331 }
324 return $cachedValue; 332 return $cachedValue;
325 } 333 }
326 334
@@ -339,9 +347,9 @@ sub getTestParameter
339 print STDERR "\n"; 347 print STDERR "\n";
340 print STDERR "Test Harness : $testharness\n"; 348 print STDERR "Test Harness : $testharness\n";
341 print STDERR "Test Parameter : $param\n"; 349 print STDERR "Test Parameter : $param\n";
342 print STDERR "Environment Variable : $envvar\n"; 350 print STDERR "Environment Variable : $envvar\n" if ($param ne $envvar);
343 print STDERR "Brief Description : $brief\n"; 351 print STDERR "Brief Description : $brief\n";
344 print STDERR "Enter value ", ($defaultValid ? "[${default}]" : "[]"), " => "; 352 print STDERR "Enter value (or 'none') ", ($defaultValid ? "[${default}]" : "[]"), " => ";
345 $userResponse = <STDIN>; 353 $userResponse = <STDIN>;
346 $userResponse = "" if ! defined( $userResponse ); # Handle EOF 354 $userResponse = "" if ! defined( $userResponse ); # Handle EOF
347 chomp( $userResponse ); 355 chomp( $userResponse );
@@ -353,6 +361,10 @@ sub getTestParameter
353 361
354 print STDERR "\n"; 362 print STDERR "\n";
355 363
364 if ($userResponse =~ /^(na|none)$/) {
365 $userResponse = "";
366 }
367
356 # define all user responses at global scope 368 # define all user responses at global scope
357 SetCacheParameter( $param, ( $scoped ? $testharness : undef ), $userResponse ); 369 SetCacheParameter( $param, ( $scoped ? $testharness : undef ), $userResponse );
358 370
@@ -378,6 +390,7 @@ sub SearchCache
378 { 390 {
379 return $CACHE{$param}; 391 return $CACHE{$param};
380 } 392 }
393 return undef; # Need this to say "nothing found"
381} 394}
382 395
383sub SetCacheParameter 396sub SetCacheParameter
diff --git a/doc/developer-guidelines.sgml b/doc/developer-guidelines.sgml
index 433a302..3c37e5c 100644
--- a/doc/developer-guidelines.sgml
+++ b/doc/developer-guidelines.sgml
@@ -572,6 +572,53 @@
572 </section> 572 </section>
573</section> 573</section>
574 574
575<section id="Testcases"><title>Test cases</title>
576<para>
577Tests are the best way of knowing if the plugins work as expected. Please
578create and update test cases where possible.
579</para>
580
581<para>
582To run a test, from the top level directory, run "make test". This will run
583all the current tests and report an overall success rate.
584</para>
585
586<para>
587See the <ulink url="http://tinderbox.altinity.org">Nagios Plugins Tinderbox server</ulink>
588for the daily test results.
589</para>
590
591<section><title>Test cases for plugins</title>
592<para>These use perl's Test::More. To do a one time test, run "cd plugins && perl t/check_disk.t".
593</para>
594
595<para>There will somtimes be failures seen in this output which are known failures that
596need to be fixed. As long as the return code is 0, it will be reported as "test pass".
597(If you have a fix so that the specific test passes, that will be gratefully received!)
598</para>
599
600<para>
601If you want a summary test, run: "cd plugins && perl -MTest::Harness -e 'runtests(@ARGV)' t/check_disk.t".
602This runs the test in a summary format.
603</para>
604
605<para>
606For a good and amusing tutorial on using Test::More, see this
607<ulink url="http://search.cpan.org/~mschwern/Test-Simple-0.62/lib/Test/Tutorial.pod">
608link</ulink>
609</para>
610
611</section>
612
613<section><title>Testing the C library functions</title>
614<para>
615Will be looking at using libtap, which is utilised by the FreeBSD team. The output is
616based on perl's TAP (Test Anything Protocol) format, so that Test::Harness will understand
617results. This is still in planning stages.
618</para>
619</section>
620
621</section>
575<section id="CodingGuidelines"><title>Coding guidelines</title> 622<section id="CodingGuidelines"><title>Coding guidelines</title>
576 <para>See <ulink url="http://www.gnu.org/prep/standards_toc.html">GNU 623 <para>See <ulink url="http://www.gnu.org/prep/standards_toc.html">GNU
577 Coding standards</ulink> for general guidelines.</para> 624 Coding standards</ulink> for general guidelines.</para>
diff --git a/plugins/t/check_disk.t b/plugins/t/check_disk.t
index d35d02c..385865f 100644
--- a/plugins/t/check_disk.t
+++ b/plugins/t/check_disk.t
@@ -6,7 +6,7 @@
6# 6#
7 7
8use strict; 8use strict;
9use Test::More tests => 26; 9use Test::More;
10use NPTest; 10use NPTest;
11use POSIX qw(ceil floor); 11use POSIX qw(ceil floor);
12 12
@@ -16,12 +16,14 @@ my $warningOutput = '/^DISK WARNING - /';
16 16
17my $result; 17my $result;
18 18
19my $mountpoint_valid = getTestParameter( "mountpoint_valid", "NP_MOUNTPOINT_VALID", "/", 19my $mountpoint_valid = getTestParameter( "NP_MOUNTPOINT_VALID", "Path to valid mountpoint", "/");
20 "The path to a valid mountpoint" ); 20my $mountpoint2_valid = getTestParameter( "NP_MOUNTPOINT2_VALID", "Path to another valid mountpoint. Must be different from 1st one", "/var");
21
22my $mountpoint2_valid = getTestParameter( "mountpoint2_valid", "NP_MOUNTPOINT2_VALID", "/var",
23 "The path to another valid mountpoint. Must be different from 1st one." );
24 21
22if ($mountpoint_valid eq "" or $mountpoint2_valid eq "") {
23 plan skip_all => "Need 2 mountpoints to test";
24} else {
25 plan tests => 26;
26}
25 27
26$result = NPTest->testCmd( 28$result = NPTest->testCmd(
27 "./check_disk -w 1% -c 1% -p $mountpoint_valid -w 1% -c 1% -p $mountpoint2_valid" 29 "./check_disk -w 1% -c 1% -p $mountpoint_valid -w 1% -c 1% -p $mountpoint2_valid"