2013-12-24 13:55:44 -05:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2017-09-10 17:02:45 -04:00
|
|
|
"net"
|
2013-12-25 01:33:49 -05:00
|
|
|
"sync"
|
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2013-12-24 13:55:44 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type DriverMock struct {
|
2013-12-25 01:33:49 -05:00
|
|
|
sync.Mutex
|
|
|
|
|
2013-12-26 16:39:41 -05:00
|
|
|
CloneCalled bool
|
|
|
|
CloneDst string
|
|
|
|
CloneSrc string
|
|
|
|
CloneErr error
|
|
|
|
|
2013-12-24 13:55:44 -05:00
|
|
|
CompactDiskCalled bool
|
|
|
|
CompactDiskPath string
|
|
|
|
CompactDiskErr error
|
|
|
|
|
|
|
|
CreateDiskCalled bool
|
|
|
|
CreateDiskOutput string
|
|
|
|
CreateDiskSize string
|
|
|
|
CreateDiskTypeId string
|
|
|
|
CreateDiskErr error
|
|
|
|
|
|
|
|
IsRunningCalled bool
|
|
|
|
IsRunningPath string
|
|
|
|
IsRunningResult bool
|
|
|
|
IsRunningErr error
|
|
|
|
|
2015-06-13 19:23:33 -04:00
|
|
|
CommHostCalled bool
|
|
|
|
CommHostState multistep.StateBag
|
|
|
|
CommHostResult string
|
|
|
|
CommHostErr error
|
2013-12-24 13:55:44 -05:00
|
|
|
|
2015-11-11 07:39:06 -05:00
|
|
|
HostAddressCalled bool
|
|
|
|
HostAddressState multistep.StateBag
|
|
|
|
HostAddressResult string
|
|
|
|
HostAddressErr error
|
|
|
|
|
|
|
|
HostIPCalled bool
|
|
|
|
HostIPState multistep.StateBag
|
|
|
|
HostIPResult string
|
|
|
|
HostIPErr error
|
|
|
|
|
|
|
|
GuestAddressCalled bool
|
|
|
|
GuestAddressState multistep.StateBag
|
|
|
|
GuestAddressResult string
|
|
|
|
GuestAddressErr error
|
|
|
|
|
|
|
|
GuestIPCalled bool
|
|
|
|
GuestIPState multistep.StateBag
|
|
|
|
GuestIPResult string
|
|
|
|
GuestIPErr error
|
|
|
|
|
2013-12-24 13:55:44 -05:00
|
|
|
StartCalled bool
|
|
|
|
StartPath string
|
|
|
|
StartHeadless bool
|
|
|
|
StartErr error
|
|
|
|
|
|
|
|
StopCalled bool
|
|
|
|
StopPath string
|
|
|
|
StopErr error
|
|
|
|
|
|
|
|
SuppressMessagesCalled bool
|
|
|
|
SuppressMessagesPath string
|
|
|
|
SuppressMessagesErr error
|
|
|
|
|
|
|
|
ToolsIsoPathCalled bool
|
|
|
|
ToolsIsoPathFlavor string
|
|
|
|
ToolsIsoPathResult string
|
|
|
|
|
2014-04-21 11:25:32 -04:00
|
|
|
ToolsInstallCalled bool
|
|
|
|
ToolsInstallErr error
|
|
|
|
|
2013-12-24 13:55:44 -05:00
|
|
|
DhcpLeasesPathCalled bool
|
|
|
|
DhcpLeasesPathDevice string
|
|
|
|
DhcpLeasesPathResult string
|
|
|
|
|
2015-11-11 07:39:06 -05:00
|
|
|
DhcpConfPathCalled bool
|
|
|
|
DhcpConfPathResult string
|
|
|
|
|
2016-04-05 21:45:58 -04:00
|
|
|
VmnetnatConfPathCalled bool
|
|
|
|
VmnetnatConfPathResult string
|
2017-02-27 16:34:53 -05:00
|
|
|
|
2016-04-05 21:45:58 -04:00
|
|
|
NetmapConfPathCalled bool
|
|
|
|
NetmapConfPathResult string
|
|
|
|
|
2013-12-24 13:55:44 -05:00
|
|
|
VerifyCalled bool
|
|
|
|
VerifyErr error
|
|
|
|
}
|
|
|
|
|
2013-12-26 16:39:41 -05:00
|
|
|
func (d *DriverMock) Clone(dst string, src string) error {
|
|
|
|
d.CloneCalled = true
|
|
|
|
d.CloneDst = dst
|
|
|
|
d.CloneSrc = src
|
|
|
|
return d.CloneErr
|
|
|
|
}
|
|
|
|
|
2013-12-24 13:55:44 -05:00
|
|
|
func (d *DriverMock) CompactDisk(path string) error {
|
|
|
|
d.CompactDiskCalled = true
|
|
|
|
d.CompactDiskPath = path
|
|
|
|
return d.CompactDiskErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DriverMock) CreateDisk(output string, size string, typeId string) error {
|
|
|
|
d.CreateDiskCalled = true
|
|
|
|
d.CreateDiskOutput = output
|
|
|
|
d.CreateDiskSize = size
|
|
|
|
d.CreateDiskTypeId = typeId
|
|
|
|
return d.CreateDiskErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DriverMock) IsRunning(path string) (bool, error) {
|
2013-12-25 01:33:49 -05:00
|
|
|
d.Lock()
|
|
|
|
defer d.Unlock()
|
|
|
|
|
2013-12-24 13:55:44 -05:00
|
|
|
d.IsRunningCalled = true
|
|
|
|
d.IsRunningPath = path
|
|
|
|
return d.IsRunningResult, d.IsRunningErr
|
|
|
|
}
|
|
|
|
|
2015-06-13 19:23:33 -04:00
|
|
|
func (d *DriverMock) CommHost(state multistep.StateBag) (string, error) {
|
|
|
|
d.CommHostCalled = true
|
|
|
|
d.CommHostState = state
|
|
|
|
return d.CommHostResult, d.CommHostErr
|
2013-12-24 13:55:44 -05:00
|
|
|
}
|
|
|
|
|
2017-09-10 17:02:45 -04:00
|
|
|
func MockInterface() net.Interface {
|
|
|
|
interfaces, err := net.Interfaces()
|
|
|
|
|
|
|
|
// Build a dummy interface due to being unable to enumerate interfaces
|
|
|
|
if err != nil || len(interfaces) == 0 {
|
|
|
|
return net.Interface{
|
|
|
|
Index: 0,
|
|
|
|
MTU: -1,
|
|
|
|
Name: "dummy",
|
|
|
|
HardwareAddr: net.HardwareAddr{0, 0, 0, 0, 0, 0},
|
|
|
|
Flags: net.FlagLoopback,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the first loopback interface
|
|
|
|
for _, intf := range interfaces {
|
|
|
|
if intf.Flags&net.FlagLoopback == net.FlagLoopback {
|
|
|
|
return intf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fall-back to just the first one
|
|
|
|
return interfaces[0]
|
|
|
|
}
|
|
|
|
|
2015-11-11 07:39:06 -05:00
|
|
|
func (d *DriverMock) HostAddress(state multistep.StateBag) (string, error) {
|
2017-09-10 17:02:45 -04:00
|
|
|
intf := MockInterface()
|
|
|
|
d.HostAddressResult = intf.HardwareAddr.String()
|
2015-11-11 07:39:06 -05:00
|
|
|
d.HostAddressCalled = true
|
|
|
|
d.HostAddressState = state
|
|
|
|
return d.HostAddressResult, d.HostAddressErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DriverMock) HostIP(state multistep.StateBag) (string, error) {
|
2017-09-10 17:02:45 -04:00
|
|
|
d.HostIPResult = "127.0.0.1"
|
2015-11-11 07:39:06 -05:00
|
|
|
d.HostIPCalled = true
|
|
|
|
d.HostIPState = state
|
|
|
|
return d.HostIPResult, d.HostIPErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DriverMock) GuestAddress(state multistep.StateBag) (string, error) {
|
|
|
|
d.GuestAddressCalled = true
|
|
|
|
d.GuestAddressState = state
|
|
|
|
return d.GuestAddressResult, d.GuestAddressErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DriverMock) GuestIP(state multistep.StateBag) (string, error) {
|
|
|
|
d.GuestIPCalled = true
|
|
|
|
d.GuestIPState = state
|
|
|
|
return d.GuestIPResult, d.GuestIPErr
|
|
|
|
}
|
|
|
|
|
2013-12-24 13:55:44 -05:00
|
|
|
func (d *DriverMock) Start(path string, headless bool) error {
|
|
|
|
d.StartCalled = true
|
|
|
|
d.StartPath = path
|
|
|
|
d.StartHeadless = headless
|
|
|
|
return d.StartErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DriverMock) Stop(path string) error {
|
|
|
|
d.StopCalled = true
|
|
|
|
d.StopPath = path
|
|
|
|
return d.StopErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DriverMock) SuppressMessages(path string) error {
|
|
|
|
d.SuppressMessagesCalled = true
|
|
|
|
d.SuppressMessagesPath = path
|
|
|
|
return d.SuppressMessagesErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DriverMock) ToolsIsoPath(flavor string) string {
|
|
|
|
d.ToolsIsoPathCalled = true
|
|
|
|
d.ToolsIsoPathFlavor = flavor
|
|
|
|
return d.ToolsIsoPathResult
|
|
|
|
}
|
|
|
|
|
2014-04-21 11:25:32 -04:00
|
|
|
func (d *DriverMock) ToolsInstall() error {
|
|
|
|
d.ToolsInstallCalled = true
|
|
|
|
return d.ToolsInstallErr
|
|
|
|
}
|
|
|
|
|
2013-12-24 13:55:44 -05:00
|
|
|
func (d *DriverMock) DhcpLeasesPath(device string) string {
|
|
|
|
d.DhcpLeasesPathCalled = true
|
|
|
|
d.DhcpLeasesPathDevice = device
|
|
|
|
return d.DhcpLeasesPathResult
|
|
|
|
}
|
|
|
|
|
2015-11-11 07:39:06 -05:00
|
|
|
func (d *DriverMock) DhcpConfPath(device string) string {
|
|
|
|
d.DhcpConfPathCalled = true
|
|
|
|
return d.DhcpConfPathResult
|
|
|
|
}
|
|
|
|
|
2016-04-05 21:45:58 -04:00
|
|
|
func (d *DriverMock) VmnetnatConfPath(device string) string {
|
|
|
|
d.VmnetnatConfPathCalled = true
|
|
|
|
return d.VmnetnatConfPathResult
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DriverMock) NetmapConfPath() string {
|
|
|
|
d.NetmapConfPathCalled = true
|
|
|
|
return d.NetmapConfPathResult
|
|
|
|
}
|
|
|
|
|
2013-12-24 13:55:44 -05:00
|
|
|
func (d *DriverMock) Verify() error {
|
|
|
|
d.VerifyCalled = true
|
|
|
|
return d.VerifyErr
|
|
|
|
}
|
2017-04-13 20:19:43 -04:00
|
|
|
|
|
|
|
func (d *DriverMock) GetVmwareDriver() VmwareDriver {
|
|
|
|
var state VmwareDriver
|
|
|
|
state.DhcpLeasesPath = func(string) string {
|
|
|
|
return "/path/to/dhcp.leases"
|
|
|
|
}
|
|
|
|
state.DhcpConfPath = func(string) string {
|
|
|
|
return "/path/to/dhcp.conf"
|
|
|
|
}
|
|
|
|
state.VmnetnatConfPath = func(string) string {
|
|
|
|
return "/path/to/vmnetnat.conf"
|
|
|
|
}
|
|
|
|
state.NetmapConfPath = func() string {
|
|
|
|
return "/path/to/netmap.conf"
|
|
|
|
}
|
|
|
|
return state
|
|
|
|
}
|