From 7971d656fe7ea9dbf1efb98a6036144148c20168 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 3 Feb 2010 00:02:12 -0800 Subject: [PATCH] Implement `forward_port` method for VMs in configuration --- config/default.rb | 3 ++- lib/hobo/config.rb | 15 ++++++++++++++- lib/hobo/ssh.rb | 11 ++++++++--- lib/hobo/vm.rb | 29 +++++++++++++++++------------ test/hobo/ssh_test.rb | 16 ++++++++++++++-- test/hobo/vm_test.rb | 14 +++++++------- test/test_helper.rb | 3 ++- 7 files changed, 64 insertions(+), 27 deletions(-) diff --git a/config/default.rb b/config/default.rb index 3e57e8175..a8979f0d5 100644 --- a/config/default.rb +++ b/config/default.rb @@ -3,11 +3,12 @@ Hobo::Config.run do |config| config.ssh.uname = "hobo" config.ssh.pass = "hobo" config.ssh.host = "localhost" - config.ssh.port = 2222 + config.ssh.forwarded_port_key = "ssh" config.ssh.max_tries = 10 config.dotfile_name = ".hobo" config.vm.base = "~/.hobo/base/base.ovf" config.vm.base_mac = "0800279C2E41" + config.vm.forward_port("ssh", 22, 2222) end \ No newline at end of file diff --git a/lib/hobo/config.rb b/lib/hobo/config.rb index c05364a16..0d8396374 100644 --- a/lib/hobo/config.rb +++ b/lib/hobo/config.rb @@ -41,13 +41,26 @@ module Hobo attr_accessor :uname attr_accessor :pass attr_accessor :host - attr_accessor :port + attr_accessor :forwarded_port_key attr_accessor :max_tries end class VMConfig < Base attr_accessor :base attr_accessor :base_mac + attr_reader :forwarded_ports + + def initialize + @forwarded_ports = {} + end + + def forward_port(name, guestport, hostport, protocol="TCP") + forwarded_ports[name] = { + :guestport => guestport, + :hostport => hostport, + :protocol => protocol + } + end end class Top < Base diff --git a/lib/hobo/ssh.rb b/lib/hobo/ssh.rb index 7d483daa4..b07ae7fc0 100644 --- a/lib/hobo/ssh.rb +++ b/lib/hobo/ssh.rb @@ -5,21 +5,26 @@ module Hobo class << self def connect(opts={}) options = {} - [:port, :host, :pass, :uname].each do |param| + [:host, :pass, :uname].each do |param| options[param] = opts[param] || Hobo.config.ssh.send(param) end + # The port is special + options[:port] = opts[:port] || Hobo.config.vm.forwarded_ports[Hobo.config.ssh.forwarded_port_key][:hostport] + Kernel.exec "#{SCRIPT} #{options[:uname]} #{options[:pass]} #{options[:host]} #{options[:port]}".strip end def execute - Net::SSH.start("localhost", Hobo.config[:ssh][:uname], :port => Hobo.config[:ssh][:port], :password => Hobo.config[:ssh][:pass]) do |ssh| + port = Hobo.config.vm.forwarded_ports[Hobo.config.ssh.forwarded_port_key][:hostport] + Net::SSH.start("localhost", Hobo.config[:ssh][:uname], :port => port, :password => Hobo.config[:ssh][:pass]) do |ssh| yield ssh end end def up? - Ping.pingecho "localhost", 1, Hobo.config[:ssh][:port] + port = Hobo.config.vm.forwarded_ports[Hobo.config.ssh.forwarded_port_key][:hostport] + Ping.pingecho "localhost", 1, port end end end diff --git a/lib/hobo/vm.rb b/lib/hobo/vm.rb index ba2c77f53..b7a0aec46 100644 --- a/lib/hobo/vm.rb +++ b/lib/hobo/vm.rb @@ -23,7 +23,7 @@ module Hobo SSH.connect end - + # Save the state of the current hobo environment to disk def suspend Env.require_persisted_vm @@ -61,7 +61,7 @@ error import persist setup_mac_address - forward_ssh + forward_ports setup_shared_folder start end @@ -92,13 +92,18 @@ error @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 + def forward_ports + HOBO_LOGGER.info "Forwarding ports..." + + Hobo.config.vm.forwarded_ports.each do |name, options| + HOBO_LOGGER.info "Forwarding \"#{name}\": #{options[:guestport]} => #{options[:hostport]}" + port = VirtualBox::ForwardedPort.new + port.name = name + port.hostport = options[:hostport] + port.guestport = options[:guestport] + @vm.forwarded_ports << port + end + @vm.save(true) end @@ -117,11 +122,11 @@ error # Now we have to wait for the boot to be successful HOBO_LOGGER.info "Waiting for VM to boot..." - + Hobo.config[:ssh][:max_tries].to_i.times do |i| sleep 5 unless ENV['HOBO_ENV'] == 'test' HOBO_LOGGER.info "Trying to connect (attempt ##{i+1} of #{Hobo.config[:ssh][:max_tries]})..." - + if Hobo::SSH.up? HOBO_LOGGER.info "VM booted and ready for use!" return true @@ -133,7 +138,7 @@ error end def saved?; @vm.saved? end - + def save_state(errs); @vm.save_state(errs) end end end diff --git a/test/hobo/ssh_test.rb b/test/hobo/ssh_test.rb index 64aeb97c2..5e128137b 100644 --- a/test/hobo/ssh_test.rb +++ b/test/hobo/ssh_test.rb @@ -1,15 +1,19 @@ require File.join(File.dirname(__FILE__), '..', 'test_helper') class SshTest < Test::Unit::TestCase + setup do + hobo_mock_config + end + context "hobo ssh" do setup do @script = Hobo::SSH::SCRIPT - hobo_mock_config end test "should call exec with defaults when no options are supplied" do ssh = Hobo.config.ssh - Kernel.expects(:exec).with("#{@script} #{ssh[:uname]} #{ssh[:pass]} #{ssh[:host]} #{ssh[:port]}") + port = Hobo.config.vm.forwarded_ports[ssh.forwarded_port_key][:hostport] + Kernel.expects(:exec).with("#{@script} #{ssh[:uname]} #{ssh[:pass]} #{ssh[:host]} #{port}") Hobo::SSH.connect end @@ -26,4 +30,12 @@ class SshTest < Test::Unit::TestCase Hobo::SSH.execute end end + + context "checking if host is up" do + should "pingecho the server" do + port = Hobo.config.vm.forwarded_ports[Hobo.config.ssh.forwarded_port_key][:hostport] + Ping.expects(:pingecho).with("localhost", 1, port).once + Hobo::SSH.up? + end + end end diff --git a/test/hobo/vm_test.rb b/test/hobo/vm_test.rb index 867f07b8f..2955eb7bd 100644 --- a/test/hobo/vm_test.rb +++ b/test/hobo/vm_test.rb @@ -77,7 +77,7 @@ class VMTest < Test::Unit::TestCase @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(:forward_ports).in_sequence(create_seq) @vm.expects(:setup_shared_folder).in_sequence(create_seq) @vm.expects(:start).in_sequence(create_seq) @vm.create @@ -157,7 +157,7 @@ class VMTest < Test::Unit::TestCase end end - context "forwarding SSH" do + context "forwarding ports" do should "create a port forwarding for the VM" do # TODO: Test the actual port value to make sure it has the # correct attributes @@ -165,7 +165,7 @@ class VMTest < Test::Unit::TestCase forwarded_ports.expects(:<<) @mock_vm.expects(:forwarded_ports).returns(forwarded_ports) @mock_vm.expects(:save).with(true).once - @vm.forward_ssh + @vm.forward_ports end end @@ -184,7 +184,7 @@ class VMTest < Test::Unit::TestCase end context "suspending and resuming a vm" do - should "put the vm in a suspended state" do + should "put the vm in a suspended state" do saved_state_expectation(false) save_expectation Hobo::VM.suspend @@ -197,7 +197,7 @@ class VMTest < Test::Unit::TestCase Hobo::VM.suspend end - should "start a vm in a suspended state" do + should "start a vm in a suspended state" do saved_state_expectation(true) start_expectation Hobo::VM.resume @@ -209,12 +209,12 @@ class VMTest < Test::Unit::TestCase # TODO research the matter of mocking exit Hobo::VM.expects(:error_and_exit) Hobo::VM.resume - end + end def saved_state_expectation(saved) @persisted_vm.expects(:saved?).returns(saved) end - + def save_expectation @persisted_vm.expects(:save_state).with(true) end diff --git a/test/test_helper.rb b/test/test_helper.rb index 35776c195..626f96f46 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -37,11 +37,12 @@ class Test::Unit::TestCase config.ssh.uname = "foo" config.ssh.pass = "bar" config.ssh.host = "baz" - config.ssh.port = "bak" + config.ssh.forwarded_port_key = "ssh" config.ssh.max_tries = 10 config.vm.base = "foo" config.vm.base_mac = "42" + config.vm.forward_port("ssh", 22, 2222) end Hobo::Config.execute!