From eff3d47f82501482496114cf545f4fdc67f96b66 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Thu, 23 Jan 2020 09:10:31 -0800 Subject: [PATCH] Add cleanup_disks test --- lib/vagrant/action/builtin/cleanup_disks.rb | 3 + .../action/builtin/cleanup_disks_test.rb | 55 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 test/unit/vagrant/action/builtin/cleanup_disks_test.rb diff --git a/lib/vagrant/action/builtin/cleanup_disks.rb b/lib/vagrant/action/builtin/cleanup_disks.rb index 77f29e049..08fbf4c8e 100644 --- a/lib/vagrant/action/builtin/cleanup_disks.rb +++ b/lib/vagrant/action/builtin/cleanup_disks.rb @@ -14,6 +14,9 @@ module Vagrant machine = env[:machine] defined_disks = get_disks(machine, env) + # TODO: Maybe always cleanup disks, even if no disks are defined. + # Check meta file first + # # Call into providers machine implementation for disk management if !defined_disks.empty? if machine.provider.capability?(:cleanup_disks) diff --git a/test/unit/vagrant/action/builtin/cleanup_disks_test.rb b/test/unit/vagrant/action/builtin/cleanup_disks_test.rb new file mode 100644 index 000000000..c79f50a0e --- /dev/null +++ b/test/unit/vagrant/action/builtin/cleanup_disks_test.rb @@ -0,0 +1,55 @@ +require File.expand_path("../../../../base", __FILE__) + +describe Vagrant::Action::Builtin::CleanupDisks do + let(:app) { lambda { |env| } } + let(:vm) { double("vm") } + let(:config) { double("config", vm: vm) } + let(:provider) { double("provider") } + let(:machine) { double("machine", config: config, provider: provider, + provider_name: "provider", data_dir: Pathname.new("/fake/dir")) } + let(:env) { { ui: ui, machine: machine} } + + let(:disks) { [double("disk")] } + + let(:ui) { double("ui") } + + let(:disk_meta_file) { {disk: [{uuid: "123456789", name: "storage"}], floppy: [], dvd: []} } + + describe "#call" do + it "calls configure_disks if disk config present" do + allow(vm).to receive(:disks).and_return(disks) + allow(machine).to receive(:disks).and_return(disks) + allow(machine.provider).to receive(:capability?).with(:cleanup_disks).and_return(true) + subject = described_class.new(app, env) + + expect(app).to receive(:call).with(env).ordered + expect(subject).to receive(:read_disk_metadata).with(machine).and_return(disk_meta_file) + expect(machine.provider).to receive(:capability). + with(:cleanup_disks, disks, disk_meta_file) + + subject.call(env) + end + + it "continues on if no disk config present" do + allow(vm).to receive(:disks).and_return([]) + subject = described_class.new(app, env) + + expect(app).to receive(:call).with(env).ordered + expect(machine.provider).not_to receive(:capability).with(:cleanup_disks, disks) + + subject.call(env) + end + + it "prints a warning if disk config capability is unsupported" do + allow(vm).to receive(:disks).and_return(disks) + allow(machine.provider).to receive(:capability?).with(:cleanup_disks).and_return(false) + subject = described_class.new(app, env) + + expect(app).to receive(:call).with(env).ordered + expect(machine.provider).not_to receive(:capability).with(:cleanup_disks, disks) + expect(ui).to receive(:warn) + + subject.call(env) + end + end +end