From f3cd0591d00eead8266372968da5fd886c1535e5 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 15 Feb 2010 18:07:11 -0800 Subject: [PATCH] Refactored SSH in how it retrieves the port --- lib/vagrant/ssh.rb | 10 +++++----- test/vagrant/ssh_test.rb | 25 ++++++++++++++++++++----- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/lib/vagrant/ssh.rb b/lib/vagrant/ssh.rb index 135d8fa63..8a30d5c64 100644 --- a/lib/vagrant/ssh.rb +++ b/lib/vagrant/ssh.rb @@ -9,10 +9,7 @@ module Vagrant options[param] = opts[param] || Vagrant.config.ssh.send(param) end - # The port is special - options[:port] = opts[:port] || Vagrant.config.vm.forwarded_ports[Vagrant.config.ssh.forwarded_port_key][:hostport] - - Kernel.exec "#{SCRIPT} #{options[:username]} #{options[:password]} #{options[:host]} #{options[:port]}".strip + Kernel.exec "#{SCRIPT} #{options[:username]} #{options[:password]} #{options[:host]} #{port(opts)}".strip end def execute @@ -30,7 +27,6 @@ module Vagrant end def up? - port = Vagrant.config.vm.forwarded_ports[Vagrant.config.ssh.forwarded_port_key][:hostport] Net::SSH.start(Vagrant.config.ssh.host, Vagrant.config.ssh.username, :port => port, :password => Vagrant.config.ssh.password, :timeout => 5) do |ssh| return true end @@ -39,6 +35,10 @@ module Vagrant rescue Errno::ECONNREFUSED false end + + def port(opts={}) + opts[:port] || Vagrant.config.vm.forwarded_ports[Vagrant.config.ssh.forwarded_port_key][:hostport] + end end end end diff --git a/test/vagrant/ssh_test.rb b/test/vagrant/ssh_test.rb index 49293a6c7..568c85d89 100644 --- a/test/vagrant/ssh_test.rb +++ b/test/vagrant/ssh_test.rb @@ -5,15 +5,14 @@ class SshTest < Test::Unit::TestCase mock_config end - context "ssh" do + context "connecting to SSH" do setup do @script = Vagrant::SSH::SCRIPT end test "should call exec with defaults when no options are supplied" do ssh = Vagrant.config.ssh - port = Vagrant.config.vm.forwarded_ports[ssh.forwarded_port_key][:hostport] - Kernel.expects(:exec).with("#{@script} #{ssh[:username]} #{ssh[:password]} #{ssh[:host]} #{port}") + Kernel.expects(:exec).with("#{@script} #{ssh[:username]} #{ssh[:password]} #{ssh[:host]} #{Vagrant::SSH.port}") Vagrant::SSH.connect end @@ -24,9 +23,15 @@ class SshTest < Test::Unit::TestCase end end - context "net-ssh interaction" do + context "executing ssh commands" do should "call net::ssh.start with the proper names" do - Net::SSH.expects(:start).with(Vagrant.config.ssh.host, Vagrant.config.ssh.username, anything).once + Net::SSH.expects(:start).once.with() do |host, username, opts| + assert_equal Vagrant.config.ssh.host, host + assert_equal Vagrant.config.ssh.username, username + assert_equal Vagrant::SSH.port, opts[:port] + assert_equal Vagrant.config.ssh.password, opts[:password] + true + end Vagrant::SSH.execute end @@ -70,4 +75,14 @@ class SshTest < Test::Unit::TestCase } end end + + context "getting the ssh port" do + should "return the configured port by default" do + assert_equal Vagrant.config.vm.forwarded_ports[Vagrant.config.ssh.forwarded_port_key][:hostport], Vagrant::SSH.port + end + + should "return the port given in options if it exists" do + assert_equal "47", Vagrant::SSH.port({ :port => "47" }) + end + end end