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 21, 2024
1 parent b69c331 commit 19d5051
Show file tree
Hide file tree
Showing 2 changed files with 13 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
14 changes: 7 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 All @@ -79,6 +77,7 @@ func TestStepReattachCDRom_Run(t *testing.T) {
expectedVmMock: &driver.VirtualMachineMock{
RemoveNCdromsCalled: true,
EjectCdromsCalled: true,
CdromDevicesCalled: true,
ReattachCDRomsCalled: true,
AddCdromCalledTimes: 0,
},
Expand All @@ -104,6 +103,7 @@ func TestStepReattachCDRom_Run(t *testing.T) {
},
expectedVmMock: &driver.VirtualMachineMock{
EjectCdromsCalled: true,
CdromDevicesCalled: true,
ReattachCDRomsCalled: true,
AddCdromCalledTimes: 0,
},
Expand Down

0 comments on commit 19d5051

Please sign in to comment.