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