Clean up disk_meta/dvd_meta in tests

- Add a type check for disk_meta/dvd_meta
- Fix up some places where metadata keys were using symbol keys instead of
quoted names
This commit is contained in:
Jeff Bonhag 2020-06-29 13:38:06 -04:00
parent c316d18e35
commit 63e168386a
No known key found for this signature in database
GPG Key ID: 32966F3FB5AC1129
2 changed files with 38 additions and 40 deletions

View File

@ -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<Hash>] 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<Hash>] 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

View File

@ -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).