mirror of https://github.com/docker/docs.git
Add more tests to virtualbox driver
Signed-off-by: David Gageot <david@gageot.net>
This commit is contained in:
parent
bbe59b8020
commit
308f9d025c
|
@ -146,20 +146,23 @@ func getHostOnlyNetwork(nets map[string]*hostOnlyNetwork, hostIP net.IP, netmask
|
|||
return nil
|
||||
}
|
||||
|
||||
func getOrCreateHostOnlyNetwork(hostIP net.IP, netmask net.IPMask, dhcpIP net.IP, dhcpUpperIP net.IP, dhcpLowerIP net.IP, vbox VBoxManager) (*hostOnlyNetwork, error) {
|
||||
func getOrCreateHostOnlyNetwork(hostIP net.IP, netmask net.IPMask, dhcpIP net.IP, dhcpLowerIP net.IP, dhcpUpperIP net.IP, vbox VBoxManager) (*hostOnlyNetwork, error) {
|
||||
nets, err := listHostOnlyNetworks(vbox)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
hostOnlyNet := getHostOnlyNetwork(nets, hostIP, netmask)
|
||||
if hostOnlyNet != nil {
|
||||
return hostOnlyNet, nil
|
||||
}
|
||||
|
||||
if hostOnlyNet == nil {
|
||||
// No existing host-only interface found. Create a new one.
|
||||
hostOnlyNet, err = createHostonlyNet(vbox)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
hostOnlyNet.IPv4.IP = hostIP
|
||||
hostOnlyNet.IPv4.Mask = netmask
|
||||
if err := hostOnlyNet.Save(vbox); err != nil {
|
||||
|
@ -169,13 +172,12 @@ func getOrCreateHostOnlyNetwork(hostIP net.IP, netmask net.IPMask, dhcpIP net.IP
|
|||
dhcp := dhcpServer{}
|
||||
dhcp.IPv4.IP = dhcpIP
|
||||
dhcp.IPv4.Mask = netmask
|
||||
dhcp.LowerIP = dhcpUpperIP
|
||||
dhcp.UpperIP = dhcpLowerIP
|
||||
dhcp.LowerIP = dhcpLowerIP
|
||||
dhcp.UpperIP = dhcpUpperIP
|
||||
dhcp.Enabled = true
|
||||
if err := addHostonlyDHCP(hostOnlyNet.Name, dhcp, vbox); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return hostOnlyNet, nil
|
||||
}
|
||||
|
|
|
@ -8,6 +8,45 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
const stdOutOneHostOnlyNetwork = `Name: vboxnet0
|
||||
GUID: 786f6276-656e-4074-8000-0a0027000000
|
||||
DHCP: Disabled
|
||||
IPAddress: 192.168.99.1
|
||||
NetworkMask: 255.255.255.0
|
||||
IPV6Address:
|
||||
IPV6NetworkMaskPrefixLength: 0
|
||||
HardwareAddress: 0a:00:27:00:00:00
|
||||
MediumType: Ethernet
|
||||
Status: Up
|
||||
VBoxNetworkName: HostInterfaceNetworking-vboxnet0
|
||||
|
||||
`
|
||||
const stdOutTwoHostOnlyNetwork = `Name: vboxnet0
|
||||
GUID: 786f6276-656e-4074-8000-0a0027000000
|
||||
DHCP: Disabled
|
||||
IPAddress: 192.168.99.1
|
||||
NetworkMask: 255.255.255.0
|
||||
IPV6Address:
|
||||
IPV6NetworkMaskPrefixLength: 0
|
||||
HardwareAddress: 0a:00:27:00:00:00
|
||||
MediumType: Ethernet
|
||||
Status: Up
|
||||
VBoxNetworkName: HostInterfaceNetworking-vboxnet0
|
||||
|
||||
Name: vboxnet1
|
||||
GUID: 786f6276-656e-4174-8000-0a0027000001
|
||||
DHCP: Disabled
|
||||
IPAddress: 192.168.99.1
|
||||
NetworkMask: 255.255.255.0
|
||||
IPV6Address:
|
||||
IPV6NetworkMaskPrefixLength: 0
|
||||
HardwareAddress: 0a:00:27:00:00:01
|
||||
MediumType: Ethernet
|
||||
Status: Up
|
||||
VBoxNetworkName: HostInterfaceNetworking-vboxnet1
|
||||
|
||||
`
|
||||
|
||||
// Tests that when we have a host only network which matches our expectations,
|
||||
// it gets returned correctly.
|
||||
func TestGetHostOnlyNetworkHappy(t *testing.T) {
|
||||
|
@ -88,19 +127,7 @@ func TestGetHostOnlyNetworkWindows10Bug(t *testing.T) {
|
|||
func TestListHostOnlyNetworks(t *testing.T) {
|
||||
vbox := &VBoxManagerMock{
|
||||
args: "list hostonlyifs",
|
||||
stdOut: `Name: vboxnet0
|
||||
GUID: 786f6276-656e-4074-8000-0a0027000000
|
||||
DHCP: Disabled
|
||||
IPAddress: 192.168.99.1
|
||||
NetworkMask: 255.255.255.0
|
||||
IPV6Address:
|
||||
IPV6NetworkMaskPrefixLength: 0
|
||||
HardwareAddress: 0a:00:27:00:00:00
|
||||
MediumType: Ethernet
|
||||
Status: Up
|
||||
VBoxNetworkName: HostInterfaceNetworking-vboxnet0
|
||||
|
||||
`,
|
||||
stdOut: stdOutOneHostOnlyNetwork,
|
||||
}
|
||||
|
||||
nets, err := listHostOnlyNetworks(vbox)
|
||||
|
@ -126,31 +153,7 @@ VBoxNetworkName: HostInterfaceNetworking-vboxnet0
|
|||
func TestListTwoHostOnlyNetworks(t *testing.T) {
|
||||
vbox := &VBoxManagerMock{
|
||||
args: "list hostonlyifs",
|
||||
stdOut: `Name: vboxnet0
|
||||
GUID: 786f6276-656e-4074-8000-0a0027000000
|
||||
DHCP: Disabled
|
||||
IPAddress: 192.168.99.1
|
||||
NetworkMask: 255.255.255.0
|
||||
IPV6Address:
|
||||
IPV6NetworkMaskPrefixLength: 0
|
||||
HardwareAddress: 0a:00:27:00:00:00
|
||||
MediumType: Ethernet
|
||||
Status: Up
|
||||
VBoxNetworkName: HostInterfaceNetworking-vboxnet0
|
||||
|
||||
Name: vboxnet1
|
||||
GUID: 786f6276-656e-4174-8000-0a0027000001
|
||||
DHCP: Disabled
|
||||
IPAddress: 192.168.99.1
|
||||
NetworkMask: 255.255.255.0
|
||||
IPV6Address:
|
||||
IPV6NetworkMaskPrefixLength: 0
|
||||
HardwareAddress: 0a:00:27:00:00:01
|
||||
MediumType: Ethernet
|
||||
Status: Up
|
||||
VBoxNetworkName: HostInterfaceNetworking-vboxnet1
|
||||
|
||||
`,
|
||||
stdOut: stdOutTwoHostOnlyNetwork,
|
||||
}
|
||||
|
||||
nets, err := listHostOnlyNetworks(vbox)
|
||||
|
@ -195,3 +198,16 @@ VBoxNetworkName: HostInterfaceNetworking-vboxnet1`,
|
|||
assert.True(t, present)
|
||||
assert.Equal(t, "vboxnet0", net.Name)
|
||||
}
|
||||
|
||||
func TestGetHostOnlyNetwork(t *testing.T) {
|
||||
vbox := &VBoxManagerMock{
|
||||
args: "list hostonlyifs",
|
||||
stdOut: stdOutOneHostOnlyNetwork,
|
||||
}
|
||||
|
||||
net, err := getOrCreateHostOnlyNetwork(net.ParseIP("192.168.99.1"), parseIPv4Mask("255.255.255.0"), nil, nil, nil, vbox)
|
||||
|
||||
assert.NotNil(t, net)
|
||||
assert.Equal(t, "HostInterfaceNetworking-vboxnet0", net.NetworkName)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue