1 #! /usr/bin/perl -w -I ..
3 # DHCP Tests via check_dhcp
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)",
14 if ($allow_sudo eq "yes" or $> == 0) {
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",
29 my $host_nonresponsive = getTestParameter
( "NP_HOST_NONRESPONSIVE",
30 "The hostname of system not responsive to dhcp requests",
33 my $hostname_invalid = getTestParameter
( "NP_HOSTNAME_INVALID",
34 "An invalid (not known to DNS) hostname",
37 # try to determince 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+)/) {
44 elsif (`ifconfig -a 2>/dev/null` =~ m/^(e\w*\d+)/mx and $1 ne 'eth0') {
45 $interface = ' -i '.$1;
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" );
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" );
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" );