Code

Attempt at fixing check_snmp multiline output:
[nagiosplug.git] / plugins / t / negate.t
1 #! /usr/bin/perl -w -I ..
2 #
3 # negate checks
4 # Need check_dummy to work for testing
5 #
7 use strict;
8 use Test::More;
9 use NPTest;
11 # 15 tests in the first part, 9 in timeout tests and 2 * 32 in the last loops
12 plan tests => 88;
14 my $res;
16 my $PWD = $ENV{PWD};
18 $res = NPTest->testCmd( "./negate" );
19 is( $res->return_code, 3, "Not enough parameters");
20 like( $res->output, "/Could not parse arguments/", "Could not parse arguments");
22 $res = NPTest->testCmd( "./negate bobthebuilder" );
23 is( $res->return_code, 3, "Require full path" );
24 like( $res->output, "/Require path to command/", "Appropriate error message");
26 $res = NPTest->testCmd( "./negate $PWD/check_dummy 0 'a dummy okay'" );
27 is( $res->return_code, 2, "OK changed to CRITICAL" );
28 is( $res->output, "OK: a dummy okay", "Output as expected" );
30 $res = NPTest->testCmd( "./negate '$PWD/check_dummy 0 redsweaterblog'");
31 is( $res->return_code, 2, "OK => CRIT with a single quote for command to run" );
32 is( $res->output, "OK: redsweaterblog", "Output as expected" );
34 $res = NPTest->testCmd( "./negate $PWD/check_dummy 1 'a warn a day keeps the managers at bay'" );
35 is( $res->return_code, 1, "WARN stays same" );
37 $res = NPTest->testCmd( "./negate $PWD/check_dummy 3 mysterious");
38 is( $res->return_code, 3, "UNKNOWN stays same" );
40 $res = NPTest->testCmd( "./negate \"$PWD/check_dummy 0 'a dummy okay'\"" );
41 is( $res->output, "OK: a dummy okay", "Checking slashed quotes - the single quotes are re-evaluated at shell" );
43 # Output is "OK: a" because check_dummy only returns the first arg
44 $res = NPTest->testCmd( "./negate $PWD/check_dummy 0 a dummy okay" );
45 is( $res->output, "OK: a", "Multiple args passed as arrays" );
47 $res = NPTest->testCmd( "./negate $PWD/check_dummy 0 'a dummy okay'" );
48 is( $res->output, "OK: a dummy okay", "The quoted string is passed through to subcommand correctly" );
50 $res = NPTest->testCmd( "./negate '$PWD/check_dummy 0' 'a dummy okay'" );
51 is( $res->output, "No data returned from command", "Bad command, as expected (trying to execute './check_dummy 0')");
53 $res = NPTest->testCmd( './negate $PWD/check_dummy 0 \'$$ a dummy okay\'' );
54 is( $res->output, 'OK: $$ a dummy okay', 'Proves that $$ is not being expanded again' );
56 my %state = (
57         ok => 0,
58         warning => 1,
59         critical => 2,
60         unknown => 3,
61         );
63 # Timeout tests
64 $res = NPTest->testCmd( "./negate -t 2 /bin/sh -c 'sleep 5'" );
65 is( $res->output, 'CRITICAL - Plugin timed out after 2 seconds' );
67 foreach my $state (keys(%state)) {
68         $res = NPTest->testCmd( "./negate -t 2 -T $state /bin/sh -c 'sleep 5'" );
69         is( $res->return_code, $state{$state}, "Got timeout state $state" );
70         is( $res->output,  uc($state)." - Plugin timed out after 2 seconds", "Timeout state $state output");
71 }
73 foreach my $current_state (keys(%state)) {
74         foreach my $new_state (keys(%state)) {
75                 $res = NPTest->testCmd( "./negate --$current_state=$new_state ./check_dummy ".$state{$current_state}." 'Fake $new_state'" );
76                 is( $res->return_code, $state{$new_state}, "Got fake $new_state" );
77                 is( $res->output, uc($current_state).": Fake $new_state", "Fake $new_state output");
78         }
79 }
81 # Same as aboce with substitute
82 foreach my $current_state (keys(%state)) {
83         foreach my $new_state (keys(%state)) {
84                 $res = NPTest->testCmd( "./negate -s --$current_state=$new_state ./check_dummy ".$state{$current_state}." 'Fake $new_state'" );
85                 is( $res->return_code, $state{$new_state}, "Got fake $new_state (with substitute)" );
86                 is( $res->output, uc($new_state).": Fake $new_state", "Substitued fake $new_state output");
87         }
88 }