Hobo::VM uses instance methods now since the VM is a form of state

This commit is contained in:
Mitchell Hashimoto 2010-01-31 17:59:19 -08:00
parent 4cffa15d4e
commit 642c94429e
2 changed files with 103 additions and 80 deletions

View File

@ -4,11 +4,7 @@ module Hobo
# Bring up the virtual machine. Imports the base image and # Bring up the virtual machine. Imports the base image and
# provisions it. # provisions it.
def up def up
vm = import new.create
persist_vm(vm)
setup_mac_address(vm)
forward_ssh(vm)
setup_shared_folder(vm)
end end
# Tear down a virtual machine. # Tear down a virtual machine.
@ -18,42 +14,54 @@ module Hobo
HOBO_LOGGER.info "Destroying VM and associated drives..." HOBO_LOGGER.info "Destroying VM and associated drives..."
Env.persisted_vm.destroy(:destroy_image => true) Env.persisted_vm.destroy(:destroy_image => true)
end end
end
def initialize(vm=nil)
@vm = vm
end
def create
import
persist
setup_mac_address
forward_ssh
setup_shared_folder
end
def import def import
HOBO_LOGGER.info "Importing base VM (#{Hobo.config[:vm][:base]})..." HOBO_LOGGER.info "Importing base VM (#{Hobo.config[:vm][:base]})..."
VirtualBox::VM.import(File.expand_path(Hobo.config[:vm][:base])) @vm = VirtualBox::VM.import(File.expand_path(Hobo.config[:vm][:base]))
end end
def persist_vm(vm) def persist
HOBO_LOGGER.info "Persisting the VM UUID (#{vm.uuid})..." HOBO_LOGGER.info "Persisting the VM UUID (#{@vm.uuid})..."
Env.persist_vm(vm) Env.persist_vm(@vm)
end end
def setup_mac_address(vm) def setup_mac_address
HOBO_LOGGER.info "Matching MAC addresses..." HOBO_LOGGER.info "Matching MAC addresses..."
vm.nics.first.macaddress = Hobo.config[:vm][:base_mac] @vm.nics.first.macaddress = Hobo.config[:vm][:base_mac]
vm.save(true) @vm.save(true)
end end
def forward_ssh(vm) def forward_ssh
HOBO_LOGGER.info "Forwarding SSH ports..." HOBO_LOGGER.info "Forwarding SSH ports..."
port = VirtualBox::ForwardedPort.new port = VirtualBox::ForwardedPort.new
port.name = "ssh" port.name = "ssh"
port.hostport = Hobo.config[:ssh][:port] port.hostport = Hobo.config[:ssh][:port]
port.guestport = 22 port.guestport = 22
vm.forwarded_ports << port @vm.forwarded_ports << port
vm.save(true) @vm.save(true)
end end
# TODO: We need to get the host path. # TODO: We need to get the host path.
def setup_shared_folder(vm) def setup_shared_folder
HOBO_LOGGER.info "Creating shared folders..." HOBO_LOGGER.info "Creating shared folders..."
folder = VirtualBox::SharedFolder.new folder = VirtualBox::SharedFolder.new
folder.name = "project-path" folder.name = "project-path"
folder.hostpath = "" folder.hostpath = ""
vm.shared_folders << folder @vm.shared_folders << folder
#vm.save(true) #vm.save(true)
end end
end end
end
end end

View File

@ -2,7 +2,7 @@ require File.join(File.dirname(__FILE__), '..', 'test_helper')
class VMTest < Test::Unit::TestCase class VMTest < Test::Unit::TestCase
setup do setup do
@vm = mock("vm") @mock_vm = mock("vm")
Hobo.config!(hobo_mock_config) Hobo.config!(hobo_mock_config)
end end
@ -25,34 +25,48 @@ class VMTest < Test::Unit::TestCase
end end
context "hobo up" do context "hobo up" do
should "create the instance in the proper order" do should "create a Hobo::VM instance and call create" do
create_seq = sequence("create_seq") inst = mock("instance")
Hobo::VM.expects(:import).in_sequence(create_seq) inst.expects(:create).once
Hobo::VM.expects(:persist_vm).in_sequence(create_seq) Hobo::VM.expects(:new).returns(inst)
Hobo::VM.expects(:setup_mac_address).in_sequence(create_seq)
Hobo::VM.expects(:forward_ssh).in_sequence(create_seq)
Hobo::VM.expects(:setup_shared_folder).in_sequence(create_seq)
Hobo::VM.up Hobo::VM.up
end end
end end
context "hobo VM instance" do
setup do
@vm = Hobo::VM.new(@mock_vm)
end
context "creating" do
should "create the VM in the proper order" do
create_seq = sequence("create_seq")
@vm.expects(:import).in_sequence(create_seq)
@vm.expects(:persist).in_sequence(create_seq)
@vm.expects(:setup_mac_address).in_sequence(create_seq)
@vm.expects(:forward_ssh).in_sequence(create_seq)
@vm.expects(:setup_shared_folder).in_sequence(create_seq)
@vm.create
end
end
context "importing" do context "importing" do
should "call import on VirtualBox::VM with the proper base" do should "call import on VirtualBox::VM with the proper base" do
VirtualBox::VM.expects(:import).once VirtualBox::VM.expects(:import).once
Hobo::VM.import @vm.import
end end
should "return the VM object" do should "return the VM object" do
VirtualBox::VM.expects(:import).returns(@vm).once VirtualBox::VM.expects(:import).returns(@mock_vm).once
assert_equal @vm, Hobo::VM.import assert_equal @mock_vm, @vm.import
end end
end end
context "persisting VM" do context "persisting" do
should "persist the VM with Env" do should "persist the VM with Env" do
@vm.stubs(:uuid) @mock_vm.stubs(:uuid)
Hobo::Env.expects(:persist_vm).with(@vm).once Hobo::Env.expects(:persist_vm).with(@mock_vm).once
Hobo::VM.persist_vm(@vm) @vm.persist
end end
end end
@ -61,10 +75,10 @@ class VMTest < Test::Unit::TestCase
nic = mock("nic") nic = mock("nic")
nic.expects(:macaddress=).once nic.expects(:macaddress=).once
@vm.expects(:nics).returns([nic]).once @mock_vm.expects(:nics).returns([nic]).once
@vm.expects(:save).with(true).once @mock_vm.expects(:save).with(true).once
Hobo::VM.setup_mac_address(@vm) @vm.setup_mac_address
end end
end end
@ -74,13 +88,14 @@ class VMTest < Test::Unit::TestCase
# correct attributes # correct attributes
forwarded_ports = mock("forwarded_ports") forwarded_ports = mock("forwarded_ports")
forwarded_ports.expects(:<<) forwarded_ports.expects(:<<)
@vm.expects(:forwarded_ports).returns(forwarded_ports) @mock_vm.expects(:forwarded_ports).returns(forwarded_ports)
@vm.expects(:save).with(true).once @mock_vm.expects(:save).with(true).once
Hobo::VM.forward_ssh(@vm) @vm.forward_ssh
end end
end end
context "setting up the shared folder" do context "setting up the shared folder" do
# TODO: Since the code actually doesn't do anything yet # TODO: Since the code actually doesn't do anything yet
end end
end
end end