Skip to content

Commit

Permalink
simplify and clean up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mpenkov committed Feb 20, 2024
1 parent 9dbf332 commit efc32ca
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 28 deletions.
11 changes: 11 additions & 0 deletions smart_open/tests/test_data/ssh.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Host another-host
HostName another-host-domain.com
User another-user
Port 2345
IdentityFile /path/to/key/file
ConnectTimeout 20
Compression yes
GSSAPIAuthentication no
GSSAPIKeyExchange no
GSSAPIDelegateCredentials no
GSSAPITrustDns no
64 changes: 36 additions & 28 deletions smart_open/tests/test_ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

import smart_open.ssh

_TEST_DATA_PATH = os.path.join(os.path.dirname(__file__), "test_data")
_CONFIG_PATH = os.path.join(_TEST_DATA_PATH, "ssh.cfg")


def mock_ssh(func):
def wrapper(*args, **kwargs):
Expand All @@ -23,28 +26,12 @@ def wrapper(*args, **kwargs):


class SSHOpen(unittest.TestCase):
MOCK_SSH_CONFIG = r"""
Host another-host
HostName another-host-domain.com
User another-user
Port 2345
IdentityFile /path/to/key/file
ConnectTimeout 20
Compression yes
GSSAPIAuthentication no
GSSAPIKeyExchange no
GSSAPIDelegateCredentials no
GSSAPITrustDns no
"""

def setUp(self):
self.mock_ssh_config_file = os.path.join(tempfile.gettempdir(), "config-" + str(uuid.uuid1()))
smart_open.ssh._SSH_CONFIG_FILES[0] = self.mock_ssh_config_file
with open(self.mock_ssh_config_file, 'w') as file:
file.write(self.MOCK_SSH_CONFIG)
self._cfg_files = smart_open.ssh._SSH_CONFIG_FILES
smart_open.ssh._SSH_CONFIG_FILES = [_CONFIG_PATH]

def tearDown(self):
os.remove(self.mock_ssh_config_file)
smart_open.ssh._SSH_CONFIG_FILES = self._cfg_files

@mock_ssh
def test_open(self, mock_connect, get_transp_mock):
Expand Down Expand Up @@ -98,27 +85,48 @@ def mocked_open_sftp():
def test_open_with_openssh_config(self, mock_connect, get_transp_mock):
smart_open.open("ssh://another-host/")
mock_connect.assert_called_with(
"another-host-domain.com", 2345, username="another-user",
key_filename=["/path/to/key/file"], timeout=20., compress=True,
gss_auth=False, gss_kex=False, gss_deleg_creds=False, gss_trust_dns=False
"another-host-domain.com",
2345,
username="another-user",
key_filename=["/path/to/key/file"],
timeout=20.,
compress=True,
gss_auth=False,
gss_kex=False,
gss_deleg_creds=False,
gss_trust_dns=False,
)

@mock_ssh
def test_open_with_openssh_config_override_port(self, mock_connect, get_transp_mock):
smart_open.open("ssh://another-host:22/")
mock_connect.assert_called_with(
"another-host-domain.com", 22, username="another-user",
key_filename=["/path/to/key/file"], timeout=20., compress=True,
gss_auth=False, gss_kex=False, gss_deleg_creds=False, gss_trust_dns=False
"another-host-domain.com",
22,
username="another-user",
key_filename=["/path/to/key/file"],
timeout=20.,
compress=True,
gss_auth=False,
gss_kex=False,
gss_deleg_creds=False,
gss_trust_dns=False,
)

@mock_ssh
def test_open_with_openssh_config_override_user(self, mock_connect, get_transp_mock):
smart_open.open("ssh://new-user@another-host/")
mock_connect.assert_called_with(
"another-host-domain.com", 2345, username="new-user",
key_filename=["/path/to/key/file"], timeout=20., compress=True,
gss_auth=False, gss_kex=False, gss_deleg_creds=False, gss_trust_dns=False
"another-host-domain.com",
2345,
username="new-user",
key_filename=["/path/to/key/file"],
timeout=20.,
compress=True,
gss_auth=False,
gss_kex=False,
gss_deleg_creds=False,
gss_trust_dns=False,
)


Expand Down

0 comments on commit efc32ca

Please sign in to comment.