negate.t 2.8 KB

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