Hobo::Env now loads the persisted VM as a Hobo::VM object

This commit is contained in:
Mitchell Hashimoto 2010-01-31 18:10:01 -08:00
parent 642c94429e
commit 652a157d51
4 changed files with 26 additions and 2 deletions

View File

@ -48,7 +48,7 @@ module Hobo
def load_vm!
File.open(dotfile_path) do |f|
@@persisted_vm = VirtualBox::VM.find(f.read)
@@persisted_vm = Hobo::VM.find(f.read)
end
rescue Errno::ENOENT
@@persisted_vm = nil

View File

@ -1,5 +1,7 @@
module Hobo
class VM
attr_reader :vm
class <<self
# Bring up the virtual machine. Imports the base image and
# provisions it.
@ -14,6 +16,14 @@ module Hobo
HOBO_LOGGER.info "Destroying VM and associated drives..."
Env.persisted_vm.destroy(:destroy_image => true)
end
# Finds a virtual machine by a given UUID and either returns
# a Hobo::VM object or returns nil.
def find(uuid)
vm = VirtualBox::VM.find(uuid)
return nil if vm.nil?
new(vm)
end
end
def initialize(vm=nil)

View File

@ -17,7 +17,7 @@ class EnvTest < Test::Unit::TestCase
def mock_persisted_vm(returnvalue="foovm")
filemock = mock("filemock")
filemock.expects(:read).returns("foo")
VirtualBox::VM.expects(:find).with("foo").returns(returnvalue)
Hobo::VM.expects(:find).with("foo").returns(returnvalue)
File.expects(:open).with(Hobo::Env.dotfile_path).once.yields(filemock)
Hobo::Env.load_vm!
end

View File

@ -33,6 +33,20 @@ class VMTest < Test::Unit::TestCase
end
end
context "finding a VM" do
should "return nil if the VM is not found" do
VirtualBox::VM.expects(:find).returns(nil)
assert_nil Hobo::VM.find("foo")
end
should "return a Hobo::VM object for that VM otherwise" do
VirtualBox::VM.expects(:find).with("foo").returns("bar")
result = Hobo::VM.find("foo")
assert result.is_a?(Hobo::VM)
assert_equal "bar", result.vm
end
end
context "hobo VM instance" do
setup do
@vm = Hobo::VM.new(@mock_vm)