ActiveList now contains meaningful information about the running VMs
This commit is contained in:
parent
661af82e63
commit
422951f0eb
|
@ -23,30 +23,36 @@ module Vagrant
|
|||
def list(reload=false)
|
||||
return @list unless @list.nil? || reload
|
||||
|
||||
@list ||= []
|
||||
@list ||= {}
|
||||
return @list unless File.file?(path)
|
||||
File.open(path, "r") do |f|
|
||||
@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
|
||||
|
||||
@list
|
||||
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.
|
||||
def filtered_list
|
||||
vms.collect { |vm| vm.uuid }
|
||||
def filter_list
|
||||
list.each do |uuid, data|
|
||||
list.delete(uuid) unless Vagrant::VM.find(uuid, env)
|
||||
end
|
||||
|
||||
list
|
||||
end
|
||||
|
||||
# Adds a virtual environment to the list of active virtual machines
|
||||
def add(vm)
|
||||
list << vm.uuid
|
||||
list.uniq!
|
||||
list[vm.uuid] = {
|
||||
:path => env.root_path,
|
||||
:last_updated => Time.now.to_i
|
||||
}
|
||||
|
||||
save
|
||||
end
|
||||
|
||||
|
@ -60,7 +66,7 @@ module Vagrant
|
|||
# Persists the list down to the JSON file.
|
||||
def save
|
||||
File.open(path, "w+") do |f|
|
||||
f.write(filtered_list.to_json)
|
||||
f.write(filter_list.to_json)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -36,13 +36,13 @@ class ActiveListTest < Test::Unit::TestCase
|
|||
should "not load if the active json file doesn't exist" do
|
||||
File.expects(:file?).with(@list.path).returns(false)
|
||||
File.expects(:open).never
|
||||
assert_equal [], @list.list(true)
|
||||
assert_equal Hash.new, @list.list(true)
|
||||
end
|
||||
|
||||
should "parse the JSON by reading the file" do
|
||||
file = mock("file")
|
||||
data = mock("data")
|
||||
result = mock("result")
|
||||
result = { :hey => :yep }
|
||||
File.expects(:file?).returns(true)
|
||||
File.expects(:open).with(@list.path, 'r').once.yields(file)
|
||||
file.expects(:read).returns(data)
|
||||
|
@ -59,50 +59,29 @@ class ActiveListTest < Test::Unit::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
context "vms" do
|
||||
setup do
|
||||
@the_list = ["foo", "bar"]
|
||||
@list.stubs(:list).returns(@the_list)
|
||||
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 = []
|
||||
context "filter list" do
|
||||
should "remove nonexistent VMs" do
|
||||
list = {}
|
||||
result = {}
|
||||
5.times do |i|
|
||||
vm = mock("vm#{i}")
|
||||
vm.expects(:uuid).returns(i)
|
||||
result << i
|
||||
vms << vm
|
||||
vm.stubs(:uuid).returns(i)
|
||||
|
||||
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
|
||||
|
||||
@list.stubs(:vms).returns(vms)
|
||||
assert_equal result, @list.filtered_list
|
||||
@list.stubs(:list).returns(list)
|
||||
assert_equal result, @list.filter_list
|
||||
end
|
||||
end
|
||||
|
||||
context "adding a VM to the list" do
|
||||
setup do
|
||||
@the_list = []
|
||||
@the_list = {}
|
||||
@list.stubs(:list).returns(@the_list)
|
||||
@list.stubs(:save)
|
||||
|
||||
|
@ -113,19 +92,13 @@ class ActiveListTest < Test::Unit::TestCase
|
|||
|
||||
should "add the VMs UUID to the list" do
|
||||
@list.add(@vm)
|
||||
assert_equal [@uuid], @the_list
|
||||
end
|
||||
|
||||
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
|
||||
assert @the_list[@uuid]
|
||||
assert @the_list[@uuid].is_a?(Hash)
|
||||
end
|
||||
|
||||
should "save after adding" do
|
||||
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.add(@vm)
|
||||
end
|
||||
|
@ -165,7 +138,7 @@ class ActiveListTest < Test::Unit::TestCase
|
|||
context "saving" do
|
||||
setup do
|
||||
@filtered = ["zoo"]
|
||||
@list.stubs(:filtered_list).returns(@filtered)
|
||||
@list.stubs(:filter_list).returns(@filtered)
|
||||
end
|
||||
|
||||
should "open the JSON path and save to it" do
|
||||
|
|
Loading…
Reference in New Issue