Code

Rewrite the "map changes to return codes" patch nearly from scratch.
[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 #
6 # $Id: negate.pl 1717 2007-05-24 08:53:50Z tonvoon $
7 #
9 use strict;
10 use Test::More;
11 use NPTest;
13 # 15 tests in the first part and 32 in the last loop
14 plan tests => 47;
16 my $res;
18 my $PWD = $ENV{PWD};
20 $res = NPTest->testCmd( "./negate" );
21 is( $res->return_code, 3, "Not enough parameters");
22 like( $res->output, "/Could not parse arguments/", "Could not parse arguments");
24 $res = NPTest->testCmd( "./negate bobthebuilder" );
25 is( $res->return_code, 3, "Require full path" );
26 like( $res->output, "/Require path to command/", "Appropriate error message");
28 $res = NPTest->testCmd( "./negate $PWD/check_dummy 0 'a dummy okay'" );
29 is( $res->return_code, 2, "OK changed to CRITICAL" );
30 is( $res->output, "OK: a dummy okay", "Output as expected" );
32 $res = NPTest->testCmd( "./negate '$PWD/check_dummy 0 redsweaterblog'");
33 is( $res->return_code, 2, "OK => CRIT with a single quote for command to run" );
34 is( $res->output, "OK: redsweaterblog", "Output as expected" );
36 $res = NPTest->testCmd( "./negate $PWD/check_dummy 1 'a warn a day keeps the managers at bay'" );
37 is( $res->return_code, 1, "WARN stays same" );
39 $res = NPTest->testCmd( "./negate $PWD/check_dummy 3 mysterious");
40 is( $res->return_code, 3, "UNKNOWN stays same" );
42 $res = NPTest->testCmd( "./negate \"$PWD/check_dummy 0 'a dummy okay'\"" );
43 is( $res->output, "OK: a dummy okay", "Checking slashed quotes - the single quotes are re-evaluated at shell" );
45 # Output is "OK: a" because check_dummy only returns the first arg
46 $res = NPTest->testCmd( "./negate $PWD/check_dummy 0 a dummy okay" );
47 is( $res->output, "OK: a", "Multiple args passed as arrays" );
49 $res = NPTest->testCmd( "./negate $PWD/check_dummy 0 'a dummy okay'" );
50 is( $res->output, "OK: a dummy okay", "The quoted string is passed through to subcommand correctly" );
52 $res = NPTest->testCmd( "./negate '$PWD/check_dummy 0' 'a dummy okay'" );
53 is( $res->output, "No data returned from command", "Bad command, as expected (trying to execute './check_dummy 0')");
55 $res = NPTest->testCmd( './negate $PWD/check_dummy 0 \'$$ a dummy okay\'' );
56 is( $res->output, 'OK: $$ a dummy okay', 'Proves that $$ is not being expanded again' );
59 my %state = (
60         ok => 0,
61         warning => 1,
62         critical => 2,
63         unknown => 3,
64         );
65 foreach my $current_state (qw(ok warning critical unknown)) {
66         foreach my $new_state (qw(ok warning critical unknown)) {
67                 $res = NPTest->testCmd( "./negate --$current_state=$new_state ./check_dummy ".$state{$current_state}." 'Fake $new_state'" );
68                 is( $res->return_code, $state{$new_state}, "Got fake $new_state" );
69                 is( $res->output, uc($current_state).": Fake $new_state" );
70         }
71 }