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 import def initialize(vm=nil)
HOBO_LOGGER.info "Importing base VM (#{Hobo.config[:vm][:base]})..." @vm = vm
VirtualBox::VM.import(File.expand_path(Hobo.config[:vm][:base])) end
end
def persist_vm(vm) def create
HOBO_LOGGER.info "Persisting the VM UUID (#{vm.uuid})..." import
Env.persist_vm(vm) persist
end setup_mac_address
forward_ssh
setup_shared_folder
end
def setup_mac_address(vm) def import
HOBO_LOGGER.info "Matching MAC addresses..." HOBO_LOGGER.info "Importing base VM (#{Hobo.config[:vm][:base]})..."
vm.nics.first.macaddress = Hobo.config[:vm][:base_mac] @vm = VirtualBox::VM.import(File.expand_path(Hobo.config[:vm][:base]))
vm.save(true) end
end
def forward_ssh(vm) def persist
HOBO_LOGGER.info "Forwarding SSH ports..." HOBO_LOGGER.info "Persisting the VM UUID (#{@vm.uuid})..."
port = VirtualBox::ForwardedPort.new Env.persist_vm(@vm)
port.name = "ssh" end
port.hostport = Hobo.config[:ssh][:port]
port.guestport = 22
vm.forwarded_ports << port
vm.save(true)
end
# TODO: We need to get the host path. def setup_mac_address
def setup_shared_folder(vm) HOBO_LOGGER.info "Matching MAC addresses..."
HOBO_LOGGER.info "Creating shared folders..." @vm.nics.first.macaddress = Hobo.config[:vm][:base_mac]
folder = VirtualBox::SharedFolder.new @vm.save(true)
folder.name = "project-path" end
folder.hostpath = ""
vm.shared_folders << folder def forward_ssh
#vm.save(true) HOBO_LOGGER.info "Forwarding SSH ports..."
end port = VirtualBox::ForwardedPort.new
port.name = "ssh"
port.hostport = Hobo.config[:ssh][:port]
port.guestport = 22
@vm.forwarded_ports << port
@vm.save(true)
end
# TODO: We need to get the host path.
def setup_shared_folder
HOBO_LOGGER.info "Creating shared folders..."
folder = VirtualBox::SharedFolder.new
folder.name = "project-path"
folder.hostpath = ""
@vm.shared_folders << folder
#vm.save(true)
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,62 +25,77 @@ 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 "importing" do context "hobo VM instance" do
should "call import on VirtualBox::VM with the proper base" do setup do
VirtualBox::VM.expects(:import).once @vm = Hobo::VM.new(@mock_vm)
Hobo::VM.import
end end
should "return the VM object" do context "creating" do
VirtualBox::VM.expects(:import).returns(@vm).once should "create the VM in the proper order" do
assert_equal @vm, Hobo::VM.import 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 end
end
context "persisting VM" do context "importing" do
should "persist the VM with Env" do should "call import on VirtualBox::VM with the proper base" do
@vm.stubs(:uuid) VirtualBox::VM.expects(:import).once
Hobo::Env.expects(:persist_vm).with(@vm).once @vm.import
Hobo::VM.persist_vm(@vm) end
should "return the VM object" do
VirtualBox::VM.expects(:import).returns(@mock_vm).once
assert_equal @mock_vm, @vm.import
end
end end
end
context "setting up MAC address" do context "persisting" do
should "match the mac address with the base" do should "persist the VM with Env" do
nic = mock("nic") @mock_vm.stubs(:uuid)
nic.expects(:macaddress=).once Hobo::Env.expects(:persist_vm).with(@mock_vm).once
@vm.persist
@vm.expects(:nics).returns([nic]).once end
@vm.expects(:save).with(true).once
Hobo::VM.setup_mac_address(@vm)
end end
end
context "forwarding SSH" do context "setting up MAC address" do
should "create a port forwarding for the VM" do should "match the mac address with the base" do
# TODO: Test the actual port value to make sure it has the nic = mock("nic")
# correct attributes nic.expects(:macaddress=).once
forwarded_ports = mock("forwarded_ports")
forwarded_ports.expects(:<<) @mock_vm.expects(:nics).returns([nic]).once
@vm.expects(:forwarded_ports).returns(forwarded_ports) @mock_vm.expects(:save).with(true).once
@vm.expects(:save).with(true).once
Hobo::VM.forward_ssh(@vm) @vm.setup_mac_address
end
end end
end
context "setting up the shared folder" do context "forwarding SSH" do
# TODO: Since the code actually doesn't do anything yet should "create a port forwarding for the VM" do
# TODO: Test the actual port value to make sure it has the
# correct attributes
forwarded_ports = mock("forwarded_ports")
forwarded_ports.expects(:<<)
@mock_vm.expects(:forwarded_ports).returns(forwarded_ports)
@mock_vm.expects(:save).with(true).once
@vm.forward_ssh
end
end
context "setting up the shared folder" do
# TODO: Since the code actually doesn't do anything yet
end
end end
end end