check_by_ssh.t 5.6 KB

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