Skip to content
This repository has been archived by the owner on Mar 22, 2021. It is now read-only.

Commit

Permalink
Merge branch 'b-ssh-dies' into dev
Browse files Browse the repository at this point in the history
Closes GitHub Issue #49
  • Loading branch information
sledz committed Jun 11, 2013
2 parents 033c229 + 84069dc commit 6e76f21
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 1 deletion.
7 changes: 6 additions & 1 deletion monk_tf/ssh_conn.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,12 @@ def cmd(self, cmd):
if ret == "" or ret is None:
break
buf += ret
chan.close()
try:
rc = chan.close()
self._logger.info("close returned %d" % rc)
except:
self._logger.info("close ssh channel failed, call wait_closed")
chan.wait_closed()
rcd = chan.exit_status()
self._logger.info("Command [%s] ret: #%d" % (cmd, rcd))
self._logger.debug("Command [%s] output:\n[%s]" % (cmd, buf))
Expand Down
109 changes: 109 additions & 0 deletions test/test_ssh_conn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# -*- coding: utf-8 -*-
#
# MONK Automated Testing Framework
#
# Copyright (C) 2012-2013 DResearch Fahrzeugelektronik GmbH, project-monk@dresearch-fe.de
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version
# 3 of the License, or (at you option) any later version.
#

from nose import tools as nt

from monk_tf import ssh_conn as msc

def test_simple():
"""check if creating a SshConn object works
"""
# prepare
test_host = None
# execute
sut = msc.SshConn(test_host)
# assert
nt.ok_(sut, "should contain a monk_tf.ssh_conn.SshConn object, but instead contains this: '{}'".format(sut))


def test_cmd_closes_without_exception():
"""check if closing without exception is called without waiting
"""
# prepare
sut = MockSshConn()
expected = ['close()']
# execute
sut.cmd()
# assert
nt.eq_(expected, sut.calls, "expected '{}', but got '{}'".format(expected, sut.calls))


def test_cmd_closes_with_exception():
"""check if closing with exception is called without waiting
"""
# prepare
sut = MockSshConn(close_callback=raise_exception)
expected = ['close()', 'wait_closed()']
# execute
sut.cmd()
# assert
nt.eq_(expected, sut.calls, "expected '{}', but got '{}'".format(expected, sut.calls))


# --------------- HELPERS -------------------------------

def raise_exception():
""" a callback that raises a simple Exception
"""
raise Exception()


class MockSshConn(msc.SshConn):


def __init__(self, close_callback=None):
mock_host = "ignore me"
super(MockSshConn, self).__init__(mock_host)
self.calls = []
if close_callback is not None:
self.close_callback = close_callback
else:
self.close_callback = self.empty_callback


def cmd(self, txt=None):
exec_txt = txt if txt is not None else "ignore the command text"
return super(MockSshConn, self).cmd(exec_txt)


def empty_callback(self):
pass


def _session(self):
return self


def open_session(self):
return self


def execute(self, throw_away):
return 0


def read(self, throw_away):
return ""


def close(self):
self.calls.append('close()')
self.close_callback()
return 0


def wait_closed(self):
self.calls.append('wait_closed()')


def exit_status(self):
return 0

0 comments on commit 6e76f21

Please sign in to comment.