Refactored SSH in how it retrieves the port

This commit is contained in:
Mitchell Hashimoto 2010-02-15 18:07:11 -08:00
parent 3d27d47d34
commit f3cd0591d0
2 changed files with 25 additions and 10 deletions

View File

@ -9,10 +9,7 @@ module Vagrant
options[param] = opts[param] || Vagrant.config.ssh.send(param) options[param] = opts[param] || Vagrant.config.ssh.send(param)
end end
# The port is special Kernel.exec "#{SCRIPT} #{options[:username]} #{options[:password]} #{options[:host]} #{port(opts)}".strip
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
end end
def execute def execute
@ -30,7 +27,6 @@ module Vagrant
end end
def up? 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| Net::SSH.start(Vagrant.config.ssh.host, Vagrant.config.ssh.username, :port => port, :password => Vagrant.config.ssh.password, :timeout => 5) do |ssh|
return true return true
end end
@ -39,6 +35,10 @@ module Vagrant
rescue Errno::ECONNREFUSED rescue Errno::ECONNREFUSED
false false
end end
def port(opts={})
opts[:port] || Vagrant.config.vm.forwarded_ports[Vagrant.config.ssh.forwarded_port_key][:hostport]
end
end end
end end
end end

View File

@ -5,15 +5,14 @@ class SshTest < Test::Unit::TestCase
mock_config mock_config
end end
context "ssh" do context "connecting to SSH" do
setup do setup do
@script = Vagrant::SSH::SCRIPT @script = Vagrant::SSH::SCRIPT
end end
test "should call exec with defaults when no options are supplied" do test "should call exec with defaults when no options are supplied" do
ssh = Vagrant.config.ssh 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]} #{Vagrant::SSH.port}")
Kernel.expects(:exec).with("#{@script} #{ssh[:username]} #{ssh[:password]} #{ssh[:host]} #{port}")
Vagrant::SSH.connect Vagrant::SSH.connect
end end
@ -24,9 +23,15 @@ class SshTest < Test::Unit::TestCase
end end
end end
context "net-ssh interaction" do context "executing ssh commands" do
should "call net::ssh.start with the proper names" 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 Vagrant::SSH.execute
end end
@ -70,4 +75,14 @@ class SshTest < Test::Unit::TestCase
} }
end end
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 end