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)
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

View File

@ -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