-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_device_handler.py
300 lines (244 loc) · 10.1 KB
/
test_device_handler.py
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import unittest
from unittest.mock import MagicMock, patch, call
import sys
import subprocess
from device_handler import process_device
class TestDeviceHandler(unittest.TestCase):
def setUp(self):
patcher_logging = patch("device_handler.logging")
self.addCleanup(patcher_logging.stop)
self.mock_logging = patcher_logging.start()
self.logger = MagicMock()
self.mock_logging.getLogger.return_value = self.logger
patcher_sys_exit = patch("device_handler.sys.exit")
self.addCleanup(patcher_sys_exit.stop)
self.mock_sys_exit = patcher_sys_exit.start()
self.device = {
"deviceUuid": "uuid1",
"deviceId": "verdin-imx8mm-07214001-9334fa",
"notes": "pid4_1",
}
self.cloud = MagicMock()
self.env_vars = {
"PUBLIC_KEY": "public_key_content",
"DEVICE_PASSWORD": "device_password",
"TARGET_BUILD_TYPE": "some_build_type",
"USE_RAC": False,
}
self.args = MagicMock()
@patch("device_handler.database")
@patch("device_handler.common")
@patch("device_handler.Device")
@patch("device_handler.Remote")
@patch("subprocess.check_call") # Mock subprocess.check_call
def test_process_device_with_command(
self,
mock_check_call,
mock_Remote,
mock_Device,
mock_common,
mock_database,
):
_ = self.device["deviceUuid"]
hardware_id = "verdin-imx8mm"
mock_common.parse_hardware_id.return_value = hardware_id
mock_database.device_exists.return_value = True
mock_database.try_until_locked.return_value = True
dut_instance = MagicMock()
mock_Device.return_value = dut_instance
dut_instance.remote_session_ip = "192.168.1.100"
dut_instance.remote_session_port = 22
dut_instance.network_info = {"ip": "192.168.1.100"}
remote_instance = MagicMock()
mock_Remote.return_value = remote_instance
remote_instance.test_connection.return_value = True
args = MagicMock()
args.command = 'echo "Hello World"'
args.before = None
args.copy_artifact = None
args.run_before_on_host = 'echo "Running pre-command on host"'
result = process_device(self.device, self.cloud, self.env_vars, args)
self.assertTrue(result)
mock_check_call.assert_called_once_with(
args.run_before_on_host,
shell=True,
stdout=sys.stdout,
stderr=subprocess.STDOUT,
)
remote_instance.connection.run.assert_called_once_with(args.command)
@patch("device_handler.database")
@patch("device_handler.common")
@patch("device_handler.Device")
@patch("device_handler.Remote")
def test_process_device_successful(
self, mock_Remote, mock_Device, mock_common, mock_database
):
uuid = self.device["deviceUuid"]
hardware_id = "verdin-imx8mm"
mock_common.parse_hardware_id.return_value = hardware_id
mock_database.device_exists.return_value = False
mock_database.try_until_locked.return_value = True
dut_instance = MagicMock()
mock_Device.return_value = dut_instance
dut_instance.remote_session_ip = "192.168.1.100"
dut_instance.remote_session_port = 22
remote_instance = MagicMock()
mock_Remote.return_value = remote_instance
remote_instance.test_connection.return_value = True
result = process_device(
self.device, self.cloud, self.env_vars, self.args
)
self.assertTrue(result)
mock_database.device_exists.assert_called_with(uuid)
mock_database.create_device.assert_called_with(uuid)
self.logger.info.assert_any_call(f"Lock acquired for device {uuid}")
@patch("device_handler.database")
@patch("device_handler.common")
@patch("device_handler.Device")
@patch("device_handler.Remote")
def test_process_device_failed_connection(
self, mock_Remote, mock_Device, mock_common, mock_database
):
uuid = self.device["deviceUuid"]
hardware_id = "verdin-imx8mm"
mock_common.parse_hardware_id.return_value = hardware_id
mock_database.device_exists.return_value = True
mock_database.try_until_locked.return_value = True
dut_instance = MagicMock()
mock_Device.return_value = dut_instance
dut_instance.remote_session_ip = "192.168.1.100"
dut_instance.remote_session_port = 22
remote_instance = MagicMock()
mock_Remote.return_value = remote_instance
remote_instance.test_connection.return_value = False
result = process_device(
self.device, self.cloud, self.env_vars, self.args
)
self.assertTrue(result)
self.logger.error.assert_called_with(
f"Connection test failed for device {uuid}"
)
mock_database.release_lock.assert_called_with(uuid)
@patch("device_handler.database")
@patch("device_handler.common")
@patch("device_handler.Device")
@patch("device_handler.Remote")
def test_process_device_exception_handling(
self, mock_Remote, mock_Device, mock_common, mock_database
):
uuid = self.device["deviceUuid"]
hardware_id = "verdin-imx8mm"
mock_common.parse_hardware_id.return_value = hardware_id
mock_database.device_exists.return_value = True
mock_database.try_until_locked.return_value = True
mock_Device.side_effect = Exception("Device initialization failed")
process_device(self.device, self.cloud, self.env_vars, self.args)
self.logger.error.assert_called_with(
f"An error occurred while processing device {uuid}: Device initialization failed"
)
mock_database.release_lock.assert_called_with(uuid)
self.mock_sys_exit.assert_called_with(1)
@patch("device_handler.database")
@patch("device_handler.common")
def test_process_device_failed_to_acquire_lock(
self, mock_common, mock_database
):
uuid = self.device["deviceUuid"]
hardware_id = "verdin-imx8mm"
mock_common.parse_hardware_id.return_value = hardware_id
mock_database.device_exists.return_value = True
mock_database.try_until_locked.return_value = False
result = process_device(
self.device, self.cloud, self.env_vars, self.args
)
self.assertFalse(result)
self.logger.info.assert_called_with(
f"Failed to acquire lock for device {uuid}"
)
@patch("device_handler.database")
@patch("device_handler.common")
@patch("device_handler.Device")
@patch("device_handler.Remote")
@patch("subprocess.check_call")
def test_process_device_with_copy_artifact(
self,
mock_check_call,
mock_Remote,
mock_Device,
mock_common,
mock_database,
):
_ = self.device["deviceUuid"]
hardware_id = "verdin-imx8mm"
mock_common.parse_hardware_id.return_value = hardware_id
args = MagicMock()
args.command = None
args.before = None
args.copy_artifact = [
"/remote/path1",
"/local/output1",
"/remote/path2",
"/local/output2",
]
args.run_before_on_host = 'echo "Running pre-command on host"'
mock_database.device_exists.return_value = True
mock_database.try_until_locked.return_value = True
dut_instance = MagicMock()
mock_Device.return_value = dut_instance
dut_instance.remote_session_ip = "192.168.1.100"
dut_instance.remote_session_port = 22
dut_instance.network_info = {"ip": "192.168.1.100"}
remote_instance = mock_Remote.return_value
remote_instance.test_connection.return_value = True
remote_instance.connection.get = MagicMock()
result = process_device(self.device, self.cloud, self.env_vars, args)
self.assertTrue(result)
calls = [
call("/remote/path1", "/local/output1"),
call("/remote/path2", "/local/output2"),
]
remote_instance.connection.get.assert_has_calls(calls, any_order=True)
@patch("device_handler.database")
@patch("device_handler.common")
@patch("device_handler.Device")
@patch("device_handler.Remote")
def test_process_device_use_rac(
self, mock_Remote, mock_Device, mock_common, mock_database
):
self.env_vars["USE_RAC"] = True
_ = self.device["deviceUuid"]
hardware_id = "verdin-imx8mm"
mock_common.parse_hardware_id.return_value = hardware_id
mock_database.device_exists.return_value = True
mock_database.try_until_locked.return_value = True
dut_instance = MagicMock()
mock_Device.return_value = dut_instance
dut_instance.remote_session_ip = "192.168.1.100"
dut_instance.remote_session_port = 22
remote_instance = MagicMock()
mock_Remote.return_value = remote_instance
remote_instance.test_connection.return_value = True
result = process_device(
self.device, self.cloud, self.env_vars, self.args
)
self.assertTrue(result)
dut_instance.setup_rac_session.assert_called_with("ras.torizon.io")
dut_instance.setup_usual_ssh_session.assert_not_called()
@patch("device_handler.database")
@patch("device_handler.common")
def test_process_device_device_not_in_database(
self, mock_common, mock_database
):
uuid = self.device["deviceUuid"]
hardware_id = "verdin-imx8mm"
mock_common.parse_hardware_id.return_value = hardware_id
mock_database.device_exists.return_value = False
result = process_device(
self.device, self.cloud, self.env_vars, self.args
)
self.assertTrue(result)
mock_database.create_device.assert_called_with(uuid)
self.logger.info.assert_any_call(f"Created new device entry for {uuid}")
self.logger.info.assert_any_call(f"Lock acquired for device {uuid}")
if __name__ == "__main__":
unittest.main()