Code

Oops - forgot to remove the SKIP block (which didn't seem to work fine)
[nagiosplug.git] / plugins / t / check_by_ssh.t
1 #! /usr/bin/perl -w -I ..
2 #
3 # check_by_ssh tests
4 #
5 # $Id$
6 #
8 use strict;
9 use Test::More;
10 use NPTest;
12 # Required parameters
13 my $ssh_service = getTestParameter( "NP_SSH_HOST",
14     "A host providing SSH service",
15     "localhost");
17 my $ssh_key = getTestParameter( "NP_SSH_IDENTITY",
18     "A key allowing access to NP_SSH_HOST",
19     "~/.ssh/id_dsa");
21 plan skip_all => "SSH_HOST and SSH_IDENTITY must be defined" unless ($ssh_service && $ssh_key);
23 plan tests => 38;
25 # Some random check strings/response
26 my @responce = ('OK: Everything is fine!',
27                 'WARNING: Hey, pick me, pick me!',
28                 'CRITICAL: Shit happens...',
29                 'UNKNOWN: What can I do for ya?',
30                 'WOOPS: What did I smoke?',
31 );
32 my @check;
33 for (@responce) {
34         push(@check, "echo $_");
35 }
37 my $result;
39 ## 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 }
49 $result = NPTest->testCmd(
50         "./check_by_ssh -i $ssh_key -H $ssh_service -C 'exit 0'"
51         );
52 cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)");
53 is($result->output, 'OK - check_by_ssh: Remote command \'exit 0\' returned status 0', "Status text if command returned none (OK)");
55 $result = NPTest->testCmd(
56         "./check_by_ssh -i $ssh_key -H $ssh_service -C 'exit 1'"
57         );
58 cmp_ok($result->return_code, '==', 1, "Exit with return code 1 (WARNING)");
59 is($result->output, 'WARNING - check_by_ssh: Remote command \'exit 1\' returned status 1', "Status text if command returned none (WARNING)");
61 $result = NPTest->testCmd(
62         "./check_by_ssh -i $ssh_key -H $ssh_service -C 'exit 2'"
63         );
64 cmp_ok($result->return_code, '==', 2, "Exit with return code 2 (CRITICAL)");
65 is($result->output, 'CRITICAL - check_by_ssh: Remote command \'exit 2\' returned status 2', "Status text if command returned none (CRITICAL)");
67 $result = NPTest->testCmd(
68         "./check_by_ssh -i $ssh_key -H $ssh_service -C 'exit 3'"
69         );
70 cmp_ok($result->return_code, '==', 3, "Exit with return code 3 (UNKNOWN)");
71 is($result->output, 'UNKNOWN - check_by_ssh: Remote command \'exit 3\' returned status 3', "Status text if command returned none (UNKNOWN)");
73 $result = NPTest->testCmd(
74         "./check_by_ssh -i $ssh_key -H $ssh_service -C 'exit 7'"
75         );
76 cmp_ok($result->return_code, '==', 7, "Exit with return code 7 (out of bounds)");
77 is($result->output, 'UNKNOWN - check_by_ssh: Remote command \'exit 7\' returned status 7', "Status text if command returned none (out of bounds)");
79 $result = NPTest->testCmd(
80         "./check_by_ssh -i $ssh_key -H $ssh_service -C '$check[4]; exit 8'"
81         );
82 cmp_ok($result->return_code, '==', 8, "Exit with return code 8 (out of bounds)");
83 is($result->output, $responce[4], "Return proper status text even with unknown status codes");
85 # Multiple active checks
86 $result = NPTest->testCmd(
87         "./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'"
88         );
89 cmp_ok($result->return_code, '==', 0, "Multiple checks always return OK");
90 my @lines = split(/\n/, $result->output);
91 my %linemap = (
92                '0' => '1',
93                '2' => '0',
94                '4' => '3',
95                '6' => '2',
96 );
97 foreach my $line (0, 2, 4, 6) {
98         my $code = $linemap{$line};
99         my $statline = $line+1;
100         is($lines[$line], "$responce[$code]", "multiple checks status text is correct for line $line");
101         is($lines[$statline], "STATUS CODE: $code", "multiple check status code is correct for line $line");
104 # Passive checks
105 $result = NPTest->testCmd(
106         "./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.$$"
107         );
108 cmp_ok($result->return_code, '==', 0, "Exit always ok on passive checks");
109 open(PASV, "/tmp/check_by_ssh.$$") or die("Unable to open '/tmp/check_by_ssh.$$': $!");
110 my $count=0;
111 while (<PASV>) {
112         like($_, '/^\[\d+\] PROCESS_SERVICE_CHECK_RESULT;flint;serv;2;$responce[2]$/', 'proper result for passive check');
113         $count++;
115 cmp_ok($count, '==', 1, 'One passive result for one check performed');
116 unlink("/tmp/check_by_ssh.$$") or die("Unable to unlink '/tmp/check_by_ssh.$$': $!");
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], 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.$$"
120         );
121 cmp_ok($result->return_code, '==', 0, "Exit always ok on passive checks");
122 $count=0;
123 open(PASV, "/tmp/check_by_ssh.$$") or die("Unable to open '/tmp/check_by_ssh.$$': $!");
124 while (<PASV>) {
125         my $ret;
126         ($count == 4 ? $ret = 7 : $ret = $count);
127         like($_, '/^\[\d+\] PROCESS_SERVICE_CHECK_RESULT;flint;c' . $count . ';' . $ret . ';' . $responce[$count] . '$/', "proper result for passive check $count");
129 cmp_ok($count, '==', 5, 'Five passive result for five checks performed');
130 unlink("/tmp/check_by_ssh.$$") or die("Unable to unlink '/tmp/check_by_ssh.$$': $!");