-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtests.py
More file actions
538 lines (480 loc) · 19.3 KB
/
tests.py
File metadata and controls
538 lines (480 loc) · 19.3 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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
#!/usr/bin/env python
'Take Photo Tests.'
import os
import sys
import unittest
os.environ['take_photo_disable_rotation_adjustment'] = '0'
import take_photo
import numpy as np
try:
import farmware_tools
except ImportError:
FT_IMPORTED = False
else:
FT_IMPORTED = True
try:
import cv2
except ImportError:
CV2_IMPORTED = False
else:
CV2_IMPORTED = True
try:
from unittest import mock
except ImportError:
import mock
OUTPUT_FILENAME = 'output.txt'
def read_output_file(output_file):
'Read test output file.'
output_file.close()
with open(OUTPUT_FILENAME, 'r') as output_file:
output = output_file.read().lower()
sys.stdout = sys.__stdout__
print('')
print(os.environ)
print('>' * 20)
print(output)
print('<' * 20)
return output
os.environ.clear()
ENVS = [
'take_photo_logging',
'camera',
'IMAGES_DIR',
'take_photo_width',
'take_photo_height',
'take_photo_args',
'take_photo_disable_rotation_adjustment',
'CAMERA_CALIBRATION_total_rotation_angle',
'FARMBOT_OS_VERSION',
'FARMWARE_URL',
'FARMWARE_TOKEN',
'FARMWARE_API_V2_REQUEST_PIPE',
'FARMWARE_API_V2_RESPONSE_PIPE',
]
def re_import():
try:
reload(take_photo)
except NameError:
import importlib
importlib.reload(take_photo)
def _prepare_fuser_mock(**kwargs):
def _fuser_mock(*_args):
if kwargs.get('missing'):
try:
MissingError = FileNotFoundError
except NameError:
MissingError = OSError
raise MissingError
if kwargs.get('busy'):
return b' 1 2 3'
else:
import subprocess
raise subprocess.CalledProcessError
return _fuser_mock
def _prepare_mock_capture(**kwargs):
def mocked_video_capture(*_args):
'Used by mock.'
class MockVideoCapture():
'Mock cv2.VideoCapture'
@staticmethod
def isOpened():
'is camera open?'
ret = kwargs.get('isOpened')
return True if ret is None else ret
@staticmethod
def getBackendName():
'get capture backend'
if kwargs.get('raise_backend'):
raise NameError('mock error')
return 'mock'
@staticmethod
def grab():
'get frame'
ret = kwargs.get('grab_return')
return True if ret is None else ret
@staticmethod
def read():
'get image'
if kwargs.get('raise_read'):
raise NameError('mock error')
default_return = True, np.zeros([10, 10, 3], np.uint8)
return kwargs.get('read_return') or default_return
@staticmethod
def set(*_args):
'set parameter'
return
@staticmethod
def release():
'close camera'
return
if kwargs.get('raise_open'):
raise IOError('mock error')
return MockVideoCapture()
return mocked_video_capture
def _prepare_mock_socket(**_kwargs):
def mocked_socket(*_args):
class MockSocket():
@staticmethod
def connect(_): return
@staticmethod
def sendall(req): print(req)
@staticmethod
def recv(_): return
@staticmethod
def close(): return
return MockSocket()
return mocked_socket
class TakePhotoTest(unittest.TestCase):
'Test Take Photo.'
def setUp(self):
for env in ENVS:
try:
del os.environ[env]
except KeyError:
pass
os.environ['IMAGES_DIR'] = '/tmp'
os.environ['take_photo_disable_rotation_adjustment'] = '0'
self.outfile = open(OUTPUT_FILENAME, 'w')
sys.stdout = self.outfile
def test_default(self):
'Test default Take Photo.'
re_import()
take_photo.take_photo()
output = read_output_file(self.outfile)
self.assertGreater(output.count('[ '), 3)
self.assertLess(output.count('send_message'), 3)
self.assertFalse('rotated' in output)
def test_quiet(self):
'Test quiet log level.'
os.environ['take_photo_logging'] = 'quiet'
re_import()
take_photo.take_photo()
output = read_output_file(self.outfile)
self.assertFalse('[ ' in output)
def test_verbose(self):
'Test verbose log level.'
os.environ['take_photo_logging'] = 'verbose'
re_import()
take_photo.take_photo()
output = read_output_file(self.outfile)
self.assertFalse('[ ' in output)
if FT_IMPORTED:
self.assertGreater(output.count('send_message'), 3)
def test_timed_verbose(self):
'Test timed verbose log level.'
os.environ['take_photo_logging'] = 'verbose_timed'
re_import()
take_photo.take_photo()
output = read_output_file(self.outfile)
self.assertTrue('[ ' in output)
if FT_IMPORTED:
self.assertGreater(output.count('send_message'), 3)
@unittest.skipIf(FT_IMPORTED, '')
@mock.patch('requests.post', mock.Mock())
def test_verbose_legacy(self):
'Test verbose log level with legacy log.'
os.environ['take_photo_logging'] = 'verbose'
os.environ['FARMWARE_URL'] = 'url'
os.environ['FARMWARE_TOKEN'] = 'token'
re_import()
take_photo.take_photo()
output = read_output_file(self.outfile)
self.assertFalse('[ ' in output)
@mock.patch('os.listdir', mock.Mock(side_effect=lambda _: ['video0']))
@mock.patch('os.path.exists', mock.Mock())
@mock.patch('cv2.VideoCapture', _prepare_mock_capture())
def test_capture_success(self):
'Test image capture.'
del os.environ['IMAGES_DIR']
re_import()
take_photo.take_photo()
output = read_output_file(self.outfile)
self.assertTrue('saved' in output)
self.assertTrue('directory does not exist' in output)
@mock.patch('os.listdir', mock.Mock(
side_effect=lambda _: ['video0', 'video1', 'video2']))
@mock.patch('os.path.exists', mock.Mock(side_effect=lambda _: False))
def test_not_at_port(self):
'Test not at video ports.'
del os.environ['IMAGES_DIR']
re_import()
take_photo.take_photo()
output = read_output_file(self.outfile)
self.assertFalse('saved' in output)
@mock.patch('os.listdir', mock.Mock(side_effect=lambda _: ['video0']))
@mock.patch('os.path.exists', mock.Mock())
@mock.patch('cv2.VideoCapture', _prepare_mock_capture(raise_open=True))
def test_camera_open_error(self):
'Test error on camera open.'
re_import()
take_photo.take_photo()
output = read_output_file(self.outfile)
self.assertTrue('mock error' in output)
self.assertTrue('could not connect' in output)
@mock.patch('os.listdir', mock.Mock(side_effect=lambda _: ['video0']))
@mock.patch('os.path.exists', mock.Mock())
@mock.patch('cv2.VideoCapture', _prepare_mock_capture(raise_backend=True))
def test_camera_get_backend_error(self):
'Test error on get backend.'
re_import()
take_photo.take_photo()
output = read_output_file(self.outfile)
self.assertTrue('not available' in output)
@mock.patch('os.listdir', mock.Mock(side_effect=lambda _: ['video0']))
@mock.patch('os.path.exists', mock.Mock())
@mock.patch('cv2.VideoCapture', _prepare_mock_capture(raise_read=True))
def test_camera_read_error(self):
'Test error on camera read.'
re_import()
take_photo.take_photo()
output = read_output_file(self.outfile)
self.assertTrue('mock error' in output)
self.assertTrue('image capture error' in output)
@mock.patch('os.listdir', mock.Mock(side_effect=lambda _: ['video0']))
@mock.patch('os.path.exists', mock.Mock())
@mock.patch('subprocess.check_output',
mock.Mock(side_effect=_prepare_fuser_mock(missing=True)))
@mock.patch('cv2.VideoCapture', _prepare_mock_capture())
def test_camera_no_busy_check(self):
'Test unable to check if camera is busy.'
re_import()
take_photo.take_photo()
output = read_output_file(self.outfile)
self.assertTrue('unable to check' in output)
@mock.patch('os.listdir', mock.Mock(side_effect=lambda _: ['video0']))
@mock.patch('os.path.exists', mock.Mock())
@mock.patch('subprocess.check_output',
mock.Mock(side_effect=_prepare_fuser_mock(busy=True)))
@mock.patch('cv2.VideoCapture', _prepare_mock_capture())
def test_camera_busy(self):
'Test camera busy.'
re_import()
take_photo.take_photo()
output = read_output_file(self.outfile)
self.assertTrue('attempting to close' in output)
@mock.patch('os.listdir', mock.Mock(side_effect=lambda _: ['video0']))
@mock.patch('os.path.exists', mock.Mock())
@mock.patch('cv2.VideoCapture', _prepare_mock_capture(isOpened=False))
def test_camera_not_open(self):
'Test camera not open.'
re_import()
take_photo.take_photo()
output = read_output_file(self.outfile)
self.assertTrue('could not connect' in output)
@mock.patch('os.listdir', mock.Mock(side_effect=lambda _: ['video0']))
@mock.patch('os.path.exists', mock.Mock())
@mock.patch('cv2.VideoCapture',
_prepare_mock_capture(read_return=(False, None)))
def test_no_image(self):
'Test no image.'
re_import()
take_photo.take_photo()
output = read_output_file(self.outfile)
self.assertTrue('no image' in output)
@mock.patch('os.listdir', mock.Mock(side_effect=lambda _: ['video0']))
@mock.patch('os.path.exists', mock.Mock())
@mock.patch('cv2.VideoCapture', _prepare_mock_capture(grab_return=False))
def test_no_grab_image(self):
'Test no grab return.'
re_import()
take_photo.take_photo()
output = read_output_file(self.outfile)
self.assertTrue('could not get frame' in output)
@mock.patch('os.listdir', mock.Mock(side_effect=lambda _: ['video0']))
@mock.patch('os.path.exists', mock.Mock())
@mock.patch('os.path.isdir', mock.Mock())
@mock.patch('cv2.VideoCapture', _prepare_mock_capture())
def test_rotated(self):
'Test image rotation.'
os.environ['CAMERA_CALIBRATION_total_rotation_angle'] = '45'
re_import()
take_photo.take_photo()
output = read_output_file(self.outfile)
self.assertTrue('rotated' in output)
self.assertFalse('directory does not exist' in output)
@mock.patch('os.listdir', mock.Mock(side_effect=lambda _: ['video0']))
@mock.patch('os.path.exists', mock.Mock())
@mock.patch('os.path.isdir', mock.Mock())
@mock.patch('cv2.VideoCapture', _prepare_mock_capture())
def test_large_rotation(self):
'Test large image rotation.'
os.environ['CAMERA_CALIBRATION_total_rotation_angle'] = '75'
re_import()
take_photo.take_photo()
output = read_output_file(self.outfile)
self.assertTrue('rotated' in output)
self.assertFalse('directory does not exist' in output)
def test_none_camera(self):
'Test none camera selection.'
os.environ['camera'] = 'none'
re_import()
take_photo.take_photo()
output = read_output_file(self.outfile)
self.assertTrue('no camera selected' in output)
self.assertFalse('USB' in output)
def test_rpi_camera(self):
'Test rpi camera selection.'
os.environ['camera'] = 'rpi'
re_import()
take_photo.take_photo()
output = read_output_file(self.outfile)
self.assertTrue('raspberry pi' in output)
self.assertTrue('raspistill' in output)
self.assertTrue('-w 640 -h 480' in output)
self.assertFalse('-md 3' in output)
self.assertFalse('USB' in output)
def test_rpi_camera_small_size(self):
'Test capture with rpi camera selection and small size inputs.'
os.environ['camera'] = 'rpi'
os.environ['take_photo_width'] = '200'
os.environ['take_photo_height'] = '100'
re_import()
take_photo.take_photo()
output = read_output_file(self.outfile)
self.assertTrue('raspistill' in output)
self.assertTrue('-w 200 -h 100' in output)
self.assertFalse('-md 3' in output)
def test_rpi_camera_large_size(self):
'Test capture with rpi camera selection and large size inputs.'
os.environ['camera'] = 'rpi'
os.environ['take_photo_width'] = '2000'
os.environ['take_photo_height'] = '2000'
re_import()
take_photo.take_photo()
output = read_output_file(self.outfile)
self.assertTrue('raspistill' in output)
self.assertTrue('-md 3' in output)
self.assertFalse('-w' in output)
@mock.patch('cv2.imread', mock.Mock(side_effect=lambda _:
np.zeros([10, 10, 3], np.uint8)))
@mock.patch('os.remove', mock.Mock())
@mock.patch('subprocess.call', mock.Mock(side_effect=lambda _: 0))
def test_rpi_camera_capture(self):
'Test rpi camera capture success.'
os.environ['camera'] = 'rpi'
re_import()
take_photo.take_photo()
output = read_output_file(self.outfile)
self.assertTrue('raspberry pi' in output)
self.assertTrue('image captured' in output)
@mock.patch('subprocess.call', mock.Mock(side_effect=lambda _: 1))
def test_rpi_camera_capture_failure(self):
'Test rpi camera capture failure.'
os.environ['camera'] = 'rpi'
re_import()
take_photo.take_photo()
output = read_output_file(self.outfile)
self.assertTrue('raspberry pi' in output)
self.assertTrue('not detected' in output)
@mock.patch('subprocess.call', mock.Mock(side_effect=lambda _: 0))
def test_quick_rpi_camera(self):
'Test quick capture with rpi camera selection.'
os.environ['take_photo_disable_rotation_adjustment'] = '1'
os.environ['camera'] = 'rpi'
with self.assertRaises(SystemExit):
re_import()
output = read_output_file(self.outfile)
self.assertTrue('raspistill' in output)
self.assertFalse('fswebcam' in output)
self.assertFalse('no camera selected' in output)
@mock.patch('os.listdir', mock.Mock(side_effect=lambda _: ['video0']))
@mock.patch('subprocess.call', mock.Mock(side_effect=lambda _: 0))
def test_quick_usb_camera(self):
'Test quick capture with usb camera selection.'
os.environ['take_photo_disable_rotation_adjustment'] = '1'
os.environ['camera'] = 'usb'
with self.assertRaises(SystemExit):
re_import()
output = read_output_file(self.outfile)
self.assertFalse('raspistill' in output)
self.assertTrue('fswebcam' in output)
self.assertFalse('no camera selected' in output)
@mock.patch('os.listdir', mock.Mock(side_effect=lambda _: ['video0']))
@mock.patch('subprocess.call', mock.Mock(side_effect=lambda _: 0))
def test_quick_usb_camera_image_size(self):
'Test quick capture with usb camera and image size selection.'
os.environ['take_photo_disable_rotation_adjustment'] = '1'
os.environ['take_photo_width'] = '200'
os.environ['take_photo_height'] = '100'
os.environ['camera'] = 'usb'
with self.assertRaises(SystemExit):
re_import()
output = read_output_file(self.outfile)
self.assertFalse('raspistill' in output)
self.assertTrue('fswebcam' in output)
self.assertTrue('200x100' in output)
self.assertFalse('no camera selected' in output)
@mock.patch('os.listdir', mock.Mock(side_effect=lambda _: ['video0']))
@mock.patch('subprocess.call', mock.Mock(side_effect=lambda _: 0))
def test_quick_usb_camera_args(self):
'Test quick capture with usb camera and argument list.'
os.environ['take_photo_disable_rotation_adjustment'] = '1'
os.environ['take_photo_width'] = '200'
os.environ['take_photo_height'] = '100'
os.environ['take_photo_args'] = '["-s", "brightness=100%"]'
os.environ['camera'] = 'usb'
with self.assertRaises(SystemExit):
re_import()
output = read_output_file(self.outfile)
self.assertFalse('raspistill' in output)
self.assertTrue('fswebcam' in output)
self.assertTrue('200x100' in output)
self.assertTrue('brightness=100%' in output)
self.assertFalse('no camera selected' in output)
@mock.patch('subprocess.call', mock.Mock(side_effect=lambda _: 0))
@mock.patch('os.listdir', mock.Mock(side_effect=lambda _: []))
def test_quick_usb_camera_missing_port(self):
'Test quick capture with usb camera selection, video port missing.'
os.environ['take_photo_disable_rotation_adjustment'] = '1'
os.environ['camera'] = 'usb'
with self.assertRaises(SystemExit):
re_import()
output = read_output_file(self.outfile)
self.assertFalse('raspistill' in output)
self.assertFalse('fswebcam' in output)
self.assertFalse('no camera selected' in output)
self.assertTrue('not detected' in output)
def test_quick_none_camera(self):
'Test quick capture with none camera selection.'
os.environ['take_photo_disable_rotation_adjustment'] = '1'
os.environ['camera'] = 'none'
with self.assertRaises(SystemExit):
re_import()
output = read_output_file(self.outfile)
self.assertFalse('raspistill' in output)
self.assertFalse('fswebcam' in output)
self.assertTrue('no camera selected' in output)
@unittest.skipIf(sys.version_info[0] < 3, '')
@mock.patch('socket.socket', _prepare_mock_socket())
def test_quick_none_camera_with_log(self):
'Test quick capture with none camera selection and log.'
os.environ['FARMWARE_API_V2_REQUEST_PIPE'] = ''
os.environ['FARMWARE_API_V2_RESPONSE_PIPE'] = ''
os.environ['take_photo_disable_rotation_adjustment'] = '1'
os.environ['camera'] = 'none'
with self.assertRaises(SystemExit):
re_import()
output = read_output_file(self.outfile)
self.assertFalse('raspistill' in output)
self.assertFalse('fswebcam' in output)
self.assertTrue('no camera selected' in output)
def test_quick_none_camera_quiet(self):
'Test quick capture with none camera selection: quiet.'
os.environ['take_photo_disable_rotation_adjustment'] = '1'
os.environ['camera'] = 'none'
os.environ['take_photo_logging'] = 'quiet'
with self.assertRaises(SystemExit):
re_import()
output = read_output_file(self.outfile)
self.assertFalse('no camera selected' in output)
@unittest.skipIf(CV2_IMPORTED, '')
def test_opencv_missing(self):
'Test for cv2 import error.'
with self.assertRaises(SystemExit):
re_import()
output = read_output_file(self.outfile)
self.assertTrue('import error' in output)
def tearDown(self):
self.outfile.close()
sys.stdout = sys.__stdout__
os.remove(OUTPUT_FILENAME)