Hobo up creates the VM, matches MAC addresses for NAT, port forwards SSH.

This commit is contained in:
Mitchell Hashimoto 2010-01-30 00:06:56 -08:00
parent 4fa88e6436
commit f4cc3d430b
4 changed files with 110 additions and 5 deletions

View File

@ -2,6 +2,8 @@
:uname: hobo
:pass: hobo
:host: localhost
:port: 2222
:vm:
:base: ~/.hobo/base/base.ovf
:base_mac: 0800279C2E41

View File

@ -4,12 +4,48 @@ module Hobo
# Bring up the virtual machine. Imports the base image and
# provisions it.
def up
HOBO_LOGGER.info "Importing base VM (#{Hobo.config[:vm][:base]})..."
vm = VirtualBox::VM.import(File.expand_path(Hobo.config[:vm][:base]))
vm = import
persist_uuid(vm)
setup_mac_address(vm)
forward_ssh(vm)
setup_shared_folder(vm)
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 persist_uuid(vm)
HOBO_LOGGER.info "Persisting the VM UUID (#{vm.uuid})..."
# TODO: persist it! dot file in the root (where Hobofile is)
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 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
# 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
end
end
end

64
test/hobo/vm_test.rb Normal file
View File

@ -0,0 +1,64 @@
require File.join(File.dirname(__FILE__), '..', 'test_helper')
class VMTest < Test::Unit::TestCase
setup do
@vm = mock("vm")
Hobo.config!(hobo_mock_config)
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_uuid).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)
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
end
should "return the VM object" do
VirtualBox::VM.expects(:import).returns(@vm).once
assert_equal @vm, Hobo::VM.import
end
end
context "persisting UUID" do
# TODO, functionality doesn't exist yet
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)
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)
end
end
context "setting up the shared folder" do
# TODO: Since the code actually doesn't do anything yet
end
end

View File

@ -28,13 +28,16 @@ require 'mocha'
class Test::Unit::TestCase
def hobo_mock_config
{ :ssh =>
{
{ :ssh => {
:uname => 'foo',
:pass => 'bar',
:host => 'baz',
:port => 'bak'
},
:vm => {
:base => "foo",
:base_mac => "42"
},
:dotfile_name => '.hobo'
}
end