Skip to content

Commit ec2828c

Browse files
[tests] Add test for CommandTimeoutException in launch_command task
Added test_launch_command_ssh_timeout to verify that when CommandTimeoutException is raised during command execution, the command status is set to 'failed' and the output contains the expected timeout message.
1 parent 55df52f commit ec2828c

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

openwisp_controller/connection/tests/test_tasks.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from ...config.tests.test_controller import TestRegistrationMixin
1111
from .. import tasks
12+
from ..connectors.exceptions import CommandTimeoutException
1213
from .utils import CreateConnectionsMixin
1314

1415
Command = load_model("connection", "Command")
@@ -67,6 +68,30 @@ def test_launch_command_timeout(self, *args):
6768
self.assertEqual(command.status, "failed")
6869
self.assertEqual(command.output, "Background task time limit exceeded.\n")
6970

71+
@mock.patch(
72+
_mock_execute,
73+
side_effect=CommandTimeoutException("connection timed out after 30s"),
74+
)
75+
@mock.patch(_mock_connect, return_value=True)
76+
def test_launch_command_ssh_timeout(self, *args):
77+
dc = self._create_device_connection()
78+
command = Command(
79+
device=dc.device,
80+
connection=dc,
81+
type="custom",
82+
input={"command": "/usr/sbin/exotic_command"},
83+
)
84+
command.full_clean()
85+
command.save()
86+
# must call this explicitly because lack of transactions in this test case
87+
tasks.launch_command.delay(command.pk)
88+
command.refresh_from_db()
89+
self.assertEqual(command.status, "failed")
90+
self.assertEqual(
91+
command.output,
92+
"The command took longer than expected: connection timed out after 30s\n",
93+
)
94+
7095
@mock.patch(_mock_execute, side_effect=RuntimeError("test error"))
7196
@mock.patch(_mock_connect, return_value=True)
7297
def test_launch_command_exception(self, *args):

0 commit comments

Comments
 (0)