Environment properly loads VMs
This commit is contained in:
parent
b313d34d5b
commit
124a9ab19d
|
@ -128,6 +128,10 @@ module Vagrant
|
|||
push_proc(&block)
|
||||
end
|
||||
|
||||
def has_multi_vms?
|
||||
!defined_vms.empty?
|
||||
end
|
||||
|
||||
def defined_vms
|
||||
@defined_vms ||= {}
|
||||
end
|
||||
|
|
|
@ -16,6 +16,7 @@ module Vagrant
|
|||
attr_reader :config
|
||||
attr_reader :box
|
||||
attr_accessor :vm
|
||||
attr_reader :vms
|
||||
attr_reader :active_list
|
||||
attr_reader :commands
|
||||
|
||||
|
@ -49,17 +50,19 @@ module Vagrant
|
|||
defaults = {
|
||||
:parent => nil,
|
||||
:vm_name => nil,
|
||||
:vm => nil,
|
||||
:cwd => nil
|
||||
}
|
||||
|
||||
opts = defaults.merge(opts || {})
|
||||
@cwd = opts[:cwd]
|
||||
@parent = opts[:parent]
|
||||
@vm_name = opts[:vm_name]
|
||||
|
||||
defaults.each do |key, value|
|
||||
instance_variable_set("@#{key}".to_sym, opts[key])
|
||||
end
|
||||
end
|
||||
|
||||
#---------------------------------------------------------------
|
||||
# Path Helpers
|
||||
# Helpers
|
||||
#---------------------------------------------------------------
|
||||
|
||||
# Specifies the "current working directory" for this environment.
|
||||
|
@ -91,6 +94,11 @@ module Vagrant
|
|||
File.join(home_path, "boxes")
|
||||
end
|
||||
|
||||
# Returns the VMs associated with this environment.
|
||||
def vms
|
||||
@vms ||= {}
|
||||
end
|
||||
|
||||
#---------------------------------------------------------------
|
||||
# Load Methods
|
||||
#---------------------------------------------------------------
|
||||
|
@ -191,13 +199,32 @@ module Vagrant
|
|||
|
||||
# Loads the persisted VM (if it exists) for this environment.
|
||||
def load_vm!
|
||||
# This environment represents a single sub VM. The VM is then
|
||||
# probably (read: should be) set on the VM attribute, so we do
|
||||
# nothing.
|
||||
return if vm_name
|
||||
return if !root_path || !File.file?(dotfile_path)
|
||||
|
||||
# Empty out previously loaded vms
|
||||
vms.clear
|
||||
|
||||
File.open(dotfile_path) do |f|
|
||||
@vm = Vagrant::VM.find(f.read, self)
|
||||
data = { :__vagrant => f.read }
|
||||
|
||||
begin
|
||||
data = JSON.parse(data[:__vagrant])
|
||||
rescue JSON::ParserError
|
||||
# Most likely an older (<= 0.3.x) dotfile. Try to load it
|
||||
# as the :__vagrant VM.
|
||||
end
|
||||
|
||||
data.each do |key, value|
|
||||
key = key.to_sym
|
||||
vms[key] = Vagrant::VM.find(value, self, key)
|
||||
end
|
||||
end
|
||||
rescue Errno::ENOENT
|
||||
@vm = nil
|
||||
# Just rescue it.
|
||||
end
|
||||
|
||||
# Loads the activelist for this environment
|
||||
|
|
|
@ -9,10 +9,10 @@ module Vagrant
|
|||
class <<self
|
||||
# Finds a virtual machine by a given UUID and either returns
|
||||
# a Vagrant::VM object or returns nil.
|
||||
def find(uuid, env=nil)
|
||||
def find(uuid, env=nil, vm_name=nil)
|
||||
vm = VirtualBox::VM.find(uuid)
|
||||
return nil if vm.nil?
|
||||
new(:vm => vm, :env => env)
|
||||
new(:vm => vm, :env => env, :vm_name => vm_name)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -28,7 +28,12 @@ module Vagrant
|
|||
@vm = opts[:vm]
|
||||
|
||||
if !opts[:env].nil?
|
||||
@env = Vagrant::Environment.new(:cwd => opts[:env].cwd, :parent => opts[:env], :vm_name => opts[:vm_name]).load!
|
||||
@env = Vagrant::Environment.new({
|
||||
:cwd => opts[:env].cwd,
|
||||
:parent => opts[:env],
|
||||
:vm_name => opts[:vm_name],
|
||||
:vm => self
|
||||
}).load!
|
||||
load_system!
|
||||
end
|
||||
end
|
||||
|
|
|
@ -234,6 +234,15 @@ class ConfigTest < Test::Unit::TestCase
|
|||
@config.define(:name, &proc)
|
||||
assert_equal proc, @config.defined_vms[:name]
|
||||
end
|
||||
|
||||
should "not have multi-VMs by default" do
|
||||
assert !@config.has_multi_vms?
|
||||
end
|
||||
|
||||
should "have multi-VMs once one is specified" do
|
||||
@config.define(:foo) {}
|
||||
assert @config.has_multi_vms?
|
||||
end
|
||||
end
|
||||
|
||||
context "customizing" do
|
||||
|
|
|
@ -372,28 +372,45 @@ class EnvironmentTest < Test::Unit::TestCase
|
|||
File.stubs(:file?).returns(true)
|
||||
end
|
||||
|
||||
should "loading of the uuid from the dotfile" do
|
||||
should "load the UUID if the JSON parsing fails" do
|
||||
vm = mock("vm")
|
||||
|
||||
filemock = mock("filemock")
|
||||
filemock.expects(:read).returns("foo")
|
||||
Vagrant::VM.expects(:find).with("foo", @env).returns(vm)
|
||||
Vagrant::VM.expects(:find).with("foo", @env, :__vagrant).returns(vm)
|
||||
File.expects(:open).with(@env.dotfile_path).once.yields(filemock)
|
||||
File.expects(:file?).with(@env.dotfile_path).once.returns(true)
|
||||
@env.load_vm!
|
||||
|
||||
assert_equal vm, @env.vm
|
||||
assert_equal vm, @env.vms.values.first
|
||||
end
|
||||
|
||||
should "not set the environment if the VM is nil" do
|
||||
should "load all the VMs from the dotfile" do
|
||||
vms = { :foo => "bar", :bar => "baz" }
|
||||
results = {}
|
||||
|
||||
filemock = mock("filemock")
|
||||
filemock.expects(:read).returns("foo")
|
||||
Vagrant::VM.expects(:find).with("foo", @env).returns(nil)
|
||||
filemock.expects(:read).returns(vms.to_json)
|
||||
File.expects(:open).with(@env.dotfile_path).once.yields(filemock)
|
||||
File.expects(:file?).with(@env.dotfile_path).once.returns(true)
|
||||
|
||||
assert_nothing_raised { @env.load_vm! }
|
||||
assert_nil @env.vm
|
||||
vms.each do |key, value|
|
||||
vm = mock("vm#{key}")
|
||||
Vagrant::VM.expects(:find).with(value, @env, key.to_sym).returns(vm)
|
||||
results[key] = vm
|
||||
end
|
||||
|
||||
@env.load_vm!
|
||||
|
||||
results.each do |key, value|
|
||||
assert_equal value, @env.vms[key]
|
||||
end
|
||||
end
|
||||
|
||||
should "do nothing if the vm_name is set" do
|
||||
@env.stubs(:vm_name).returns(:foo)
|
||||
File.expects(:open).never
|
||||
@env.load_vm!
|
||||
end
|
||||
|
||||
should "do nothing if the root path is nil" do
|
||||
|
|
Loading…
Reference in New Issue