From f4cc3d430ba4f0ee1c854a07ffaf3984486a34ca Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 30 Jan 2010 00:06:56 -0800 Subject: [PATCH] Hobo up creates the VM, matches MAC addresses for NAT, port forwards SSH. --- config/default.yml | 4 ++- lib/hobo/vm.rb | 40 +++++++++++++++++++++++++-- test/hobo/vm_test.rb | 64 ++++++++++++++++++++++++++++++++++++++++++++ test/test_helper.rb | 7 +++-- 4 files changed, 110 insertions(+), 5 deletions(-) create mode 100644 test/hobo/vm_test.rb diff --git a/config/default.yml b/config/default.yml index 3537bc357..7d13d96af 100644 --- a/config/default.yml +++ b/config/default.yml @@ -2,6 +2,8 @@ :uname: hobo :pass: hobo :host: localhost + :port: 2222 :vm: - :base: ~/.hobo/base/base.ovf \ No newline at end of file + :base: ~/.hobo/base/base.ovf + :base_mac: 0800279C2E41 \ No newline at end of file diff --git a/lib/hobo/vm.rb b/lib/hobo/vm.rb index 98503de15..9a56abfa2 100644 --- a/lib/hobo/vm.rb +++ b/lib/hobo/vm.rb @@ -4,12 +4,48 @@ module Hobo # Bring up the virtual machine. Imports the base image and # provisions it. def up + 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]})..." - vm = VirtualBox::VM.import(File.expand_path(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 \ No newline at end of file diff --git a/test/hobo/vm_test.rb b/test/hobo/vm_test.rb new file mode 100644 index 000000000..c29d8037f --- /dev/null +++ b/test/hobo/vm_test.rb @@ -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 diff --git a/test/test_helper.rb b/test/test_helper.rb index 9d4f33cef..b8cd33d8a 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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