negate.t 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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, 9 in timeout tests and 2 * 32 in the last loops
  10. plan tests => 88;
  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, "", "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. # Timeout tests
  47. $res = NPTest->testCmd( "./negate -t 2 /bin/sh -c 'sleep 5'" );
  48. is( $res->output, 'CRITICAL - Plugin timed out' );
  49. foreach my $state (keys(%state)) {
  50. $res = NPTest->testCmd( "./negate -t 2 -T $state /bin/sh -c 'sleep 5'" );
  51. is( $res->return_code, $state{$state}, "Got timeout state $state" );
  52. is( $res->output, uc($state)." - Plugin timed out", "Timeout state $state output");
  53. }
  54. foreach my $current_state (keys(%state)) {
  55. foreach my $new_state (keys(%state)) {
  56. $res = NPTest->testCmd( "./negate --$current_state=$new_state ./check_dummy ".$state{$current_state}." 'Fake $new_state'" );
  57. is( $res->return_code, $state{$new_state}, "Got fake $new_state" );
  58. is( $res->output, uc($current_state).": Fake $new_state", "Fake $new_state output");
  59. }
  60. }
  61. # Same as above with substitute
  62. foreach my $current_state (keys(%state)) {
  63. foreach my $new_state (keys(%state)) {
  64. $res = NPTest->testCmd( "./negate -s --$current_state=$new_state ./check_dummy ".$state{$current_state}." 'Fake $new_state'" );
  65. is( $res->return_code, $state{$new_state}, "Got fake $new_state (with substitute)" );
  66. is( $res->output, uc($new_state).": Fake $new_state", "Substitued fake $new_state output");
  67. }
  68. }