Prevent multiple calls to #read_storage_controllers

This commit is contained in:
Jeff Bonhag 2020-06-17 16:20:35 -04:00
parent f72ae72aaa
commit a4a082e70e
No known key found for this signature in database
GPG Key ID: 32966F3FB5AC1129
5 changed files with 26 additions and 24 deletions

View File

@ -32,7 +32,7 @@ module VagrantPlugins
else
primary_controller = storage_controllers.detect { |c| c.storage_bus == "SATA" }
if primary_controller.nil?
# raise exception
raise Vagrant::Errors::VirtualBoxDisksControllerNotFound, storage_bus: "SATA"
end
end

View File

@ -43,7 +43,11 @@ module VagrantPlugins
else
disks_defined = defined_disks.select { |d| d.type == :disk }
if disks_defined.any?
disk_controller = machine.provider.driver.get_controller("SATA")
disk_controller = storage_controllers.detect { |c| c.storage_bus == "SATA" }
if disk_controller.nil?
raise Vagrant::Errors::VirtualBoxDisksControllerNotFound, storage_bus: "SATA"
end
if (disks_defined.any? { |d| d.primary } && disks_defined.size > disk_controller.limit) ||
disks_defined.size > disk_controller.limit - 1
raise Vagrant::Errors::VirtualBoxDisksDefinedExceedLimit,
@ -54,7 +58,11 @@ module VagrantPlugins
dvds_defined = defined_disks.select { |d| d.type == :dvd }
if dvds_defined.any?
dvd_controller = machine.provider.driver.get_controller("IDE")
dvd_controller = storage_controllers.detect { |c| c.storage_bus == "IDE" }
if dvd_controller.nil?
raise Vagrant::Errors::VirtualBoxDisksControllerNotFound, storage_bus: "IDE"
end
if disks_defined.size > dvd_controller.limit
raise Vagrant::Errors::VirtualBoxDisksDefinedExceedLimit,
limit: dvd_controller.limit,
@ -174,7 +182,8 @@ module VagrantPlugins
machine.provider.driver.attach_disk(controller.name, port, device, "dvddrive", dvd.file)
# Refresh the controller information
controller = machine.provider.driver.get_controller(controller.storage_bus)
storage_controllers = machine.provider.driver.read_storage_controllers
controller = storage_controllers.detect { |c| c.name == controller.name }
attachment = controller.attachments.detect { |a| a[:port] == port && a[:device] == device }
if attachment

View File

@ -954,22 +954,6 @@ module VagrantPlugins
end
end
# Get the controller that uses the specified storage bus.
#
# A VirtualBoxDisksControllerNotFound error is raised if a compatible
# storage controller cannot be found.
#
# @param [String] storage_bus - for example, 'IDE' or 'SATA'
# @return [VagrantPlugins::ProviderVirtualBox::Model::StorageController] controller
def get_controller(storage_bus)
storage_controllers = read_storage_controllers
controller = storage_controllers.detect { |c| c.storage_bus == storage_bus }
if controller.nil?
raise Vagrant::Errors::VirtualBoxDisksControllerNotFound, storage_bus: storage_bus
end
controller
end
protected
def valid_ip_address?(ip)

View File

@ -35,10 +35,12 @@ describe VagrantPlugins::ProviderVirtualBox::Cap::CleanupDisks do
let(:controller) { double("controller", name: "controller", limit: 30, storage_bus: "SATA", maxportcount: 30) }
let(:storage_controllers) { [controller] }
before do
allow(Vagrant::Util::Experimental).to receive(:feature_enabled?).and_return(true)
allow(controller).to receive(:attachments).and_return(attachments)
allow(driver).to receive(:read_storage_controllers).and_return([controller])
allow(driver).to receive(:read_storage_controllers).and_return(storage_controllers)
end
describe "#cleanup_disks" do
@ -100,6 +102,16 @@ describe VagrantPlugins::ProviderVirtualBox::Cap::CleanupDisks do
subject.handle_cleanup_disk(machine, defined_disks, disk_meta_file[:disk])
end
end
context "with multiple storage controllers" do
let(:storage_controllers) { [ double("controller1", storage_bus: "IDE"),
double("controller2", storage_bus: "SCSI") ] }
it "assumes that disks will be attached to the SATA controller" do
expect { subject.handle_cleanup_disk(machine, defined_disks, disk_meta_file[:disk]) }.
to raise_error(Vagrant::Errors::VirtualBoxDisksControllerNotFound)
end
end
end
describe "#handle_cleanup_dvd" do

View File

@ -69,7 +69,6 @@ describe VagrantPlugins::ProviderVirtualBox::Cap::ConfigureDisks do
allow(Vagrant::Util::Experimental).to receive(:feature_enabled?).and_return(true)
allow(controller).to receive(:attachments).and_return(attachments)
allow(driver).to receive(:read_storage_controllers).and_return([controller])
allow(driver).to receive(:get_controller).with(controller.storage_bus).and_return(controller)
end
describe "#configure_disks" do
@ -120,8 +119,6 @@ describe VagrantPlugins::ProviderVirtualBox::Cap::ConfigureDisks do
before do
allow(driver).to receive(:read_storage_controllers).and_return([controller1, controller2])
allow(driver).to receive(:get_controller).with(controller1.storage_bus).and_return(controller1)
allow(driver).to receive(:get_controller).with(controller2.storage_bus).and_return(controller2)
end
it "attaches disks to the SATA controller" do