check_by_ssh.t 5.2 KB

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