diff options
author | Jamis Buck <jamis@37signals.com> | 2005-08-13 18:36:02 +0000 |
---|---|---|
committer | Jamis Buck <jamis@37signals.com> | 2005-08-13 18:36:02 +0000 |
commit | f44dac89356d648a2b3a4249e232a76b82e6275b (patch) | |
tree | 342c9af79ae6c84616e2692967044876f54a0d87 /switchtower/test | |
parent | cbf709fc5c7725f64471980ca52f3f99d6bb568b (diff) | |
download | rails-f44dac89356d648a2b3a4249e232a76b82e6275b.tar.gz rails-f44dac89356d648a2b3a4249e232a76b82e6275b.tar.bz2 rails-f44dac89356d648a2b3a4249e232a76b82e6275b.zip |
Works with public keys now, for passwordless operation
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2000 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'switchtower/test')
-rw-r--r-- | switchtower/test/scm/subversion_test.rb | 20 | ||||
-rw-r--r-- | switchtower/test/ssh_test.rb | 104 | ||||
-rw-r--r-- | switchtower/test/utils.rb | 36 |
3 files changed, 141 insertions, 19 deletions
diff --git a/switchtower/test/scm/subversion_test.rb b/switchtower/test/scm/subversion_test.rb index 47942d9b9c..fa65714b6b 100644 --- a/switchtower/test/scm/subversion_test.rb +++ b/switchtower/test/scm/subversion_test.rb @@ -1,28 +1,10 @@ $:.unshift File.dirname(__FILE__) + "/../../lib" +require File.dirname(__FILE__) + "/../utils" require 'test/unit' require 'switchtower/scm/subversion' class ScmSubversionTest < Test::Unit::TestCase - class MockLogger - def info(msg,pfx=nil) end - def debug(msg,pfx=nil) end - end - - class MockConfiguration < Hash - def logger - @logger ||= MockLogger.new - end - - def method_missing(sym, *args) - if args.length == 0 - self[sym] - else - super - end - end - end - class SubversionTest < SwitchTower::SCM::Subversion attr_accessor :story attr_reader :last_path diff --git a/switchtower/test/ssh_test.rb b/switchtower/test/ssh_test.rb new file mode 100644 index 0000000000..60e791beb3 --- /dev/null +++ b/switchtower/test/ssh_test.rb @@ -0,0 +1,104 @@ +$:.unshift File.dirname(__FILE__) + "/../lib" + +require File.dirname(__FILE__) + "/utils" +require 'test/unit' +require 'switchtower/ssh' + +class SSHTest < Test::Unit::TestCase + class MockSSH + AuthenticationFailed = Net::SSH::AuthenticationFailed + + class <<self + attr_accessor :story + attr_accessor :invocations + end + + def self.start(server, opts, &block) + @invocations << [server, opts, block] + err = story.shift + raise err if err + end + end + + def setup + @config = MockConfiguration.new + @config[:user] = 'demo' + @config[:password] = 'c0c0nutfr0st1ng' + MockSSH.story = [] + MockSSH.invocations = [] + end + + def test_publickey_auth_succeeds_default_port_no_block + Net.const_during(:SSH, MockSSH) do + SwitchTower::SSH.connect('demo.server.i', @config) + end + + assert_equal 1, MockSSH.invocations.length + assert_equal 'demo.server.i', MockSSH.invocations.first[0] + assert_equal 22, MockSSH.invocations.first[1][:port] + assert_equal 'demo', MockSSH.invocations.first[1][:username] + assert_nil MockSSH.invocations.first[1][:password] + assert_equal %w(publickey hostbased), + MockSSH.invocations.first[1][:auth_methods] + assert_nil MockSSH.invocations.first[2] + end + + def test_publickey_auth_succeeds_explicit_port_no_block + Net.const_during(:SSH, MockSSH) do + SwitchTower::SSH.connect('demo.server.i', @config, 23) + end + + assert_equal 1, MockSSH.invocations.length + assert_equal 23, MockSSH.invocations.first[1][:port] + assert_nil MockSSH.invocations.first[2] + end + + def test_publickey_auth_succeeds_with_block + Net.const_during(:SSH, MockSSH) do + SwitchTower::SSH.connect('demo.server.i', @config) do |session| + end + end + + assert_equal 1, MockSSH.invocations.length + assert_instance_of Proc, MockSSH.invocations.first[2] + end + + def test_publickey_auth_fails + MockSSH.story << Net::SSH::AuthenticationFailed + + Net.const_during(:SSH, MockSSH) do + SwitchTower::SSH.connect('demo.server.i', @config) + end + + assert_equal 2, MockSSH.invocations.length + + assert_nil MockSSH.invocations.first[1][:password] + assert_equal %w(publickey hostbased), + MockSSH.invocations.first[1][:auth_methods] + + assert_equal 'c0c0nutfr0st1ng', MockSSH.invocations.last[1][:password] + assert_equal %w(password keyboard-interactive), + MockSSH.invocations.last[1][:auth_methods] + end + + def test_password_auth_fails + MockSSH.story << Net::SSH::AuthenticationFailed + MockSSH.story << Net::SSH::AuthenticationFailed + + Net.const_during(:SSH, MockSSH) do + assert_raises(Net::SSH::AuthenticationFailed) do + SwitchTower::SSH.connect('demo.server.i', @config) + end + end + + assert_equal 2, MockSSH.invocations.length + + assert_nil MockSSH.invocations.first[1][:password] + assert_equal %w(publickey hostbased), + MockSSH.invocations.first[1][:auth_methods] + + assert_equal 'c0c0nutfr0st1ng', MockSSH.invocations.last[1][:password] + assert_equal %w(password keyboard-interactive), + MockSSH.invocations.last[1][:auth_methods] + end +end diff --git a/switchtower/test/utils.rb b/switchtower/test/utils.rb new file mode 100644 index 0000000000..b483f0c005 --- /dev/null +++ b/switchtower/test/utils.rb @@ -0,0 +1,36 @@ +class Module + def const_during(constant, value) + if const_defined?(constant) + overridden = true + saved = const_get(constant) + remove_const(constant) + end + + const_set(constant, value) + yield + ensure + if overridden + remove_const(constant) + const_set(constant, saved) + end + end +end + +class MockLogger + def info(msg,pfx=nil) end + def debug(msg,pfx=nil) end +end + +class MockConfiguration < Hash + def logger + @logger ||= MockLogger.new + end + + def method_missing(sym, *args) + if args.length == 0 + self[sym] + else + super + end + end +end |