def test_report_state_newly_disconnected(self, get_by_id, get_by_args): get_by_args.side_effect = exception.NotFound() get_by_id.side_effect = db_exc.DBConnectionError() with mock.patch.object(objects.service, 'db') as mock_db: mock_db.service_create.return_value = self.service_ref serv = service.Service( self.host, self.binary, self.topic, 'cinder.tests.unit.test_service.FakeManager' ) serv.start() serv.report_state() self.assertTrue(serv.model_disconnected) self.assertFalse(mock_db.service_update.called)
def _raise_operational_errors_directly_filter(operational_error, match, engine_name, is_disconnect): """Filter for all remaining OperationalError classes and apply. Filter for all remaining OperationalError classes and apply special rules. """ if is_disconnect: # operational errors that represent disconnect # should be wrapped raise exception.DBConnectionError(operational_error) else: # NOTE(comstud): A lot of code is checking for OperationalError # so let's not wrap it for now. raise operational_error
def _api_raise(self, *args, **kwargs): """Simulate raising a database-has-gone-away error This method creates a fake OperationalError with an ID matching a valid MySQL "database has gone away" situation. It also decrements the error_counter so that we can artificially keep track of how many times this function is called by the wrapper. When error_counter reaches zero, this function returns True, simulating the database becoming available again and the query succeeding. """ if self.error_counter > 0: self.error_counter -= 1 orig = sqla.exc.DBAPIError(False, False, False) orig.args = [2006, 'Test raise operational error'] e = exception.DBConnectionError(orig) raise e else: return True
def test_report_state_newly_disconnected(self): service_ref = { 'host': self.host, 'binary': self.binary, 'topic': self.topic, 'report_count': 0, 'id': 1 } with mock.patch.object(service, 'db') as mock_db: mock_db.service_get_by_args.side_effect = exception.NotFound() mock_db.service_create.return_value = service_ref mock_db.service_get.side_effect = db_exc.DBConnectionError() serv = service.Service( self.host, self.binary, self.topic, 'karbor.tests.unit.test_service.FakeManager') serv.start() serv.report_state() self.assertTrue(serv.model_disconnected) self.assertFalse(mock_db.service_update.called)
def test_dbconnection_error_caught(self): e = db_exc.DBConnectionError() self.assertIsNone(self._decorated_function(1, e))
def _is_db_connection_error(operational_error, match, engine_name, is_disconnect): """Detect the exception as indicating a recoverable error on connect.""" raise exception.DBConnectionError(operational_error)