negate.t 2.6 KB

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