def test_inner_folder_zip(self): self.zip_file.writestr('myzip/', '') self.zip_file.writestr('myzip/playbook.yml', 'some yml code') self.zip_file.writestr('myzip/Roles/', '') self.zip_file.writestr('myzip/Roles/a.yml', 'some yml code') self.zip_service.extract_all('') self.assertEqual(self.zip_file.extract.call_count, 2) self.zip_file.extract.assert_any_call( Any(lambda x: x.filename == 'playbook.yml')) self.zip_file.extract.assert_any_call( Any(lambda x: x.filename == 'Roles/a.yml'))
def test_get_expected_file_extensions_fails(self): self.script_conf.host_conf.ip = '1.2.3.4' self.script_conf.host_conf.connection_method = 'ABCD' self.executor.get_expected_file_extensions = Mock( return_value=['B', 'C']) self.downloader.return_value = ScriptFile('file.A', '') CustomScriptShell().execute_script(self.context, '', self.cancel_context) self.api_session.WriteMessageToReservationOutput.assert_called_with( Any(), Any(lambda x: 'Trying to run ".A" file via ABCD on host 1.2.3.4' in x))
def test_connection_failure(self, mocker): """Test the connection fails to be established.""" api_mock = mocker.Mock() api_mock.authenticate.return_value = Token(True, 'abc', {}, 2000000, 1000000) sse_mock = mocker.Mock(spec=SplitSSEClient) sse_constructor_mock = mocker.Mock() sse_constructor_mock.return_value = sse_mock timer_mock = mocker.Mock() mocker.patch('splitio.push.manager.Timer', new=timer_mock) mocker.patch('splitio.push.manager.SplitSSEClient', new=sse_constructor_mock) feedback_loop = Queue() manager = PushManager(api_mock, mocker.Mock(), feedback_loop, mocker.Mock()) def new_start(*args, **kwargs): # pylint: disable=unused-argument """splitsse.start mock.""" thread = Thread(target=manager._handle_connection_end) thread.setDaemon(True) thread.start() return False sse_mock.start.side_effect = new_start manager.start() assert feedback_loop.get() == Status.PUSH_RETRYABLE_ERROR assert timer_mock.mock_calls == [mocker.call(0, Any())]
def test_connection_success(self, mocker): """Test the initial status is ok and reset() works as expected.""" api_mock = mocker.Mock() api_mock.authenticate.return_value = Token(True, 'abc', {}, 2000000, 1000000) sse_mock = mocker.Mock(spec=SplitSSEClient) sse_constructor_mock = mocker.Mock() sse_constructor_mock.return_value = sse_mock timer_mock = mocker.Mock() mocker.patch('splitio.push.manager.Timer', new=timer_mock) mocker.patch('splitio.push.manager.SplitSSEClient', new=sse_constructor_mock) feedback_loop = Queue() manager = PushManager(api_mock, mocker.Mock(), feedback_loop) def new_start(*args, **kwargs): # pylint: disable=unused-argument """splitsse.start mock.""" thread = Thread(target=manager._handle_connection_ready) thread.setDaemon(True) thread.start() return True sse_mock.start.side_effect = new_start manager.start() assert feedback_loop.get() == Status.PUSH_SUBSYSTEM_UP assert timer_mock.mock_calls == [ mocker.call(0, Any()), mocker.call().cancel(), mocker.call(1000000 - _TOKEN_REFRESH_GRACE_PERIOD, manager._token_refresh), mocker.call().setName('TokenRefresh'), mocker.call().start() ]
def test_execute_is_called(self): CustomScriptShell().execute_script(self.context, '', self.cancel_context) self.selector_get.assert_called_with(self.script_conf.host_conf, Any(), self.cancel_sampler) self.executor.execute.assert_called_once()
def test_copy_script_success(self): transport = Mock() self.session.get_transport.return_value = transport self.executor.copy_script('tmp123', ScriptFile('script1', 'some script code')) self.scp_ctor.assert_called_once_with(transport, 'tmp123') self.scp.send.assert_called_once_with( Any(lambda x: x.getvalue() == 'some script code'), 'script1', '0601', 16) self.scp.close.assert_called_once()
def test_download_script_with_auth(self): self.script_conf.script_repo.url = 'some url' self.script_conf.script_repo.username = '******' self.script_conf.script_repo.password = '******' CustomScriptShell().execute_script(self.context, '', self.cancel_context) self.downloader.assert_called_with( 'some url', Any(lambda x: x.username == 'admin' and x.password == '1234'))
def test_push_disabled(self, mocker): """Test the initial status is ok and reset() works as expected.""" api_mock = mocker.Mock() api_mock.authenticate.return_value = Token(False, 'abc', {}, 1, 2) sse_mock = mocker.Mock(spec=SplitSSEClient) sse_constructor_mock = mocker.Mock() sse_constructor_mock.return_value = sse_mock timer_mock = mocker.Mock() mocker.patch('splitio.push.manager.Timer', new=timer_mock) mocker.patch('splitio.push.manager.SplitSSEClient', new=sse_constructor_mock) feedback_loop = Queue() manager = PushManager(api_mock, mocker.Mock(), feedback_loop) manager.start() assert feedback_loop.get() == Status.PUSH_NONRETRYABLE_ERROR assert timer_mock.mock_calls == [mocker.call(0, Any())] assert sse_mock.mock_calls == []
def test_auth_apiexception(self, mocker): """Test the initial status is ok and reset() works as expected.""" api_mock = mocker.Mock() api_mock.authenticate.side_effect = APIException('something') sse_mock = mocker.Mock(spec=SplitSSEClient) sse_constructor_mock = mocker.Mock() sse_constructor_mock.return_value = sse_mock timer_mock = mocker.Mock() mocker.patch('splitio.push.manager.Timer', new=timer_mock) mocker.patch('splitio.push.manager.SplitSSEClient', new=sse_constructor_mock) feedback_loop = Queue() manager = PushManager(api_mock, mocker.Mock(), feedback_loop) manager.start() assert feedback_loop.get() == Status.PUSH_RETRYABLE_ERROR assert timer_mock.mock_calls == [mocker.call(0, Any())] assert sse_mock.mock_calls == []
def test_segment_change(self, mocker): """Test update-type messages are properly forwarded to the processor.""" sse_event = SSEEvent('1', EventType.MESSAGE, '', '{}') update_message = SegmentChangeUpdate('chan', 123, 456, 'some_segment') parse_event_mock = mocker.Mock(spec=parse_incoming_event) parse_event_mock.return_value = update_message mocker.patch('splitio.push.manager.parse_incoming_event', new=parse_event_mock) processor_mock = mocker.Mock(spec=MessageProcessor) mocker.patch('splitio.push.manager.MessageProcessor', new=processor_mock) manager = PushManager(mocker.Mock(), mocker.Mock(), mocker.Mock()) manager._event_handler(sse_event) assert parse_event_mock.mock_calls == [mocker.call(sse_event)] assert processor_mock.mock_calls == [ mocker.call(Any()), mocker.call().handle(update_message) ]