github.com/mitchellh/packer@v1.3.2/builder/vmware/common/driver_mock.go (about)

     1  package common
     2  
     3  import (
     4  	"net"
     5  	"sync"
     6  
     7  	"github.com/hashicorp/packer/helper/multistep"
     8  )
     9  
    10  type DriverMock struct {
    11  	sync.Mutex
    12  
    13  	CloneCalled bool
    14  	CloneDst    string
    15  	CloneSrc    string
    16  	Linked      bool
    17  	CloneErr    error
    18  
    19  	CompactDiskCalled bool
    20  	CompactDiskPath   string
    21  	CompactDiskErr    error
    22  
    23  	CreateDiskCalled      bool
    24  	CreateDiskOutput      string
    25  	CreateDiskSize        string
    26  	CreateDiskAdapterType string
    27  	CreateDiskTypeId      string
    28  	CreateDiskErr         error
    29  
    30  	IsRunningCalled bool
    31  	IsRunningPath   string
    32  	IsRunningResult bool
    33  	IsRunningErr    error
    34  
    35  	CommHostCalled bool
    36  	CommHostState  multistep.StateBag
    37  	CommHostResult string
    38  	CommHostErr    error
    39  
    40  	HostAddressCalled bool
    41  	HostAddressState  multistep.StateBag
    42  	HostAddressResult string
    43  	HostAddressErr    error
    44  
    45  	HostIPCalled bool
    46  	HostIPState  multistep.StateBag
    47  	HostIPResult string
    48  	HostIPErr    error
    49  
    50  	GuestAddressCalled bool
    51  	GuestAddressState  multistep.StateBag
    52  	GuestAddressResult string
    53  	GuestAddressErr    error
    54  
    55  	GuestIPCalled bool
    56  	GuestIPState  multistep.StateBag
    57  	GuestIPResult string
    58  	GuestIPErr    error
    59  
    60  	StartCalled   bool
    61  	StartPath     string
    62  	StartHeadless bool
    63  	StartErr      error
    64  
    65  	StopCalled bool
    66  	StopPath   string
    67  	StopErr    error
    68  
    69  	SuppressMessagesCalled bool
    70  	SuppressMessagesPath   string
    71  	SuppressMessagesErr    error
    72  
    73  	ToolsIsoPathCalled bool
    74  	ToolsIsoPathFlavor string
    75  	ToolsIsoPathResult string
    76  
    77  	ToolsInstallCalled bool
    78  	ToolsInstallErr    error
    79  
    80  	DhcpLeasesPathCalled bool
    81  	DhcpLeasesPathDevice string
    82  	DhcpLeasesPathResult string
    83  
    84  	DhcpConfPathCalled bool
    85  	DhcpConfPathResult string
    86  
    87  	VmnetnatConfPathCalled bool
    88  	VmnetnatConfPathResult string
    89  
    90  	NetmapConfPathCalled bool
    91  	NetmapConfPathResult string
    92  
    93  	VerifyCalled bool
    94  	VerifyErr    error
    95  }
    96  
    97  type NetworkMapperMock struct {
    98  	NameIntoDeviceCalled int
    99  	DeviceIntoNameCalled int
   100  }
   101  
   102  func (m NetworkMapperMock) NameIntoDevices(name string) ([]string, error) {
   103  	m.NameIntoDeviceCalled += 1
   104  	return make([]string, 0), nil
   105  }
   106  func (m NetworkMapperMock) DeviceIntoName(device string) (string, error) {
   107  	m.DeviceIntoNameCalled += 1
   108  	return "", nil
   109  }
   110  
   111  func (d *DriverMock) Clone(dst string, src string, linked bool) error {
   112  	d.CloneCalled = true
   113  	d.CloneDst = dst
   114  	d.CloneSrc = src
   115  	d.Linked = linked
   116  	return d.CloneErr
   117  }
   118  
   119  func (d *DriverMock) CompactDisk(path string) error {
   120  	d.CompactDiskCalled = true
   121  	d.CompactDiskPath = path
   122  	return d.CompactDiskErr
   123  }
   124  
   125  func (d *DriverMock) CreateDisk(output string, size string, adapterType string, typeId string) error {
   126  	d.CreateDiskCalled = true
   127  	d.CreateDiskOutput = output
   128  	d.CreateDiskSize = size
   129  	d.CreateDiskAdapterType = adapterType
   130  	d.CreateDiskTypeId = typeId
   131  	return d.CreateDiskErr
   132  }
   133  
   134  func (d *DriverMock) IsRunning(path string) (bool, error) {
   135  	d.Lock()
   136  	defer d.Unlock()
   137  
   138  	d.IsRunningCalled = true
   139  	d.IsRunningPath = path
   140  	return d.IsRunningResult, d.IsRunningErr
   141  }
   142  
   143  func (d *DriverMock) CommHost(state multistep.StateBag) (string, error) {
   144  	d.CommHostCalled = true
   145  	d.CommHostState = state
   146  	return d.CommHostResult, d.CommHostErr
   147  }
   148  
   149  func MockInterface() net.Interface {
   150  	interfaces, err := net.Interfaces()
   151  
   152  	// Build a dummy interface due to being unable to enumerate interfaces
   153  	if err != nil || len(interfaces) == 0 {
   154  		return net.Interface{
   155  			Index:        0,
   156  			MTU:          -1,
   157  			Name:         "dummy",
   158  			HardwareAddr: net.HardwareAddr{0, 0, 0, 0, 0, 0},
   159  			Flags:        net.FlagLoopback,
   160  		}
   161  	}
   162  
   163  	// Find the first loopback interface
   164  	for _, intf := range interfaces {
   165  		if intf.Flags&net.FlagLoopback == net.FlagLoopback {
   166  			return intf
   167  		}
   168  	}
   169  
   170  	// Fall-back to just the first one
   171  	return interfaces[0]
   172  }
   173  
   174  func (d *DriverMock) HostAddress(state multistep.StateBag) (string, error) {
   175  	intf := MockInterface()
   176  	d.HostAddressResult = intf.HardwareAddr.String()
   177  	d.HostAddressCalled = true
   178  	d.HostAddressState = state
   179  	return d.HostAddressResult, d.HostAddressErr
   180  }
   181  
   182  func (d *DriverMock) HostIP(state multistep.StateBag) (string, error) {
   183  	d.HostIPResult = "127.0.0.1"
   184  	d.HostIPCalled = true
   185  	d.HostIPState = state
   186  	return d.HostIPResult, d.HostIPErr
   187  }
   188  
   189  func (d *DriverMock) GuestAddress(state multistep.StateBag) (string, error) {
   190  	d.GuestAddressCalled = true
   191  	d.GuestAddressState = state
   192  	return d.GuestAddressResult, d.GuestAddressErr
   193  }
   194  
   195  func (d *DriverMock) GuestIP(state multistep.StateBag) (string, error) {
   196  	d.GuestIPCalled = true
   197  	d.GuestIPState = state
   198  	return d.GuestIPResult, d.GuestIPErr
   199  }
   200  
   201  func (d *DriverMock) Start(path string, headless bool) error {
   202  	d.StartCalled = true
   203  	d.StartPath = path
   204  	d.StartHeadless = headless
   205  	return d.StartErr
   206  }
   207  
   208  func (d *DriverMock) Stop(path string) error {
   209  	d.StopCalled = true
   210  	d.StopPath = path
   211  	return d.StopErr
   212  }
   213  
   214  func (d *DriverMock) SuppressMessages(path string) error {
   215  	d.SuppressMessagesCalled = true
   216  	d.SuppressMessagesPath = path
   217  	return d.SuppressMessagesErr
   218  }
   219  
   220  func (d *DriverMock) ToolsIsoPath(flavor string) string {
   221  	d.ToolsIsoPathCalled = true
   222  	d.ToolsIsoPathFlavor = flavor
   223  	return d.ToolsIsoPathResult
   224  }
   225  
   226  func (d *DriverMock) ToolsInstall() error {
   227  	d.ToolsInstallCalled = true
   228  	return d.ToolsInstallErr
   229  }
   230  
   231  func (d *DriverMock) DhcpLeasesPath(device string) string {
   232  	d.DhcpLeasesPathCalled = true
   233  	d.DhcpLeasesPathDevice = device
   234  	return d.DhcpLeasesPathResult
   235  }
   236  
   237  func (d *DriverMock) DhcpConfPath(device string) string {
   238  	d.DhcpConfPathCalled = true
   239  	return d.DhcpConfPathResult
   240  }
   241  
   242  func (d *DriverMock) VmnetnatConfPath(device string) string {
   243  	d.VmnetnatConfPathCalled = true
   244  	return d.VmnetnatConfPathResult
   245  }
   246  
   247  func (d *DriverMock) NetmapConfPath() string {
   248  	d.NetmapConfPathCalled = true
   249  	return d.NetmapConfPathResult
   250  }
   251  
   252  func (d *DriverMock) Verify() error {
   253  	d.VerifyCalled = true
   254  	return d.VerifyErr
   255  }
   256  
   257  func (d *DriverMock) GetVmwareDriver() VmwareDriver {
   258  	var state VmwareDriver
   259  	state.DhcpLeasesPath = func(string) string {
   260  		return "/path/to/dhcp.leases"
   261  	}
   262  	state.DhcpConfPath = func(string) string {
   263  		return "/path/to/dhcp.conf"
   264  	}
   265  	state.VmnetnatConfPath = func(string) string {
   266  		return "/path/to/vmnetnat.conf"
   267  	}
   268  	state.NetworkMapper = func() (NetworkNameMapper, error) {
   269  		return NetworkMapperMock{}, nil
   270  	}
   271  	return state
   272  }