ActiveList now contains meaningful information about the running VMs

This commit is contained in:
Mitchell Hashimoto 2010-04-30 23:34:22 -07:00
parent 661af82e63
commit 422951f0eb
2 changed files with 38 additions and 59 deletions

View File

@ -23,30 +23,36 @@ module Vagrant
def list(reload=false) def list(reload=false)
return @list unless @list.nil? || reload return @list unless @list.nil? || reload
@list ||= [] @list ||= {}
return @list unless File.file?(path) return @list unless File.file?(path)
File.open(path, "r") do |f| File.open(path, "r") do |f|
@list = JSON.parse(f.read) @list = JSON.parse(f.read)
# This forces earlier versions of Vagrant to use the new hash
# format. Clearing out the old data isn't a big deal since it
# was never used.
@list = {} unless @list.is_a?(Hash)
end end
@list @list
end end
# Returns an array of {Vagrant::VM} objects which are currently
# active.
def vms
list.collect { |uuid| Vagrant::VM.find(uuid, env) }.compact
end
# Returns an array of UUIDs filtered so each is verified to exist. # Returns an array of UUIDs filtered so each is verified to exist.
def filtered_list def filter_list
vms.collect { |vm| vm.uuid } list.each do |uuid, data|
list.delete(uuid) unless Vagrant::VM.find(uuid, env)
end
list
end end
# Adds a virtual environment to the list of active virtual machines # Adds a virtual environment to the list of active virtual machines
def add(vm) def add(vm)
list << vm.uuid list[vm.uuid] = {
list.uniq! :path => env.root_path,
:last_updated => Time.now.to_i
}
save save
end end
@ -60,7 +66,7 @@ module Vagrant
# Persists the list down to the JSON file. # Persists the list down to the JSON file.
def save def save
File.open(path, "w+") do |f| File.open(path, "w+") do |f|
f.write(filtered_list.to_json) f.write(filter_list.to_json)
end end
end end

View File

@ -36,13 +36,13 @@ class ActiveListTest < Test::Unit::TestCase
should "not load if the active json file doesn't exist" do should "not load if the active json file doesn't exist" do
File.expects(:file?).with(@list.path).returns(false) File.expects(:file?).with(@list.path).returns(false)
File.expects(:open).never File.expects(:open).never
assert_equal [], @list.list(true) assert_equal Hash.new, @list.list(true)
end end
should "parse the JSON by reading the file" do should "parse the JSON by reading the file" do
file = mock("file") file = mock("file")
data = mock("data") data = mock("data")
result = mock("result") result = { :hey => :yep }
File.expects(:file?).returns(true) File.expects(:file?).returns(true)
File.expects(:open).with(@list.path, 'r').once.yields(file) File.expects(:open).with(@list.path, 'r').once.yields(file)
file.expects(:read).returns(data) file.expects(:read).returns(data)
@ -59,50 +59,29 @@ class ActiveListTest < Test::Unit::TestCase
end end
end end
context "vms" do context "filter list" do
setup do should "remove nonexistent VMs" do
@the_list = ["foo", "bar"] list = {}
@list.stubs(:list).returns(@the_list) result = {}
end
should "return the list, but with each value as a VM" do
new_seq = sequence("new")
results = []
@the_list.each do |item|
result = mock("result-#{item}")
Vagrant::VM.expects(:find).with(item, @env).returns(result).in_sequence(new_seq)
results << result
end
assert_equal results, @list.vms
end
should "compact out the nil values" do
Vagrant::VM.stubs(:find).returns(nil)
results = @list.vms
assert results.empty?
end
end
context "filtered list" do
should "return a list of UUIDs from the VMs" do
vms = []
result = []
5.times do |i| 5.times do |i|
vm = mock("vm#{i}") vm = mock("vm#{i}")
vm.expects(:uuid).returns(i) vm.stubs(:uuid).returns(i)
result << i
vms << vm list[vm.uuid] = {}
found_vm = i % 2 ? nil : vm
Vagrant::VM.stubs(:find).with(vm.uuid, @env).returns(found_vm)
results[vm.uuid] = {} if found_vm
end end
@list.stubs(:vms).returns(vms) @list.stubs(:list).returns(list)
assert_equal result, @list.filtered_list assert_equal result, @list.filter_list
end end
end end
context "adding a VM to the list" do context "adding a VM to the list" do
setup do setup do
@the_list = [] @the_list = {}
@list.stubs(:list).returns(@the_list) @list.stubs(:list).returns(@the_list)
@list.stubs(:save) @list.stubs(:save)
@ -113,19 +92,13 @@ class ActiveListTest < Test::Unit::TestCase
should "add the VMs UUID to the list" do should "add the VMs UUID to the list" do
@list.add(@vm) @list.add(@vm)
assert_equal [@uuid], @the_list assert @the_list[@uuid]
end assert @the_list[@uuid].is_a?(Hash)
should "uniq the array so multiples never exist" do
@the_list << @uuid
assert_equal 1, @the_list.length
@list.add(@vm)
assert_equal 1, @the_list.length
end end
should "save after adding" do should "save after adding" do
save_seq = sequence('save') save_seq = sequence('save')
@the_list.expects(:<<).in_sequence(save_seq) @the_list.expects(:[]=).in_sequence(save_seq)
@list.expects(:save).in_sequence(save_seq) @list.expects(:save).in_sequence(save_seq)
@list.add(@vm) @list.add(@vm)
end end
@ -165,7 +138,7 @@ class ActiveListTest < Test::Unit::TestCase
context "saving" do context "saving" do
setup do setup do
@filtered = ["zoo"] @filtered = ["zoo"]
@list.stubs(:filtered_list).returns(@filtered) @list.stubs(:filter_list).returns(@filtered)
end end
should "open the JSON path and save to it" do should "open the JSON path and save to it" do