From ff53c64fbc86fac843ee5bbb0f5bf49e6e4b8b9e Mon Sep 17 00:00:00 2001 From: Jeff Bonhag Date: Wed, 17 Jun 2020 13:50:06 -0400 Subject: [PATCH] Fix test, add test .recover_from_resize --- .../virtualbox/cap/configure_disks.rb | 10 +++--- .../virtualbox/cap/configure_disks_test.rb | 33 +++++++++++++++---- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/plugins/providers/virtualbox/cap/configure_disks.rb b/plugins/providers/virtualbox/cap/configure_disks.rb index cb00dcb4b..124c04e45 100644 --- a/plugins/providers/virtualbox/cap/configure_disks.rb +++ b/plugins/providers/virtualbox/cap/configure_disks.rb @@ -306,11 +306,12 @@ module VagrantPlugins def self.resize_disk(machine, disk_config, defined_disk, controller) machine.ui.detail(I18n.t("vagrant.cap.configure_disks.resize_disk", name: disk_config.name), prefix: true) + # grab disk to be resized port and device number + disk_info = machine.provider.driver.get_port_and_device(defined_disk["UUID"]) + if defined_disk["Storage format"] == "VMDK" LOGGER.warn("Disk type VMDK cannot be resized in VirtualBox. Vagrant will convert disk to VDI format to resize first, and then convert resized disk back to VMDK format") - # grab disk to be resized port and device number - disk_info = machine.provider.driver.get_port_and_device(defined_disk["UUID"]) # original disk information in case anything goes wrong during clone/resize original_disk = defined_disk backup_disk_location = "#{original_disk["Location"]}.backup" @@ -345,7 +346,6 @@ module VagrantPlugins location: original_disk["Location"], name: machine.name)) recover_from_resize(machine, disk_info, backup_disk_location, original_disk, vdi_disk_file, controller) - raise ensure # Remove backup disk file if all goes well @@ -360,7 +360,6 @@ module VagrantPlugins defined_disk = new_disk_info else machine.provider.driver.resize_disk(defined_disk["Location"], disk_config.size.to_i) - disk_info = machine.provider.driver.get_port_and_device(defined_disk["UUID"]) end disk_metadata = { uuid: defined_disk["UUID"], name: disk_config.name, controller: controller.name, @@ -379,7 +378,8 @@ module VagrantPlugins # @param [String] backup_disk_location - The place on disk where vagrant made a backup of the original disk being resized # @param [Hash] original_disk - The disk information from VirtualBox # @param [String] vdi_disk_file - The place on disk where vagrant made a clone of the original disk being resized - def self.recover_from_resize(machine, disk_info, backup_disk_location, original_disk, vdi_disk_file) + # @param [VagrantPlugins::ProviderVirtualBox::Model::StorageController] controller - the storage controller to use + def self.recover_from_resize(machine, disk_info, backup_disk_location, original_disk, vdi_disk_file, controller) begin # move backup to original name FileUtils.mv(backup_disk_location, original_disk["Location"], force: true) diff --git a/test/unit/plugins/providers/virtualbox/cap/configure_disks_test.rb b/test/unit/plugins/providers/virtualbox/cap/configure_disks_test.rb index 616c51ee3..94c88c7db 100644 --- a/test/unit/plugins/providers/virtualbox/cap/configure_disks_test.rb +++ b/test/unit/plugins/providers/virtualbox/cap/configure_disks_test.rb @@ -108,7 +108,7 @@ describe VagrantPlugins::ProviderVirtualBox::Cap::ConfigureDisks do context "with over the disk limit for a given device" do let(:defined_disks) { (1..controller.limit).map { |i| double("disk-#{i}", type: :disk, primary: false) }.to_a } - it "raises an exception if the disks defined exceed the limit for a SATA Controller" do + it "raises an exception if the disks defined exceed the limit" do expect{subject.configure_disks(machine, defined_disks)}. to raise_error(Vagrant::Errors::VirtualBoxDisksDefinedExceedLimit) end @@ -264,6 +264,7 @@ describe VagrantPlugins::ProviderVirtualBox::Cap::ConfigureDisks do expect(driver).to receive(:attach_disk).with((disk_info[:port].to_i + 1).to_s, disk_info[:device], all_disks[1]["Location"], + "hdd", controller.name) subject.handle_configure_disk(machine, defined_disks[1], all_disks, controller) @@ -320,6 +321,7 @@ describe VagrantPlugins::ProviderVirtualBox::Cap::ConfigureDisks do expect(driver).to receive(:attach_disk).with(port_and_device[:port], port_and_device[:device], disk_file, + "hdd", controller.name) subject.create_disk(machine, disk_config, controller) @@ -403,12 +405,7 @@ describe VagrantPlugins::ProviderVirtualBox::Cap::ConfigureDisks do allow(driver).to receive(:vdi_to_vmdk).and_raise(StandardError) - expect(FileUtils).to receive(:mv).with("#{vmdk_disk_file}.backup", vmdk_disk_file, force: true). - and_return(true) - - expect(driver).to receive(:attach_disk). - with(attach_info[:port], attach_info[:device], vmdk_disk_file, "hdd", controller).and_return(true) - expect(driver).to receive(:close_medium).with(vdi_disk_file).and_return(true) + expect(subject).to receive(:recover_from_resize).with(machine, attach_info, "#{vmdk_disk_file}.backup", all_disks[0], vdi_disk_file, controller) expect{subject.resize_disk(machine, disk_config, all_disks[0], controller)}.to raise_error(Exception) end @@ -439,6 +436,28 @@ describe VagrantPlugins::ProviderVirtualBox::Cap::ConfigureDisks do end end + describe ".recover_from_resize" do + let(:disk_config) { double("disk", name: "vagrant_primary", size: 1073741824.0, + primary: false, type: :disk, disk_ext: "vmdk", + provider_config: nil) } + let(:attach_info) { {port: "0", device: "0"} } + let(:vdi_disk_file) { "/home/vagrant/VirtualBox VMs/ubuntu-18.04-amd64-disk001.vdi" } + let(:vmdk_disk_file) { "/home/vagrant/VirtualBox VMs/ubuntu-18.04-amd64-disk001.vmdk" } + let(:vmdk_backup_file) { "/home/vagrant/VirtualBox VMs/ubuntu-18.04-amd64-disk001.vmdk.backup" } + + it "reattaches the original disk file and closes the cloned medium" do + expect(FileUtils).to receive(:mv).with(vmdk_backup_file, vmdk_disk_file, force: true). + and_return(true) + + expect(driver).to receive(:attach_disk). + with(attach_info[:port], attach_info[:device], vmdk_disk_file, "hdd", controller.name).and_return(true) + + expect(driver).to receive(:close_medium).with(vdi_disk_file).and_return(true) + + subject.recover_from_resize(machine, attach_info, vmdk_backup_file, all_disks[0], vdi_disk_file, controller) + end + end + describe ".handle_configure_dvd" do let(:dvd_config) { double("dvd", file: "/tmp/untitled.iso", name: "dvd1") }