From 768d453739ccf3e577bd6360173b6380dfbf2f4c Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 23 Oct 2014 15:52:42 -0700 Subject: [PATCH] core: Environment#default_provider can look into machines --- lib/vagrant/environment.rb | 11 +++++++- lib/vagrant/vagrantfile.rb | 40 +++++++++++++++------------ test/unit/vagrant/environment_test.rb | 19 ++++++++++++- test/unit/vagrant/vagrantfile_test.rb | 15 ++++++++++ 4 files changed, 66 insertions(+), 19 deletions(-) diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index c92d0f732..62f309a2a 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -308,10 +308,19 @@ module Vagrant # that (the default behavior) return default if default && opts[:force_default] + # Determine the config to use to look for provider definitions. By + # default it is the global but if we're targeting a specific machine, + # then look there. + root_config = vagrantfile.config + if opts[:machine] + machine_info = vagrantfile.machine_config(opts[:machine], nil, nil) + root_config = machine_info[:config] + end + # Get the list of providers within our configuration and assign # a priority to each in the order they exist so that we try these first. config = {} - vagrantfile.config.vm.__providers.each_with_index do |key, idx| + root_config.vm.__providers.each_with_index do |key, idx| config[key] = idx end diff --git a/lib/vagrant/vagrantfile.rb b/lib/vagrant/vagrantfile.rb index 511d1443e..011d16351 100644 --- a/lib/vagrant/vagrantfile.rb +++ b/lib/vagrant/vagrantfile.rb @@ -114,24 +114,30 @@ module Vagrant name: name, provider: provider end - provider_plugin = Vagrant.plugin("2").manager.providers[provider] - if !provider_plugin - raise Errors::ProviderNotFound, - machine: name, provider: provider - end + provider_plugin = nil + provider_cls = nil + provider_options = {} + box_formats = nil + if provider != nil + provider_plugin = Vagrant.plugin("2").manager.providers[provider] + if !provider_plugin + raise Errors::ProviderNotFound, + machine: name, provider: provider + end - provider_cls = provider_plugin[0] - provider_options = provider_plugin[1] - box_formats = provider_options[:box_format] || provider + provider_cls = provider_plugin[0] + provider_options = provider_plugin[1] + box_formats = provider_options[:box_format] || provider - # Test if the provider is usable or not - begin - provider_cls.usable?(true) - rescue Errors::VagrantError => e - raise Errors::ProviderNotUsable, - machine: name.to_s, - provider: provider.to_s, - message: e.to_s + # Test if the provider is usable or not + begin + provider_cls.usable?(true) + rescue Errors::VagrantError => e + raise Errors::ProviderNotUsable, + machine: name.to_s, + provider: provider.to_s, + message: e.to_s + end end # Add the sub-machine configuration to the loader and keys @@ -153,7 +159,7 @@ module Vagrant local_keys = keys.dup # Load the box Vagrantfile, if there is one - if config.vm.box + if config.vm.box && boxes box = boxes.find(config.vm.box, box_formats, config.vm.box_version) if box box_vagrantfile = find_vagrantfile(box.directory) diff --git a/test/unit/vagrant/environment_test.rb b/test/unit/vagrant/environment_test.rb index 0d8200762..7391e0d5f 100644 --- a/test/unit/vagrant/environment_test.rb +++ b/test/unit/vagrant/environment_test.rb @@ -822,7 +822,7 @@ VF end end - it "is the provider in the Vagrantfile that is usable" do + it "is the highest usable provider outside the Vagrantfile" do subject.vagrantfile.config.vm.provider "foo" subject.vagrantfile.config.vm.finalize! @@ -835,6 +835,23 @@ VF expect(subject.default_provider).to eq(:bar) end end + + it "is the provider in the Vagrantfile that is usable for a machine" do + subject.vagrantfile.config.vm.provider "foo" + subject.vagrantfile.config.vm.define "sub" do |v| + v.vm.provider "bar" + end + subject.vagrantfile.config.vm.finalize! + + 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 + expect(subject.default_provider(machine: :sub)).to eq(:bar) + end + end end describe "local data path" do diff --git a/test/unit/vagrant/vagrantfile_test.rb b/test/unit/vagrant/vagrantfile_test.rb index 8e5b3584c..e4ed830c4 100644 --- a/test/unit/vagrant/vagrantfile_test.rb +++ b/test/unit/vagrant/vagrantfile_test.rb @@ -149,6 +149,21 @@ describe Vagrant::Vagrantfile do expect(results[:provider_cls]).to equal(provider_cls) end + it "configures without a provider or boxes" do + register_provider("foo") + + configure do |config| + config.vm.box = "foo" + end + + results = subject.machine_config(:default, nil, nil) + box = results[:box] + config = results[:config] + expect(config.vm.box).to eq("foo") + expect(box).to be_nil + expect(results[:provider_cls]).to be_nil + end + it "configures with sub-machine config" do register_provider("foo")