check_by_ssh.t 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 => 40;
  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 @responce_re;
  27. my @check;
  28. for (@responce) {
  29. push(@check, "echo $_");
  30. my $re_str = $_;
  31. $re_str =~ s{(.)} { "\Q$1" }ge;
  32. push(@responce_re, $re_str);
  33. }
  34. my $result;
  35. ## Single active checks
  36. for (my $i=0; $i<4; $i++) {
  37. $result = NPTest->testCmd(
  38. "./check_by_ssh -i $ssh_key -H $ssh_service -C '$check[$i]; exit $i'"
  39. );
  40. cmp_ok($result->return_code, '==', $i, "Exit with return code $i");
  41. is($result->output, $responce[$i], "Status text is correct for check $i");
  42. }
  43. $result = NPTest->testCmd(
  44. "./check_by_ssh -i $ssh_key -H $ssh_service -C 'exit 0'"
  45. );
  46. cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)");
  47. is($result->output, 'OK - check_by_ssh: Remote command \'exit 0\' returned status 0', "Status text if command returned none (OK)");
  48. $result = NPTest->testCmd(
  49. "./check_by_ssh -i $ssh_key -H $ssh_service -C 'exit 1'"
  50. );
  51. cmp_ok($result->return_code, '==', 1, "Exit with return code 1 (WARNING)");
  52. is($result->output, 'WARNING - check_by_ssh: Remote command \'exit 1\' returned status 1', "Status text if command returned none (WARNING)");
  53. $result = NPTest->testCmd(
  54. "./check_by_ssh -i $ssh_key -H $ssh_service -C 'exit 2'"
  55. );
  56. cmp_ok($result->return_code, '==', 2, "Exit with return code 2 (CRITICAL)");
  57. is($result->output, 'CRITICAL - check_by_ssh: Remote command \'exit 2\' returned status 2', "Status text if command returned none (CRITICAL)");
  58. $result = NPTest->testCmd(
  59. "./check_by_ssh -i $ssh_key -H $ssh_service -C 'exit 3'"
  60. );
  61. cmp_ok($result->return_code, '==', 3, "Exit with return code 3 (UNKNOWN)");
  62. is($result->output, 'UNKNOWN - check_by_ssh: Remote command \'exit 3\' returned status 3', "Status text if command returned none (UNKNOWN)");
  63. $result = NPTest->testCmd(
  64. "./check_by_ssh -i $ssh_key -H $ssh_service -C 'exit 7'"
  65. );
  66. cmp_ok($result->return_code, '==', 7, "Exit with return code 7 (out of bounds)");
  67. is($result->output, 'UNKNOWN - check_by_ssh: Remote command \'exit 7\' returned status 7', "Status text if command returned none (out of bounds)");
  68. $result = NPTest->testCmd(
  69. "./check_by_ssh -i $ssh_key -H $ssh_service -C '$check[4]; exit 8'"
  70. );
  71. cmp_ok($result->return_code, '==', 8, "Exit with return code 8 (out of bounds)");
  72. is($result->output, $responce[4], "Return proper status text even with unknown status codes");
  73. # Multiple active checks
  74. $result = NPTest->testCmd(
  75. "./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'"
  76. );
  77. cmp_ok($result->return_code, '==', 0, "Multiple checks always return OK");
  78. my @lines = split(/\n/, $result->output);
  79. cmp_ok(scalar(@lines), '==', 8, "Correct number of output lines for multiple checks");
  80. my %linemap = (
  81. '0' => '1',
  82. '2' => '0',
  83. '4' => '3',
  84. '6' => '2',
  85. );
  86. foreach my $line (0, 2, 4, 6) {
  87. my $code = $linemap{$line};
  88. my $statline = $line+1;
  89. is($lines[$line], "$responce[$code]", "multiple checks status text is correct for line $line");
  90. is($lines[$statline], "STATUS CODE: $code", "multiple check status code is correct for line $line");
  91. }
  92. # Passive checks
  93. unlink("/tmp/check_by_ssh.$$");
  94. $result = NPTest->testCmd(
  95. "./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.$$"
  96. );
  97. cmp_ok($result->return_code, '==', 0, "Exit always ok on passive checks");
  98. open(PASV, "/tmp/check_by_ssh.$$") or die("Unable to open '/tmp/check_by_ssh.$$': $!");
  99. my @pasv = <PASV>;
  100. close(PASV) or die("Unable to close '/tmp/check_by_ssh.$$': $!");
  101. cmp_ok(scalar(@pasv), '==', 1, 'One passive result for one check performed');
  102. for (0) {
  103. if ($pasv[$_]) {
  104. like($pasv[$_], '/^\[\d+\] PROCESS_SERVICE_CHECK_RESULT;flint;serv;2;' . $responce_re[2] . '$/', 'proper result for passive check');
  105. } else {
  106. fail('proper result for passive check');
  107. }
  108. }
  109. unlink("/tmp/check_by_ssh.$$") or die("Unable to unlink '/tmp/check_by_ssh.$$': $!");
  110. undef @pasv;
  111. $result = NPTest->testCmd(
  112. "./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.$$"
  113. );
  114. cmp_ok($result->return_code, '==', 0, "Exit always ok on passive checks");
  115. open(PASV, "/tmp/check_by_ssh.$$") or die("Unable to open '/tmp/check_by_ssh.$$': $!");
  116. @pasv = <PASV>;
  117. close(PASV) or die("Unable to close '/tmp/check_by_ssh.$$': $!");
  118. cmp_ok(scalar(@pasv), '==', 5, 'Five passive result for five checks performed');
  119. for (0, 1, 2, 3, 4) {
  120. if ($pasv[$_]) {
  121. my $ret = $_;
  122. $ret = 9 if ($_ == 4);
  123. like($pasv[$_], '/^\[\d+\] PROCESS_SERVICE_CHECK_RESULT;flint;c' . $_ . ';' . $ret . ';' . $responce_re[$_] . '$/', "proper result for passive check $_");
  124. } else {
  125. fail("proper result for passive check $_");
  126. }
  127. }
  128. unlink("/tmp/check_by_ssh.$$") or die("Unable to unlink '/tmp/check_by_ssh.$$': $!");