Change dhcp plugin to send ClientID allowing container to have multiple CNI

interfaces using dhcp ipam.

Vendor latest dhcp4server, dhcp4client, dhcp4

Added additional tests for new functionality in dhcp2_test.go

Wrap d2g dhcp4client calls with our own which add clientID to packet.
This commit is contained in:
Michael Cambria
2018-10-15 10:41:02 -04:00
parent 227a4c15fa
commit 0af31fc4d0
17 changed files with 882 additions and 131 deletions

View File

@@ -3,6 +3,7 @@ package dhcp4server_test
import (
"bytes"
"encoding/binary"
"fmt"
"log"
"net"
"sync"
@@ -409,6 +410,69 @@ func BenchmarkServeDHCP(test *testing.B) {
}
}
/*
*
*/
func TestLeaseByClientID(test *testing.T) {
//Setup the Server
myServer, err := dhcp4server.New(
net.IPv4(127, 0, 0, 1),
getTestLeasePool(),
)
if err != nil {
test.Error("Error: Can't Configure Server " + err.Error())
}
// Setup A Client
// Although We Won't send the packets over the network we'll use the client to create the requests.
c, err := dhcp4client.NewInetSock(dhcp4client.SetLocalAddr(net.UDPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 1068}), dhcp4client.SetRemoteAddr(net.UDPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 1067}))
if err != nil {
test.Error("Client Conection Generation:" + err.Error())
}
client, err := dhcp4client.New(dhcp4client.Connection(c))
if err != nil {
test.Error("Error: Can't Configure Client " + err.Error())
}
defer client.Close()
//Generate Hardware Address; used by both clients
HardwareMACAddress, err := hardwareaddr.GenerateEUI48()
if err != nil {
test.Error("Error: Can't Generate Valid MACAddress" + err.Error())
}
for i := 0; i < 2; i++ {
client.SetOption(dhcp4client.HardwareAddr(HardwareMACAddress))
test.Log("MAC:" + HardwareMACAddress.String())
clientID := []byte(fmt.Sprintf("clientid-%d", i))
test.Log("ClientID:" + string(clientID))
discovery := client.DiscoverPacket()
discovery.AddOption(dhcp4.OptionClientIdentifier, clientID)
//Run the Discovery On the Server
offer, err := myServer.ServeDHCP(discovery)
_, err = myServer.ServeDHCP(discovery)
if err != nil {
test.Error("Discovery Error:" + err.Error())
}
request := client.RequestPacket(&offer)
request.AddOption(dhcp4.OptionClientIdentifier, clientID)
acknowledgement, err := myServer.ServeDHCP(request)
if err != nil {
test.Error("Acknowledge Error:" + err.Error())
}
test.Logf("Received Lease:%v\n", acknowledgement.YIAddr().String())
if !dhcp4.IPAdd(net.IPv4(192, 168, 1, 1), i).Equal(acknowledgement.YIAddr()) {
test.Error("Expected IP:" + dhcp4.IPAdd(net.IPv4(192, 168, 1, 1), i).String() + " Received:" + acknowledgement.YIAddr().String())
}
}
}
func getTestLeasePool() *memorypool.MemoryPool {
//Create a Lease Pool We're going to use a memory pool
//Remember the memory is cleared on restart so you will reissue the same IP Addresses.