blib/lib/ASNMTAP/Asnmtap/Plugins/NPTest.pm

filter: Statementstmt 38.3% 81 / 211 Branchbran 19.5% 18 / 92 Conditioncond 5.4% 2 / 37 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 58.3% 14 / 24 Podpod 25.0% 4 / 16 totaltotal 29.5% 119 / 403 CC 59 SCAR 48.9
CC
59
Cov
30%
getTestParameter
462.0
TestsFrom
56.0
DetermineTestHarnessDirectory
42.0
CRAP 132.7
« blib/lib/ASNMTAP/Asnmtap/Plugins/Nagios.pm ↑ Summary plugins/templates/check_template-SOAP.pl »
1 package ASNMTAP::Asnmtap::Plugins::NPTest;
2  
3 #
4 # Helper Functions for testing Nagios Plugins
5 #
6  
7 26 require Exporter;
8 26 @ISA = qw(Exporter);
9 26 @EXPORT = qw(getTestParameter checkCmd skipMissingCmd);
10 26 @EXPORT_OK = qw(DetermineTestHarnessDirectory TestsFrom SetCacheFilename);
11  
12 26 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 26 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 26 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 26 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 26 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 26 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 26 ▶use Test;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 26 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 26 $VERSION = do { my @r = (q$Revision: 1.13 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; # must be all one line, for MakeMaker
25  
26 =head1 NAME
27  
28 NPTest - Simplify the testing of Nagios Plugins
29  
30 =head1 DESCRIPTION
31  
32 This modules provides convenience functions to assist in the testing
33 of Nagios Plugins, making the testing code easier to read and write;
34 hopefully encouraging the development of a more complete test suite for
35 the Nagios Plugins. It is based on the patterns of testing seen in the
36 1.4.0 release, and continues to use the L<Test> module as the basis of
37 testing.
38  
39 =head1 FUNCTIONS
40  
41 This module defines three public functions, C<getTestParameter(...)>,
42 C<checkCmd(...)> and C<skipMissingCmd(...)>. These are exported by
43 default via the C<use NPTest;> statement.
44  
45 =over
46  
47 =item getTestParameter( "ENV_VARIABLE", $brief_description, $default )
48  
49 $default is optional.
50  
51 This function allows the test harness
52 developer to interactively request test parameter information from the
53 user. The user can accept the developer's default value or reply "none"
54 which will then be returned as "" for the test to skip if appropriate.
55  
56 If a parameter needs to be entered and the test is run without a tty
57 attached (such as a cronjob), the parameter will be assigned as if it
58 was "none". Tests can check for the parameter and skip if not set.
59  
60 Responses are stored in an external, file-based
61 cache so subsequent test runs will use these values. The user is able
62 to change the values by amending the values in the file /var/tmp/NPTest.pm,
63 or by setting the appropriate environment variable before running the test.
64  
65 The option exists to store parameters in a scoped means, allowing a
66 test harness to a localise a parameter should the need arise. This
67 allows a parameter of the same name to exist in a test harness
68 specific scope, while not affecting the globally scoped parameter. The
69 scoping identifier is the name of the test harness sans the trailing
70 ".t". All cache searches first look to a scoped parameter before
71 looking for the parameter at global scope. Thus for a test harness
72 called "check_disk.t" requesting the parameter "mountpoint_valid", the
73 cache is first searched for "check_disk"/"mountpoint_valid", if this
74 fails, then a search is conducted for "mountpoint_valid".
75  
76 To facilitate quick testing setup, it is possible to accept all the
77 developer provided defaults by setting the environment variable
78 "NPTEST_ACCEPTDEFAULT" to "1" (or any other perl truth value). Note
79 that, such defaults are not stored in the cache, as there is currently
80 no mechanism to edit existing cache entries, save the use of text
81 editor or removing the cache file completely.
82  
83 =item C<testCmd($command)>
84  
85 Call with ASNMTAP::Asnmtap::Plugins::NPTest->testCmd("./check_disk ...."). This returns a NPTest object
86 which you can then run $object->return_code or $object->output against.
87  
88 Testing of results would be done in your test script, not in this module.
89  
90 =item C<checkCmd(...)>
91  
92 This function is obsolete. Use C<testCmd()> instead.
93  
94 This function attempts to encompass the majority of test styles used
95 in testing Nagios Plugins. As each plug-in is a separate command, the
96 typical tests we wish to perform are against the exit status of the
97 command and the output (if any) it generated. Simplifying these tests
98 into a single function call, makes the test harness easier to read and
99 maintain and allows additional functionality (such as debugging) to be
100 provided without additional effort on the part of the test harness
101 developer.
102  
103 It is possible to enable debugging via the environment variable
104 C<NPTEST_DEBUG>. If this environment variable exists and its value in PERL's
105 boolean context evaluates to true, debugging is enabled.
106  
107 The function prototype can be expressed as follows:
108  
109 Parameter 1 : command => DEFINED SCALAR(string)
110 Parameter 2 : desiredExitStatus => ONE OF
111 SCALAR(integer)
112 ARRAYREF(integer)
113 HASHREF(integer,string)
114 UNDEFINED
115 Parameter 3 : desiredOutput => SCALAR(string) OR UNDEFINED
116 Parameter 4 : exceptions => HASH(integer,string) OR UNDEFINED
117 Returns : SCALAR(integer) as defined by Test::ok(...)
118  
119 The function treats the first parameter C<$command> as a command line
120 to execute as part of the test, it is executed only once and its exit
121 status (C<$?E<gt>E<gt>8>) and output are captured.
122  
123 At this point if debugging is enabled the command, its exit status and
124 output are displayed to the tester.
125  
126 C<checkCmd(...)> allows the testing of either the exit status or the
127 generated output or both, not testing either will result in neither
128 the C<Test::ok(...)> or C<Test::skip(...)> functions being called,
129 something you probably don't want. Note that each defined test
130 (C<$desiredExitStatus> and C<$desiredOutput>) results in a invocation
131 of either C<Test::ok(...)> or C<Test::skip(...)>, so remember this
132 when counting the number of tests to place in the C<Test::plan(...)>
133 call.
134  
135 Many Nagios Plugins test network services, some of which may not be
136 present on all systems. To cater for this, C<checkCmd(...)> allows the
137 tester to define exceptions based on the command's exit status. These
138 exceptions are provided to skip tests if the test case developer
139 believes the service is not being provided. For example, if a site
140 does not have a POP3 server, the test harness could map the
141 appropriate exit status to a useful message the person running the
142 tests, telling the reason the test is being skipped.
143  
144 Example:
145  
146 my %exceptions = ( 2 =E<gt> "No POP Server present?" );
147  
148 $t += checkCmd( "./check_pop I<some args>", 0, undef, %exceptions );
149  
150 Thus, in the above example, an exit status of 2 does not result in a
151 failed test case (as the exit status is not the desired value of 0),
152 but a skipped test case with the message "No POP Server present?"
153 given as the reason.
154  
155 Sometimes the exit status of a command should be tested against a set
156 of possible values, rather than a single value, this could especially
157 be the case in failure testing. C<checkCmd(...)> support two methods
158 of testing against a set of desired exit status values.
159  
160 =over
161  
162 =item *
163  
164 Firstly, if C<$desiredExitStatus> is a reference to an array of exit
165 stati, if the actual exit status of the command is present in the
166 array, it is used in the call to C<Test::ok(...)> when testing the
167 exit status.
168  
169 =item *
170  
171 Alternatively, if C<$desiredExitStatus> is a reference to a hash of
172 exit stati (mapped to the strings "continue" or "skip"), similar
173 processing to the above occurs with the side affect of determining if
174 any generated output testing should proceed. Note: only the string
175 "skip" will result in generated output testing being skipped.
176  
177 =back
178  
179 =item C<skipMissingCmd(...)>
180  
181 If a command is missing and the test harness must C<Test::skip()> some
182 or all of the tests in a given test harness this function provides a
183 simple iterator to issue an appropriate message the requested number
184 of times.
185  
186 =back
187  
188 =head1 SEE ALSO
189  
190 L<Test>
191  
192 The rest of the code, as I have only commented on the major public
193 functions that test harness writers will use, not all the code present
194 in this helper module.
195  
196 =head1 AUTHOR
197  
198 Copyright (c) 2005 Peter Bray. All rights reserved.
199  
200 This package is free software and is provided "as is" without express
201 or implied warranty. It may be used, redistributed and/or modified
202 under the same terms as the Nagios Plugins release.
203  
204 =cut
205  
206 #
207 # Package Scope Variables
208 #
209  
210 26 my( %CACHE ) = ();
211  
212 # I'm not really sure wether to house a site-specific cache inside
213 # or outside of the extracted source / build tree - lets default to outside
214 my( $CACHEFILENAME ) = ( exists( $ENV{'NPTEST_CACHE'} ) && $ENV{'NPTEST_CACHE'} )
215 26 ▶ ? $ENV{'NPTEST_CACHE'} : "/var/tmp/NPTest.cache"; # "../Cache.pdd";
Branch: 1 branch
BranchTrueFalseTotal
exists $ENV{'NPTEST_CACHE'} && $ENV{'NPTEST_CACHE'} ? : 0 26 26
Condition33%1/3
exists $ENV{"NPTEST_CACHE"} and $ENV{"NPTEST_CACHE"}
!ll&&!rl&&r
2600
MC/DC0%0/2
exists $ENV{"NPTEST_CACHE"} and $ENV{"NPTEST_CACHE"}
exists $ENV{"NPTEST_CACHE"} $ENV{"NPTEST_CACHE"}
Truth table33%1/3
A and B
A= exists $ENV{"NPTEST_CACHE"}B= $ENV{"NPTEST_CACHE"}
ABresult
0X0
100
111
216  
217 #
218 # Testing Functions
219 #
220  
221 sub checkCmd
222 95 ▶{
Subroutine checkCmd: called CC=12 SCAR=31.8
Pod documented
223 95 my( $command, $desiredExitStatus, $desiredOutput, %exceptions ) = @_;
224  
225 95 my $result = ASNMTAP::Asnmtap::Plugins::NPTest->testCmd($command);
226  
227 95 my $output = $result->output;
228 95 my $exitStatus = $result->return_code;
229  
230 95 ▶ $output = "" unless defined( $output );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $output 0 95 95
231 95 chomp( $output );
232  
233 95 my $testStatus;
234  
235 95 my $testOutput = "continue";
236  
237 95 ▶ if ( defined( $desiredExitStatus ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $desiredExitStatus) 95 0 95
238 {
239 95 ▶ if ( ref $desiredExitStatus eq "ARRAY" )
Branch: 2 branches
BranchTrueFalseTotal
if (ref $desiredExitStatus eq 'ARRAY') { } 10 85 95
elsif (ref $desiredExitStatus eq 'HASH') { } 0 85 85
240 {
241 10 ▶ if ( scalar( grep { $_ == $exitStatus } @{$desiredExitStatus} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar grep({$_ == $exitStatus;} @{$desiredExitStatus;})) { } 10 0 10
242 {
243 10 $desiredExitStatus = $exitStatus;
244 }
245 else
246 {
247 0 $desiredExitStatus = -1;
248 }
249 }
250 elsif ( ref $desiredExitStatus eq "HASH" )
251 {
252 0 ▶ if ( exists( ${$desiredExitStatus}{$exitStatus} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists do { $desiredExitStatus }->{$exitStatus}) { } 0 0 0
253 {
254 0 ▶ if ( defined( ${$desiredExitStatus}{$exitStatus} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined do { $desiredExitStatus }->{$exitStatus}) 0 0 0
255 {
256 0 $testOutput = ${$desiredExitStatus}{$exitStatus};
257 }
258 0 $desiredExitStatus = $exitStatus;
259 }
260 else
261 {
262 0 $desiredExitStatus = -1;
263 }
264 }
265  
266 95 ▶ if ( %exceptions && exists( $exceptions{$exitStatus} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (%exceptions and exists $exceptions{$exitStatus}) { } 0 95 95
Condition33%1/3
%exceptions and exists $exceptions{$exitStatus}
!ll&&!rl&&r
9500
MC/DC0%0/2
%exceptions and exists $exceptions{$exitStatus}
%exceptions exists $exceptions{$exitStatus}
Truth table33%1/3
A and B
A= %exceptionsB= exists $exceptions{$exitStatus}
ABresult
0X0
100
111
267 {
268 0 $testStatus += skip( $exceptions{$exitStatus}, $exitStatus, $desiredExitStatus );
269 0 $testOutput = "skip";
270 }
271 else
272 {
273 95 $testStatus += ok( $exitStatus, $desiredExitStatus );
274 }
275 }
276  
277 95 ▶ if ( defined( $desiredOutput ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $desiredOutput) 56 39 95
278 {
279 56 ▶ if ( $testOutput ne "skip" )
Branch: 1 branch
BranchTrueFalseTotal
if ($testOutput ne 'skip') { } 56 0 56
280 {
281 56 $testStatus += ok( $output, $desiredOutput );
282 }
283 else
284 {
285 0 $testStatus += skip( "Skipping output test as requested", $output, $desiredOutput );
286 }
287 }
288  
289 95 return $testStatus;
290 }
291  
292  
293 sub skipMissingCmd
294 2 ▶{
Subroutine skipMissingCmd: called CC=2 SCAR=6.9
Pod documented
295 2 my( $command, $count ) = @_;
296  
297 2 my $testStatus;
298  
299 2 for ( 1 .. $count )
300 {
301 25 $testStatus += skip( "Missing ${command} - tests skipped", 1 );
302 }
303  
304 2 return $testStatus;
305 }
306  
307 sub getTestParameter
308 0 ▶{
Subroutine getTestParameter: not called CC=21 SCAR=61.4
Pod documented
309 0 my( $param, $envvar, $default, $brief, $scoped );
310 0 my $new_style;
311 0 ▶ if (scalar @_ <= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ <= 3) { } 0 0 0
312 0 ($param, $brief, $default) = @_;
313 0 $envvar = $param;
314 0 $new_style = 1;
315 } else {
316 0 ( $param, $envvar, $default, $brief, $scoped ) = @_;
317 0 $new_style = 0;
318 }
319  
320 # Apply default values for optional arguments
321 0 ▶ $scoped = ( defined( $scoped ) && $scoped );
Condition0%0/3
defined $scoped && $scoped
!ll&&!rl&&r
000
MC/DC0%0/2
defined $scoped && $scoped
defined $scoped $scoped
Truth table0%0/3
A && B
A= defined $scopedB= $scoped
ABresult
0X0
100
111
322  
323 0 my $testharness = basename( (caller(0))[1], ".t" ); # used for scoping
324  
325 0 ▶ if ( defined( $envvar ) && exists( $ENV{$envvar} ) && $ENV{$envvar} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $envvar and exists $ENV{$envvar} and $ENV{$envvar}) 0 0 0
Condition0%0/6
defined $envvar and exists $ENV{$envvar} and $ENV{$envvar}
!ll&&!rl&&r
000
defined $envvar and exists $ENV{$envvar}
!ll&&!rl&&r
000
MC/DC0%0/3
defined $envvar and exists $ENV{$envvar} and $ENV{$envvar}
defined $envvar exists $ENV{$envvar} $ENV{$envvar}
Truth table0%0/4
A and B and C
A= defined $envvarB= exists $ENV{$envvar}C= $ENV{$envvar}
ABCresult
0XX0
10X0
1100
1111
326 {
327 0 return $ENV{$envvar};
328 }
329  
330 0 my $cachedValue = SearchCache( $param, $testharness );
331 0 ▶ if ( defined( $cachedValue ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cachedValue) 0 0 0
332 {
333 # This save required to convert to new style because the key required is
334 # changing to the environment variable
335 0 ▶ if ($new_style == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_style == 0) 0 0 0
336 0 SetCacheParameter( $envvar, undef, $cachedValue );
337 }
338 0 return $cachedValue;
339 }
340  
341 0 ▶ my $defaultValid = ( defined( $default ) && $default );
Condition0%0/3
defined $default && $default
!ll&&!rl&&r
000
MC/DC0%0/2
defined $default && $default
defined $default $default
Truth table0%0/3
A && B
A= defined $defaultB= $default
ABresult
0X0
100
111
342 0 ▶ my $autoAcceptDefault = ( exists( $ENV{'NPTEST_ACCEPTDEFAULT'} ) && $ENV{'NPTEST_ACCEPTDEFAULT'} );
Condition0%0/3
exists $ENV{'NPTEST_ACCEPTDEFAULT'} && $ENV{'NPTEST_ACCEPTDEFAULT'}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $ENV{'NPTEST_ACCEPTDEFAULT'} && $ENV{'NPTEST_ACCEPTDEFAULT'}
exists $ENV{'NPTEST_ACCEPTDEFAULT'} $ENV{'NPTEST_ACCEPTDEFAULT'}
Truth table0%0/3
A && B
A= exists $ENV{'NPTEST_ACCEPTDEFAULT'}B= $ENV{'NPTEST_ACCEPTDEFAULT'}
ABresult
0X0
100
111
343  
344 0 ▶ if ( $autoAcceptDefault && $defaultValid )
Branch: 1 branch
BranchTrueFalseTotal
if ($autoAcceptDefault and $defaultValid) 0 0 0
Condition0%0/3
$autoAcceptDefault and $defaultValid
!ll&&!rl&&r
000
MC/DC0%0/2
$autoAcceptDefault and $defaultValid
$autoAcceptDefault $defaultValid
Truth table0%0/3
A and B
A= $autoAcceptDefaultB= $defaultValid
ABresult
0X0
100
111
345 {
346 0 return $default;
347 }
348  
349 # Set "none" if no terminal attached (eg, tinderbox build servers when new variables set)
350 0 ▶ return "" unless (-t STDERR);
Branch: 1 branch
BranchTrueFalseTotal
unless -t STDERR 0 0 0
351  
352 0 my $userResponse = '';
353  
354 0 ▶ while ( $userResponse eq '' )
Branch: 1 branch
BranchTrueFalseTotal
if ($userResponse eq "") 0 0 0
355 {
356 0 print STDERR "\n";
357 0 print STDERR "Test Harness : $testharness\n";
358 0 print STDERR "Test Parameter : $param\n";
359 0 ▶ print STDERR "Environment Variable : $envvar\n" if ($param ne $envvar);
Branch: 1 branch
BranchTrueFalseTotal
if $param ne $envvar 0 0 0
360 0 print STDERR "Brief Description : $brief\n";
361 0 ▶ print STDERR "Enter value (or 'none') ", ($defaultValid ? "[${default}]" : "[]"), " => ";
Branch: 1 branch
BranchTrueFalseTotal
$defaultValid ? : 0 0 0
362 0 $userResponse = <STDIN>;
363 0 ▶ $userResponse = "" if ! defined( $userResponse ); # Handle EOF
Branch: 1 branch
BranchTrueFalseTotal
unless defined $userResponse 0 0 0
364 0 chomp( $userResponse );
365 0 ▶ if ( $defaultValid && $userResponse eq '' )
Branch: 1 branch
BranchTrueFalseTotal
if ($defaultValid and $userResponse eq "") 0 0 0
Condition0%0/3
$defaultValid and $userResponse eq ""
!ll&&!rl&&r
000
MC/DC0%0/2
$defaultValid and $userResponse eq ""
$defaultValid $userResponse eq ""
Truth table0%0/3
A and B
A= $defaultValidB= $userResponse eq ""
ABresult
0X0
100
111
366 {
367 0 $userResponse = $default;
368 }
369 }
370  
371 0 print STDERR "\n";
372  
373 0 ▶ if ($userResponse =~ /^(na|none)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($userResponse =~ /^(na|none)$/) 0 0 0
374 0 $userResponse = '';
375 }
376  
377 # define all user responses at global scope
378 0 ▶ SetCacheParameter( $param, ( $scoped ? $testharness : undef ), $userResponse );
Branch: 1 branch
BranchTrueFalseTotal
$scoped ? : 0 0 0
379  
380 0 return $userResponse;
381 }
382  
383 #
384 # Internal Cache Management Functions
385 #
386  
387 sub SearchCache
388 0 ▶{
Subroutine SearchCache: not called CC=4 SCAR=30.0
Pod undocumented
389 0 my( $param, $scope ) = @_;
390  
391 0 LoadCache();
392  
393 0 ▶ if ( exists( $CACHE{$scope} ) && exists( $CACHE{$scope}{$param} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $CACHE{$scope} and exists $CACHE{$scope}{$param}) 0 0 0
Condition0%0/3
exists $CACHE{$scope} and exists $CACHE{$scope}{$param}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $CACHE{$scope} and exists $CACHE{$scope}{$param}
exists $CACHE{$scope} exists $CACHE{$scope}{$param}
Truth table0%0/3
A and B
A= exists $CACHE{$scope}B= exists $CACHE{$scope}{$param}
ABresult
0X0
100
111
394 {
395 0 return $CACHE{$scope}{$param};
396 }
397  
398 0 ▶ if ( exists( $CACHE{$param} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $CACHE{$param}) 0 0 0
399 {
400 0 return $CACHE{$param};
401 }
402 0 return undef; # Need this to say "nothing found"
403 }
404  
405 sub SetCacheParameter
406 0 ▶{
Subroutine SetCacheParameter: not called CC=2 SCAR=17.9
Pod undocumented
407 0 my( $param, $scope, $value ) = @_;
408  
409 0 ▶ if ( defined( $scope ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $scope) { } 0 0 0
410 {
411 0 $CACHE{$scope}{$param} = $value;
412 }
413 else
414 {
415 0 $CACHE{$param} = $value;
416 }
417  
418 0 SaveCache();
419 }
420  
421 sub LoadCache
422 0 ▶{
Subroutine LoadCache: not called CC=4 SCAR=30.0
Pod undocumented
423 0 ▶ return if exists( $CACHE{'_cache_loaded_'} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $CACHE{"_cache_loaded_"} 0 0 0
424  
425 0 ▶ if ( -f $CACHEFILENAME )
Branch: 1 branch
BranchTrueFalseTotal
if (-f $CACHEFILENAME) 0 0 0
426 {
427 0 my( $fileHandle ) = new IO::File;
428  
429 0 ▶ if ( ! $fileHandle->open( "< ${CACHEFILENAME}" ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($fileHandle->open("< $CACHEFILENAME")) 0 0 0
430 {
431 0 print STDERR "NPTest::LoadCache() : Problem opening ${CACHEFILENAME} : $!\n";
432 0 return;
433 }
434  
435 0 my( $fileContents ) = join( "\n", <$fileHandle> );
436  
437 0 $fileHandle->close();
438  
439 0 my( $contentsRef ) = eval $fileContents;
440 0 %CACHE = %{$contentsRef};
441  
442 }
443  
444 0 $CACHE{'_cache_loaded_'} = 1;
445 }
446  
447  
448 sub SaveCache
449 0 ▶{
Subroutine SaveCache: not called CC=2 SCAR=17.9
Pod undocumented
450 0 delete $CACHE{'_cache_loaded_'};
451  
452 0 my( $fileHandle ) = new IO::File;
453  
454 0 ▶ if ( ! $fileHandle->open( "> ${CACHEFILENAME}" ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($fileHandle->open("> $CACHEFILENAME")) 0 0 0
455 {
456 0 print STDERR "NPTest::LoadCache() : Problem saving ${CACHEFILENAME} : $!\n";
457 0 return;
458 }
459  
460 0 my( $dataDumper ) = new Data::Dumper( [ \%CACHE ] );
461  
462 0 $dataDumper->Terse(1);
463  
464 0 print $fileHandle $dataDumper->Dump();
465  
466 0 $fileHandle->close();
467  
468 0 $CACHE{'_cache_loaded_'} = 1;
469 }
470  
471 #
472 # (Questionable) Public Cache Management Functions
473 #
474  
475 sub SetCacheFilename
476 0 ▶{
Subroutine SetCacheFilename: not called CC=1 SCAR=6.9
Pod undocumented
477 0 my( $filename ) = @_;
478  
479 # Unfortunately we can not validate the filename
480 # in any meaningful way, as it may not yet exist
481 0 $CACHEFILENAME = $filename;
482 }
483  
484  
485 #
486 # Test Harness Wrapper Functions
487 #
488  
489 sub DetermineTestHarnessDirectory
490 0 ▶{
Subroutine DetermineTestHarnessDirectory: not called CC=6 SCAR=37.4
Pod undocumented
491 0 my( $userSupplied ) = @_;
492  
493 # User Supplied
494 0 ▶ if ( defined( $userSupplied ) && $userSupplied )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $userSupplied and $userSupplied) 0 0 0
Condition0%0/3
defined $userSupplied and $userSupplied
!ll&&!rl&&r
000
MC/DC0%0/2
defined $userSupplied and $userSupplied
defined $userSupplied $userSupplied
Truth table0%0/3
A and B
A= defined $userSuppliedB= $userSupplied
ABresult
0X0
100
111
495 {
496 0 ▶ if ( -d $userSupplied )
Branch: 1 branch
BranchTrueFalseTotal
if (-d $userSupplied) { } 0 0 0
497 {
498 0 return $userSupplied;
499 }
500 else
501 {
502 0 return undef; # userSupplied is invalid -> FAIL
503 }
504 }
505  
506 # Simple Case : "t" is a subdirectory of the current directory
507 0 ▶ if ( -d "./t" )
Branch: 1 branch
BranchTrueFalseTotal
if (-d "./t") 0 0 0
508 {
509 0 return "./t";
510 }
511  
512 # To be honest I don't understand which case satisfies the
513 # original code in test.pl : when $tstdir == `pwd` w.r.t.
514 # $tstdir =~ s|^(.*)/([^/]+)/?$|$1/$2|; and if (-d "../../$2/t")
515 # Assuming pwd is "/a/b/c/d/e" then we are testing for "/a/b/c/e/t"
516 # if I understand the code correctly (a big assumption)
517  
518 # Simple Case : the current directory is "t"
519 0 my $pwd = cwd();
520  
521 0 ▶ if ( $pwd =~ m|/t$| )
Branch: 1 branch
BranchTrueFalseTotal
if ($pwd =~ m[/t$]) 0 0 0
522 {
523 0 return $pwd;
524  
525 # The alternate that might work better is
526 # chdir( ".." );
527 # return "./t";
528 # As the current test harnesses assume the application
529 # to be tested is in the current directory (ie "./check_disk ....")
530 }
531  
532 0 return undef;
533 }
534  
535 sub TestsFrom
536 0 ▶{
Subroutine TestsFrom: not called CC=7 SCAR=40.3
Pod undocumented
537 0 my( $directory, $excludeIfAppMissing ) = @_;
538  
539 0 ▶ $excludeIfAppMissing = 0 unless defined( $excludeIfAppMissing );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $excludeIfAppMissing 0 0 0
540  
541 0 ▶ if ( ! opendir( DIR, $directory ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (opendir DIR, $directory) 0 0 0
542 {
543 0 print STDERR "NPTest::TestsFrom() - Failed to open ${directory} : $!\n";
544 0 return ();
545 }
546  
547 0 my( @tests ) = ();
548  
549 0 my $filename;
550 my $application;
551  
552 0 ▶ while ( $filename = readdir( DIR ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined($filename = readdir DIR)) 0 0 0
553 {
554 0 ▶ if ( $filename =~ m/\.t$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ /\.t$/) 0 0 0
555 {
556 0 ▶ if ( $excludeIfAppMissing )
Branch: 1 branch
BranchTrueFalseTotal
if ($excludeIfAppMissing) 0 0 0
557 {
558 0 $application = basename( $filename, ".t" );
559 0 ▶ if ( ! -e $application )
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $application) 0 0 0
560 {
561 0 print STDERR "No application (${application}) found for test harness (${filename})\n";
562 0 next;
563 }
564 }
565 0 push @tests, "${directory}/${filename}";
566 }
567 }
568  
569 0 closedir( DIR );
570  
571 0 return sort @tests;
572 }
573  
574 # All the new object oriented stuff below
575  
576 95 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
577 95 my $type = shift;
578 95 my $self = {};
579 95 return bless $self, $type;
580 }
581  
582 # Accessors
583 190 ▶sub return_code {
Subroutine return_code: called CC=2 SCAR=6.9
Pod undocumented
584 190 my $self = shift;
585 190 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 95 95 190
586 95 return $self->{return_code} = shift;
587 } else {
588 95 return $self->{return_code};
589 }
590 }
591 190 ▶sub output {
Subroutine output: called CC=2 SCAR=6.9
Pod undocumented
592 190 my $self = shift;
593 190 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 95 95 190
594 95 return $self->{output} = shift;
595 } else {
596 95 return $self->{output};
597 }
598 }
599  
600 0 ▶sub perf_output {
Subroutine perf_output: not called CC=2 SCAR=17.9
Pod undocumented
601 0 my $self = shift;
602 0 $_ = $self->{output};
603 0 /\|(.*)$/;
604 0 ▶ return $1 || "";
Condition0%0/2
$1 || ''
l!l
00
MC/DC0%0/1
$1 || ''
$1
Truth table0%0/2
A
A= $1
Aresult
11
00
605 }
606  
607 0 ▶sub only_output {
Subroutine only_output: not called CC=2 SCAR=17.9
Pod undocumented
608 0 my $self = shift;
609 0 $_ = $self->{output};
610 0 /(.*?)\|/;
611 0 ▶ return $1 || "";
Condition0%0/2
$1 || ''
l!l
00
MC/DC0%0/1
$1 || ''
$1
Truth table0%0/2
A
A= $1
Aresult
11
00
612 }
613  
614 95 ▶sub testCmd {
Subroutine testCmd: called CC=4 SCAR=16.0
Pod documented
615 95 my $class = shift;
616 95 ▶ my $command = shift or die "No command passed to testCmd";
Branch: 1 branch
BranchTrueFalseTotal
my $command = shift() or die "No command passed to testCmd" 95 0 95
617 95 my $object = $class->new;
618
619 95 my $output = `$command`;
620 95 $object->return_code($? >> 8);
621 95 $_ = $? & 127;
622 95 ▶ if ($_) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_) 0 95 95
623 0 die "Got signal $_ for command $command";
624 }
625 95 chomp $output;
626 95 $object->output($output);
627  
628 95 ▶ if ($ENV{'NPTEST_DEBUG'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"NPTEST_DEBUG"}) 0 95 95
629 0 my ($pkg, $file, $line) = caller(0);
630 0 print "testCmd: Called from line $line in $file", $/;
631 0 print "Testing: $command", $/;
632 0 print "Output: ", $object->output, $/;
633 0 print "Return code: ", $object->return_code, $/;
634 }
635  
636 95 return $object;
637 }
638  
639 26 1;
640 #
641 # End of File
642 #
« blib/lib/ASNMTAP/Asnmtap/Plugins/Nagios.pm ↑ Summary plugins/templates/check_template-SOAP.pl »