def set_api_version_request(self, url): """Set API version request based on the request header information. Microversions starts with /v3, so if a client sends a request for version 1.0 or 2.0 with the /v3 endpoint, throw an exception. Sending a header with any microversion to a /v1 or /v2 endpoint will be ignored. Note that a microversion must be set for the legacy endpoints. This will appear as 1.0 and 2.0 for /v1 and /v2. """ if API_VERSION_REQUEST_HEADER in self.headers and 'v3' in url: hdr_string = self.headers[API_VERSION_REQUEST_HEADER] # 'latest' is a special keyword which is equivalent to requesting # the maximum version of the API supported hdr_string_list = hdr_string.split(",") volume_version = None for hdr in hdr_string_list: if VOLUME_SERVICE in hdr: service, volume_version = hdr.split() break if not volume_version: raise exception.VersionNotFoundForAPIMethod( version=volume_version) if volume_version == 'latest': self.api_version_request = api_version.max_api_version() else: self.api_version_request = api_version.APIVersionRequest( volume_version) # Check that the version requested is within the global # minimum/maximum of supported API versions if not self.api_version_request.matches( api_version.min_api_version(), api_version.max_api_version()): raise exception.InvalidGlobalAPIVersion( req_ver=self.api_version_request.get_string(), min_ver=api_version.min_api_version().get_string(), max_ver=api_version.max_api_version().get_string()) else: if 'v1' in url: self.api_version_request = api_version.legacy_api_version1() elif 'v2' in url: self.api_version_request = api_version.legacy_api_version2() else: self.api_version_request = api_version.APIVersionRequest( api_version._MIN_API_VERSION)
def test_get_all_messages_paging(self): self.create_message_for_tests() # first request of this test url = ('/v3/fake/messages?limit=2') req = fakes.HTTPRequest.blank(url) req.method = 'GET' req.content_type = 'application/json' req.headers = {version_header_name: 'volume 3.5'} req.api_version_request = api_version.max_api_version() req.environ['cinder.context'].is_admin = True res = self.controller.index(req) self.assertEqual(2, len(res['messages'])) next_link = ('http://localhost/v3/%s/messages?limit=' '2&marker=%s') % (fake_constants.PROJECT_ID, res['messages'][1]['id']) self.assertEqual(next_link, res['messages_links'][0]['href']) # Second request in this test # Test for second page using marker (res['messages][0]['id']) # values fetched in first request with limit 2 in this test url = ('/v3/fake/messages?limit=1&marker=%s') % ( res['messages'][0]['id']) req = fakes.HTTPRequest.blank(url) req.method = 'GET' req.content_type = 'application/json' req.headers = {version_header_name: 'volume 3.5'} req.api_version_request = api_version.max_api_version() req.environ['cinder.context'].is_admin = True result = self.controller.index(req) self.assertEqual(1, len(result['messages'])) # checking second message of first request in this test with first # message of second request. (to test paging mechanism) self.assertEqual(res['messages'][1], result['messages'][0])
def test_get_all_messages_with_limit(self): self.create_message_for_tests() url = '/v3/messages?limit=1' req = fakes.HTTPRequest.blank(url) req.method = 'GET' req.content_type = 'application/json' req.headers = {version_header_name: 'volume 3.5'} req.api_version_request = api_version.max_api_version() req.environ['cinder.context'].is_admin = True res = self.controller.index(req) self.assertEqual(1, len(res['messages'])) url = '/v3/messages?limit=3' req = fakes.HTTPRequest.blank(url) req.method = 'GET' req.content_type = 'application/json' req.headers = {version_header_name: 'volume 3.5'} req.api_version_request = api_version.max_api_version() req.environ['cinder.context'].is_admin = True res = self.controller.index(req) self.assertEqual(3, len(res['messages']))
def test_get_all_messages_with_limit(self): self.create_message_for_tests() url = ('/v3/messages?limit=1') req = fakes.HTTPRequest.blank(url) req.method = 'GET' req.content_type = 'application/json' req.headers = {version_header_name: 'volume 3.5'} req.api_version_request = api_version.max_api_version() req.environ['cinder.context'].is_admin = True res = self.controller.index(req) self.assertEqual(1, len(res['messages'])) url = ('/v3/messages?limit=3') req = fakes.HTTPRequest.blank(url) req.method = 'GET' req.content_type = 'application/json' req.headers = {version_header_name: 'volume 3.5'} req.api_version_request = api_version.max_api_version() req.environ['cinder.context'].is_admin = True res = self.controller.index(req) self.assertEqual(3, len(res['messages']))
def test_check_volume_filters_strict_called(self): with mock.patch.object(vol_get, 'check_volume_filters') as volume_get: req = fakes.HTTPRequest.blank('/v3/volumes?bootable=True') req.method = 'GET' req.content_type = 'application/json' req.headers = {version_header_name: 'volume 3.2'} req.environ['cinder.context'].is_admin = True req.api_version_request = api_version.max_api_version() self.override_config('query_volume_filters', 'bootable') self.controller.index(req) filters = req.params.copy() volume_get.assert_called_with(filters, True)
def test_check_volume_filters_strict_called(self): with mock.patch.object(vol_get, 'check_volume_filters') as volume_get: req = fakes.HTTPRequest.blank('/v3/volumes?bootable=True') req.method = 'GET' req.content_type = 'application/json' req.headers = {version_header_name: 'volume 3.2'} req.environ['cinder.context'].is_admin = True req.api_version_request = api_version.max_api_version() self.override_config('query_volume_filters', 'bootable') self.controller.index(req) filters = req.params.copy() volume_get.assert_called_with(filters, True)
def test_max_api_version(self): self.assertEqual( api_version_request.APIVersionRequest( api_version_request._MAX_API_VERSION), api_version_request.max_api_version())
def test_max_api_version(self): self.assertEqual( api_version_request.APIVersionRequest(api_version_request._MAX_API_VERSION), api_version_request.max_api_version(), )