Skip to content

Commit 2807fde

Browse files
aiordacheUlysses Souza
authored andcommitted
Fix SSH port parsing and add regression tests
Signed-off-by: aiordache <[email protected]>
1 parent d065daf commit 2807fde

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

docker/transport/sshconn.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ def __init__(self, host):
3030
self.host = host
3131
self.port = None
3232
self.user = None
33-
if ':' in host:
34-
self.host, self.port = host.split(':')
33+
if ':' in self.host:
34+
self.host, self.port = self.host.split(':')
3535
if '@' in self.host:
36-
self.user, self.host = host.split('@')
36+
self.user, self.host = self.host.split('@')
3737

3838
self.proc = None
3939

@@ -167,7 +167,7 @@ class SSHHTTPAdapter(BaseHTTPAdapter):
167167
def __init__(self, base_url, timeout=60,
168168
pool_connections=constants.DEFAULT_NUM_POOLS,
169169
max_pool_size=constants.DEFAULT_MAX_POOL_SIZE,
170-
shell_out=True):
170+
shell_out=False):
171171
self.ssh_client = None
172172
if not shell_out:
173173
self._create_paramiko_client(base_url)

tests/unit/sshadapter_test.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import unittest
2+
import docker
3+
from docker.transport.sshconn import SSHSocket
4+
5+
class SSHAdapterTest(unittest.TestCase):
6+
def test_ssh_hostname_prefix_trim(self):
7+
conn = docker.transport.SSHHTTPAdapter(base_url="ssh://user@hostname:1234", shell_out=True)
8+
assert conn.ssh_host == "user@hostname:1234"
9+
10+
def test_ssh_parse_url(self):
11+
c = SSHSocket(host="user@hostname:1234")
12+
assert c.host == "hostname"
13+
assert c.port == "1234"
14+
assert c.user == "user"
15+
16+
def test_ssh_parse_hostname_only(self):
17+
c = SSHSocket(host="hostname")
18+
assert c.host == "hostname"
19+
assert c.port == None
20+
assert c.user == None
21+
22+
def test_ssh_parse_user_and_hostname(self):
23+
c = SSHSocket(host="user@hostname")
24+
assert c.host == "hostname"
25+
assert c.port == None
26+
assert c.user == "user"
27+
28+
def test_ssh_parse_hostname_and_port(self):
29+
c = SSHSocket(host="hostname:22")
30+
assert c.host == "hostname"
31+
assert c.port == "22"
32+
assert c.user == None

0 commit comments

Comments
 (0)