Persist/depersist VM methods replaced with dotfile updates. Active list information disabled for now.

This commit is contained in:
Mitchell Hashimoto 2010-05-16 16:53:56 -07:00
parent a56f4a43dd
commit 0314e6ef6c
4 changed files with 67 additions and 64 deletions

View File

@ -19,13 +19,13 @@ module Vagrant
end end
def after_import def after_import
persist update_dotfile
setup_mac_address setup_mac_address
end end
def persist def update_dotfile
logger.info "Persisting the VM UUID (#{@runner.uuid})..." logger.info "Persisting the VM UUID (#{@runner.uuid})..."
@runner.env.persist_vm @runner.env.update_dotfile
end end
def setup_mac_address def setup_mac_address

View File

@ -263,23 +263,25 @@ module Vagrant
# Persists this environment's VM to the dotfile so it can be # Persists this environment's VM to the dotfile so it can be
# re-loaded at a later time. # re-loaded at a later time.
def persist_vm def update_dotfile
# Save to the dotfile for this project if parent
parent.update_dotfile
return
end
# Generate and save the persisted VM info
data = vms.inject({}) do |acc, data|
key, value = data
acc[key] = value.uuid if value.created?
acc
end
File.open(dotfile_path, 'w+') do |f| File.open(dotfile_path, 'w+') do |f|
f.write(vm.uuid) f.write(data.to_json)
end end
# Also add to the global store # Also add to the global store
active_list.add(vm) # active_list.add(vm)
end
# Removes this environment's VM from the dotfile.
def depersist_vm
# Delete the dotfile if it exists
File.delete(dotfile_path) if File.exist?(dotfile_path)
# Remove from the global store
active_list.remove(vm)
end end
#--------------------------------------------------------------- #---------------------------------------------------------------

View File

@ -63,19 +63,19 @@ class UpActionTest < Test::Unit::TestCase
end end
context "callbacks" do context "callbacks" do
should "call persist and mac address setup after import" do should "call update dotfile and mac address setup after import" do
boot_seq = sequence("boot") boot_seq = sequence("boot")
@action.expects(:persist).once.in_sequence(boot_seq) @action.expects(:update_dotfile).once.in_sequence(boot_seq)
@action.expects(:setup_mac_address).once.in_sequence(boot_seq) @action.expects(:setup_mac_address).once.in_sequence(boot_seq)
@action.after_import @action.after_import
end end
end end
context "persisting" do context "updating the dotfile" do
should "persist the VM with Env" do should "call update dotfile on the VM's environment" do
@runner.stubs(:uuid) @runner.stubs(:uuid)
@runner.env.expects(:persist_vm).once @runner.env.expects(:update_dotfile).once
@action.persist @action.update_dotfile
end end
end end

View File

@ -460,8 +460,10 @@ class EnvironmentTest < Test::Unit::TestCase
@env.load_blank_vms! @env.load_blank_vms!
assert_equal 2, @env.vms.length assert_equal 2, @env.vms.length
assert_equal [:foo, :bar], @env.vms.keys
assert(@env.vms.all? { |name, vm| !vm.created? }) 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 end
should "load the default VM blank if no multi-VMs are specified" do should "load the default VM blank if no multi-VMs are specified" do
@ -593,54 +595,53 @@ class EnvironmentTest < Test::Unit::TestCase
@vm.stubs(:uuid).returns("foo") @vm.stubs(:uuid).returns("foo")
@env.stubs(:vm).returns(@vm) @env.stubs(:vm).returns(@vm)
end end
end
context "persisting the VM into a file" do context "updating the dotfile" do
setup do setup do
mock_vm @env = mock_environment
@env.stubs(:parent).returns(nil)
File.stubs(:open) @env.stubs(:dotfile_path).returns("foo")
@env.active_list.stubs(:add) File.stubs(:open)
end
should "should save it to the dotfile path" do
filemock = mock("filemock")
filemock.expects(:write).with(@vm.uuid)
File.expects(:open).with(@env.dotfile_path, 'w+').once.yields(filemock)
@env.persist_vm
end
should "add the VM to the activelist" do
@env.active_list.expects(:add).with(@vm)
@env.persist_vm
end
end end
context "depersisting the VM" do def create_vm(created)
setup do vm = mock("vm")
mock_vm vm.stubs(:created?).returns(created)
vm.stubs(:uuid).returns("foo")
vm
end
File.stubs(:exist?).returns(false) should "call parent if exists" do
File.stubs(:delete) parent = mock("parent")
@env.stubs(:parent).returns(parent)
parent.expects(:update_dotfile).once
@env.active_list.stubs(:remove) @env.update_dotfile
end
should "write the proper data to dotfile" do
vms = {
:foo => create_vm(false),
:bar => create_vm(true),
:baz => create_vm(true)
}
f = mock("f")
@env.stubs(:vms).returns(vms)
File.expects(:open).with(@env.dotfile_path, 'w+').yields(f)
f.expects(:write).with() do |json|
assert_nothing_raised {
data = JSON.parse(json)
assert_equal 2, data.length
assert_equal vms[:bar].uuid, data["bar"]
assert_equal vms[:baz].uuid, data["baz"]
}
true
end end
should "remove the dotfile if it exists" do @env.update_dotfile
File.expects(:exist?).with(@env.dotfile_path).returns(true)
File.expects(:delete).with(@env.dotfile_path).once
@env.depersist_vm
end
should "not remove the dotfile if it doesn't exist" do
File.expects(:exist?).returns(false)
File.expects(:delete).never
@env.depersist_vm
end
should "remove from the active list" do
@env.active_list.expects(:remove).with(@vm)
@env.depersist_vm
end
end end
end end
end end