Cache the machine objects created in Environment#machine.

Each machine and provider backing should be represented by only one
machine.
This commit is contained in:
Mitchell Hashimoto 2012-11-07 22:08:06 -08:00
parent 67855be77b
commit 2ef6ff10f0
2 changed files with 44 additions and 5 deletions

View File

@ -138,6 +138,10 @@ module Vagrant
# backed by. # backed by.
# @return [Machine] # @return [Machine]
def machine(name, provider) def machine(name, provider)
cache_key = [name, provider]
@machines ||= {}
return @machines[cache_key] if @machines.has_key?(cache_key)
vm_config = config.for_vm(name) vm_config = config.for_vm(name)
if !vm_config if !vm_config
raise Errors::MachineNotFound, :name => name, :provider => provider raise Errors::MachineNotFound, :name => name, :provider => provider
@ -149,7 +153,7 @@ module Vagrant
end end
box = boxes.find(vm_config.vm.box, provider) box = boxes.find(vm_config.vm.box, provider)
Machine.new(name, provider_cls, vm_config, box, self) @machines[cache_key] = Machine.new(name, provider_cls, vm_config, box, self)
end end
# This returns a list of the configured machines for this environment. # This returns a list of the configured machines for this environment.

View File

@ -238,12 +238,20 @@ VF
end end
describe "getting a machine" do describe "getting a machine" do
# A helper to register a provider for use in tests.
def register_provider(name)
provider_cls = Class.new(Vagrant.plugin("2", :provider))
register_plugin("2") do |p|
p.provider(name) { provider_cls }
end
provider_cls
end
it "should return a machine object with the correct provider" do it "should return a machine object with the correct provider" do
# Create a provider # Create a provider
foo_provider = Class.new(Vagrant.plugin("2", :provider)) foo_provider = register_provider("foo")
register_plugin("2") do |p|
p.provider("foo") { foo_provider }
end
# Create the configuration # Create the configuration
isolated_env = isolated_environment do |e| isolated_env = isolated_environment do |e|
@ -263,6 +271,33 @@ VF
machine.provider.should be_kind_of(foo_provider) machine.provider.should be_kind_of(foo_provider)
end end
it "should cache the machine objects by name and provider" do
# Create a provider
foo_provider = register_provider("foo")
bar_provider = register_provider("bar")
# Create the configuration
isolated_env = isolated_environment do |e|
e.vagrantfile(<<-VF)
Vagrant.configure("2") do |config|
config.vm.box = "base"
config.vm.define "vm1"
config.vm.define "vm2"
end
VF
end
env = isolated_env.create_vagrant_env
vm1_foo = env.machine(:vm1, :foo)
vm1_bar = env.machine(:vm1, :bar)
vm2_foo = env.machine(:vm2, :foo)
vm1_foo.should eql(env.machine(:vm1, :foo))
vm1_bar.should eql(env.machine(:vm1, :bar))
vm1_foo.should_not eql(vm1_bar)
vm2_foo.should eql(env.machine(:vm2, :foo))
end
it "should raise an error if the VM is not found" do it "should raise an error if the VM is not found" do
expect { instance.machine("i-definitely-dont-exist", :virtualbox) }. expect { instance.machine("i-definitely-dont-exist", :virtualbox) }.
to raise_error(Vagrant::Errors::MachineNotFound) to raise_error(Vagrant::Errors::MachineNotFound)