Skip to content

Commit

Permalink
Reuse Shell objects after initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
mvdbeek committed Jul 30, 2017
1 parent d6807b7 commit 1f2fa31
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/galaxy/jobs/runners/util/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def __load(module_path, d):

self.cli_shells = {}
self.cli_job_interfaces = {}
self.active_cli_shells = {}

module_prefix = self.__module__
__load('%s.shell' % module_prefix, self.cli_shells)
Expand All @@ -55,8 +56,9 @@ def get_plugins(self, shell_params, job_params):

def get_shell_plugin(self, shell_params):
shell_plugin = shell_params.get('plugin', DEFAULT_SHELL_PLUGIN)
shell = self.cli_shells[shell_plugin](**shell_params)
return shell
if shell_plugin not in self.active_cli_shells:
self.active_cli_shells[shell_plugin] = self.cli_shells[shell_plugin](**shell_params)
return self.active_cli_shells[shell_plugin]

def get_job_interface(self, job_params):
job_plugin = job_params.get('plugin', None)
Expand Down
57 changes: 57 additions & 0 deletions test/unit/test_remote_shell.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import os
import tempfile
import unittest

import mockssh

from Crypto.PublicKey import RSA

from galaxy.jobs.runners.cli import CliInterface


class TestCliInterface(unittest.TestCase):


@classmethod
def tearDownClass(cls):
os.remove(cls.private_key)

@classmethod
def setUpClass(cls):
cls.private_key = make_private_key()
cls.username = 'testuser'
cls.shell_params = {'username': cls.username,
'private_key': cls.private_key,
'hostname': 'localhost'}
cls.cli_interface = CliInterface()

def test_secure_shell_plugin_without_strict(self):
with mockssh.Server(users={self.username: self.private_key}) as server:
self.shell_params['port'] = server.port
self.shell_params['plugin'] = 'SecureShell'
self.shell_params['strict_host_key_checking'] = False
self.shell = self.cli_interface.get_shell_plugin(self.shell_params)
result = self.shell.execute(cmd='echo hello')
assert result.stdout.strip() == 'hello1'

def test_get_shell_plugin(self):
with mockssh.Server(users={self.username: self.private_key}) as server:
self.shell_params['port'] = server.port
self.shell_params['plugin'] = 'ParamikoShell'
self.shell = self.cli_interface.get_shell_plugin(self.shell_params)
assert self.shell.username == self.username

def test_paramiko_shell_plugin(self):
with mockssh.Server(users={self.username: self.private_key}) as server:
self.shell_params['port'] = server.port
self.shell_params['plugin'] = 'ParamikoShell'
self.shell = self.cli_interface.get_shell_plugin(self.shell_params)
result = self.shell.execute(cmd='echo hello')
assert result.stdout.strip() == 'hello1'


def make_private_key():
key = RSA.generate(1024)
private_fd, private_path = tempfile.mkstemp()
open(private_path, 'w').write(key.exportKey('PEM'))
return private_path

0 comments on commit 1f2fa31

Please sign in to comment.