Skip to content

Commit

Permalink
common: use cdroms len instead of ISOPaths len
Browse files Browse the repository at this point in the history
There's special case when option `remove_cdrom` is set that there
would be zero cdrom devices by the time reattach_cdroms processing is
executed, so the comparison to ISOPaths len is wrong. Instead use the
amount of cdroms we have at that point.

Fixes: hashicorp#393
  • Loading branch information
Hi-Angel committed Mar 22, 2024
1 parent 4093c35 commit eb2e51c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
7 changes: 6 additions & 1 deletion builder/vsphere/common/step_reattach_cdrom.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ func (s *StepReattachCDRom) Run(_ context.Context, state multistep.StateBag) mul

// Add the CD-ROM devices to the image based on the value of `reattach_cdroms`.
// A valid ISO path is required for this step. The media will subsequently be ejected.
nAttachableCdroms := ReattachCDRom - len(s.CDRomConfig.ISOPaths)
cdroms, err := vm.CdromDevices()
if err != nil {
state.Put("error listing cdrom devices: %v", err)
return multistep.ActionHalt
}
nAttachableCdroms := ReattachCDRom - len(cdroms)
if nAttachableCdroms < 0 {
err = vm.RemoveNCdroms(int(math.Abs(float64(nAttachableCdroms))))
if err != nil {
Expand Down
16 changes: 9 additions & 7 deletions builder/vsphere/common/step_reattach_cdrom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,16 @@ func TestStepReattachCDRom_Run(t *testing.T) {
expectedAction: multistep.ActionContinue,
vmMock: &driver.VirtualMachineMock{
ReattachCDRomsCalled: true,
CdromDevicesList: object.VirtualDeviceList{nil},
},
expectedVmMock: &driver.VirtualMachineMock{
EjectCdromsCalled: true,
CdromDevicesList: object.VirtualDeviceList{nil, nil, nil, nil, nil, nil},
CdromDevicesCalled: true,
CdromDevicesList: object.VirtualDeviceList{nil, nil, nil, nil},
ReattachCDRomsCalled: true,
FindSATAControllerCalled: true,
AddCdromCalledTimes: 6,
AddCdromTypes: []string{
"sata", "sata",
"sata", "sata",
"sata", "sata",
},
AddCdromCalledTimes: 3,
AddCdromTypes: []string{"sata", "sata", "sata"},
},
fail: false,
},
Expand Down Expand Up @@ -80,6 +78,7 @@ func TestStepReattachCDRom_Run(t *testing.T) {
expectedVmMock: &driver.VirtualMachineMock{
RemoveNCdromsCalled: true,
EjectCdromsCalled: true,
CdromDevicesCalled: true,
ReattachCDRomsCalled: true,
AddCdromCalledTimes: 0,
CdromDevicesList: object.VirtualDeviceList{nil, nil},
Expand All @@ -103,11 +102,14 @@ func TestStepReattachCDRom_Run(t *testing.T) {
expectedAction: multistep.ActionContinue,
vmMock: &driver.VirtualMachineMock{
ReattachCDRomsCalled: true,
CdromDevicesList: object.VirtualDeviceList{nil, nil},
},
expectedVmMock: &driver.VirtualMachineMock{
EjectCdromsCalled: true,
CdromDevicesCalled: true,
ReattachCDRomsCalled: true,
AddCdromCalledTimes: 0,
CdromDevicesList: object.VirtualDeviceList{nil, nil},
},
fail: false,
},
Expand Down

0 comments on commit eb2e51c

Please sign in to comment.