Change VM loading to be lazy loaded so config can be loaded without VMs

This commit is contained in:
Mitchell Hashimoto 2010-09-21 01:27:36 -06:00
parent 59c6995b50
commit 3b735e545f
2 changed files with 18 additions and 76 deletions

View File

@ -100,7 +100,7 @@ module Vagrant
def vms def vms
return parent.vms if parent return parent.vms if parent
load! if !loaded? load! if !loaded?
@vms ||= {} @vms ||= load_vms!
end end
# Returns the primray VM associated with this environment # Returns the primray VM associated with this environment
@ -227,7 +227,6 @@ module Vagrant
@loaded = true @loaded = true
self.class.check_virtualbox! self.class.check_virtualbox!
load_config! load_config!
load_vm!
actions.run(:environment_load) actions.run(:environment_load)
end end
@ -287,33 +286,23 @@ module Vagrant
end end
# Loads the persisted VM (if it exists) for this environment. # Loads the persisted VM (if it exists) for this environment.
def load_vm! def load_vms!
# This environment represents a single sub VM. The VM is then result = {}
# probably (read: should be) set on the VM attribute, so we do
# nothing.
return if parent
# First load the defaults (blank, noncreated VMs)
load_blank_vms!
# Load the VM UUIDs from the local data store # Load the VM UUIDs from the local data store
(local_data[:active] || {}).each do |name, uuid| (local_data[:active] || {}).each do |name, uuid|
vms[name.to_sym] = Vagrant::VM.find(uuid, self, name.to_sym) result[name.to_sym] = Vagrant::VM.find(uuid, self, name.to_sym)
end end
end
# Loads blank VMs into the `vms` attribute. # For any VMs which aren't created, create a blank VM instance for
def load_blank_vms! # them
# Clear existing vms all_keys = config.vm.defined_vms.keys
vms.clear all_keys = [DEFAULT_VM] if all_keys.empty?
all_keys.each do |name|
# Load up the blank VMs result[name] = Vagrant::VM.new(:name => name, :env => self) if !result.has_key?(name)
defined_vms = config.vm.defined_vms.keys
defined_vms = [DEFAULT_VM] if defined_vms.empty?
defined_vms.each do |name|
vms[name] = Vagrant::VM.new(:name => name, :env => self)
end end
result
end end
end end
end end

View File

@ -193,15 +193,13 @@ class EnvironmentTest < Test::Unit::TestCase
assert env.global_data.equal?(store) assert env.global_data.equal?(store)
end end
should "return the parent's local data if a parent exists" do should "return the parent's global data if a parent exists" do
env = vagrant_env(vagrantfile(<<-vf)) env = vagrant_env(vagrantfile(<<-vf))
config.vm.define :web config.vm.define :web
config.vm.define :db config.vm.define :db
vf vf
result = env.global_data result = env.global_data
Vagrant::DataStore.expects(:new).never
assert env.vms[:web].env.global_data.equal?(result) assert env.vms[:web].env.global_data.equal?(result)
end end
end end
@ -323,8 +321,9 @@ class EnvironmentTest < Test::Unit::TestCase
context "accessing the VMs hash" do context "accessing the VMs hash" do
should "load the environment if its not already loaded" do should "load the environment if its not already loaded" do
env = @klass.new(:cwd => vagrantfile) env = @klass.new(:cwd => vagrantfile)
env.expects(:load!).once assert !env.loaded?
env.vms env.vms
assert env.loaded?
end end
should "not load the environment if its already loaded" do should "not load the environment if its already loaded" do
@ -350,7 +349,6 @@ class EnvironmentTest < Test::Unit::TestCase
call_seq = sequence("call_sequence") call_seq = sequence("call_sequence")
@klass.expects(:check_virtualbox!).once.in_sequence(call_seq) @klass.expects(:check_virtualbox!).once.in_sequence(call_seq)
env.expects(:load_config!).once.in_sequence(call_seq) env.expects(:load_config!).once.in_sequence(call_seq)
env.expects(:load_vm!).once.in_sequence(call_seq)
env.actions.expects(:run).with(:environment_load).once.in_sequence(call_seq) env.actions.expects(:run).with(:environment_load).once.in_sequence(call_seq)
assert_equal env, env.load! assert_equal env, env.load!
end end
@ -445,13 +443,6 @@ class EnvironmentTest < Test::Unit::TestCase
@env = vagrant_env @env = vagrant_env
end end
should "blank the VMs" do
load_seq = sequence("load_seq")
@env.stubs(:root_path).returns("foo")
@env.expects(:load_blank_vms!).in_sequence(load_seq)
@env.load_vm!
end
should "load all the VMs from the dotfile" do should "load all the VMs from the dotfile" do
@env.local_data[:active] = { "foo" => "bar", "bar" => "baz" } @env.local_data[:active] = { "foo" => "bar", "bar" => "baz" }
@ -462,56 +453,18 @@ class EnvironmentTest < Test::Unit::TestCase
results[key.to_sym] = vm results[key.to_sym] = vm
end end
@env.load_vm! returned = @env.load_vms!
results.each do |key, value| results.each do |key, value|
assert_equal value, @env.vms[key] assert_equal value, returned[key]
end end
end end
should "do nothing if the parent is set" do
env = vagrant_env(vagrantfile(<<-vf))
config.vm.define :web
config.vm.define :db
vf
subenv = env.vms[:web].env
subenv.expects(:load_blank_vms!).never
subenv.load_vm!
end
should "uuid should be nil if local data contains nothing" do should "uuid should be nil if local data contains nothing" do
assert @env.local_data.empty? # sanity assert @env.local_data.empty? # sanity
@env.load_vm! @env.load_vms!
assert_nil @env.vm assert_nil @env.vm
end end
end end
context "loading blank VMs" do
should "blank the VMs" do
env = vagrant_env(vagrantfile(<<-vf))
config.vm.define :foo
config.vm.define :bar
vf
env.load_blank_vms!
assert_equal 2, env.vms.length
assert(env.vms.all? { |name, vm| !vm.created? })
sorted_vms = env.vms.keys.sort { |a,b| a.to_s <=> b.to_s }
assert_equal [:bar, :foo], sorted_vms
end
should "load the default VM blank if no multi-VMs are specified" do
env = vagrant_env
assert env.config.vm.defined_vms.empty? # sanity
env.load_blank_vms!
assert_equal 1, env.vms.length
assert !env.vms.values.first.created?
end
end
end end
end end