check_by_ssh.t 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #! /usr/bin/perl -w -I ..
  2. #
  3. # check_by_ssh tests
  4. #
  5. # $Id$
  6. #
  7. use strict;
  8. use Test::More;
  9. use NPTest;
  10. # Required parameters
  11. my $ssh_service = getTestParameter( "NP_SSH_HOST",
  12. "A host providing SSH service",
  13. "localhost");
  14. my $ssh_key = getTestParameter( "NP_SSH_IDENTITY",
  15. "A key allowing access to NP_SSH_HOST",
  16. "~/.ssh/id_dsa");
  17. plan skip_all => "SSH_HOST and SSH_IDENTITY must be defined" unless ($ssh_service && $ssh_key);
  18. plan tests => 38;
  19. # Some random check strings/response
  20. my @responce = ('OK: Everything is fine!',
  21. 'WARNING: Hey, pick me, pick me!',
  22. 'CRITICAL: Shit happens...',
  23. 'UNKNOWN: What can I do for ya?',
  24. 'WOOPS: What did I smoke?',
  25. );
  26. my @check;
  27. for (@responce) {
  28. push(@check, "echo $_");
  29. }
  30. my $result;
  31. ## Single active checks
  32. for (my $i=0; $i<4; $i++) {
  33. $result = NPTest->testCmd(
  34. "./check_by_ssh -i $ssh_key -H $ssh_service -C '$check[$i]; exit $i'"
  35. );
  36. cmp_ok($result->return_code, '==', $i, "Exit with return code $i");
  37. is($result->output, $responce[$i], "Status text is correct for check $i");
  38. }
  39. $result = NPTest->testCmd(
  40. "./check_by_ssh -i $ssh_key -H $ssh_service -C 'exit 0'"
  41. );
  42. cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)");
  43. is($result->output, 'OK - check_by_ssh: Remote command \'exit 0\' returned status 0', "Status text if command returned none (OK)");
  44. $result = NPTest->testCmd(
  45. "./check_by_ssh -i $ssh_key -H $ssh_service -C 'exit 1'"
  46. );
  47. cmp_ok($result->return_code, '==', 1, "Exit with return code 1 (WARNING)");
  48. is($result->output, 'WARNING - check_by_ssh: Remote command \'exit 1\' returned status 1', "Status text if command returned none (WARNING)");
  49. $result = NPTest->testCmd(
  50. "./check_by_ssh -i $ssh_key -H $ssh_service -C 'exit 2'"
  51. );
  52. cmp_ok($result->return_code, '==', 2, "Exit with return code 2 (CRITICAL)");
  53. is($result->output, 'CRITICAL - check_by_ssh: Remote command \'exit 2\' returned status 2', "Status text if command returned none (CRITICAL)");
  54. $result = NPTest->testCmd(
  55. "./check_by_ssh -i $ssh_key -H $ssh_service -C 'exit 3'"
  56. );
  57. cmp_ok($result->return_code, '==', 3, "Exit with return code 3 (UNKNOWN)");
  58. is($result->output, 'UNKNOWN - check_by_ssh: Remote command \'exit 3\' returned status 3', "Status text if command returned none (UNKNOWN)");
  59. $result = NPTest->testCmd(
  60. "./check_by_ssh -i $ssh_key -H $ssh_service -C 'exit 7'"
  61. );
  62. cmp_ok($result->return_code, '==', 7, "Exit with return code 7 (out of bounds)");
  63. is($result->output, 'UNKNOWN - check_by_ssh: Remote command \'exit 7\' returned status 7', "Status text if command returned none (out of bounds)");
  64. $result = NPTest->testCmd(
  65. "./check_by_ssh -i $ssh_key -H $ssh_service -C '$check[4]; exit 8'"
  66. );
  67. cmp_ok($result->return_code, '==', 8, "Exit with return code 8 (out of bounds)");
  68. is($result->output, $responce[4], "Return proper status text even with unknown status codes");
  69. # Multiple active checks
  70. $result = NPTest->testCmd(
  71. "./check_by_ssh -i $ssh_key -H $ssh_service -C '$check[1]; sh -c exit\\ 1' -C '$check[0]; sh -c exit\\ 0' -C '$check[3]; sh -c exit\\ 3' -C '$check[2]; sh -c exit\\ 2'"
  72. );
  73. cmp_ok($result->return_code, '==', 0, "Multiple checks always return OK");
  74. my @lines = split(/\n/, $result->output);
  75. my %linemap = (
  76. '0' => '1',
  77. '2' => '0',
  78. '4' => '3',
  79. '6' => '2',
  80. );
  81. foreach my $line (0, 2, 4, 6) {
  82. my $code = $linemap{$line};
  83. my $statline = $line+1;
  84. is($lines[$line], "$responce[$code]", "multiple checks status text is correct for line $line");
  85. is($lines[$statline], "STATUS CODE: $code", "multiple check status code is correct for line $line");
  86. }
  87. # Passive checks
  88. $result = NPTest->testCmd(
  89. "./check_by_ssh -i $ssh_key -H $ssh_service -n flint -s serv -C '$check[2]; sh -c exit\\ 2' -O /tmp/check_by_ssh.$$"
  90. );
  91. cmp_ok($result->return_code, '==', 0, "Exit always ok on passive checks");
  92. open(PASV, "/tmp/check_by_ssh.$$") or die("Unable to open '/tmp/check_by_ssh.$$': $!");
  93. my $count=0;
  94. while (<PASV>) {
  95. like($_, '/^\[\d+\] PROCESS_SERVICE_CHECK_RESULT;flint;serv;2;$responce[2]$/', 'proper result for passive check');
  96. $count++;
  97. }
  98. cmp_ok($count, '==', 1, 'One passive result for one check performed');
  99. unlink("/tmp/check_by_ssh.$$") or die("Unable to unlink '/tmp/check_by_ssh.$$': $!");
  100. $result = NPTest->testCmd(
  101. "./check_by_ssh -i $ssh_key -H $ssh_service -n flint -s c0:c1:c2:c3:c4 -C '$check[0], exit 0' -C '$check[1]; exit 1' -C '$check[2]; exit 2' -C '$check[3]; exit 3' -C '$check[4]; exit 9' -O /tmp/check_by_ssh.$$"
  102. );
  103. cmp_ok($result->return_code, '==', 0, "Exit always ok on passive checks");
  104. $count=0;
  105. open(PASV, "/tmp/check_by_ssh.$$") or die("Unable to open '/tmp/check_by_ssh.$$': $!");
  106. while (<PASV>) {
  107. my $ret;
  108. ($count == 4 ? $ret = 7 : $ret = $count);
  109. like($_, '/^\[\d+\] PROCESS_SERVICE_CHECK_RESULT;flint;c' . $count . ';' . $ret . ';' . $responce[$count] . '$/', "proper result for passive check $count");
  110. }
  111. cmp_ok($count, '==', 5, 'Five passive result for five checks performed');
  112. unlink("/tmp/check_by_ssh.$$") or die("Unable to unlink '/tmp/check_by_ssh.$$': $!");