def test_raises_enoent(self): """Test what happens when ProcessMonitor raises an OSError ENOENT""" with patch('gstswitch.server.ProcessMonitor') as mock: mock.side_effect = OSError(ENOENT, "not found") serv = Server(path='abc') with pytest.raises(PathError): serv._start_process('cmd')
def get_encode_port(num): """Test get_encode_port - num times""" video_port = 3000 serv = Server(path=PATH, video_port=video_port) try: serv.run() sources = TestSources(video_port=video_port) sources.new_test_video() expected_res = [video_port + 2] * num controller = Controller() controller.establish_connection() res = [] for _ in range(num): res.append(controller.get_encode_port()) assert expected_res == res finally: if serv.proc: poll = serv.proc.poll() if poll == -11: print "SEGMENTATION FAULT OCCURRED" print "ERROR CODE - {0}".format(poll) serv.terminate(1) log = open('server.log') print log.read()
def test_raises_oserror(self): """Test what happens when ProcessMonitor raises an OSError""" with patch('gstswitch.server.ProcessMonitor') as mock: mock.side_effect = OSError() serv = Server(path='abc') with pytest.raises(ServerProcessError): serv._start_process('cmd')
def main(args): print("running server") serv = Server(path=args.path) serv.run() wait(5) print("running source pattern=1") sources = TestSources(video_port=3000, audio_port=4000) sources.new_test_video(pattern=1) wait(5) print("running gst-switch-ui") # the & will run this in the background so control returns # and we can bring up the 2nd source call("gst-switch-ui &", shell=True) wait(5) print("running source pattern=18") sources.new_test_video(pattern=18) raw_input("hit enter:") # need to kill off the processes. # a better wy of doing is would be to use # https://docs.python.org/2/library/subprocess.html#subprocess.Popen.kill # but I don't feel like figuring it out :p call("pkill gst-switch-ui", shell=True) call("pkill gst-switch-srv", shell=True)
def get_audio_port(num): """Test get_audio_port""" audio_port = 8000 serv = Server(path=PATH, audio_port=audio_port) try: serv.run() sources = TestSources(audio_port=audio_port) sources.new_test_audio() expected_res = [3003] * num controller = Controller() controller.establish_connection() res = [] for _ in range(num): res.append(controller.get_audio_port()) assert expected_res == res finally: if serv.proc: poll = serv.proc.poll() if poll == -11: print("SEGMENTATION FAULT OCCURRED") print("ERROR CODE - {0}".format(poll)) serv.terminate(1) log = open('server.log') print(log.read())
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_log_to_stderr(self): """Test log_to_file=False property""" with patch('gstswitch.server.ProcessMonitor') as mock: serv = Server(path='abc') serv.log_to_file = False serv._start_process('cmd') mock.assert_called_with('cmd')
def main(args): print("running server") serv = Server(path=args.path) serv.run() wait(5) print("running source pattern=1") sources = TestSources(video_port=3000, audio_port=4000) sources.new_test_video(pattern=1) wait(5) print("running gst-switch-ui") # the & will run this in the background so control returns # and we can bring up the 2nd source # Replaced call with subprocess.Popen. SwitchUi = subprocess.Popen("gst-switch-ui &",shell=True) wait(5) print("running source pattern=18") sources.new_test_video(pattern=18) raw_input("hit enter:") # Replaced pkill call with Popen.kill subprocess.Popen.kill(SwitchUi) serv.kill()
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_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()
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_start_process_error(self, monkeypatch): """Test _start_process method""" with patch('subprocess.Popen.__init__') as mock: mock.side_effect = OSError serv = Server(path='abc') with pytest.raises(ServerProcessError): serv._start_process('def')
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_start_process_error(self, monkeypatch): """Test _start_process method""" with patch("subprocess.Popen.__init__") as mock: mock.side_effect = OSError serv = Server(path="abc") with pytest.raises(ServerProcessError): serv._start_process("def")
def test_raises_oserror(self): """Test what happens when ProcessMonitor raises an OSError""" with patch("gstswitch.server.ProcessMonitor") as mock: mock.side_effect = OSError() serv = Server(path="abc") with pytest.raises(ServerProcessError): serv._start_process("cmd")
def get_preview_ports(num): """Get Preview Ports when num number of sources are added""" video_port = 3000 serv = Server(path=PATH, video_port=video_port) try: serv.run() sources = TestSources(video_port=video_port) expected_res = [ i for i in range(3003, 3003 + num)] controller = Controller() res = [] for _ in range(num): sources.new_test_video() controller.establish_connection() res = controller.get_preview_ports() print res assert expected_res == res finally: if serv.proc: poll = serv.proc.poll() if poll == -11: print "SEGMENTATION FAULT OCCURRED" print "ERROR CODE - {0}".format(poll) serv.terminate(1) log = open('server.log') print log.read()
def test_raises_enoent(self): """Test what happens when ProcessMonitor raises an OSError ENOENT""" with patch("gstswitch.server.ProcessMonitor") as mock: mock.side_effect = OSError(ENOENT, "not found") serv = Server(path="abc") with pytest.raises(PathError): serv._start_process("cmd")
def permutate_adjust_pip(num, delay): """Adjust_pip num number of times""" import random video_port = 3000 serv = Server(path=PATH, video_port=video_port) try: serv.run() sources = TestSources(video_port=video_port) sources.new_test_video(pattern=6) sources.new_test_video(pattern=5) preview = PreviewSinks() preview.run() controller = Controller() for _ in range(num): xpos = random.randrange(-20, 20) ypos = random.randrange(-20, 20) res = controller.adjust_pip(xpos, ypos, 0, 0) time.sleep(delay) assert res is not None preview.terminate() sources.terminate_video() finally: if serv.proc: poll = serv.proc.poll() if poll == -11: print "SEGMENTATION FAULT OCCURRED" print "ERROR CODE - {0}".format(poll) serv.terminate(1) log = open('server.log') print log.read()
def test_log_to_stderr(self): """Test log_to_file=False property""" with patch("gstswitch.server.ProcessMonitor") as mock: serv = Server(path="abc") serv.log_to_file = False serv._start_process("cmd") mock.assert_called_with("cmd")
def test_gov_flush_fail(self): """Test when gov_flush fails""" serv = Server(path=PATH) serv.proc = Mock(ProcessMonitor) serv.proc.send_signal.side_effect = OSError with pytest.raises(ServerProcessError): serv.gcov_flush()
def test_binary_not_found(self, monkeypatch): """Test that a PathError os raised when distutils.spawn.find_executable return nothing""" monkeypatch.setattr(spawn, "find_executable", Mock(return_value="")) with pytest.raises(PathError): serv = Server() serv._run_process()
def test_option_string(self): """Test option_string property""" serv = Server(path="abc") serv._start_process = Mock() serv.gst_option_string = "-ZZZ" serv._run_process() serv._start_process.assert_called_once() assert "-ZZZ" in serv._start_process.call_args[0][0]
def test_video_format(self): """Test video_format property""" serv = Server(path="abc") serv._start_process = Mock() serv.video_format = "ZZZ" serv._run_process() serv._start_process.assert_called_once() assert "--video-format=ZZZ" in serv._start_process.call_args[0][0]
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_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')
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_binary_not_found(self, monkeypatch): """Test that a PathError os raised when distutils.spawn.find_executable return nothing""" monkeypatch.setattr(spawn, 'find_executable', Mock(return_value='')) with pytest.raises(PathError): serv = Server() serv._run_process()
def test_is_not_alive(self, monkeypatch): """ Test that is_alive returns True when the underlying proc returns None on Poll """ serv = Server() serv.proc = Mock() monkeypatch.setattr(serv.proc, 'poll', Mock(return_value=123)) assert serv.is_alive() is False
def test_is_not_alive(self, monkeypatch): """ Test that is_alive returns True when the underlying proc returns None on Poll """ serv = Server() serv.proc = Mock() monkeypatch.setattr(serv.proc, "poll", Mock(return_value=123)) assert serv.is_alive() is False
def test_log_to_file(self): """Test log_to_file=True property""" with patch("gstswitch.server.open", create=True) as mock_open: mock_open.return_value = 123 with patch("gstswitch.server.ProcessMonitor") as mock: serv = Server(path="abc") serv.log_to_file = True serv._start_process("cmd") mock.assert_called_with("cmd", 123)
def test_log_to_file(self): """Test log_to_file=True property""" with patch('gstswitch.server.open', create=True) as mock_open: mock_open.return_value = 123 with patch('gstswitch.server.ProcessMonitor') as mock: serv = Server(path='abc') serv.log_to_file = True serv._start_process('cmd') mock.assert_called_with('cmd', 123)
def test_error_is_passed(self, monkeypatch): """ Test that wait_for_output passes all Exceptions from the underlying ProcessMonitor """ serv = Server() serv.proc = Mock() monkeypatch.setattr(serv.proc, "wait_for_output", Mock(side_effect=MatchTimeoutError)) with pytest.raises(MatchTimeoutError): serv.wait_for_output("foo", timeout=123, count=456)
def test_args_are_passed(self, monkeypatch): """ Test that wait_for_output passes all Arguments to the underlying ProcessMonitor """ serv = Server() serv.proc = Mock() monkeypatch.setattr(serv.proc, "wait_for_output", Mock()) serv.wait_for_output("foo", timeout=123, count=456) serv.proc.wait_for_output.called_once_with("foo", timeout=123, count=456)
def test_error_is_passed(self, monkeypatch): """ Test that wait_for_output passes all Exceptions from the underlying ProcessMonitor """ serv = Server() serv.proc = Mock() monkeypatch.setattr(serv.proc, 'wait_for_output', Mock(side_effect=MatchTimeoutError)) with pytest.raises(MatchTimeoutError): serv.wait_for_output("foo", timeout=123, count=456)
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() == "/usr/gst-switch-srv \ --video-input-port=3000 --audio-input-port=4000 \ --control-port=5000".split()
def test_record_file_valid_space(self): """Test if record file is valid and has a space""" def mock_method(arg): """Mocking _start_process""" return arg path = '/usr' serv = Server(path=path, record_file='record 1.data') serv._start_process = mock_method assert serv._run_process() == "/usr/gst-switch-srv \ --video-input-port=3000 --audio-input-port=4000 \ --control-port=5000".split() + ["--record=record 1.data"]
def test_record_file_true(self): """Test if record file is True""" def mock_method(arg): """Mocking _start_process""" return arg path = '/usr' serv = Server(path=path, record_file=True) serv._start_process = mock_method assert serv._run_process() == "/usr/gst-switch-srv \ --video-input-port=3000 --audio-input-port=4000 \ --control-port=5000 -r".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() == "/usr/gst-switch-srv \ --video-input-port=3000 --audio-input-port=4000 \ --controller-address=tcp:host=0.0.0.0,port=5000".split()
def test_record_file_valid(self): """Test if record file is valid""" def mock_method(arg): """Mocking _start_process""" return arg path = '/usr' serv = Server(path=path, record_file="record.data") serv._start_process = mock_method assert serv._run_process() == "/usr/gst-switch-srv \ --video-input-port=3000 --audio-input-port=4000 \ --controller-address=tcp:host=0.0.0.0,port=5000 --record=record.data".split()
def test_args_are_passed(self, monkeypatch): """ Test that wait_for_output passes all Arguments to the underlying ProcessMonitor """ serv = Server() serv.proc = Mock() monkeypatch.setattr(serv.proc, 'wait_for_output', Mock()) serv.wait_for_output("foo", timeout=123, count=456) serv.proc.wait_for_output.called_once_with("foo", timeout=123, count=456)
def test_record_file_valid(self): """Test if record file is valid""" def mock_method(arg): """Mocking _start_process""" return arg path = '/usr' serv = Server(path=path, record_file="record.data") serv._start_process = mock_method assert serv._run_process() == "/usr/gst-switch-srv \ --video-input-port=3000 --audio-input-port=4000 \ --controller-address=tcp:host=::,port=5000 --record=record.data".split()
def switch(self, channel, port, index): """Create Controller object and call switch method""" for _ in range(self.NUM): serv = Server(path=PATH) try: serv.run() sources = TestSources(3000) sources.new_test_video(pattern=4) sources.new_test_video(pattern=5) preview = PreviewSinks(3001) preview.run() out_file = "output-{0}.data".format(index) video_sink = VideoFileSink(PATH, 3001, out_file) time.sleep(3) controller = Controller() res = controller.switch(channel, port) print res time.sleep(3) video_sink.terminate() sources.terminate_video() preview.terminate() serv.terminate(1) finally: if serv.proc: poll = serv.proc.poll() print self.__class__ if poll == -11: print "SEGMENTATION FAULT OCCURRED" print "ERROR CODE - {0}".format(poll) serv.terminate(1) log = open('server.log') print log.read()
def test_get_preview_ports(self): """Test get_preview_ports""" for _ in range(self.NUM): serv = Server(path=PATH) try: serv.run() sources = TestSources(video_port=3000, audio_port=4000) for _ in range(self.NUM): sources.new_test_audio() sources.new_test_video() expected_result = map( tuple, [[x for x in range(3003, 3004 + self.NUM)]] * self.NUM * self.FACTOR) res = map(tuple, self.get_preview_ports()) print '\n', res, '\n' print expected_result assert set(expected_result) == set(res) sources.terminate_video() sources.terminate_audio() serv.terminate(1) finally: if serv.proc: poll = serv.proc.poll() print self.__class__ if poll == -11: print "SEGMENTATION FAULT OCCURRED" print "ERROR CODE - {0}".format(poll) serv.terminate(1) log = open('server.log') print log.read()
def test_new_record(self): """Test new_record""" for _ in range(self.NUM): serv = Server(path=PATH, record_file="test.data") try: serv.run() sources = TestSources(video_port=3000) sources.new_test_video() sources.new_test_video() curr_time = datetime.datetime.now() alt_curr_time = curr_time + datetime.timedelta(0, 1) time_str = curr_time.strftime('%Y-%m-%d %H%M%S') alt_time_str = alt_curr_time.strftime('%Y-%m-%d %H%M%S') test_filename = "test {0}.data".format(time_str) alt_test_filename = "test {0}.data".format(alt_time_str) res = self.new_record() print res sources.terminate_video() serv.terminate(1) assert ((os.path.exists(test_filename)) or (os.path.exists(alt_test_filename))) is True finally: if serv.proc: poll = serv.proc.poll() print self.__class__ if poll == -11: print "SEGMENTATION FAULT OCCURRED" print "ERROR CODE - {0}".format(poll) serv.terminate(1) log = open('server.log') print log.read()
def test_compose_ports(self): """Test get_compose_port""" res = [] expected_result = [] for i in range(self.NUM): video_port = (i + 7) * 1000 expected_result.append([video_port + 1] * self.NUM * self.FACTOR) serv = Server(path=PATH, video_port=video_port) try: serv.run() sources = TestSources(video_port=video_port) sources.new_test_video() sources.new_test_video() res.append(self.get_compose_port()) sources.terminate_video() serv.terminate(1) finally: if serv.proc: poll = serv.proc.poll() print self.__class__ if poll == -11: print "SEGMENTATION FAULT OCCURRED" print "ERROR CODE - {0}".format(poll) serv.terminate(1) log = open('server.log') print log.read() set_expected = [tuple(i) for i in expected_result] set_res = [tuple(i) for i in res] assert set(set_expected) == set(set_res)
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_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_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_controller_address_type(self): """Test when the control port is not a valid integral value""" controller_addresses = [[1, 2, 3], {1: 2, 2: 3}] for controller_address in controller_addresses: with pytest.raises(TypeError): Server(path=PATH, controller_address=controller_address)
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() == "/usr/gst-switch-srv \ --video-input-port=3000 --audio-input-port=4000 \ --controller-address=tcp:host=::,port=5000".split()
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)