-
Notifications
You must be signed in to change notification settings - Fork 16
Create an ssh server to emulate ssh connection #202
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
Create an ssh server to emulate ssh connection #202
Conversation
This patch has the same purpose as #185 , but for ssh devices (currently rpi2 and artik530) instead of serial devices. |
jstest/__init__.py
Outdated
@@ -14,7 +14,7 @@ | |||
|
|||
from jstest.builder.builder import Builder | |||
from jstest.common import console, paths, symbol_resolver, utils | |||
from jstest.emulate import pseudo_terminal | |||
from jstest.emulate import pseudo_terminal, mockssh_server |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please fix the order of the modules.
jstest/__main__.py
Outdated
@@ -193,12 +203,15 @@ def main(): | |||
testresult.append(env.options.id, env.paths.builddir) | |||
# Upload all the results to the Firebase database. | |||
testresult.upload() | |||
exitcode = 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the exitcode
variable really needed? If the Python script ends normally, the return value is 0
. If error happens, it's enough to use the sys.exit(1)
to stop the program.
jstest/__main__.py
Outdated
|
||
except (Exception, KeyboardInterrupt) as e: | ||
jstest.resources.patch_modules(env, revert=True) | ||
jstest.console.error('[%s] %s' % (type(e).__name__, str(e))) | ||
|
||
sys.exit(1) | ||
exitcode = 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto.
jstest/emulate/mockssh_server.py
Outdated
|
||
class ThreadPrinter(object): | ||
''' | ||
#Class to redirect stdout from non-main threads into /dev/null. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a multi-line comment, no need # character.
jstest/emulate/mockssh_server.py
Outdated
#Class to redirect stdout from non-main threads into /dev/null. | ||
''' | ||
def __init__(self): | ||
pass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unnecessary constructor definition.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pylint was giving a warning when the constructor was missing. Should I disable that warning in pylintrc
or should we keep this constructor?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nevermind, I just tried it, it's no longer giving a warning. I will delete the constructor.
.gitignore
Outdated
@@ -5,3 +5,6 @@ | |||
build/* | |||
deps/* | |||
results/* | |||
|
|||
# Skip ssh key files. | |||
*.key |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any .key
file in the projects?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the mockssh server needs them, so they shouldn't even be in .gitignore
, I just realized it.
jstest/__main__.py
Outdated
@@ -82,12 +82,19 @@ def parse_options(): | |||
action='store_true', default=False, | |||
help='display less verbose output') | |||
|
|||
parser.add_argument('--emulate', | |||
default=False, action='store_true', | |||
help='Emulate the connection.') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Emulate the connection.
-> emulate the connection
jstest/__main__.py
Outdated
group = parser.add_argument_group("Secure Shell communication") | ||
|
||
group.add_argument('--username', | ||
metavar='USER', | ||
help='specify the username to login to the device.') | ||
|
||
group.add_argument('--password', | ||
help='specify the password to login to the device.') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no need a dot at the end of the text. Could you please modify the help of the --username
option as well?
jstest/emulate/mockssh_server.py
Outdated
break | ||
|
||
self.writeln(ret) | ||
self.exit() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a shorter solution:
import re
iotjs_build_info = re.compile('.*iotjs_build_info.js')
if filter(iotjs_build_info.match, self.args):
self.writeln('{ "builtins": {}, "features": {}, "stability": "stable" }')
self.exit()
self.ssh.connect(hostname=self.ip, port=self.port, username=self.username, | ||
password=self.password, look_for_keys=False) | ||
else: | ||
self.ssh.connect(hostname=self.ip, port=self.port, username=self.username) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a shorter solution:
look_for_keys=True
if self.password:
look_for_keys=False
self.ssh.connect(hostname=self.ip, port=self.port, username=self.username, password=self.password, look_for_keys=look_for_keys)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or more simply:
self.ssh.connect(hostname=self.ip, port=self.port, username=self.username, password=self.password, look_for_keys=bool(self.password))
self.ip = device_info['ip'] | ||
self.port = device_info['port'] | ||
self.timeout = device_info['timeout'] | ||
self._no_exec_command = device_info['_no_exec_command'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice if this variable could have a comment about its purpose. For example: FIXME: the exec_command of Paramico SSH library can't be used with the emulated SSH server.
(or something else).
self.ip = device_info['ip'] | ||
self.port = device_info['port'] | ||
self.timeout = device_info['timeout'] | ||
# FIXME the exec_command method of paramiko.client.SSHClient cannot be used with the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FIXME
-> FIXME:
jstest/emulate/twisted_server.py
Outdated
exitcode = 1 if 'run_fail' in cmd else 0 | ||
self.write('{"output": "",' | ||
' "memstat": {"heap-system": "n/a", "heap-jerry": "n/a", "stack": "n/a"},' | ||
' "exitcode": %d}\r\n' % exitcode) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exitcode
can be replaced with int('fail' in cmd)
. Please note that JerryScript uses fail
folder name for the expected failure tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A better solution to format JSON string is to create an object and use json.dumps
:
if 'iotjs_build_info' in cmd:
result = {
'builtins': {},
'features': {}
'stability': 'stable'
}
else:
result = {
'output': 'Hello from emulated device.',
'memstat': {
'heap-system': 'n/a',
'heap-jerry': 'n/a',
'stack': 'n/a'
},
'exitcode': int('fail' in cmd)
}
self.write(json.dumps(result) + '\r\n')
options.password = 'jerry' | ||
options.ip = '127.0.0.1' | ||
options.port = 2022 | ||
options.remote_workdir = 'emulated_workdir' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unnecessary overwrite.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we don't overwrite remote_workdir
, we will get an error message:
[SystemError] Please use --remote-workdir for the device.
This error message comes from the SSHDevice
class, which should not distinguish if we are using an emulated device or a real device, so overwriting remote_workdir
is necessary.
JSRemoteTest-DCO-1.0-Signed-off-by: Peter Marki marpeter@inf.u-szeged.hu
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
JSRemoteTest-DCO-1.0-Signed-off-by: Peter Marki marpeter@inf.u-szeged.hu