Skip to content

Commit a5fbe40

Browse files
YuriyAMlbajolet-hashicorp
authored andcommitted
vsphere-clone: rearrange build steps to ensure source VM exists before deleting target
1 parent c038f87 commit a5fbe40

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

builder/vsphere/clone/step_clone.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,19 +135,20 @@ func (s *StepCloneVM) Run(ctx context.Context, state multistep.StateBag) multist
135135
d := state.Get("driver").(driver.Driver)
136136
vmPath := path.Join(s.Location.Folder, s.Location.VMName)
137137

138-
err := d.PreCleanVM(ui, vmPath, s.Force, s.Location.Cluster, s.Location.Host, s.Location.ResourcePool)
138+
ui.Say("Finding virtual machine to clone...")
139+
template, err := d.FindVM(s.Config.Template)
139140
if err != nil {
140-
state.Put("error", err)
141+
state.Put("error", fmt.Errorf("error finding virtual machine to clone: %s", err))
141142
return multistep.ActionHalt
142143
}
143144

144-
ui.Say("Cloning virtual machine...")
145-
template, err := d.FindVM(s.Config.Template)
145+
err = d.PreCleanVM(ui, vmPath, s.Force, s.Location.Cluster, s.Location.Host, s.Location.ResourcePool)
146146
if err != nil {
147-
state.Put("error", fmt.Errorf("error finding virtual machine to clone: %s", err))
147+
state.Put("error", err)
148148
return multistep.ActionHalt
149149
}
150150

151+
ui.Say("Cloning virtual machine...")
151152
var disks []driver.Disk
152153
for _, disk := range s.Config.StorageConfig.Storage {
153154
disks = append(disks, driver.Disk{

builder/vsphere/clone/step_clone_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ func TestStepCreateVM_Run(t *testing.T) {
159159
t.Fatalf("unexpected action: expected '%#v', but returned '%#v'", multistep.ActionContinue, action)
160160
}
161161

162+
// Find VM
163+
if !driverMock.FindVMCalled {
164+
t.Fatalf("unexpected result: expected '%s' to be called", "FindVM")
165+
}
166+
162167
// Pre clean VM
163168
if !driverMock.PreCleanVMCalled {
164169
t.Fatalf("unexpected result: expected '%s' to be called", "PreCleanVM")
@@ -170,13 +175,10 @@ func TestStepCreateVM_Run(t *testing.T) {
170175
t.Fatalf("unexpected result: expected '%s', but returned '%s'", vmPath, driverMock.PreCleanVMPath)
171176
}
172177

173-
if !driverMock.FindVMCalled {
174-
t.Fatalf("unexpected result: expected '%s' to be called", "FindVM")
175-
}
178+
// Clone VM
176179
if !vmMock.CloneCalled {
177180
t.Fatalf("unexpected result: expected '%s' to be called", "Clone")
178181
}
179-
180182
if diff := cmp.Diff(vmMock.CloneConfig, driverCreateConfig(step.Config, step.Location)); diff != "" {
181183
t.Fatalf("unexpected result: '%s'", diff)
182184
}

0 commit comments

Comments
 (0)