virtualbox: Fix usability test to reject bad installs without crashing

If VirtualBox is installed but the kernel module is missing or the
service is stopped, the usability test should fail without crashing so
we can fall back to other providers.

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
This commit is contained in:
Anders Kaseorg 2019-06-28 01:35:51 -07:00
parent ae0d574e8a
commit 4afd370d6a
2 changed files with 24 additions and 7 deletions

View File

@ -8,9 +8,10 @@ module VagrantPlugins
def self.installed? def self.installed?
Driver::Meta.new Driver::Meta.new
true true
rescue Vagrant::Errors::VirtualBoxInvalidVersion rescue Vagrant::Errors::VirtualBoxInvalidVersion,
return false Vagrant::Errors::VirtualBoxNotDetected,
rescue Vagrant::Errors::VirtualBoxNotDetected Vagrant::Errors::VirtualBoxKernelModuleNotLoaded,
Vagrant::Errors::VirtualBoxInstallIncomplete
return false return false
end end
@ -19,10 +20,10 @@ module VagrantPlugins
# version and all that, which checks for VirtualBox being present # version and all that, which checks for VirtualBox being present
Driver::Meta.new Driver::Meta.new
true true
rescue Vagrant::Errors::VirtualBoxInvalidVersion rescue Vagrant::Errors::VirtualBoxInvalidVersion,
raise if raise_error Vagrant::Errors::VirtualBoxNotDetected,
return false Vagrant::Errors::VirtualBoxKernelModuleNotLoaded,
rescue Vagrant::Errors::VirtualBoxNotDetected Vagrant::Errors::VirtualBoxInstallIncomplete
raise if raise_error raise if raise_error
return false return false
end end

View File

@ -47,6 +47,22 @@ describe VagrantPlugins::ProviderVirtualBox::Provider do
expect { subject.usable?(true) }. expect { subject.usable?(true) }.
to raise_error(Vagrant::Errors::VirtualBoxInvalidVersion) to raise_error(Vagrant::Errors::VirtualBoxInvalidVersion)
end end
it "raises an exception if virtualbox kernel module is not loaded" do
allow(VagrantPlugins::ProviderVirtualBox::Driver::Meta).to receive(:new).
and_raise(Vagrant::Errors::VirtualBoxKernelModuleNotLoaded)
expect { subject.usable?(true) }.
to raise_error(Vagrant::Errors::VirtualBoxKernelModuleNotLoaded)
end
it "raises an exception if virtualbox installation is incomplete" do
allow(VagrantPlugins::ProviderVirtualBox::Driver::Meta).to receive(:new).
and_raise(Vagrant::Errors::VirtualBoxInstallIncomplete)
expect { subject.usable?(true) }.
to raise_error(Vagrant::Errors::VirtualBoxInstallIncomplete)
end
end end
describe "#driver" do describe "#driver" do