From 422951f0eb9a538be2a092281c0d33d50cee31a2 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 30 Apr 2010 23:34:22 -0700 Subject: [PATCH] ActiveList now contains meaningful information about the running VMs --- lib/vagrant/active_list.rb | 30 ++++++++------ test/vagrant/active_list_test.rb | 67 ++++++++++---------------------- 2 files changed, 38 insertions(+), 59 deletions(-) diff --git a/lib/vagrant/active_list.rb b/lib/vagrant/active_list.rb index da292f49a..6efcc6521 100644 --- a/lib/vagrant/active_list.rb +++ b/lib/vagrant/active_list.rb @@ -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 diff --git a/test/vagrant/active_list_test.rb b/test/vagrant/active_list_test.rb index 843aeba1b..29d90b1ad 100644 --- a/test/vagrant/active_list_test.rb +++ b/test/vagrant/active_list_test.rb @@ -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