check_by_ssh.t 6.0 KB

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