Skip to content

Commit

Permalink
F-93: Fix log output and improve test coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Jaime <[email protected]>
  • Loading branch information
jaimecb committed Dec 2, 2024
1 parent 5bff324 commit f50daba
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (p *PluginState) Handler4(req, resp *dhcpv4.DHCPv4) (*dhcpv4.DHCPv4, bool)
macPrefixMatches = true
//propose the least 4 bytes of the mac address for allocating an IP address
ipToAllocate = net.IPNet{IP: ipFromMAC}
log.Infof("MAC %s matches the prefix %x, trying to allocate IP %s...", macAddress.String(), p.MACPrefix, ipToAllocate.IP.String())
log.Infof("MAC %s matches the prefix %02x:%02x, trying to allocate IP %s...", macAddress.String(), p.MACPrefix[0], p.MACPrefix[1], ipToAllocate.IP.String())
} else {
log.Infof("MAC %s does not match the prefix %x, providing conventional lease...", macAddress.String(), p.MACPrefix)
}
Expand All @@ -98,7 +98,7 @@ func (p *PluginState) Handler4(req, resp *dhcpv4.DHCPv4) (*dhcpv4.DHCPv4, bool)
// if the MAC address is mapped to an IP address, check if the allocated IP address matches the requested one, if not, revert the allocation and return
if p.enableMAC2IP && macPrefixMatches && !ip.IP.Equal(ipToAllocate.IP) {
p.allocator.Free(ip)
log.Errorf("MAC2IP: Could not allocate IP %s for MAC %s: IP already allocated", req.ClientHWAddr.String(), err)
log.Errorf("MAC2IP: Could not allocate IP %s for MAC \"%s\": IP already allocated", ipToAllocate.IP.String(), req.ClientHWAddr.String())
return resp, true
}

Expand Down Expand Up @@ -127,7 +127,7 @@ func (p *PluginState) Handler4(req, resp *dhcpv4.DHCPv4) (*dhcpv4.DHCPv4, bool)
}
resp.YourIPAddr = record.IP
resp.Options.Update(dhcpv4.OptIPAddressLeaseTime(p.LeaseTime.Round(time.Second)))
log.Printf("found IP address %s for MAC %s", record.IP, req.ClientHWAddr.String())
log.Printf("Found IP address %s for MAC %s", record.IP, req.ClientHWAddr.String())
return resp, true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ func TestSetupRange(t *testing.T) {
expectHandlerError: false,
},
{
name: "Try to out of range IP (192.168.1.255) with MAC2IP",
name: "Try to get out of range IP (192.168.1.255) with MAC2IP",
args: []string{
"test_leases.db", // filename
"192.168.1.1", // start IP
Expand Down Expand Up @@ -392,6 +392,20 @@ func TestSetupRange(t *testing.T) {
expectSetUpError: false,
expectHandlerError: false,
},
{
name: "Try to get IP with all IPs leased and no MAC2IP",
args: []string{
"test_leases.db", // filename
"192.168.1.1", // start IP
"192.168.1.7", // end IP
"60s", // lease time
"--excluded-ips", "192.168.1.3,192.168.1.4", // excluded IPs
},
expectedLeaseTime: 60 * time.Second,
reqMACAddr: net.HardwareAddr{0x53, 0x21, 0xc0, 0xa8, 0x01, 0x64},
expectSetUpError: false,
expectHandlerError: true,
},
{
name: "Invalid Number of Arguments",
args: []string{
Expand Down Expand Up @@ -423,6 +437,17 @@ func TestSetupRange(t *testing.T) {
expectSetUpError: true,
expectHandlerError: false,
},
{
name: "Not compatible IP range with previously defined",
args: []string{
"test_leases.db", // filename
"0.0.0.0", // invalid start IP
"0.0.0.1", // end IP
"1h", // lease time
},
expectSetUpError: true,
expectHandlerError: false,
},
{
name: "Invalid Lease Time",
args: []string{
Expand Down

0 comments on commit f50daba

Please sign in to comment.