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