negate.t 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. #
  8. use strict;
  9. use Test::More;
  10. use NPTest;
  11. # 15 tests in the first part and 32 in the last loop
  12. plan tests => 47;
  13. my $res;
  14. my $PWD = $ENV{PWD};
  15. $res = NPTest->testCmd( "./negate" );
  16. is( $res->return_code, 3, "Not enough parameters");
  17. like( $res->output, "/Could not parse arguments/", "Could not parse arguments");
  18. $res = NPTest->testCmd( "./negate bobthebuilder" );
  19. is( $res->return_code, 3, "Require full path" );
  20. like( $res->output, "/Require path to command/", "Appropriate error message");
  21. $res = NPTest->testCmd( "./negate $PWD/check_dummy 0 'a dummy okay'" );
  22. is( $res->return_code, 2, "OK changed to CRITICAL" );
  23. is( $res->output, "OK: a dummy okay", "Output as expected" );
  24. $res = NPTest->testCmd( "./negate '$PWD/check_dummy 0 redsweaterblog'");
  25. is( $res->return_code, 2, "OK => CRIT with a single quote for command to run" );
  26. is( $res->output, "OK: redsweaterblog", "Output as expected" );
  27. $res = NPTest->testCmd( "./negate $PWD/check_dummy 1 'a warn a day keeps the managers at bay'" );
  28. is( $res->return_code, 1, "WARN stays same" );
  29. $res = NPTest->testCmd( "./negate $PWD/check_dummy 3 mysterious");
  30. is( $res->return_code, 3, "UNKNOWN stays same" );
  31. $res = NPTest->testCmd( "./negate \"$PWD/check_dummy 0 'a dummy okay'\"" );
  32. is( $res->output, "OK: a dummy okay", "Checking slashed quotes - the single quotes are re-evaluated at shell" );
  33. # Output is "OK: a" because check_dummy only returns the first arg
  34. $res = NPTest->testCmd( "./negate $PWD/check_dummy 0 a dummy okay" );
  35. is( $res->output, "OK: a", "Multiple args passed as arrays" );
  36. $res = NPTest->testCmd( "./negate $PWD/check_dummy 0 'a dummy okay'" );
  37. is( $res->output, "OK: a dummy okay", "The quoted string is passed through to subcommand correctly" );
  38. $res = NPTest->testCmd( "./negate '$PWD/check_dummy 0' 'a dummy okay'" );
  39. is( $res->output, "No data returned from command", "Bad command, as expected (trying to execute './check_dummy 0')");
  40. $res = NPTest->testCmd( './negate $PWD/check_dummy 0 \'$$ a dummy okay\'' );
  41. is( $res->output, 'OK: $$ a dummy okay', 'Proves that $$ is not being expanded again' );
  42. my %state = (
  43. ok => 0,
  44. warning => 1,
  45. critical => 2,
  46. unknown => 3,
  47. );
  48. foreach my $current_state (qw(ok warning critical unknown)) {
  49. foreach my $new_state (qw(ok warning critical unknown)) {
  50. $res = NPTest->testCmd( "./negate --$current_state=$new_state ./check_dummy ".$state{$current_state}." 'Fake $new_state'" );
  51. is( $res->return_code, $state{$new_state}, "Got fake $new_state" );
  52. is( $res->output, uc($current_state).": Fake $new_state" );
  53. }
  54. }