mirror of https://github.com/docker/docs.git
FIX #2231 - Detect duplicate interfaces
Signed-off-by: David Gageot <david@gageot.net>
This commit is contained in:
parent
35b2b042e1
commit
bf52eceed5
|
@ -16,6 +16,7 @@ const (
|
|||
|
||||
var (
|
||||
reHostonlyInterfaceCreated = regexp.MustCompile(`Interface '(.+)' was successfully created`)
|
||||
errDuplicateHostOnlyInterfaceNetworks = errors.New("VirtualBox is configured with multiple host-only interfaces with the same IP. Please remove all of them but one.")
|
||||
)
|
||||
|
||||
// Host-only network.
|
||||
|
@ -152,6 +153,10 @@ func getOrCreateHostOnlyNetwork(hostIP net.IP, netmask net.IPMask, dhcpIP net.IP
|
|||
return nil, err
|
||||
}
|
||||
|
||||
if len(nets) != countUniqueIps(nets) {
|
||||
return nil, errDuplicateHostOnlyInterfaceNetworks
|
||||
}
|
||||
|
||||
hostOnlyNet := getHostOnlyNetwork(nets, hostIP, netmask)
|
||||
if hostOnlyNet != nil {
|
||||
return hostOnlyNet, nil
|
||||
|
@ -182,6 +187,16 @@ func getOrCreateHostOnlyNetwork(hostIP net.IP, netmask net.IPMask, dhcpIP net.IP
|
|||
return hostOnlyNet, nil
|
||||
}
|
||||
|
||||
func countUniqueIps(nets map[string]*hostOnlyNetwork) int {
|
||||
ips := map[string]bool{}
|
||||
|
||||
for _, n := range nets {
|
||||
ips[n.IPv4.IP.String()] = true
|
||||
}
|
||||
|
||||
return len(ips)
|
||||
}
|
||||
|
||||
// DHCP server info.
|
||||
type dhcpServer struct {
|
||||
NetworkName string
|
||||
|
|
|
@ -211,3 +211,15 @@ func TestGetHostOnlyNetwork(t *testing.T) {
|
|||
assert.Equal(t, "HostInterfaceNetworking-vboxnet0", net.NetworkName)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestFailWithDuplicateHostOnlyNetworks(t *testing.T) {
|
||||
vbox := &VBoxManagerMock{
|
||||
args: "list hostonlyifs",
|
||||
stdOut: stdOutTwoHostOnlyNetwork,
|
||||
}
|
||||
|
||||
net, err := getOrCreateHostOnlyNetwork(net.ParseIP("192.168.99.1"), parseIPv4Mask("255.255.255.0"), nil, nil, nil, vbox)
|
||||
|
||||
assert.Nil(t, net)
|
||||
assert.Equal(t, errDuplicateHostOnlyInterfaceNetworks, err)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue