diff --git a/plugins/providers/virtualbox/cap/cleanup_disks.rb b/plugins/providers/virtualbox/cap/cleanup_disks.rb index 33e0efc10..c95a91f42 100644 --- a/plugins/providers/virtualbox/cap/cleanup_disks.rb +++ b/plugins/providers/virtualbox/cap/cleanup_disks.rb @@ -24,8 +24,9 @@ module VagrantPlugins # @param [Vagrant::Machine] machine # @param [VagrantPlugins::Kernel_V2::VagrantConfigDisk] defined_disks - # @param [Hash] disk_meta - A hash of all the previously defined disks from the last configure_disk action + # @param [Array] disk_meta - An array of all the previously defined disks from the last configure_disk action def self.handle_cleanup_disk(machine, defined_disks, disk_meta) + raise TypeError, "Expected `Array` but received `#{disk_meta.class}`" if !disk_meta.is_a?(Array) storage_controllers = machine.provider.driver.read_storage_controllers primary_controller = storage_controllers.get_primary_controller @@ -35,52 +36,49 @@ module VagrantPlugins end primary_uuid = primary[:uuid] - if disk_meta - disk_meta.each do |d| - dsk = defined_disks.select { |dk| dk.name == d["name"] } - if !dsk.empty? || d["uuid"] == primary_uuid - next + disk_meta.each do |d| + dsk = defined_disks.select { |dk| dk.name == d["name"] } + if !dsk.empty? || d["uuid"] == primary_uuid + next + else + LOGGER.warn("Found disk not in Vagrantfile config: '#{d["name"]}'. Removing disk from guest #{machine.name}") + machine.ui.warn(I18n.t("vagrant.cap.cleanup_disks.disk_cleanup", name: d["name"]), prefix: true) + + controller = storage_controllers.get_controller!(name: d["controller"]) + attachment = controller.get_attachment(uuid: d["uuid"]) + + if !attachment + LOGGER.warn("Disk '#{d["name"]}' not attached to guest, but still exists.") else - LOGGER.warn("Found disk not in Vagrantfile config: '#{d["name"]}'. Removing disk from guest #{machine.name}") - machine.ui.warn(I18n.t("vagrant.cap.cleanup_disks.disk_cleanup", name: d["name"]), prefix: true) - - controller = storage_controllers.get_controller!(name: d["controller"]) - attachment = controller.get_attachment(uuid: d["uuid"]) - - if !attachment - LOGGER.warn("Disk '#{d["name"]}' not attached to guest, but still exists.") - else - machine.provider.driver.remove_disk(controller.name, attachment[:port], attachment[:device]) - end - - machine.provider.driver.close_medium(d["uuid"]) + machine.provider.driver.remove_disk(controller.name, attachment[:port], attachment[:device]) end + + machine.provider.driver.close_medium(d["uuid"]) end end end # @param [Vagrant::Machine] machine # @param [VagrantPlugins::Kernel_V2::VagrantConfigDisk] defined_dvds - # @param [Hash] dvd_meta - A hash of all the previously defined dvds from the last configure_disk action + # @param [Array] dvd_meta - An array of all the previously defined dvds from the last configure_disk action def self.handle_cleanup_dvd(machine, defined_dvds, dvd_meta) - if dvd_meta - dvd_meta.each do |d| - dsk = defined_dvds.select { |dk| dk.name == d["name"] } - if !dsk.empty? - next + raise TypeError, "Expected `Array` but received `#{dvd_meta.class}`" if !dvd_meta.is_a?(Array) + dvd_meta.each do |d| + dsk = defined_dvds.select { |dk| dk.name == d["name"] } + if !dsk.empty? + next + else + LOGGER.warn("Found dvd not in Vagrantfile config: '#{d["name"]}'. Removing dvd from guest #{machine.name}") + machine.ui.warn("DVD '#{d["name"]}' no longer exists in Vagrant config. Removing medium from guest...", prefix: true) + + storage_controllers = machine.provider.driver.read_storage_controllers + controller = storage_controllers.get_controller!(name: d["controller"]) + attachment = controller.get_attachment(uuid: d["uuid"]) + + if !attachment + LOGGER.warn("DVD '#{d["name"]}' not attached to guest, but still exists.") else - LOGGER.warn("Found dvd not in Vagrantfile config: '#{d["name"]}'. Removing dvd from guest #{machine.name}") - machine.ui.warn("DVD '#{d["name"]}' no longer exists in Vagrant config. Removing medium from guest...", prefix: true) - - storage_controllers = machine.provider.driver.read_storage_controllers - controller = storage_controllers.get_controller!(name: d["controller"]) - attachment = controller.get_attachment(uuid: d["uuid"]) - - if !attachment - LOGGER.warn("DVD '#{d["name"]}' not attached to guest, but still exists.") - else - machine.provider.driver.remove_disk(controller.name, attachment[:port], attachment[:device]) - end + machine.provider.driver.remove_disk(controller.name, attachment[:port], attachment[:device]) end end end diff --git a/test/unit/plugins/providers/virtualbox/cap/cleanup_disks_test.rb b/test/unit/plugins/providers/virtualbox/cap/cleanup_disks_test.rb index dcbf95274..897085af8 100644 --- a/test/unit/plugins/providers/virtualbox/cap/cleanup_disks_test.rb +++ b/test/unit/plugins/providers/virtualbox/cap/cleanup_disks_test.rb @@ -27,7 +27,7 @@ describe VagrantPlugins::ProviderVirtualBox::Cap::CleanupDisks do let(:subject) { described_class } - let(:disk_meta_file) { {disk: [], floppy: [], dvd: []} } + let(:disk_meta_file) { {"disk" => [], "floppy" => [], "dvd" => []} } let(:defined_disks) { {} } let(:attachments) { [{port: "0", device: "0", uuid: "12345"}, @@ -54,7 +54,7 @@ describe VagrantPlugins::ProviderVirtualBox::Cap::CleanupDisks do end context "with disks to clean up" do - let(:disk_meta_file) { {disk: [{uuid: "1234", name: "storage"}], floppy: [], dvd: []} } + let(:disk_meta_file) { {"disk" => [{"uuid" => "1234", "name" => "storage"}], "floppy" => [], "dvd" => []} } it "calls the cleanup method if a disk_meta file is defined" do expect(subject).to receive(:handle_cleanup_disk). @@ -72,7 +72,7 @@ describe VagrantPlugins::ProviderVirtualBox::Cap::CleanupDisks do end context "with dvd attached" do - let(:disk_meta_file) { {dvd: [{uuid: "12345", name: "iso"}]} } + let(:disk_meta_file) { {"disk" => [], "floppy" => [], "dvd" => [{"uuid" => "12345", "name" => "iso"}] } } it "calls the cleanup method if a disk_meta file is defined" do expect(subject).to receive(:handle_cleanup_dvd).