core: default provider chosen by usability and prority
This commit is contained in:
parent
429bd73495
commit
a9dfb6b3bd
|
@ -22,7 +22,7 @@ module Vagrant
|
||||||
ordered << [priority, key, impl]
|
ordered << [priority, key, impl]
|
||||||
end
|
end
|
||||||
|
|
||||||
# Order the plugins by priority. Higher is tries before lower.
|
# Order the plugins by priority. Higher is tried before lower.
|
||||||
ordered = ordered.sort { |a, b| b[0] <=> a[0] }
|
ordered = ordered.sort { |a, b| b[0] <=> a[0] }
|
||||||
|
|
||||||
# Find the proper implementation
|
# Find the proper implementation
|
||||||
|
|
|
@ -254,8 +254,35 @@ module Vagrant
|
||||||
# environment.
|
# environment.
|
||||||
#
|
#
|
||||||
# @return [Symbol] Name of the default provider.
|
# @return [Symbol] Name of the default provider.
|
||||||
def default_provider
|
def default_provider(**opts)
|
||||||
(ENV['VAGRANT_DEFAULT_PROVIDER'] || :virtualbox).to_sym
|
default = ENV["VAGRANT_DEFAULT_PROVIDER"]
|
||||||
|
default = nil if default == ""
|
||||||
|
default = default.to_sym if default
|
||||||
|
|
||||||
|
ordered = []
|
||||||
|
Vagrant.plugin("2").manager.providers.each do |key, data|
|
||||||
|
impl = data[0]
|
||||||
|
opts = data[1]
|
||||||
|
|
||||||
|
ordered << [opts[:priority], key, impl, opts]
|
||||||
|
end
|
||||||
|
|
||||||
|
# Order the providers by priority. Higher values are tried first.
|
||||||
|
ordered = ordered.sort do |a, b|
|
||||||
|
# If we see the default, then that one always wins
|
||||||
|
next -1 if a[1] == default
|
||||||
|
next 1 if b[1] == default
|
||||||
|
|
||||||
|
b[0] <=> a[0]
|
||||||
|
end
|
||||||
|
|
||||||
|
# Find the matching implementation
|
||||||
|
ordered.each do |_, key, impl, _|
|
||||||
|
return key if impl.usable?(false)
|
||||||
|
end
|
||||||
|
|
||||||
|
# If all else fails, return VirtualBox
|
||||||
|
return :virtualbox
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns the collection of boxes for the environment.
|
# Returns the collection of boxes for the environment.
|
||||||
|
|
|
@ -7,6 +7,14 @@ shared_context "capability_helpers" do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def provider_usable_class(result)
|
||||||
|
Class.new do
|
||||||
|
define_singleton_method(:usable?) do |*args|
|
||||||
|
result
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def cap_instance(name, options=nil)
|
def cap_instance(name, options=nil)
|
||||||
options ||= {}
|
options ||= {}
|
||||||
|
|
||||||
|
|
|
@ -699,15 +699,32 @@ VF
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "default provider" do
|
describe "default provider" do
|
||||||
it "is virtualbox without any environmental variable" do
|
let(:plugin_providers) { {} }
|
||||||
|
|
||||||
|
before do
|
||||||
|
m = Vagrant.plugin("2").manager
|
||||||
|
m.stub(providers: plugin_providers)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "is the highest matching usable provider" do
|
||||||
|
plugin_providers[:foo] = [provider_usable_class(true), { priority: 5 }]
|
||||||
|
plugin_providers[:bar] = [provider_usable_class(true), { priority: 7 }]
|
||||||
|
plugin_providers[:baz] = [provider_usable_class(true), { priority: 2 }]
|
||||||
|
plugin_providers[:boom] = [provider_usable_class(true), { priority: 3 }]
|
||||||
|
|
||||||
with_temp_env("VAGRANT_DEFAULT_PROVIDER" => nil) do
|
with_temp_env("VAGRANT_DEFAULT_PROVIDER" => nil) do
|
||||||
expect(subject.default_provider).to eq(:virtualbox)
|
expect(subject.default_provider).to eq(:bar)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "is whatever the environmental variable is if set" do
|
it "is the default provider if it is usable" do
|
||||||
with_temp_env("VAGRANT_DEFAULT_PROVIDER" => "foo") do
|
plugin_providers[:foo] = [provider_usable_class(true), { priority: 5 }]
|
||||||
expect(subject.default_provider).to eq(:foo)
|
plugin_providers[:bar] = [provider_usable_class(true), { priority: 7 }]
|
||||||
|
plugin_providers[:baz] = [provider_usable_class(true), { priority: 2 }]
|
||||||
|
plugin_providers[:boom] = [provider_usable_class(true), { priority: 3 }]
|
||||||
|
|
||||||
|
with_temp_env("VAGRANT_DEFAULT_PROVIDER" => "baz") do
|
||||||
|
expect(subject.default_provider).to eq(:baz)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue