forked from duzy/gst-switch
/
test_server_unit.py
371 lines (294 loc) · 11.2 KB
/
test_server_unit.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
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
"""Unittests for Server class in server.py"""
import sys
import os
sys.path.insert(0, os.path.abspath(os.path.join(__file__, "../../../")))
from gstswitch.server import Server
import pytest
from gstswitch.exception import ServerProcessError
import subprocess
from distutils import spawn
from mock import Mock
PATH = '/usr/bin/'
class TestPath(object):
"""Test the path parameter"""
# Path Tests
def test_path_provided_slash(self):
"""Test if a path is provided"""
def mock_method(arg):
"""Mocking _start_process"""
return arg
path = '/usr/'
serv = Server(path=path)
serv._start_process = mock_method
assert serv._run_process().split() == "/usr/gst-switch-srv \
--video-input-port=3000 --audio-input-port=4000 \
--control-port=5000 --record=record-%Y-%m-%d_%H%M%S.data".split()
def test_path_provided_no_slash(self):
"""Test if a path is provided"""
def mock_method(arg):
"""Mocking _start_process"""
return arg
path = '/usr'
serv = Server(path=path)
serv._start_process = mock_method
assert serv._run_process().split() == "/usr/gst-switch-srv \
--video-input-port=3000 --audio-input-port=4000 \
--control-port=5000 --record=record-%Y-%m-%d_%H%M%S.data".split()
def test_path_empty(self, monkeypatch):
"""Test if null path is given"""
def mock_method(arg):
"Mocking _start_process"
return arg
def mockreturn(path):
"Mocking distutils.spawn.find_executable"
return '/usr/gst-switch-srv'
monkeypatch.setattr(spawn, 'find_executable', mockreturn)
paths = [None, '']
for path in paths:
serv = Server(path=path)
serv._start_process = mock_method
assert serv._run_process().split() == "/usr/gst-switch-srv \
--video-input-port=3000 --audio-input-port=4000 \
--control-port=5000 --record=record-%Y-%m-%d_%H%M%S.data".split()
class TestVideoPort(object):
"""Test for video_port parameter"""
# Video Port Tests
def test_invalid_video_port_null(self):
"""Test when the video_port is null"""
video_ports = [None, '', [], {}]
for video_port in video_ports:
with pytest.raises(ValueError):
Server(path=PATH, video_port=video_port)
def test_invalid_video_port_type(self):
"""Test when the video port given is not a valid
integral value"""
video_ports = [[1, 2, 3], {1: 2, 2: 3}]
for video_port in video_ports:
with pytest.raises(TypeError):
Server(path=PATH, video_port=video_port)
def test_invalid_video_port_range(self):
"""Test when the video port is not in range"""
video_ports = [-99, -1, 1e6]
for video_port in video_ports:
with pytest.raises(ValueError):
Server(path=PATH, video_port=video_port)
class TestAudioPort(object):
"""Test for audio_port parameter"""
# Audio Port Tests
def test_invalid_audio_port_null(self):
"""Test when the audio_port is null"""
audio_ports = [None, '', [], {}]
for audio_port in audio_ports:
with pytest.raises(ValueError):
Server(path=PATH, audio_port=audio_port)
def test_invalid_audio_port_type(self):
"""Test when the audio port given is not a valid
integral value"""
audio_ports = [[1, 2, 3], {1: 2, 2: 3}]
for audio_port in audio_ports:
with pytest.raises(TypeError):
Server(path=PATH, audio_port=audio_port)
def test_invalid_audio_port_range(self):
"""Test when the audio port is not in range"""
audio_ports = [-99, -1, 1e6]
for audio_port in audio_ports:
with pytest.raises(ValueError):
Server(path=PATH, audio_port=audio_port)
class TestControlPort(object):
"""Test the control_port parameter"""
# Control Port Tests
def test_invalid_control_port_null(self):
"""Test when the control port is null"""
control_ports = [None, '', [], {}]
for control_port in control_ports:
with pytest.raises(ValueError):
Server(path=PATH, control_port=control_port)
def test_invalid_control_port_type(self):
"""Test when the control port is not a valid
integral value"""
control_ports = [[1, 2, 3], {1: 2, 2: 3}]
for control_port in control_ports:
with pytest.raises(TypeError):
Server(path=PATH, control_port=control_port)
def test_invalid_control_port_range(self):
"""Test when the control port is not in range"""
control_ports = [-99, -1, 1e6]
for control_port in control_ports:
with pytest.raises(ValueError):
Server(path=PATH, control_port=control_port)
class TestRecordFile(object):
"""Test the record_file parameter"""
# Record File
def test_record_file_blank(self):
"""Test when the record_file is null"""
files = ['', None, [], {}]
for record_file in files:
with pytest.raises(ValueError):
Server(path=PATH, record_file=record_file)
def test_record_file_slashes(self):
"""Test when the record_file has forward slashes"""
filename = 'abcd/xyz/'
with pytest.raises(ValueError):
Server(path=PATH, record_file=filename)
class TestKillTerminate(object):
"""Test kill, terminate and gcov_flush methods"""
# OS Errors
def test_terminate_fail(self):
"""Test when terminate fails"""
class FakeProc(object):
"""A mock process"""
def __init__(self):
pass
def terminate(self):
"""Terminate the mock process"""
raise OSError
serv = Server(path=PATH)
serv.proc = FakeProc()
with pytest.raises(ServerProcessError):
serv.terminate()
def test_kill_fail(self):
"""Test when kill fails"""
serv = Server(path=PATH)
serv.proc = 1
serv.pid = -300
with pytest.raises(ServerProcessError):
serv.kill()
def test_no_process_kill(self):
"""Test when no process exists and kill is called"""
serv = Server(path=PATH)
with pytest.raises(ServerProcessError):
serv.kill()
def test_no_process_terminate(self):
"""Test when no process exists and terminate is called"""
serv = Server(path=PATH)
with pytest.raises(ServerProcessError):
serv.terminate()
def test_no_process_gov_flush(self):
"""Test when no process exists and gcov_flush is called"""
serv = Server(path=PATH)
with pytest.raises(ServerProcessError):
serv.gcov_flush()
def test_gcov_flush_fail(self):
"""Test when gcov_flush fails"""
serv = Server(path=PATH)
serv.proc = 1
serv.pid = -300
with pytest.raises(ServerProcessError):
serv.gcov_flush()
class TestRun(object):
"""Test running the server"""
def test_run(self):
"""Test the run method"""
serv = Server(path='abc')
serv._run_process = Mock(return_value=MockProcess())
serv.run()
assert serv.pid == 1
assert serv.proc is not None
def test_run_process(self):
"""Test _run_process method"""
serv = Server(path='abc')
serv._start_process = Mock(return_value=MockProcess())
serv.gst_option_string = ''
ret = serv._run_process()
assert ret is not None
def test_start_process_error(self, monkeypatch):
"""Test _start_process method"""
serv = Server(path='abc')
monkeypatch.setattr(subprocess, 'Popen', Mock(side_effect=OSError))
with pytest.raises(ServerProcessError):
serv._start_process('cmd')
def test_start_process_normal(self, monkeypatch):
"""Test _start_process normally"""
serv = Server(path='abc')
monkeypatch.setattr(
subprocess,
'Popen',
Mock(return_value=MockProcess()))
serv._start_process('cmd')
class MockProcess(object):
"""A mock process"""
def __init__(self, mode=True):
self.mode = mode
self.pid = 1
def terminate(self):
"""Terminate the mock process"""
if self.mode is True:
pass
if self.mode is False:
raise OSError('Testing terminate')
def kill(self):
"""Kill the mock process"""
if self.mode is True:
pass
if self.mode is False:
raise OSError('Testing kill')
def make_coverage(self):
"""Dump coverage"""
pass
class MockPopen(object):
"""Mock Popen method"""
def __init__(self, cmd, bufsize, shell):
pass
def communicate(self):
"""Mock communicate method of Popen"""
return 0, 0
class TestNormal(object):
# Normal Functioning Tests
"""Test the functioning of tests under valid conditions"""
def test_normal_terminate(self):
"""Test terminal when normally called"""
serv = Server(path='abc')
serv.proc = MockProcess(True)
serv.terminate()
assert serv.proc is None
def test_normal_kill(self, monkeypatch):
"""Test kill when normally called"""
serv = Server(path='abc')
serv.proc = Mock()
monkeypatch.setattr(os, 'kill', Mock())
res = serv.kill()
assert res is True
assert serv.proc is None
def test_terminate(self):
"""Test terminate ServerProcessError"""
serv = Server(path='abc')
serv.proc = MockProcess(False)
with pytest.raises(ServerProcessError):
serv.terminate()
def test_terminate_cov(self):
"""Test terminate and gcov_flush ServerProcessError"""
serv = Server(path='abc')
serv.proc = MockProcess(False)
serv.gcov_flush = Mock()
serv.make_coverage = Mock()
with pytest.raises(ServerProcessError):
serv.terminate(True)
def test_kill(self, monkeypatch):
"""Test kill ServerProcessError"""
serv = Server(path='abc')
serv.proc = Mock()
monkeypatch.setattr(os, 'kill', Mock(side_effect=OSError))
with pytest.raises(ServerProcessError):
serv.kill()
def test_kill_cov(self, monkeypatch):
"""Test kill and gcov_flush ServerProcessError"""
serv = Server(path='abc')
serv.proc = MockProcess(False)
serv.gcov_flush = Mock()
serv.make_coverage = Mock()
monkeypatch.setattr(os, 'kill', Mock(side_effect=OSError))
with pytest.raises(ServerProcessError):
serv.kill(True)
def test_normal_gcov_flush(self, monkeypatch):
"""Test gcov_flush"""
serv = Server(path='abc')
serv.proc = Mock()
monkeypatch.setattr(os, 'kill', Mock())
res = serv.gcov_flush()
assert res is True
assert serv.proc is not None
def test_make_coverage(self, monkeypatch):
"""Test dumping coverage"""
serv = Server(path='abc')
monkeypatch.setattr(subprocess, 'Popen', MockPopen)
serv.make_coverage()