negate.t 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 Cwd;
  9. use NPTest;
  10. # 15 tests in the first part, 9 in timeout tests and 2 * 32 in the last loops
  11. plan tests => 88;
  12. my $res;
  13. my $PWD = getcwd();
  14. $res = NPTest->testCmd( "./negate" );
  15. is( $res->return_code, 3, "Not enough parameters");
  16. like( $res->output, "/Could not parse arguments/", "Could not parse arguments");
  17. $res = NPTest->testCmd( "./negate bobthebuilder" );
  18. is( $res->return_code, 3, "Require full path" );
  19. like( $res->output, "/Require path to command/", "Appropriate error message");
  20. $res = NPTest->testCmd( "./negate $PWD/check_dummy 0 'a dummy okay'" );
  21. is( $res->return_code, 2, "OK changed to CRITICAL" );
  22. is( $res->output, "OK: a dummy okay", "Output as expected" );
  23. $res = NPTest->testCmd( "./negate '$PWD/check_dummy 0 redsweaterblog'");
  24. is( $res->return_code, 2, "OK => CRIT with a single quote for command to run" );
  25. is( $res->output, "OK: redsweaterblog", "Output as expected" );
  26. $res = NPTest->testCmd( "./negate $PWD/check_dummy 1 'a warn a day keeps the managers at bay'" );
  27. is( $res->return_code, 1, "WARN stays same" );
  28. $res = NPTest->testCmd( "./negate $PWD/check_dummy 3 mysterious");
  29. is( $res->return_code, 3, "UNKNOWN stays same" );
  30. $res = NPTest->testCmd( "./negate \"$PWD/check_dummy 0 'a dummy okay'\"" );
  31. is( $res->output, "OK: a dummy okay", "Checking slashed quotes - the single quotes are re-evaluated at shell" );
  32. # Output is "OK: a" because check_dummy only returns the first arg
  33. $res = NPTest->testCmd( "./negate $PWD/check_dummy 0 a dummy okay" );
  34. is( $res->output, "OK: a", "Multiple args passed as arrays" );
  35. $res = NPTest->testCmd( "./negate $PWD/check_dummy 0 'a dummy okay'" );
  36. is( $res->output, "OK: a dummy okay", "The quoted string is passed through to subcommand correctly" );
  37. $res = NPTest->testCmd( "./negate '$PWD/check_dummy 0' 'a dummy okay'" );
  38. is( $res->output, "No data returned from command", "Bad command, as expected (trying to execute './check_dummy 0')");
  39. $res = NPTest->testCmd( './negate '.$PWD.'/check_dummy 0 \'$$ a dummy okay\'' );
  40. is( $res->output, 'OK: $$ a dummy okay', 'Proves that $$ is not being expanded again' );
  41. my %state = (
  42. ok => 0,
  43. warning => 1,
  44. critical => 2,
  45. unknown => 3,
  46. );
  47. # Timeout tests
  48. $res = NPTest->testCmd( "./negate -t 2 /bin/sh -c 'sleep 5'" );
  49. is( $res->output, 'CRITICAL - Plugin timed out after 2 seconds' );
  50. foreach my $state (keys(%state)) {
  51. $res = NPTest->testCmd( "./negate -t 2 -T $state /bin/sh -c 'sleep 5'" );
  52. is( $res->return_code, $state{$state}, "Got timeout state $state" );
  53. is( $res->output, uc($state)." - Plugin timed out after 2 seconds", "Timeout state $state output");
  54. }
  55. foreach my $current_state (keys(%state)) {
  56. foreach my $new_state (keys(%state)) {
  57. $res = NPTest->testCmd( "./negate --$current_state=$new_state ./check_dummy ".$state{$current_state}." 'Fake $new_state'" );
  58. is( $res->return_code, $state{$new_state}, "Got fake $new_state" );
  59. is( $res->output, uc($current_state).": Fake $new_state", "Fake $new_state output");
  60. }
  61. }
  62. # Same as above with substitute
  63. foreach my $current_state (keys(%state)) {
  64. foreach my $new_state (keys(%state)) {
  65. $res = NPTest->testCmd( "./negate -s --$current_state=$new_state ./check_dummy ".$state{$current_state}." 'Fake $new_state'" );
  66. is( $res->return_code, $state{$new_state}, "Got fake $new_state (with substitute)" );
  67. is( $res->output, uc($new_state).": Fake $new_state", "Substitued fake $new_state output");
  68. }
  69. }