"Fossies" - the Fresh Open Source Software Archive

Member "glance-20.0.1/glance/tests/functional/test_client_exceptions.py" (12 Aug 2020, 4520 Bytes) of package /linux/misc/openstack/glance-20.0.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "test_client_exceptions.py": 20.0.0_vs_20.0.1.

    1 # Copyright 2011 OpenStack Foundation
    2 # Copyright 2012 Red Hat, Inc
    3 # All Rights Reserved.
    4 #
    5 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
    6 #    not use this file except in compliance with the License. You may obtain
    7 #    a copy of the License at
    8 #
    9 #         http://www.apache.org/licenses/LICENSE-2.0
   10 #
   11 #    Unless required by applicable law or agreed to in writing, software
   12 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   13 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   14 #    License for the specific language governing permissions and limitations
   15 #    under the License.
   16 
   17 """Functional test asserting strongly typed exceptions from glance client"""
   18 import eventlet.patcher
   19 import httplib2
   20 from six.moves import http_client
   21 import webob.dec
   22 import webob.exc
   23 
   24 from glance.common import client
   25 from glance.common import exception
   26 from glance.common import wsgi
   27 from glance.tests import functional
   28 from glance.tests import utils
   29 
   30 eventlet.patcher.monkey_patch(socket=True)
   31 
   32 
   33 class ExceptionTestApp(object):
   34     """
   35     Test WSGI application which can respond with multiple kinds of HTTP
   36     status codes
   37     """
   38 
   39     @webob.dec.wsgify
   40     def __call__(self, request):
   41         path = request.path_qs
   42 
   43         if path == "/rate-limit":
   44             request.response = webob.exc.HTTPRequestEntityTooLarge()
   45 
   46         elif path == "/rate-limit-retry":
   47             request.response.retry_after = 10
   48             request.response.status = http_client.REQUEST_ENTITY_TOO_LARGE
   49 
   50         elif path == "/service-unavailable":
   51             request.response = webob.exc.HTTPServiceUnavailable()
   52 
   53         elif path == "/service-unavailable-retry":
   54             request.response.retry_after = 10
   55             request.response.status = http_client.SERVICE_UNAVAILABLE
   56 
   57         elif path == "/expectation-failed":
   58             request.response = webob.exc.HTTPExpectationFailed()
   59 
   60         elif path == "/server-error":
   61             request.response = webob.exc.HTTPServerError()
   62 
   63         elif path == "/server-traceback":
   64             raise exception.ServerError()
   65 
   66 
   67 class TestClientExceptions(functional.FunctionalTest):
   68 
   69     def setUp(self):
   70         super(TestClientExceptions, self).setUp()
   71         self.port = utils.get_unused_port()
   72         server = wsgi.Server()
   73         self.config(bind_host='127.0.0.1')
   74         self.config(workers=0)
   75         server.start(ExceptionTestApp(), self.port)
   76         self.client = client.BaseClient("127.0.0.1", self.port)
   77 
   78     def _do_test_exception(self, path, exc_type):
   79         try:
   80             self.client.do_request("GET", path)
   81             self.fail('expected %s' % exc_type)
   82         except exc_type as e:
   83             if 'retry' in path:
   84                 self.assertEqual(10, e.retry_after)
   85 
   86     def test_rate_limited(self):
   87         """
   88         Test rate limited response
   89         """
   90         self._do_test_exception('/rate-limit', exception.LimitExceeded)
   91 
   92     def test_rate_limited_retry(self):
   93         """
   94         Test rate limited response with retry
   95         """
   96         self._do_test_exception('/rate-limit-retry', exception.LimitExceeded)
   97 
   98     def test_service_unavailable(self):
   99         """
  100         Test service unavailable response
  101         """
  102         self._do_test_exception('/service-unavailable',
  103                                 exception.ServiceUnavailable)
  104 
  105     def test_service_unavailable_retry(self):
  106         """
  107         Test service unavailable response with retry
  108         """
  109         self._do_test_exception('/service-unavailable-retry',
  110                                 exception.ServiceUnavailable)
  111 
  112     def test_expectation_failed(self):
  113         """
  114         Test expectation failed response
  115         """
  116         self._do_test_exception('/expectation-failed',
  117                                 exception.UnexpectedStatus)
  118 
  119     def test_server_error(self):
  120         """
  121         Test server error response
  122         """
  123         self._do_test_exception('/server-error',
  124                                 exception.ServerError)
  125 
  126     def test_server_traceback(self):
  127         """
  128         Verify that the wsgi server does not return tracebacks to the client on
  129         500 errors (bug 1192132)
  130         """
  131         http = httplib2.Http()
  132         path = ('http://%s:%d/server-traceback' %
  133                 ('127.0.0.1', self.port))
  134         response, content = http.request(path, 'GET')
  135         self.assertNotIn(b'ServerError', content)
  136         self.assertEqual(http_client.INTERNAL_SERVER_ERROR, response.status)