Skip to content

Commit

Permalink
Add driver mocks and write tests to steps Remote Upload and Create VM (
Browse files Browse the repository at this point in the history
  • Loading branch information
sylviamoss authored Aug 31, 2020
1 parent cf62234 commit 942bfbf
Show file tree
Hide file tree
Showing 43 changed files with 958 additions and 143 deletions.
2 changes: 1 addition & 1 deletion builder/vsphere/clone/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
}
artifact := &common.Artifact{
Name: b.config.VMName,
VM: state.Get("vm").(*driver.VirtualMachine),
VM: state.Get("vm").(*driver.VirtualMachineDriver),
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
}
if b.config.Export != nil {
Expand Down
4 changes: 2 additions & 2 deletions builder/vsphere/clone/step_clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ type StepCloneVM struct {

func (s *StepCloneVM) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
d := state.Get("driver").(*driver.Driver)
d := state.Get("driver").(*driver.VCenterDriver)
vmPath := path.Join(s.Location.Folder, s.Location.VMName)

err := d.PreCleanVM(ui, vmPath, s.Force)
Expand Down Expand Up @@ -128,7 +128,7 @@ func (s *StepCloneVM) Cleanup(state multistep.StateBag) {
if st == nil {
return
}
vm := st.(*driver.VirtualMachine)
vm := st.(*driver.VirtualMachineDriver)

ui.Say("Destroying VM...")

Expand Down
2 changes: 1 addition & 1 deletion builder/vsphere/clone/step_customize.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (c *CustomizeConfig) Prepare() []error {
}

func (s *StepCustomize) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
vm := state.Get("vm").(*driver.VirtualMachine)
vm := state.Get("vm").(*driver.VirtualMachineDriver)
ui := state.Get("ui").(packer.Ui)

identity, err := s.identitySettings()
Expand Down
2 changes: 1 addition & 1 deletion builder/vsphere/common/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const BuilderId = "jetbrains.vsphere"
type Artifact struct {
Outconfig *OutputConfig
Name string
VM *driver.VirtualMachine
VM *driver.VirtualMachineDriver

// StateData should store data such as GeneratedData
// to be shared with post-processors
Expand Down
2 changes: 1 addition & 1 deletion builder/vsphere/common/step_boot_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type StepBootCommand struct {
func (s *StepBootCommand) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
debug := state.Get("debug").(bool)
ui := state.Get("ui").(packer.Ui)
vm := state.Get("vm").(*driver.VirtualMachine)
vm := state.Get("vm").(*driver.VirtualMachineDriver)

if s.Config.BootCommand == nil {
return multistep.ActionContinue
Expand Down
2 changes: 1 addition & 1 deletion builder/vsphere/common/step_config_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type StepConfigParams struct {

func (s *StepConfigParams) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
vm := state.Get("vm").(*driver.VirtualMachine)
vm := state.Get("vm").(*driver.VirtualMachineDriver)
configParams := make(map[string]string)

if s.Config.ConfigParams != nil {
Expand Down
2 changes: 1 addition & 1 deletion builder/vsphere/common/step_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (s *StepExport) Cleanup(multistep.StateBag) {

func (s *StepExport) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
vm := state.Get("vm").(*driver.VirtualMachine)
vm := state.Get("vm").(*driver.VirtualMachineDriver)

ui.Message("Starting export...")
lease, err := vm.Export()
Expand Down
2 changes: 1 addition & 1 deletion builder/vsphere/common/step_hardware.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type StepConfigureHardware struct {

func (s *StepConfigureHardware) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
vm := state.Get("vm").(*driver.VirtualMachine)
vm := state.Get("vm").(*driver.VirtualMachineDriver)

if *s.Config != (HardwareConfig{}) {
ui.Say("Customizing hardware...")
Expand Down
6 changes: 3 additions & 3 deletions builder/vsphere/common/step_import_to_content_library.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ type StepImportToContentLibrary struct {

func (s *StepImportToContentLibrary) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
vm := state.Get("vm").(*driver.VirtualMachine)
vm := state.Get("vm").(*driver.VirtualMachineDriver)
var err error

if s.ContentLibConfig.Ovf {
Expand All @@ -142,7 +142,7 @@ func (s *StepImportToContentLibrary) Run(_ context.Context, state multistep.Stat
return multistep.ActionContinue
}

func (s *StepImportToContentLibrary) importOvfTemplate(vm *driver.VirtualMachine) error {
func (s *StepImportToContentLibrary) importOvfTemplate(vm *driver.VirtualMachineDriver) error {
ovf := vcenter.OVF{
Spec: vcenter.CreateSpec{
Name: s.ContentLibConfig.Name,
Expand All @@ -154,7 +154,7 @@ func (s *StepImportToContentLibrary) importOvfTemplate(vm *driver.VirtualMachine
return vm.ImportOvfToContentLibrary(ovf)
}

func (s *StepImportToContentLibrary) importVmTemplate(vm *driver.VirtualMachine) error {
func (s *StepImportToContentLibrary) importVmTemplate(vm *driver.VirtualMachineDriver) error {
template := vcenter.Template{
Name: s.ContentLibConfig.Name,
Description: s.ContentLibConfig.Description,
Expand Down
4 changes: 2 additions & 2 deletions builder/vsphere/common/step_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type StepRun struct {

func (s *StepRun) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
vm := state.Get("vm").(*driver.VirtualMachine)
vm := state.Get("vm").(*driver.VirtualMachineDriver)

if s.Config.BootOrder != "" {
ui.Say("Set boot order...")
Expand Down Expand Up @@ -55,7 +55,7 @@ func (s *StepRun) Run(_ context.Context, state multistep.StateBag) multistep.Ste

func (s *StepRun) Cleanup(state multistep.StateBag) {
ui := state.Get("ui").(packer.Ui)
vm := state.Get("vm").(*driver.VirtualMachine)
vm := state.Get("vm").(*driver.VirtualMachineDriver)

if s.Config.BootOrder == "" && s.SetOrder {
ui.Say("Clear boot order...")
Expand Down
2 changes: 1 addition & 1 deletion builder/vsphere/common/step_shutdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type StepShutdown struct {
func (s *StepShutdown) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
comm := state.Get("communicator").(packer.Communicator)
vm := state.Get("vm").(*driver.VirtualMachine)
vm := state.Get("vm").(*driver.VirtualMachineDriver)

if off, _ := vm.IsPoweredOff(); off {
// Probably power off initiated by last provisioner, though disable_shutdown is not set
Expand Down
2 changes: 1 addition & 1 deletion builder/vsphere/common/step_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type StepCreateSnapshot struct {

func (s *StepCreateSnapshot) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
vm := state.Get("vm").(*driver.VirtualMachine)
vm := state.Get("vm").(*driver.VirtualMachineDriver)

if s.CreateSnapshot {
ui.Say("Creating snapshot...")
Expand Down
2 changes: 1 addition & 1 deletion builder/vsphere/common/step_ssh_key_pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (s *StepSshKeyPair) Run(ctx context.Context, state multistep.StateBag) mult
s.Comm.SSHPublicKey = kp.PublicKeyAuthorizedKeysLine
s.Comm.SSHClearAuthorizedKeys = true

vm := state.Get("vm").(*driver.VirtualMachine)
vm := state.Get("vm").(*driver.VirtualMachineDriver)
err = vm.AddPublicKeys(ctx, string(s.Comm.SSHPublicKey))
if err != nil {
state.Put("error", fmt.Errorf("error saving temporary keypair in the vm: %s", err))
Expand Down
2 changes: 1 addition & 1 deletion builder/vsphere/common/step_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type StepConvertToTemplate struct {

func (s *StepConvertToTemplate) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
vm := state.Get("vm").(*driver.VirtualMachine)
vm := state.Get("vm").(*driver.VirtualMachineDriver)

if s.ConvertToTemplate {
ui.Say("Convert VM into template...")
Expand Down
4 changes: 2 additions & 2 deletions builder/vsphere/common/step_wait_for_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (c *WaitIpConfig) GetIPNet() *net.IPNet {

func (s *StepWaitForIp) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
vm := state.Get("vm").(*driver.VirtualMachine)
vm := state.Get("vm").(*driver.VirtualMachineDriver)

var ip string
var err error
Expand Down Expand Up @@ -128,7 +128,7 @@ func (s *StepWaitForIp) Run(ctx context.Context, state multistep.StateBag) multi
}
}

func doGetIp(vm *driver.VirtualMachine, ctx context.Context, c *WaitIpConfig) (string, error) {
func doGetIp(vm *driver.VirtualMachineDriver, ctx context.Context, c *WaitIpConfig) (string, error) {
var prevIp = ""
var stopTime time.Time
var interval time.Duration
Expand Down
4 changes: 2 additions & 2 deletions builder/vsphere/common/testing/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func RenderConfig(config map[string]interface{}) string {
return string(j)
}

func TestConn(t *testing.T) *driver.Driver {
func TestConn(t *testing.T) driver.Driver {
username := os.Getenv("VSPHERE_USERNAME")
if username == "" {
username = "root"
Expand All @@ -56,7 +56,7 @@ func TestConn(t *testing.T) *driver.Driver {
return d
}

func GetVM(t *testing.T, d *driver.Driver, artifacts []packer.Artifact) *driver.VirtualMachine {
func GetVM(t *testing.T, d driver.Driver, artifacts []packer.Artifact) driver.VirtualMachine {
artifactRaw := artifacts[0]
artifact, _ := artifactRaw.(*common.Artifact)

Expand Down
4 changes: 2 additions & 2 deletions builder/vsphere/driver/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package driver
import "github.com/vmware/govmomi/object"

type Cluster struct {
driver *Driver
driver *VCenterDriver
cluster *object.ClusterComputeResource
}

func (d *Driver) FindCluster(name string) (*Cluster, error) {
func (d *VCenterDriver) FindCluster(name string) (*Cluster, error) {
c, err := d.finder.ClusterComputeResource(d.ctx, name)
if err != nil {
return nil, err
Expand Down
47 changes: 31 additions & 16 deletions builder/vsphere/driver/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,31 @@ import (
"github.com/vmware/govmomi/vim25/types"
)

type Datastore struct {
type Datastore interface {
Info(params ...string) (*mo.Datastore, error)
FileExists(path string) bool
Name() string
ResolvePath(path string) string
UploadFile(src, dst, host string, setHost bool) error
Delete(path string) error
MakeDirectory(path string) error
Reference() types.ManagedObjectReference
}

type DatastoreDriver struct {
ds *object.Datastore
driver *Driver
driver *VCenterDriver
}

func (d *Driver) NewDatastore(ref *types.ManagedObjectReference) *Datastore {
return &Datastore{
func (d *VCenterDriver) NewDatastore(ref *types.ManagedObjectReference) Datastore {
return &DatastoreDriver{
ds: object.NewDatastore(d.client.Client, *ref),
driver: d,
}
}

// If name is an empty string, then resolve host's one
func (d *Driver) FindDatastore(name string, host string) (*Datastore, error) {
func (d *VCenterDriver) FindDatastore(name string, host string) (Datastore, error) {
if name == "" {
h, err := d.FindHost(host)
if err != nil {
Expand Down Expand Up @@ -55,13 +66,13 @@ func (d *Driver) FindDatastore(name string, host string) (*Datastore, error) {
return nil, err
}

return &Datastore{
return &DatastoreDriver{
ds: ds,
driver: d,
}, nil
}

func (d *Driver) GetDatastoreName(id string) (string, error) {
func (d *VCenterDriver) GetDatastoreName(id string) (string, error) {
obj := types.ManagedObjectReference{
Type: "Datastore",
Value: id,
Expand All @@ -76,7 +87,7 @@ func (d *Driver) GetDatastoreName(id string) (string, error) {
return me.Name, nil
}

func (ds *Datastore) Info(params ...string) (*mo.Datastore, error) {
func (ds *DatastoreDriver) Info(params ...string) (*mo.Datastore, error) {
var p []string
if len(params) == 0 {
p = []string{"*"}
Expand All @@ -91,21 +102,25 @@ func (ds *Datastore) Info(params ...string) (*mo.Datastore, error) {
return &info, nil
}

func (ds *Datastore) FileExists(path string) bool {
func (ds *DatastoreDriver) FileExists(path string) bool {
_, err := ds.ds.Stat(ds.driver.ctx, path)
return err == nil
}

func (ds *Datastore) Name() string {
func (ds *DatastoreDriver) Name() string {
return ds.ds.Name()
}

func (ds *Datastore) ResolvePath(path string) string {
func (ds *DatastoreDriver) Reference() types.ManagedObjectReference {
return ds.ds.Reference()
}

func (ds *DatastoreDriver) ResolvePath(path string) string {
return ds.ds.Path(path)
}

// The file ID isn't available via the API, so we use DatastoreBrowser to search
func (d *Driver) GetDatastoreFilePath(datastoreID, dir, filename string) (string, error) {
func (d *VCenterDriver) GetDatastoreFilePath(datastoreID, dir, filename string) (string, error) {
ref := types.ManagedObjectReference{Type: "Datastore", Value: datastoreID}
ds := object.NewDatastore(d.vimClient, ref)

Expand Down Expand Up @@ -140,11 +155,11 @@ func (d *Driver) GetDatastoreFilePath(datastoreID, dir, filename string) (string
return res.File[0].GetFileInfo().Path, nil
}

func (ds *Datastore) UploadFile(src, dst, host string, set_host_for_datastore_uploads bool) error {
func (ds *DatastoreDriver) UploadFile(src, dst, host string, setHost bool) error {
p := soap.DefaultUpload
ctx := ds.driver.ctx

if set_host_for_datastore_uploads && host != "" {
if setHost && host != "" {
h, err := ds.driver.FindHost(host)
if err != nil {
return err
Expand All @@ -155,7 +170,7 @@ func (ds *Datastore) UploadFile(src, dst, host string, set_host_for_datastore_up
return ds.ds.UploadFile(ctx, src, dst, &p)
}

func (ds *Datastore) Delete(path string) error {
func (ds *DatastoreDriver) Delete(path string) error {
dc, err := ds.driver.finder.Datacenter(ds.driver.ctx, ds.ds.DatacenterPath)
if err != nil {
return err
Expand All @@ -164,7 +179,7 @@ func (ds *Datastore) Delete(path string) error {
return fm.Delete(ds.driver.ctx, path)
}

func (ds *Datastore) MakeDirectory(path string) error {
func (ds *DatastoreDriver) MakeDirectory(path string) error {
dc, err := ds.driver.finder.Datacenter(ds.driver.ctx, ds.ds.DatacenterPath)
if err != nil {
return err
Expand Down
47 changes: 47 additions & 0 deletions builder/vsphere/driver/datastore_mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package driver

import (
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/types"
)

type DatastoreMock struct {
FileExistsCalled bool
MakeDirectoryCalled bool
UploadFileCalled bool
}

func (ds *DatastoreMock) Info(params ...string) (*mo.Datastore, error) {
return nil, nil
}

func (ds *DatastoreMock) FileExists(path string) bool {
ds.FileExistsCalled = true
return false
}

func (ds *DatastoreMock) Name() string {
return "datastore-mock"
}

func (ds *DatastoreMock) Reference() types.ManagedObjectReference {
return types.ManagedObjectReference{}
}

func (ds *DatastoreMock) ResolvePath(path string) string {
return ""
}

func (ds *DatastoreMock) UploadFile(src, dst, host string, setHost bool) error {
ds.UploadFileCalled = true
return nil
}

func (ds *DatastoreMock) Delete(path string) error {
return nil
}

func (ds *DatastoreMock) MakeDirectory(path string) error {
ds.MakeDirectoryCalled = true
return nil
}
Loading

0 comments on commit 942bfbf

Please sign in to comment.