From 4c460917466f97e6adb244d41f1910c90cdcdbe2 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 30 Dec 2012 11:12:56 -1000 Subject: [PATCH] Environment#primary_machine_name and use it for with_target_vms This makes the single-provider and default provider semantics work with primary VMs. --- lib/vagrant/environment.rb | 21 ++++++++-------- lib/vagrant/plugin/v2/command.rb | 6 ++--- test/unit/vagrant/environment_test.rb | 23 +++++++++++++++--- test/unit/vagrant/plugin/v2/command_test.rb | 27 +++++++++++++++++++-- 4 files changed, 59 insertions(+), 18 deletions(-) diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index 2c70db3eb..c2ec4c06f 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -289,21 +289,22 @@ module Vagrant config_global.vm.defined_vm_keys.dup end - # Returns the primary VM associated with this environment. This - # method is only applicable for multi-VM environments. This can - # potentially be nil if no primary VM is specified. + # This returns the name of the machine that is the "primary." In the + # case of a single-machine environment, this is just the single machine + # name. In the case of a multi-machine environment, then this can + # potentially be nil if no primary machine is specified. # - # @param [Symbol] provider The provider to back the primary machine. - # @return [VM] - def primary_machine(provider) - if machine_names.length == 1 - return machine(machine_names[0], provider) - end + # @return [Symbol] + def primary_machine_name + # If it is a single machine environment, then return the name + return machine_names.first if machine_names.length == 1 + # If it is a multi-machine environment, then return the primary config_global.vm.defined_vms.each do |name, subvm| - return machine(name, provider) if subvm.options[:primary] + return name if subvm.options[:primary] end + # If no primary was specified, nil it is nil end diff --git a/lib/vagrant/plugin/v2/command.rb b/lib/vagrant/plugin/v2/command.rb index bc32b8b54..2c643ec7c 100644 --- a/lib/vagrant/plugin/v2/command.rb +++ b/lib/vagrant/plugin/v2/command.rb @@ -167,9 +167,9 @@ module Vagrant # Make sure we're only working with one VM if single target if options[:single_target] && machines.length != 1 @logger.debug("Using primary machine since single target") - primary = @env.primary_machine(provider) - raise Errors::MultiVMTargetRequired if !primary - machines = [primary] + primary_name = @env.primary_machine_name + raise Errors::MultiVMTargetRequired if !primary_name + machines = [get_machine.call(primary_name)] end # If we asked for reversed ordering, then reverse it diff --git a/test/unit/vagrant/environment_test.rb b/test/unit/vagrant/environment_test.rb index 3ab18b4f1..f779fb6e9 100644 --- a/test/unit/vagrant/environment_test.rb +++ b/test/unit/vagrant/environment_test.rb @@ -205,9 +205,9 @@ describe Vagrant::Environment do end end - describe "primary machine" do + describe "primary machine name" do it "should be the only machine if not a multi-machine environment" do - instance.primary_machine(:dummy).name.should == instance.machine_names.first + instance.primary_machine_name.should == instance.machine_names.first end it "should be the machine marked as the primary" do @@ -224,7 +224,24 @@ VF end env = environment.create_vagrant_env - env.primary_machine(:dummy).name.should == :bar + env.primary_machine_name.should == :bar + end + + it "should be nil if no primary is specified in a multi-machine environment" do + environment = isolated_environment do |env| + env.vagrantfile(<<-VF) +Vagrant.configure("2") do |config| + config.vm.box = "base" + config.vm.define :foo + config.vm.define :bar +end +VF + + env.box2("base", :virtualbox) + end + + env = environment.create_vagrant_env + env.primary_machine_name.should be_nil end end diff --git a/test/unit/vagrant/plugin/v2/command_test.rb b/test/unit/vagrant/plugin/v2/command_test.rb index 613868f2d..c413daf28 100644 --- a/test/unit/vagrant/plugin/v2/command_test.rb +++ b/test/unit/vagrant/plugin/v2/command_test.rb @@ -171,11 +171,34 @@ describe Vagrant::Plugin::V2::Command do end it "should use the primary machine with the active provider" do - pending + name = :foo + provider = :vmware + vmware_vm = double("vmware_vm") + + environment.stub(:active_machines => [[name, provider]]) + environment.stub(:machine).with(name, provider).and_return(vmware_vm) + environment.stub(:machine_names => []) + environment.stub(:primary_machine_name => name) + vmware_vm.stub(:name => name, :provider => provider) + + vms = [] + instance.with_target_vms(nil, :single_target => true) { |vm| vms << vm } + vms.should == [vmware_vm] end it "should use the primary machine with the default provider" do - pending + name = :foo + machine = double("machine") + + environment.stub(:active_machines => []) + environment.stub(:machine).with(name, default_provider).and_return(machine) + environment.stub(:machine_names => []) + environment.stub(:primary_machine_name => name) + machine.stub(:name => name, :provider => default_provider) + + vms = [] + instance.with_target_vms(nil, :single_target => true) { |vm| vms << machine } + vms.should == [machine] end end