Code

0efa0ca891423950847e347a94546b5c3a9f0d6c
[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 # 47 tests if the "map changes to return codes" patch is applied
14 #plan tests => 47;
15 plan tests => 15;
17 my $res;
19 my $PWD = $ENV{PWD};
21 $res = NPTest->testCmd( "./negate" );
22 is( $res->return_code, 3, "Not enough parameters");
23 like( $res->output, "/Could not parse arguments/", "Could not parse arguments");
25 $res = NPTest->testCmd( "./negate bobthebuilder" );
26 is( $res->return_code, 3, "Require full path" );
27 like( $res->output, "/Require path to command/", "Appropriate error message");
29 $res = NPTest->testCmd( "./negate $PWD/check_dummy 0 'a dummy okay'" );
30 is( $res->return_code, 2, "OK changed to CRITICAL" );
31 is( $res->output, "OK: a dummy okay", "Output as expected" );
33 $res = NPTest->testCmd( "./negate '$PWD/check_dummy 0 redsweaterblog'");
34 is( $res->return_code, 2, "OK => CRIT with a single quote for command to run" );
35 is( $res->output, "OK: redsweaterblog", "Output as expected" );
37 $res = NPTest->testCmd( "./negate $PWD/check_dummy 1 'a warn a day keeps the managers at bay'" );
38 is( $res->return_code, 1, "WARN stays same" );
40 $res = NPTest->testCmd( "./negate $PWD/check_dummy 3 mysterious");
41 is( $res->return_code, 3, "UNKNOWN stays same" );
43 $res = NPTest->testCmd( "./negate \"$PWD/check_dummy 0 'a dummy okay'\"" );
44 is( $res->output, "OK: a dummy okay", "Checking slashed quotes - the single quotes are re-evaluated at shell" );
46 # Output is "OK: a" because check_dummy only returns the first arg
47 $res = NPTest->testCmd( "./negate $PWD/check_dummy 0 a dummy okay" );
48 is( $res->output, "OK: a", "Multiple args passed as arrays" );
50 $res = NPTest->testCmd( "./negate $PWD/check_dummy 0 'a dummy okay'" );
51 is( $res->output, "OK: a dummy okay", "The quoted string is passed through to subcommand correctly" );
53 $res = NPTest->testCmd( "./negate '$PWD/check_dummy 0' 'a dummy okay'" );
54 is( $res->output, "No data returned from command", "Bad command, as expected (trying to execute './check_dummy 0')");
56 $res = NPTest->testCmd( './negate $PWD/check_dummy 0 \'$$ a dummy okay\'' );
57 is( $res->output, 'OK: $$ a dummy okay', 'Proves that $$ is not being expanded again' );
60 # Remove __DATA__ to run tests with future patch
61 __DATA__
63 TODO: {
64         local $TODO = "Codes can be switched";
65         my %state = (
66                 ok => 0,
67                 warning => 1,
68                 critical => 2,
69                 unknown => 3,
70                 );
71         foreach my $current_state (qw(ok warning critical unknown)) {
72                 foreach my $new_state (qw(ok warning critical unknown)) {
73                         $res = NPTest->testCmd( "./negate --$current_state=$new_state ./check_dummy ".$state{$current_state}." 'Fake $new_state'" );
74                         is( $res->return_code, $state{$new_state}, "Got fake $new_state" );
75                         is( $res->output, uc($current_state).": Fake $new_state" );
76                 }
77         }
78 }