Merge pull request #2056 from monitoring-plugins/fix_check_http_state_regex
[monitoring-plugins.git] / plugins / t / check_udp.t
blob6c47d0959f7fdc2bc94ad29ed0dfc11e96a0b950
1 #! /usr/bin/perl -w -I ..
3 # UDP Connection Based Tests via check_udp
7 use strict;
8 use Test::More;
9 use NPTest;
11 my $res;
13 alarm(120); # make sure tests don't hang
15 plan tests => 14;
17 $res = NPTest->testCmd( "./check_udp -H localhost -p 3333" );
18 cmp_ok( $res->return_code, '==', 3, "Need send/expect string");
19 like ( $res->output, '/With UDP checks, a send/expect string must be specified./', "Output OK");
21 $res = NPTest->testCmd( "./check_udp -H localhost -p 3333 -s send" );
22 cmp_ok( $res->return_code, '==', 3, "Need expect string");
23 like ( $res->output, '/With UDP checks, a send/expect string must be specified./', "Output OK");
25 $res = NPTest->testCmd( "./check_udp -H localhost -p 3333 -e expect" );
26 cmp_ok( $res->return_code, '==', 3, "Need send string");
27 like ( $res->output, '/With UDP checks, a send/expect string must be specified./', "Output OK");
29 $res = NPTest->testCmd( "./check_udp -H localhost -p 3333 -s foo -e bar" );
30 cmp_ok( $res->return_code, '==', 2, "Errors correctly because no udp service running" );
31 like ( $res->output, '/No data received from host/', "Output OK");
33 my $nc;
34 if(system("which nc.traditional >/dev/null 2>&1") == 0) {
35 $nc = 'nc.traditional -w 3 -l -u -p 3333';
37 elsif(system("which nc >/dev/null 2>&1") == 0) {
38 $nc = 'nc -w 3 -l -u -4 localhost 3333';
40 elsif(system("which netcat >/dev/null 2>&1") == 0) {
41 $nc = 'netcat -w 3 -l -u -p 3333';
44 SKIP: {
45 skip "solaris netcat does not listen to udp", 6 if $^O eq 'solaris';
46 skip "No netcat available", 6 unless $nc;
47 open (NC, "echo 'barbar' | $nc |");
48 sleep 1;
49 $res = NPTest->testCmd( "./check_udp -H localhost -p 3333 -s '' -e barbar -4" );
50 cmp_ok( $res->return_code, '==', 0, "Got barbar response back" );
51 like ( $res->output, '/\[barbar\]/', "Output OK");
52 close NC;
54 # Start up a udp server listening on port 3333, quit after 3 seconds
55 # Otherwise will hang at close
56 my $pid = open(NC, "$nc </dev/null |");
57 sleep 1; # Allow nc to startup
59 my $start = time;
60 $res = NPTest->testCmd( "./check_udp -H localhost -p 3333 -s foofoo -e barbar -t 5 -4" );
61 my $duration = time - $start;
62 cmp_ok( $res->return_code, '==', '2', "Hung waiting for response");
63 like ( $res->output, '/Socket timeout after 5 seconds/', "Timeout message");
64 like ( $duration, '/^[56]$/', "Timeout after 5 (possibly 6) seconds");
65 my $read_nc = <NC> || '';
66 close NC;
67 cmp_ok( $read_nc, 'eq', "foofoo", "Data received correctly" );
71 alarm(0); # disable alarm