check_by_ssh.t 6.1 KB

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