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 b8725ed commit fbbae0a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
9 changes: 7 additions & 2 deletions 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 All @@ -75,7 +80,7 @@ func (s *StepReattachCDRom) Run(_ context.Context, state multistep.StateBag) mul
}

// Add CD-ROMs, if required.
if nAttachableCdroms > 0 {
if nAttachableCdroms >= 0 {
// If the CD-ROM device type is SATA, make sure SATA controller is present.
if s.CDRomConfig.CdromType == "sata" {
if _, err := vm.FindSATAController(); err == driver.ErrNoSataController {
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 fbbae0a

Please sign in to comment.