Code

Add comment to recent check_disk patch
[nagiosplug.git] / plugins / t / check_udp.t
1 #! /usr/bin/perl -w -I ..
2 #
3 # UDP Connection Based Tests via check_udp
4 #
5 # $Id$
6 #
8 use strict;
9 use Test::More;
10 use NPTest;
12 my $res;
14 plan tests => 14;
16 $res = NPTest->testCmd( "./check_udp -H localhost -p 3333" );
17 cmp_ok( $res->return_code, '==', 3, "Need send/expect string");
18 like  ( $res->output, '/With UDP checks, a send/expect string must be specified./', "Output OK");
20 $res = NPTest->testCmd( "./check_udp -H localhost -p 3333 -s send" );
21 cmp_ok( $res->return_code, '==', 3, "Need expect string");
22 like  ( $res->output, '/With UDP checks, a send/expect string must be specified./', "Output OK");
24 $res = NPTest->testCmd( "./check_udp -H localhost -p 3333 -e expect" );
25 cmp_ok( $res->return_code, '==', 3, "Need send string");
26 like  ( $res->output, '/With UDP checks, a send/expect string must be specified./', "Output OK");
28 $res = NPTest->testCmd( "./check_udp -H localhost -p 3333 -s foo -e bar" );
29 cmp_ok( $res->return_code, '==', 2, "Errors correctly because no udp service running" );
30 like  ( $res->output, '/No data received from host/', "Output OK");
32 SKIP: {
33         skip "No netcat available", 6 unless (system("which nc > /dev/null") == 0);
34         open (NC, "echo 'barbar' | nc -l -p 3333 -u |");
35         sleep 1;
36         $res = NPTest->testCmd( "./check_udp -H localhost -p 3333 -s '' -e barbar -4" );
37         cmp_ok( $res->return_code, '==', 0, "Got barbar response back" );
38         like  ( $res->output, '/\[barbar\]/', "Output OK");
39         close NC;
41         # Start up a udp server listening on port 3333, quit after 3 seconds
42         # Otherwise will hang at close
43         my $pid = open(NC, "nc -l -p 3333 -u -w 3 </dev/null |");
44         sleep 1;        # Allow nc to startup
46         my $start = time;
47         $res = NPTest->testCmd( "./check_udp -H localhost -p 3333 -s foofoo -e barbar -t 5 -4" );
48         my $duration = time - $start;
49         cmp_ok( $res->return_code, '==', '2', "Hung waiting for response");
50         like  ( $res->output, '/Socket timeout after 5 seconds/', "Timeout message");
51         like  ( $duration, '/^[56]$/', "Timeout after 5 (possibly 6) seconds");
52         my $read_nc = <NC>;
53         close NC;
54         cmp_ok( $read_nc, 'eq', "foofoo", "Data received correctly" );
55 }