Code

Removing CVS/SVN tags and replacing with git-based versioning
[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 and 32 in the last loop
12 plan tests => 47;
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' );
57 my %state = (
58         ok => 0,
59         warning => 1,
60         critical => 2,
61         unknown => 3,
62         );
63 foreach my $current_state (qw(ok warning critical unknown)) {
64         foreach my $new_state (qw(ok warning critical unknown)) {
65                 $res = NPTest->testCmd( "./negate --$current_state=$new_state ./check_dummy ".$state{$current_state}." 'Fake $new_state'" );
66                 is( $res->return_code, $state{$new_state}, "Got fake $new_state" );
67                 is( $res->output, uc($current_state).": Fake $new_state" );
68         }
69 }