Skip to content
This repository has been archived by the owner on Sep 26, 2021. It is now read-only.

Commit

Permalink
Merge pull request #2369 from dgageot/detect-multiple-host-only
Browse files Browse the repository at this point in the history
Detect multiple host only
  • Loading branch information
nathanleclaire committed Nov 20, 2015
2 parents c249659 + bf52ece commit 863e517
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
17 changes: 16 additions & 1 deletion drivers/virtualbox/network.go
Expand Up @@ -15,7 +15,8 @@ const (
)

var (
reHostonlyInterfaceCreated = regexp.MustCompile(`Interface '(.+)' was successfully created`)
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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions drivers/virtualbox/network_test.go
Expand Up @@ -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)
}

0 comments on commit 863e517

Please sign in to comment.