def test_retries_on_timeout_if_wanted_and_returns_first_call(self): import elasticsearch failed_return_value = {"weight": 1, "unit": "ops", "success": False} delegate = mock.Mock(side_effect=[ elasticsearch.ConnectionError("N/A", "no route to host"), failed_return_value ]) es = None params = { "retries": 3, "retry-wait-period": 0.01, "retry-on-timeout": True, "retry-on-error": False } retrier = runner.Retry(delegate) result = retrier(es, params) self.assertEqual(failed_return_value, result) delegate.assert_has_calls([ # has returned a connection error mock.call(es, params), # has returned normally mock.call(es, params) ])
def checkConnection(self): try: self.es.info() except elasticsearch.ConnectionError: raise elasticsearch.ConnectionError( "Cannot find Elastic Search process") except Exception, e: raise EnvironmentError("Unknown Exception: %s" % e)
def test_ssl_error(self, es): es.cluster.health.side_effect = elasticsearch.ConnectionError( "N/A", "[SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:719)", urllib3.exceptions.SSLError("[SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:719)"), ) with pytest.raises(exceptions.SystemSetupError, match="Could not connect to cluster via https. Is this an https endpoint?"): client.wait_for_rest_layer(es, max_attempts=3)
def _check_status(self): """ If we're in a known bad state, try to reconnect """ if not (self.status or self._attempt_reconnect()): raise elasticsearch.ConnectionError( "Could not connect to Elasticsearch") return self.status
def upload_single_file(self, index, mapping, filename, delimiter=":", retries=10): """ Upload a single CSV file to the Server. :param filename: File to upload. :type filename: str :param delimiter: Delimiter character used in the CSV, defaults to ":" :type delimiter: str :param retries: Number of retries before giving up. :type retries: int :param index: The index into which upload the data :type index: str """ if index and mapping: index = index.lower() if self.__server_connection.indices.exists( index) and self.__server_connection.indices.get_mapping( index, mapping): time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") fails = { "1": ["connection", []], "2": ["malformed", []], "3": ["other", []] } with open(filename) as csvfile: data = csv.reader(csvfile, delimiter=delimiter) first = True for row in data: if first: first = False else: success = self.__upload_line( row, index, mapping, retries) if success != 0: fails[str(success)][1].append(row) for key, value in fails.iteritems(): if len(value[1]) > 0: fail_file = os.path.join( self.fail_directory, "Fail-" + filename.split("/")[-1] + "-" + value[0] + " " + time) if len(value[1]) > 0: with open(fail_file, "w") as f: for row in value[1]: f.write(row) else: raise elasticsearch.ConnectionError( "The selected index or mapping doesn't exist!") else: raise ValueError("The index or the mapping to use wasn't defined!")
def test_ssl_error(self, es): import elasticsearch import urllib3.exceptions es.info.side_effect = elasticsearch.ConnectionError("N/A", "[SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:719)", urllib3.exceptions.SSLError( "[SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:719)")) with self.assertRaisesRegex(expected_exception=exceptions.LaunchError, expected_regex="Could not connect to cluster via https. Is this a https endpoint?"): launcher.wait_for_rest_layer(es, max_attempts=3)
def test_is_transparent_on_exception_when_no_retries(self): import elasticsearch delegate = mock.Mock(side_effect=elasticsearch.ConnectionError( "N/A", "no route to host")) es = None params = { # no retries } retrier = runner.Retry(delegate) with self.assertRaises(elasticsearch.ConnectionError): retrier(es, params) delegate.assert_called_once_with(es, params)
def test_execute_single_with_connection_error(self): import elasticsearch es = None params = None # ES client uses pseudo-status "N/A" in this case... runner = mock.Mock(side_effect=elasticsearch.ConnectionError("N/A", "no route to host")) total_ops, total_ops_unit, request_meta_data = driver.execute_single(self.context_managed(runner), es, params) self.assertEqual(0, total_ops) self.assertEqual("ops", total_ops_unit) self.assertEqual({ # Look ma: No http-status! "error-description": "no route to host", "success": False }, request_meta_data)
def test_retries_on_timeout_if_wanted_and_raises_if_no_recovery(self): import elasticsearch delegate = mock.Mock(side_effect=elasticsearch.ConnectionError( "N/A", "no route to host")) es = None params = { "retries": 3, "retry-wait-period": 0.01, "retry-on-timeout": True, "retry-on-error": True } retrier = runner.Retry(delegate) with self.assertRaises(elasticsearch.ConnectionError): retrier(es, params) delegate.assert_has_calls([ mock.call(es, params), mock.call(es, params), mock.call(es, params) ])
def test_retries_mixed_timeout_and_application_errors(self): import elasticsearch connection_error = elasticsearch.ConnectionError( "N/A", "no route to host") failed_return_value = {"weight": 1, "unit": "ops", "success": False} success_return_value = {"weight": 1, "unit": "ops", "success": False} delegate = mock.Mock(side_effect=[ connection_error, failed_return_value, connection_error, connection_error, failed_return_value, success_return_value ]) es = None params = { # we try exactly as often as there are errors to also test the semantics of "retry". "retries": 5, "retry-wait-period": 0.01, "retry-on-timeout": True, "retry-on-error": True } retrier = runner.Retry(delegate) result = retrier(es, params) self.assertEqual(success_return_value, result) delegate.assert_has_calls([ # connection error mock.call(es, params), # application error mock.call(es, params), # connection error mock.call(es, params), # connection error mock.call(es, params), # application error mock.call(es, params), # success mock.call(es, params) ])
def info(self): if self.client_options.get("raise-error-on-info", False): import elasticsearch raise elasticsearch.ConnectionError("Unittest error") return self._info
def raiser(*args, **kwargs): raise elasticsearch.ConnectionError()