net-ssh-gateway 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,116 @@
1
+ require 'test/unit'
2
+ require 'mocha'
3
+ require 'net/ssh/gateway'
4
+
5
+ class GatewayTest < Test::Unit::TestCase
6
+ def teardown
7
+ Thread.list { |t| t.kill unless Thread.current == t }
8
+ end
9
+
10
+ def test_shutdown_without_any_open_connections_should_terminate_session
11
+ session, gateway = new_gateway
12
+ session.expects(:close)
13
+ gateway.shutdown!
14
+ assert !gateway.active?
15
+ assert session.forward.active_locals.empty?
16
+ end
17
+
18
+ def test_open_should_start_local_ports_at_65535
19
+ gateway_session, gateway = new_gateway
20
+ assert_equal 65535, gateway.open("app1", 22)
21
+ assert_equal [65535, "app1", 22], gateway_session.forward.active_locals[65535]
22
+ end
23
+
24
+ def test_open_should_decrement_port_and_retry_if_ports_are_in_use
25
+ gateway_session, gateway = new_gateway(:reserved => lambda { |n| n > 65000 })
26
+ assert_equal 65000, gateway.open("app1", 22)
27
+ assert_equal [65000, "app1", 22], gateway_session.forward.active_locals[65000]
28
+ end
29
+
30
+ def test_ssh_should_return_connection_when_no_block_is_given
31
+ gateway_session, gateway = new_gateway
32
+ expect_connect_to("127.0.0.1", "user", :port => 65535).returns(result = mock("session"))
33
+ newsess = gateway.ssh("app1", "user")
34
+ assert_equal result, newsess
35
+ assert_equal [65535, "app1", 22], gateway_session.forward.active_locals[65535]
36
+ end
37
+
38
+ def test_ssh_with_block_should_yield_session_and_then_close_port
39
+ gateway_session, gateway = new_gateway
40
+ expect_connect_to("127.0.0.1", "user", :port => 65535).yields(result = mock("session"))
41
+ yielded = false
42
+ gateway.ssh("app1", "user") do |newsess|
43
+ yielded = true
44
+ assert_equal result, newsess
45
+ end
46
+ assert yielded
47
+ assert gateway_session.forward.active_locals.empty?
48
+ end
49
+
50
+ def test_shutdown_should_cancel_active_forwarded_ports
51
+ gateway_session, gateway = new_gateway
52
+ gateway.open("app1", 80)
53
+ assert !gateway_session.forward.active_locals.empty?
54
+ gateway.shutdown!
55
+ assert gateway_session.forward.active_locals.empty?
56
+ end
57
+
58
+ private
59
+
60
+ def expect_connect_to(host, user, options={})
61
+ Net::SSH.expects(:start).with do |real_host, real_user, real_options|
62
+ host == real_host &&
63
+ user == real_user &&
64
+ options[:port] == real_options[:port]
65
+ end
66
+ end
67
+
68
+ def new_gateway(options={})
69
+ session = MockSession.new(options)
70
+ expect_connect_to("test.host", "tester").returns(session)
71
+ [session, Net::SSH::Gateway.new("test.host", "tester")]
72
+ end
73
+
74
+ class MockForward
75
+ attr_reader :active_locals
76
+
77
+ def initialize(options)
78
+ @options = options
79
+ @active_locals = {}
80
+ end
81
+
82
+ def cancel_local(port)
83
+ @active_locals.delete(port)
84
+ end
85
+
86
+ def local(lport, host, rport)
87
+ raise Errno::EADDRINUSE if @options[:reserved] && @options[:reserved][lport]
88
+ @active_locals[lport] = [lport, host, rport]
89
+ end
90
+ end
91
+
92
+ class MockSession
93
+ attr_reader :forward
94
+
95
+ def initialize(options={})
96
+ @forward = MockForward.new(options)
97
+ end
98
+
99
+ def close
100
+ end
101
+
102
+ def process(wait=nil)
103
+ true
104
+ end
105
+
106
+ def looping?
107
+ @looping
108
+ end
109
+
110
+ def loop
111
+ @looping = true
112
+ sleep 0.1 while yield
113
+ @looping = false
114
+ end
115
+ end
116
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: net-ssh-gateway
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jamis Buck
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-05-01 00:00:00 -06:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: net-ssh
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.99.1
23
+ version:
24
+ description: A simple library to assist in establishing tunneled Net::SSH connections
25
+ email: jamis@jamisbuck.org
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files: []
31
+
32
+ files:
33
+ - CHANGELOG.rdoc
34
+ - lib/net/ssh/gateway.rb
35
+ - Manifest
36
+ - Rakefile
37
+ - README.rdoc
38
+ - setup.rb
39
+ - test/gateway_test.rb
40
+ - net-ssh-gateway.gemspec
41
+ has_rdoc: true
42
+ homepage: http://net-ssh.rubyforge.org/gateway
43
+ post_install_message:
44
+ rdoc_options: []
45
+
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ requirements: []
61
+
62
+ rubyforge_project: net-ssh-gateway
63
+ rubygems_version: 1.1.1
64
+ signing_key:
65
+ specification_version: 2
66
+ summary: A simple library to assist in establishing tunneled Net::SSH connections
67
+ test_files:
68
+ - test/gateway_test.rb