Merge pull request #2044 from RincewindsHat/fix/fedora-rpm-build
[monitoring-plugins.git] / plugins-root / t / check_dhcp.t
blobce62773693f3fa1bc0ef3cefe06be616a4cdd596
1 #! /usr/bin/perl -w -I ..
3 # DHCP Tests via check_dhcp
6 use strict;
7 use Test::More;
8 use NPTest;
10 my $allow_sudo = getTestParameter( "NP_ALLOW_SUDO",
11 "If sudo is setup for this user to run any command as root ('yes' to allow)",
12 "no" );
14 if ($allow_sudo eq "yes" or $> == 0) {
15 plan tests => 6;
16 } else {
17 plan skip_all => "Need sudo to test check_dhcp";
19 my $sudo = $> == 0 ? '' : 'sudo';
21 my $successOutput = '/OK: Received \d+ DHCPOFFER\(s\), \d+ of 1 requested servers responded, max lease time = \d+ sec\./';
22 my $failureOutput = '/CRITICAL: (No DHCPOFFERs were received|Received \d+ DHCPOFFER\(s\), 0 of 1 requested servers responded, max lease time = \d+ sec\.)/';
23 my $invalidOutput = '/Invalid hostname/';
25 my $host_responsive = getTestParameter( "NP_HOST_DHCP_RESPONSIVE",
26 "The hostname of system responsive to dhcp requests",
27 "localhost" );
29 my $host_nonresponsive = getTestParameter( "NP_HOST_NONRESPONSIVE",
30 "The hostname of system not responsive to dhcp requests",
31 "10.0.0.1" );
33 my $hostname_invalid = getTestParameter( "NP_HOSTNAME_INVALID",
34 "An invalid (not known to DNS) hostname",
35 "nosuchhost" );
37 # try to determince interface
38 my $interface = '';
40 # find interface used for default route
41 if (-x '/usr/sbin/ip' and `/usr/sbin/ip route get 1.1.1.1 2>/dev/null` =~ m/\sdev\s(\S+)/) {
42 $interface = "-i $1";
44 elsif (`ifconfig -a 2>/dev/null` =~ m/^(e\w*\d+)/mx and $1 ne 'eth0') {
45 $interface = ' -i '.$1;
48 my $res;
49 SKIP: {
50 skip('need responsive test host', 2) unless $host_responsive;
51 $res = NPTest->testCmd(
52 "$sudo ./check_dhcp $interface -u -s $host_responsive"
54 is( $res->return_code, 0, "Syntax ok" );
55 like( $res->output, $successOutput, "Output OK" );
58 SKIP: {
59 skip('need nonresponsive test host', 2) unless $host_nonresponsive;
60 $res = NPTest->testCmd(
61 "$sudo ./check_dhcp $interface -u -s $host_nonresponsive"
63 is( $res->return_code, 2, "Exit code - host nonresponsive" );
64 like( $res->output, $failureOutput, "Output OK" );
67 SKIP: {
68 skip('need invalid test host', 2) unless $hostname_invalid;
69 $res = NPTest->testCmd(
70 "$sudo ./check_dhcp $interface -u -s $hostname_invalid"
72 is( $res->return_code, 3, "Exit code - host invalid" );
73 like( $res->output, $invalidOutput, "Output OK" );