def _cs_request(self, url, method, **kwargs): auth_attempts = 0 attempts = 0 backoff = 1 while True: attempts += 1 if not self.management_url or not self.auth_token: self.authenticate() kwargs.setdefault('headers', {})['X-Auth-Token'] = self.auth_token kwargs['headers']['wrs-header'] = 'true' if self.projectid: kwargs['headers']['X-Auth-Project-Id'] = self.projectid try: if not url.startswith(self.management_url): url = self.management_url + url resp, body = self.request(url, method, **kwargs) return resp, body except exceptions.BadRequest as e: if attempts > self.retries: raise except exceptions.Unauthorized: if auth_attempts > 0: raise self._logger.debug("Unauthorized, reauthenticating.") self.management_url = self.auth_token = None # First reauth. Discount this attempt. attempts -= 1 auth_attempts += 1 continue except exceptions.OverLimit as overlim: if attempts > self.retries or overlim.retry_after < 1: raise msg = "Retrying after %s seconds." % overlim.retry_after self._logger.debug(msg) sleep(overlim.retry_after) continue except exceptions.ClientException as e: if attempts > self.retries: raise if 500 <= e.code <= 599: pass else: raise except requests.exceptions.ConnectionError as e: self._logger.debug("Connection error: %s" % e) if attempts > self.retries: msg = 'Unable to establish connection: %s' % e raise exceptions.ConnectionError(msg) except requests.exceptions.Timeout as e: self._logger.debug("Timeout error: %s" % e) if attempts > self.retries: raise self._logger.debug( "Failed attempt(%s of %s), retrying in %s seconds" % (attempts, self.retries, backoff)) sleep(backoff) backoff *= 2
def test_get_snapshot_failed(self): snapshot_id = 'snapshot_id' cinder.cinderclient(self.ctx).AndRaise(cinder_exception.NotFound('')) cinder.cinderclient(self.ctx).AndRaise( cinder_exception.ConnectionError('')) self.mox.ReplayAll() self.assertRaises(exception.SnapshotNotFound, self.api.get_snapshot, self.ctx, snapshot_id) self.assertRaises(exception.CinderConnectionFailed, self.api.get_snapshot, self.ctx, snapshot_id)
def test_get_failed(self): volume_id = 'volume_id' cinder.cinderclient(self.ctx).AndRaise(cinder_exception.NotFound('')) cinder.cinderclient(self.ctx).AndRaise(cinder_exception.BadRequest('')) cinder.cinderclient(self.ctx).AndRaise( cinder_exception.ConnectionError('')) self.mox.ReplayAll() self.assertRaises(exception.VolumeNotFound, self.api.get, self.ctx, volume_id) self.assertRaises(exception.InvalidInput, self.api.get, self.ctx, volume_id) self.assertRaises(exception.CinderConnectionFailed, self.api.get, self.ctx, volume_id)
def _cs_request(self, url, method, **kwargs): auth_attempts = 0 attempts = 0 backoff = 1 while True: attempts += 1 if not self.management_url or not self.auth_token: self.authenticate() kwargs.setdefault('headers', {})['X-Auth-Token'] = self.auth_token if self.projectid: kwargs['headers']['X-Auth-Project-Id'] = self.projectid try: resp, body = self.request(self.management_url + url, method, **kwargs) return resp, body except exceptions.BadRequest as e: if attempts > self.retries: raise except exceptions.Unauthorized: if auth_attempts > 0: raise self._logger.debug("Unauthorized, reauthenticating.") self.management_url = self.auth_token = None # First reauth. Discount this attempt. attempts -= 1 auth_attempts += 1 continue except exceptions.ClientException as e: if attempts > self.retries: raise if 500 <= e.code <= 599: pass else: raise except requests.exceptions.ConnectionError as e: # Catch a connection refused from requests.request self._logger.debug("Connection refused: %s" % e) msg = 'Unable to establish connection: %s' % e raise exceptions.ConnectionError(msg) self._logger.debug( "Failed attempt(%s of %s), retrying in %s seconds" % (attempts, self.retries, backoff)) sleep(backoff) backoff *= 2
def test_get_snapshot_connection_failed(self, mock_cinderclient): mock_cinderclient.return_value.volume_snapshots.get.side_effect = ( cinder_exception.ConnectionError('')) self.assertRaises(exception.CinderConnectionFailed, self.api.get_snapshot, self.ctx, 'snapshot_id')