net-ssh-gateway 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,123 +0,0 @@
1
- # ruby -I../net-ssh/lib -Ilib -Itest -rrubygems test/gateway_test.rb
2
- require 'test/unit'
3
- require 'mocha'
4
- require 'net/ssh/gateway'
5
-
6
- class GatewayTest < Test::Unit::TestCase
7
- def teardown
8
- Thread.list { |t| t.kill unless Thread.current == t }
9
- end
10
-
11
- def test_shutdown_without_any_open_connections_should_terminate_session
12
- session, gateway = new_gateway
13
- session.expects(:close)
14
- gateway.shutdown!
15
- assert !gateway.active?
16
- assert session.forward.active_locals.empty?
17
- end
18
-
19
- def test_open_should_start_local_ports_at_65535
20
- gateway_session, gateway = new_gateway
21
- assert_equal 65535, gateway.open("app1", 22)
22
- assert_equal [65535, "app1", 22], gateway_session.forward.active_locals[65535]
23
- end
24
-
25
- def test_open_should_decrement_port_and_retry_if_ports_are_in_use
26
- gateway_session, gateway = new_gateway(:reserved => lambda { |n| n > 65000 })
27
- assert_equal 65000, gateway.open("app1", 22)
28
- assert_equal [65000, "app1", 22], gateway_session.forward.active_locals[65000]
29
- end
30
-
31
- def test_open_with_explicit_local_port_should_use_that_port
32
- gateway_session, gateway = new_gateway
33
- assert_equal 8181, gateway.open("app1", 22, 8181)
34
- assert_equal [8181, "app1", 22], gateway_session.forward.active_locals[8181]
35
- end
36
-
37
- def test_ssh_should_return_connection_when_no_block_is_given
38
- gateway_session, gateway = new_gateway
39
- expect_connect_to("127.0.0.1", "user", :port => 65535).returns(result = mock("session"))
40
- newsess = gateway.ssh("app1", "user")
41
- assert_equal result, newsess
42
- assert_equal [65535, "app1", 22], gateway_session.forward.active_locals[65535]
43
- end
44
-
45
- def test_ssh_with_block_should_yield_session_and_then_close_port
46
- gateway_session, gateway = new_gateway
47
- expect_connect_to("127.0.0.1", "user", :port => 65535).yields(result = mock("session"))
48
- yielded = false
49
- gateway.ssh("app1", "user") do |newsess|
50
- yielded = true
51
- assert_equal result, newsess
52
- end
53
- assert yielded
54
- assert gateway_session.forward.active_locals.empty?
55
- end
56
-
57
- def test_shutdown_should_cancel_active_forwarded_ports
58
- gateway_session, gateway = new_gateway
59
- gateway.open("app1", 80)
60
- assert !gateway_session.forward.active_locals.empty?
61
- gateway.shutdown!
62
- assert gateway_session.forward.active_locals.empty?
63
- end
64
-
65
- private
66
-
67
- def expect_connect_to(host, user, options={})
68
- Net::SSH.expects(:start).with do |real_host, real_user, real_options|
69
- host == real_host &&
70
- user == real_user &&
71
- options[:port] == real_options[:port]
72
- end
73
- end
74
-
75
- def new_gateway(options={})
76
- session = MockSession.new(options)
77
- expect_connect_to("test.host", "tester").returns(session)
78
- [session, Net::SSH::Gateway.new("test.host", "tester")]
79
- end
80
-
81
- class MockForward
82
- attr_reader :active_locals
83
-
84
- def initialize(options)
85
- @options = options
86
- @active_locals = {}
87
- end
88
-
89
- def cancel_local(port)
90
- @active_locals.delete(port)
91
- end
92
-
93
- def local(lport, host, rport)
94
- raise Errno::EADDRINUSE if @options[:reserved] && @options[:reserved][lport]
95
- @active_locals[lport] = [lport, host, rport]
96
- end
97
- end
98
-
99
- class MockSession
100
- attr_reader :forward
101
-
102
- def initialize(options={})
103
- @forward = MockForward.new(options)
104
- end
105
-
106
- def close
107
- end
108
-
109
- def process(wait=nil)
110
- true
111
- end
112
-
113
- def looping?
114
- @looping
115
- end
116
-
117
- def loop
118
- @looping = true
119
- sleep 0.1 while yield
120
- @looping = false
121
- end
122
- end
123
- end