def test_ignores_error_outside_API(self): middleware = APIRPCErrorsMiddleware() non_api_request = factory.make_fake_request("/middleware/api/hello") exception_class = random.choice( (NoConnectionsAvailable, PowerActionAlreadyInProgress)) exception = exception_class(factory.make_string()) self.assertIsNone( middleware.process_exception(non_api_request, exception))
def test_ignores_non_rpc_errors(self): middleware = APIRPCErrorsMiddleware() request = factory.make_fake_request( "/api/2.0/" + factory.make_string(), 'POST') exception = ZeroDivisionError( "You may think it's a long walk down the street to the chemist " "but that's just peanuts to space!") response = middleware.process_exception(request, exception) self.assertIsNone(response)
def test_handles_TimeoutError(self): middleware = APIRPCErrorsMiddleware() request = factory.make_fake_request( "/api/2.0/" + factory.make_string(), 'POST') error_message = "No thanks, I'm trying to give them up." error = TimeoutError(error_message) response = middleware.process_exception(request, error) self.assertEqual((http.client.GATEWAY_TIMEOUT, error_message), (response.status_code, response.content.decode(settings.DEFAULT_CHARSET)))
def test_503_response_includes_retry_after_header_by_default(self): middleware = APIRPCErrorsMiddleware() request = factory.make_fake_request( "/api/2.0/" + factory.make_string(), 'POST') error = NoConnectionsAvailable(factory.make_name()) response = middleware.process_exception(request, error) self.assertEqual(( http.client.SERVICE_UNAVAILABLE, '%s' % middleware_module.RETRY_AFTER_SERVICE_UNAVAILABLE, ), (response.status_code, response['Retry-after']))
def test_handles_error_on_API(self): middleware = APIRPCErrorsMiddleware() api_request = factory.make_fake_request("/api/2.0/hello") error_message = factory.make_string() exception_class = random.choice( (NoConnectionsAvailable, PowerActionAlreadyInProgress)) exception = exception_class(error_message) response = middleware.process_exception(api_request, exception) self.assertEqual( (middleware.handled_exceptions[exception_class], error_message), (response.status_code, response.content.decode(settings.DEFAULT_CHARSET)))
def test_power_action_already_in_progress_returned_as_503(self): middleware = APIRPCErrorsMiddleware() request = factory.make_fake_request( "/api/2.0/" + factory.make_string(), 'POST') error_message = ( "Unable to execute power action: another action is already in " "progress for node %s" % factory.make_name('node')) error = PowerActionAlreadyInProgress(error_message) response = middleware.process_exception(request, error) self.assertEqual((http.client.SERVICE_UNAVAILABLE, error_message), (response.status_code, response.content.decode(settings.DEFAULT_CHARSET)))
def test_no_connections_available_returned_as_503(self): middleware = APIRPCErrorsMiddleware() request = factory.make_fake_request( "/api/2.0/" + factory.make_string(), 'POST') error_message = ( "Unable to connect to cluster '%s'; no connections available" % factory.make_name('cluster')) error = NoConnectionsAvailable(error_message) response = middleware.process_exception(request, error) self.assertEqual((http.client.SERVICE_UNAVAILABLE, error_message), (response.status_code, response.content.decode(settings.DEFAULT_CHARSET)))