2007-02-28 14:13:13 -05:00
|
|
|
require "#{File.dirname(__FILE__)}/utils"
|
2006-03-05 22:48:35 -05:00
|
|
|
require 'capistrano/ssh'
|
2005-08-13 14:36:02 -04:00
|
|
|
|
|
|
|
class SSHTest < Test::Unit::TestCase
|
2007-02-28 14:13:13 -05:00
|
|
|
def setup
|
|
|
|
@options = { :username => nil,
|
|
|
|
:password => nil,
|
|
|
|
:port => 22,
|
|
|
|
:auth_methods => %w(publickey hostbased) }
|
|
|
|
@server = server("capistrano")
|
|
|
|
end
|
2005-08-13 14:36:02 -04:00
|
|
|
|
2007-02-28 14:13:13 -05:00
|
|
|
def test_connect_with_bare_server_without_options_or_config_with_public_key_succeeding_should_only_loop_once
|
2007-03-16 17:22:10 -04:00
|
|
|
Net::SSH.expects(:start).with(@server.host, @options).returns(success = Object.new)
|
|
|
|
assert_equal success, Capistrano::SSH.connect(@server)
|
2005-08-13 14:36:02 -04:00
|
|
|
end
|
|
|
|
|
2007-02-28 14:13:13 -05:00
|
|
|
def test_connect_with_bare_server_without_options_with_public_key_failing_should_try_password
|
|
|
|
Net::SSH.expects(:start).with(@server.host, @options).raises(Net::SSH::AuthenticationFailed)
|
2007-03-16 17:22:10 -04:00
|
|
|
Net::SSH.expects(:start).with(@server.host, @options.merge(:password => "f4b13n", :auth_methods => %w(password keyboard-interactive))).returns(success = Object.new)
|
|
|
|
assert_equal success, Capistrano::SSH.connect(@server, :password => "f4b13n")
|
2005-08-13 14:36:02 -04:00
|
|
|
end
|
|
|
|
|
2007-02-28 14:13:13 -05:00
|
|
|
def test_connect_with_bare_server_without_options_public_key_and_password_failing_should_raise_error
|
|
|
|
Net::SSH.expects(:start).with(@server.host, @options).raises(Net::SSH::AuthenticationFailed)
|
|
|
|
Net::SSH.expects(:start).with(@server.host, @options.merge(:password => "f4b13n", :auth_methods => %w(password keyboard-interactive))).raises(Net::SSH::AuthenticationFailed)
|
|
|
|
assert_raises(Net::SSH::AuthenticationFailed) do
|
|
|
|
Capistrano::SSH.connect(@server, :password => "f4b13n")
|
2005-08-13 14:36:02 -04:00
|
|
|
end
|
|
|
|
end
|
2006-11-10 23:21:55 -05:00
|
|
|
|
2007-02-28 14:13:13 -05:00
|
|
|
def test_connect_with_bare_server_and_user_via_public_key_should_pass_user_to_net_ssh
|
2007-03-16 17:22:10 -04:00
|
|
|
Net::SSH.expects(:start).with(@server.host, @options.merge(:username => "jamis")).returns(success = Object.new)
|
|
|
|
assert_equal success, Capistrano::SSH.connect(@server, :user => "jamis")
|
2006-11-10 23:21:55 -05:00
|
|
|
end
|
|
|
|
|
2007-02-28 14:13:13 -05:00
|
|
|
def test_connect_with_bare_server_and_user_via_password_should_pass_user_to_net_ssh
|
|
|
|
Net::SSH.expects(:start).with(@server.host, @options.merge(:username => "jamis")).raises(Net::SSH::AuthenticationFailed)
|
2007-03-16 17:22:10 -04:00
|
|
|
Net::SSH.expects(:start).with(@server.host, @options.merge(:username => "jamis", :password => "f4b13n", :auth_methods => %w(password keyboard-interactive))).returns(success = Object.new)
|
|
|
|
assert_equal success, Capistrano::SSH.connect(@server, :user => "jamis", :password => "f4b13n")
|
2006-11-10 23:21:55 -05:00
|
|
|
end
|
|
|
|
|
2007-02-28 14:13:13 -05:00
|
|
|
def test_connect_with_bare_server_with_explicit_port_should_pass_port_to_net_ssh
|
2007-03-16 17:22:10 -04:00
|
|
|
Net::SSH.expects(:start).with(@server.host, @options.merge(:port => 1234)).returns(success = Object.new)
|
|
|
|
assert_equal success, Capistrano::SSH.connect(@server, :port => 1234)
|
2006-11-10 23:21:55 -05:00
|
|
|
end
|
2005-08-13 14:36:02 -04:00
|
|
|
|
2007-02-28 14:13:13 -05:00
|
|
|
def test_connect_with_server_with_user_should_pass_user_to_net_ssh
|
|
|
|
server = server("jamis@capistrano")
|
2007-03-16 17:22:10 -04:00
|
|
|
Net::SSH.expects(:start).with(server.host, @options.merge(:username => "jamis")).returns(success = Object.new)
|
|
|
|
assert_equal success, Capistrano::SSH.connect(server)
|
2005-08-13 14:36:02 -04:00
|
|
|
end
|
|
|
|
|
2007-02-28 14:13:13 -05:00
|
|
|
def test_connect_with_server_with_port_should_pass_port_to_net_ssh
|
|
|
|
server = server("capistrano:1235")
|
2007-03-16 17:22:10 -04:00
|
|
|
Net::SSH.expects(:start).with(server.host, @options.merge(:port => 1235)).returns(success = Object.new)
|
|
|
|
assert_equal success, Capistrano::SSH.connect(server)
|
2006-11-10 23:21:55 -05:00
|
|
|
end
|
|
|
|
|
2007-02-28 14:13:13 -05:00
|
|
|
def test_connect_with_server_with_user_and_port_should_pass_user_and_port_to_net_ssh
|
|
|
|
server = server("jamis@capistrano:1235")
|
2007-03-16 17:22:10 -04:00
|
|
|
Net::SSH.expects(:start).with(server.host, @options.merge(:username => "jamis", :port => 1235)).returns(success = Object.new)
|
|
|
|
assert_equal success, Capistrano::SSH.connect(server)
|
2005-08-13 14:36:02 -04:00
|
|
|
end
|
|
|
|
|
2007-02-28 14:13:13 -05:00
|
|
|
def test_connect_with_ssh_options_should_override_options
|
|
|
|
ssh_options = { :username => "JamisMan", :port => 8125 }
|
2007-03-16 17:22:10 -04:00
|
|
|
Net::SSH.expects(:start).with(@server.host, @options.merge(:username => "JamisMan", :port => 8125)).returns(success = Object.new)
|
|
|
|
assert_equal success, Capistrano::SSH.connect(@server, {:ssh_options => ssh_options, :user => "jamis", :port => 1235})
|
2005-08-13 14:36:02 -04:00
|
|
|
end
|
|
|
|
|
2007-02-28 14:13:13 -05:00
|
|
|
def test_connect_with_ssh_options_should_override_server_options
|
|
|
|
ssh_options = { :username => "JamisMan", :port => 8125 }
|
|
|
|
server = server("jamis@capistrano:1235")
|
2007-03-16 17:22:10 -04:00
|
|
|
Net::SSH.expects(:start).with(server.host, @options.merge(:username => "JamisMan", :port => 8125)).returns(success = Object.new)
|
|
|
|
assert_equal success, Capistrano::SSH.connect(server, {:ssh_options => ssh_options})
|
|
|
|
end
|
|
|
|
|
2007-05-09 00:45:54 -04:00
|
|
|
def test_connect_should_add_xserver_accessor_to_connection
|
2007-03-16 17:22:10 -04:00
|
|
|
Net::SSH.expects(:start).with(@server.host, @options).returns(success = Object.new)
|
|
|
|
assert_equal success, Capistrano::SSH.connect(@server)
|
2007-05-09 00:45:54 -04:00
|
|
|
assert success.respond_to?(:xserver)
|
|
|
|
assert success.respond_to?(:xserver)
|
|
|
|
assert_equal success.xserver, @server
|
2005-08-13 14:36:02 -04:00
|
|
|
end
|
2007-05-10 23:45:16 -04:00
|
|
|
|
|
|
|
def test_connect_should_not_retry_if_custom_auth_methods_are_given
|
|
|
|
Net::SSH.expects(:start).with(@server.host, @options.merge(:auth_methods => %w(publickey))).raises(Net::SSH::AuthenticationFailed)
|
|
|
|
assert_raises(Net::SSH::AuthenticationFailed) { Capistrano::SSH.connect(@server, :ssh_options => { :auth_methods => %w(publickey) }) }
|
|
|
|
end
|
2005-08-13 14:36:02 -04:00
|
|
|
end
|