Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SSH port parsing and add regression tests #2770

Merged
merged 1 commit into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docker/transport/sshconn.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ def __init__(self, host):
self.host = host
self.port = None
self.user = None
if ':' in host:
self.host, self.port = host.split(':')
if ':' in self.host:
self.host, self.port = self.host.split(':')
if '@' in self.host:
self.user, self.host = host.split('@')
self.user, self.host = self.host.split('@')

self.proc = None

Expand Down Expand Up @@ -167,7 +167,7 @@ class SSHHTTPAdapter(BaseHTTPAdapter):
def __init__(self, base_url, timeout=60,
pool_connections=constants.DEFAULT_NUM_POOLS,
max_pool_size=constants.DEFAULT_MAX_POOL_SIZE,
shell_out=True):
shell_out=False):
self.ssh_client = None
if not shell_out:
self._create_paramiko_client(base_url)
Expand Down
32 changes: 32 additions & 0 deletions tests/unit/sshadapter_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import unittest
import docker
from docker.transport.sshconn import SSHSocket

class SSHAdapterTest(unittest.TestCase):
def test_ssh_hostname_prefix_trim(self):
conn = docker.transport.SSHHTTPAdapter(base_url="ssh://user@hostname:1234", shell_out=True)
assert conn.ssh_host == "user@hostname:1234"

def test_ssh_parse_url(self):
c = SSHSocket(host="user@hostname:1234")
assert c.host == "hostname"
assert c.port == "1234"
assert c.user == "user"

def test_ssh_parse_hostname_only(self):
c = SSHSocket(host="hostname")
assert c.host == "hostname"
assert c.port == None
assert c.user == None

def test_ssh_parse_user_and_hostname(self):
c = SSHSocket(host="user@hostname")
assert c.host == "hostname"
assert c.port == None
assert c.user == "user"

def test_ssh_parse_hostname_and_port(self):
c = SSHSocket(host="hostname:22")
assert c.host == "hostname"
assert c.port == "22"
assert c.user == None