-
-
Notifications
You must be signed in to change notification settings - Fork 288
Expand file tree
/
Copy pathtest_tasks.py
More file actions
133 lines (120 loc) · 5.04 KB
/
test_tasks.py
File metadata and controls
133 lines (120 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import uuid
from contextlib import redirect_stderr
from io import StringIO
from unittest import mock
from celery.exceptions import SoftTimeLimitExceeded
from django.test import TestCase, TransactionTestCase
from swapper import load_model
from ...config.tests.test_controller import TestRegistrationMixin
from .. import tasks
from ..connectors.exceptions import CommandTimeoutException
from .utils import CreateConnectionsMixin
Command = load_model("connection", "Command")
OrganizationConfigSettings = load_model("config", "OrganizationConfigSettings")
class TestTasks(CreateConnectionsMixin, TestCase):
_mock_execute = "openwisp_controller.connection.base.models.AbstractCommand.execute"
_mock_connect = (
"openwisp_controller.connection.base.models.AbstractDeviceConnection.connect"
)
@mock.patch("logging.Logger.warning")
@mock.patch("time.sleep")
def test_update_config_missing_config(self, mocked_sleep, mocked_warning):
pk = self._create_device().pk
tasks.update_config.delay(pk)
mocked_warning.assert_called_with(
f'update_config("{pk}") failed: Device has no config.'
)
mocked_sleep.assert_called_once()
@mock.patch("logging.Logger.warning")
@mock.patch("time.sleep")
def test_update_config_missing_device(self, mocked_sleep, mocked_warning):
pk = uuid.uuid4()
tasks.update_config.delay(pk)
mocked_warning.assert_called_with(
f'update_config("{pk}") failed: Device matching query does not exist.'
)
mocked_sleep.assert_called_once()
@mock.patch("logging.Logger.warning")
def test_launch_command_missing(self, mocked_warning):
pk = uuid.uuid4()
tasks.launch_command.delay(pk)
mocked_warning.assert_called_with(
f'launch_command("{pk}") failed: Command matching query does not exist.'
)
@mock.patch(_mock_execute, side_effect=SoftTimeLimitExceeded())
@mock.patch(_mock_connect, return_value=True)
def test_launch_command_timeout(self, *args):
dc = self._create_device_connection()
command = Command(
device=dc.device,
connection=dc,
type="custom",
input={"command": "/usr/sbin/exotic_command"},
)
command.full_clean()
command.save()
# must call this explicitly because lack of transactions in this test case
tasks.launch_command.delay(command.pk)
command.refresh_from_db()
self.assertEqual(command.status, "failed")
self.assertEqual(command.output, "Background task time limit exceeded.\n")
@mock.patch(
_mock_execute,
side_effect=CommandTimeoutException("connection timed out after 30s"),
)
@mock.patch(_mock_connect, return_value=True)
def test_launch_command_ssh_timeout(self, *args):
dc = self._create_device_connection()
command = Command(
device=dc.device,
connection=dc,
type="custom",
input={"command": "/usr/sbin/exotic_command"},
)
command.full_clean()
command.save()
# must call this explicitly because lack of transactions in this test case
tasks.launch_command.delay(command.pk)
command.refresh_from_db()
self.assertEqual(command.status, "failed")
self.assertEqual(
command.output,
"The command took longer than expected: connection timed out after 30s\n",
)
@mock.patch(_mock_execute, side_effect=RuntimeError("test error"))
@mock.patch(_mock_connect, return_value=True)
def test_launch_command_exception(self, *args):
dc = self._create_device_connection()
command = Command(
device=dc.device,
connection=dc,
type="custom",
input={"command": "/usr/sbin/exotic_command"},
)
command.full_clean()
command.save()
# must call this explicitly because lack of transactions in this test case
with redirect_stderr(StringIO()) as stderr:
tasks.launch_command.delay(command.pk)
expected = f"An exception was raised while executing command {command.pk}"
self.assertIn(expected, stderr.getvalue())
command.refresh_from_db()
self.assertEqual(command.status, "failed")
self.assertEqual(command.output, "Internal system error: test error\n")
class TestTransactionTasks(
TestRegistrationMixin, CreateConnectionsMixin, TransactionTestCase
):
@mock.patch.object(tasks.update_config, "delay")
def test_update_config_hostname_changed_on_reregister(self, mocked_update_config):
device = self._create_device_config()
self._create_device_connection(device=device)
# Trigger re-registration with new hostname
response = self.client.post(
self.register_url,
self._get_reregistration_payload(
device,
name="new-hostname",
),
)
self.assertEqual(response.status_code, 201)
mocked_update_config.assert_not_called()