Skip to content

Commit 4bab0ce

Browse files
author
Alexandre Vincent
committed
[change] Add output on Command timeout exceeded
1 parent 24ec502 commit 4bab0ce

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

openwisp_controller/connection/connectors/ssh.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,17 @@ def exec_command(
198198
logger.exception(e)
199199
raise e
200200
# store command exit status
201+
exit_status = None
201202
# workaround https://github.com/paramiko/paramiko/issues/1815
202203
# workaround https://github.com/paramiko/paramiko/issues/1787
203204
# Ref. https://docs.paramiko.org/en/stable/api/channel.html#paramiko.channel.Channel.recv_exit_status # noqa
204-
stdout.channel.status_event.wait(
205+
if not stdout.channel.status_event.wait(
205206
timeout=timeout - int(time.perf_counter() - start_cmd)
206-
)
207-
assert stdout.channel.status_event.is_set()
207+
):
208+
output = "Command timeout exceeded."
209+
logger.info(output)
210+
return output, -1
211+
208212
exit_status = stdout.channel.exit_status
209213
# log standard output
210214
# try to decode to UTF-8, ignoring unconvertible characters

openwisp_controller/connection/tests/test_ssh.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,19 @@ def test_connection_failed_command(self, mocked_debug, mocked_info):
7070
]
7171
)
7272

73+
@mock.patch.object(ssh_logger, "info")
74+
@mock.patch.object(ssh_logger, "debug")
75+
def test_connection_command_timeout(self, mocked_debug, mocked_info):
76+
ckey = self._create_credentials_with_key(port=self.ssh_server.port)
77+
dc = self._create_device_connection(credentials=ckey)
78+
dc.connector_instance.connect()
79+
dc.connector_instance.exec_command("sleep 1", timeout=0)
80+
mocked_info.assert_has_calls(
81+
[
82+
mock.call("Command timeout exceeded."),
83+
]
84+
)
85+
7386
@mock.patch.object(ssh_logger, "info")
7487
@mock.patch.object(ssh_logger, "debug")
7588
def test_connection_failed_command_suppressed_output(

0 commit comments

Comments
 (0)