Skip to content

Commit ee59934

Browse files
committed
Add info/1 function for debugging connections
1 parent f9cd6a4 commit ee59934

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

lib/sshkit/connection.ex

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ defmodule SSHKit.Connection do
101101
102102
Returns `{:ok, conn}` or `{:error, reason}`.
103103
"""
104-
@spec reopen(term(), keyword()) :: {:ok, t()} | {:error, term()}
104+
@spec reopen(t(), keyword()) :: {:ok, t()} | {:error, term()}
105105
def reopen(conn, options \\ []) do
106106
options =
107107
conn.options
@@ -110,4 +110,21 @@ defmodule SSHKit.Connection do
110110

111111
open(conn.host, options)
112112
end
113+
114+
@doc """
115+
Returns information about a connection.
116+
117+
For OTP versions prior to 21.1, only `:client_version`, `:server_version`,
118+
`:user`, `:peer` and `:sockname` are available.
119+
120+
For details, see [`:ssh.connection_info/1`](http://erlang.org/doc/man/ssh.html#connection_info-1).
121+
"""
122+
@spec info(t()) :: keyword()
123+
def info(conn) do
124+
if function_exported?(@core, :connection_info, 1) do
125+
@core.connection_info(conn.ref)
126+
else
127+
@core.connection_info(conn.ref, [:client_version, :server_version, :user, :peer, :sockname])
128+
end
129+
end
113130
end

test/sshkit/connection_test.exs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ defmodule SSHKit.ConnectionTest do
129129
end)
130130

131131
conn = %Connection{
132-
host: 'foo.io',
132+
host: 'test.io',
133133
port: 22,
134134
options: [user_interaction: false],
135135
ref: :connection_ref
@@ -140,7 +140,7 @@ defmodule SSHKit.ConnectionTest do
140140
end
141141

142142
describe "reopen/2" do
143-
test "opens a new connection with the same options as the existing connection" do
143+
test "opens a new connection with the same options as an existing connection" do
144144
conn = %Connection{
145145
host: 'test.io',
146146
port: 22,
@@ -160,7 +160,7 @@ defmodule SSHKit.ConnectionTest do
160160
assert reopen(conn) == {:ok, new_conn}
161161
end
162162

163-
test "reopens a connection on new port" do
163+
test "reopens a connection on a new port" do
164164
conn = %Connection{
165165
host: 'test.io',
166166
port: 22,
@@ -178,12 +178,12 @@ defmodule SSHKit.ConnectionTest do
178178
assert reopen(conn, port: 666) == {:ok, new_conn}
179179
end
180180

181-
test "errors when unable to open connection" do
181+
test "errors when unable to open a connection" do
182182
conn = %Connection{
183183
host: 'test.io',
184184
port: 22,
185-
options: [user_interaction: false],
186-
ref: :sandbox
185+
options: [],
186+
ref: :connection_ref
187187
}
188188

189189
expect(@core, :connect, fn _, _, _, _ ->
@@ -193,4 +193,30 @@ defmodule SSHKit.ConnectionTest do
193193
assert reopen(conn) == {:error, :failed}
194194
end
195195
end
196+
197+
describe "info/1" do
198+
test "returns information about a connection" do
199+
if function_exported?(@core, :connection_info, 1) do
200+
expect(@core, :connection_info, fn ref ->
201+
assert ref == :connection_ref
202+
[info: :test]
203+
end)
204+
else
205+
expect(@core, :connection_info, fn ref, keys ->
206+
assert ref == :connection_ref
207+
assert keys == [:client_version, :server_version, :user, :peer, :sockname]
208+
[info: :test]
209+
end)
210+
end
211+
212+
conn = %Connection{
213+
host: 'test.io',
214+
port: 22,
215+
options: [],
216+
ref: :connection_ref
217+
}
218+
219+
assert info(conn) == [info: :test]
220+
end
221+
end
196222
end

0 commit comments

Comments
 (0)