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

View File

@ -2,7 +2,7 @@ require File.join(File.dirname(__FILE__), '..', 'test_helper')
class VMTest < Test::Unit::TestCase
setup do
@vm = mock("vm")
@mock_vm = mock("vm")
Hobo.config!(hobo_mock_config)
end
@ -25,62 +25,77 @@ class VMTest < Test::Unit::TestCase
end
context "hobo up" do
should "create the instance in the proper order" do
create_seq = sequence("create_seq")
Hobo::VM.expects(:import).in_sequence(create_seq)
Hobo::VM.expects(:persist_vm).in_sequence(create_seq)
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)
should "create a Hobo::VM instance and call create" do
inst = mock("instance")
inst.expects(:create).once
Hobo::VM.expects(:new).returns(inst)
Hobo::VM.up
end
end
context "importing" do
should "call import on VirtualBox::VM with the proper base" do
VirtualBox::VM.expects(:import).once
Hobo::VM.import
context "hobo VM instance" do
setup do
@vm = Hobo::VM.new(@mock_vm)
end
should "return the VM object" do
VirtualBox::VM.expects(:import).returns(@vm).once
assert_equal @vm, Hobo::VM.import
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
end
context "persisting VM" do
should "persist the VM with Env" do
@vm.stubs(:uuid)
Hobo::Env.expects(:persist_vm).with(@vm).once
Hobo::VM.persist_vm(@vm)
context "importing" do
should "call import on VirtualBox::VM with the proper base" do
VirtualBox::VM.expects(:import).once
@vm.import
end
should "return the VM object" do
VirtualBox::VM.expects(:import).returns(@mock_vm).once
assert_equal @mock_vm, @vm.import
end
end
end
context "setting up MAC address" do
should "match the mac address with the base" do
nic = mock("nic")
nic.expects(:macaddress=).once
@vm.expects(:nics).returns([nic]).once
@vm.expects(:save).with(true).once
Hobo::VM.setup_mac_address(@vm)
context "persisting" do
should "persist the VM with Env" do
@mock_vm.stubs(:uuid)
Hobo::Env.expects(:persist_vm).with(@mock_vm).once
@vm.persist
end
end
end
context "forwarding SSH" do
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(:<<)
@vm.expects(:forwarded_ports).returns(forwarded_ports)
@vm.expects(:save).with(true).once
Hobo::VM.forward_ssh(@vm)
context "setting up MAC address" do
should "match the mac address with the base" do
nic = mock("nic")
nic.expects(:macaddress=).once
@mock_vm.expects(:nics).returns([nic]).once
@mock_vm.expects(:save).with(true).once
@vm.setup_mac_address
end
end
end
context "setting up the shared folder" do
# TODO: Since the code actually doesn't do anything yet
context "forwarding SSH" do
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