Code

Add check_by_ssh tests (passive checks are broken - should be fixed before next release!)
[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 #SKIP {
38 #       skip "SSH_HOST and SSH_IDENTITY must be defined", 36 unless($ssh_service && $ssh_key);
40         my $result;
41         
42         ## Single active checks
43         
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         }
51         
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)");
57         
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)");
63         
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)");
69         
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)");
75         
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)");
81         
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");
87         
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         my %linemap = (
95                        '0' => '1',
96                        '2' => '0',
97                        '4' => '3',
98                        '6' => '2',
99         );
100         foreach my $line (0, 2, 4, 6) {
101                 my $code = $linemap{$line};
102                 my $statline = $line+1;
103                 is($lines[$line], "$responce[$code]", "multiple checks status text is correct for line $line");
104                 is($lines[$statline], "STATUS CODE: $code", "multiple check status code is correct for line $line");
105         }
106         
107         # Passive checks
108         $result = NPTest->testCmd(
109                 "./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.$$"
110                 );
111         cmp_ok($result->return_code, '==', 0, "Exit always ok on passive checks");
112         open(PASV, "/tmp/check_by_ssh.$$") or die("Unable to open '/tmp/check_by_ssh.$$': $!");
113         my $count=0;
114         while (<PASV>) {
115                 like($_, '/^\[\d+\] PROCESS_SERVICE_CHECK_RESULT;flint;serv;2;$responce[2]$/', 'proper result for passive check');
116                 $count++;
117         }
118         cmp_ok($count, '==', 1, 'One passive result for one check performed');
119         unlink("/tmp/check_by_ssh.$$") or die("Unable to unlink '/tmp/check_by_ssh.$$': $!");
120         
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], 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.$$"
123                 );
124         cmp_ok($result->return_code, '==', 0, "Exit always ok on passive checks");
125         $count=0;
126         open(PASV, "/tmp/check_by_ssh.$$") or die("Unable to open '/tmp/check_by_ssh.$$': $!");
127         while (<PASV>) {
128                 my $ret;
129                 ($count == 4 ? $ret = 7 : $ret = $count);
130                 like($_, '/^\[\d+\] PROCESS_SERVICE_CHECK_RESULT;flint;c' . $count . ';' . $ret . ';' . $responce[$count] . '$/', "proper result for passive check $count");
131         }
132         cmp_ok($count, '==', 5, 'Five passive result for five checks performed');
133         unlink("/tmp/check_by_ssh.$$") or die("Unable to unlink '/tmp/check_by_ssh.$$': $!");
134 #}