def test_aggregate_key(self): # Aggregate 'images' response = json.loads(services.aggregate(IMAGES, 'images')) self.assertEqual(IMAGES_IN_SAMPLE, len(response['images'])) # Aggregate 'volumes' response = json.loads(services.aggregate(VOLUMES, 'volumes')) self.assertEqual(VOLUMES_IN_SAMPLE, len(response['volumes']))
def test_aggregate_limit(self): params = { 'limit': 1 } response = json.loads(services.aggregate(IMAGES, 'images', params, IMAGE_PATH)) self.assertEqual(1, len(response['images']))
def test_sort_images_date_ascending_pagination(self): """Sort images by last update, ascending, skip the first one.""" params = { 'sort': 'updated_at:asc', 'limit': 1, 'marker': EARLIEST_IMAGE } response = json.loads(services.aggregate(IMAGES, 'images', params, IMAGE_PATH)) # Ensure we skipped the first one self.assertEqual(response['images'][0]['id'], SECOND_EARLIEST_IMAGE) self.assertEqual(1, len(response['images'])) # Next link self.assertEqual( Url(response['next']), Url(self._prepare_url( IMAGE_PATH, self._prepare_params(params, marker=SECOND_EARLIEST_IMAGE) )) ) # Start link self.assertEqual( Url(response['start']), Url(self._prepare_url( IMAGE_PATH, self._prepare_params(params) )) )
def test_sort_images_date_ascending_pagination(self): """Sort images by last update, ascending, skip the first one.""" params = { 'sort': 'updated_at:asc', 'limit': 1, 'marker': EARLIEST_IMAGE } response = json.loads( services.aggregate(IMAGES, 'images', params, IMAGE_PATH)) # Ensure we skipped the first one self.assertEqual(response['images'][0]['id'], SECOND_EARLIEST_IMAGE) self.assertEqual(1, len(response['images'])) # Next link self.assertEqual( Url(response['next']), Url( self._prepare_url( IMAGE_PATH, self._prepare_params(params, marker=SECOND_EARLIEST_IMAGE)))) # Start link self.assertEqual( Url(response['start']), Url(self._prepare_url(IMAGE_PATH, self._prepare_params(params))))
def test_aggregate_limit(self): params = {'limit': 1} response = json.loads( services.aggregate(IMAGES, 'images', 'image', params=params, path=IMAGE_PATH)) self.assertEqual(1, len(response['images'])) response = json.loads( services.aggregate(VOLUMES, 'volumes', 'volume', params=params, path=IMAGE_PATH)) self.assertEqual(1, len(response['volumes']))
def test_aggregate_sort_images_ascending(self): """Sort images by smallest size, ascending.""" params = { 'sort': 'size:asc' } response = json.loads(services.aggregate(IMAGES, 'images', params, IMAGE_PATH)) self.assertEqual(response['images'][0]['id'], SMALLEST_IMAGE)
def test_aggregate_sort_images_ascending(self): """Sort images by smallest size, ascending.""" params = {'sort': 'size:asc'} response = json.loads( services.aggregate(IMAGES, 'images', 'image', params=params, path=IMAGE_PATH)) self.assertEqual(response['images'][0]['id'], SMALLEST_IMAGE)
def test_remove_details_v2(self): """Test aggregation on volumes v2 with strip_details = True""" response = json.loads( services.aggregate(VOLUMES, 'volumes', 'volume', version='v2', strip_details=True)) for v in response['volumes']: self.assertEqual(set(v.keys()), {'id', 'links', 'name'})
def _forward(self): if self.fallback_to_local: return self._local_forward() responses = {} errors = collections.defaultdict(lambda: []) for sp in self.enabled_sps: if sp == 'default': r = self._do_request_on('default') if 200 <= r.status_code < 300: responses['default'] = r if not self.aggregate: return self._finalize(r) else: errors[r.status_code].append(r) else: for p in auth.get_projects_at_sp(sp, self.details.token): r = self._do_request_on(sp, p) if 200 <= r.status_code < 300: responses[(sp, p)] = r if not self.aggregate: return self._finalize(r) else: errors[r.status_code].append(r) # NOTE(knikolla): If we haven't returned yet, either we're aggregating # or there are errors. if not errors: # TODO(knikolla): Plug this into _finalize to have a common path # for everything that is returned. return flask.Response(services.aggregate( responses, self.details.action[0], self.details.service, version=self.details.version, params=self.details.args, path=request.base_url, strip_details=self.strip_details), 200, content_type='application/json') if six.viewkeys(errors) == {404}: return self._finalize(errors[404][0]) else: utils.safe_pop(errors, 404) if len(errors.keys()) == 1: return self._finalize(list(errors.values())[0][0]) # TODO(jfreud): log return flask.Response("Something strange happened.\n", 500)
def test_marker_last(self): """Test marker without limit, nothing to return.""" params = {'sort': 'updated_at:asc', 'marker': LATEST_IMAGE} response = json.loads( services.aggregate(IMAGES, 'images', params, IMAGE_PATH)) # Ensure we skipped the first one self.assertEqual(0, len(response['images'])) # Start link self.assertEqual( Url(response['start']), Url(self._prepare_url(IMAGE_PATH, self._prepare_params(params))))
def test_marker_without_limit(self): """Test marker without limit.""" params = {'sort': 'updated_at:asc', 'marker': EARLIEST_IMAGE} response = json.loads( services.aggregate(IMAGES, 'images', params, IMAGE_PATH)) # Ensure we skipped the first one self.assertEqual(response['images'][0]['id'], SECOND_EARLIEST_IMAGE) self.assertEqual(IMAGES_IN_SAMPLE - 1, len(response['images'])) # Start link self.assertEqual( Url(response['start']), Url(self._prepare_url(IMAGE_PATH, self._prepare_params(params))))
def test_remove_details_v1(self): """Test aggregation on volumes v2 with strip_details = True""" response = json.loads( services.aggregate(VOLUMES_V1, 'volumes', 'volume', version='v1', strip_details=True)) for v in response['volumes']: self.assertEqual( set(v.keys()), { 'status', 'attachments', 'availability_zone', 'encrypted', 'source_volid', 'display_description', 'snapshot_id', 'id', 'size', 'display_name', 'bootable', 'created_at', 'multiattach', 'volume_type', 'metadata' })
def test_aggregate_sort_images_limit(self): """Sort images by smallest size, ascending, limit to 1, alt format.""" params = {'sort_key': 'size', 'sort_dir': 'asc', 'limit': 1} response = json.loads( services.aggregate(IMAGES, 'images', params, IMAGE_PATH)) # Ensure the smallest is first and there is only 1 entry. self.assertEqual(response['images'][0]['id'], SMALLEST_IMAGE) self.assertEqual(1, len(response['images'])) # Ensure the 'next' url is correct. self.assertEqual( Url(response['next']), Url( self._prepare_url( IMAGE_PATH, self._prepare_params(params, marker=SMALLEST_IMAGE))))
def test_sort_images_date_limit_descending(self): """Sort images by last update, descending, limit 1.""" params = {'sort': 'updated_at:desc', 'limit': 1} response = json.loads( services.aggregate(IMAGES, 'images', params, IMAGE_PATH)) # Check the id and size self.assertEqual(response['images'][0]['id'], LATEST_IMAGE) self.assertEqual(1, len(response['images'])) # Check the next link self.assertEqual( Url(response['next']), Url( self._prepare_url( IMAGE_PATH, self._prepare_params(params, marker=LATEST_IMAGE))))
def test_sort_images_date_limit_ascending(self): """Sort images by last update, ascending, limit to 2.""" params = {'sort': 'updated_at:asc', 'limit': 2} response = json.loads( services.aggregate(IMAGES, 'images', params, IMAGE_PATH)) # Check the first and second are the correct ids. self.assertEqual(response['images'][0]['id'], EARLIEST_IMAGE) self.assertEqual(response['images'][1]['id'], SECOND_EARLIEST_IMAGE) self.assertEqual(2, len(response['images'])) # Check the next link self.assertEqual( Url(response['next']), Url( self._prepare_url( IMAGE_PATH, self._prepare_params(params, marker=SECOND_EARLIEST_IMAGE))))
def test_marker_last(self): """Test marker without limit, nothing to return.""" params = { 'sort': 'updated_at:asc', 'marker': LATEST_IMAGE } response = json.loads(services.aggregate(IMAGES, 'images', params, IMAGE_PATH)) # Ensure we skipped the first one self.assertEqual(0, len(response['images'])) # Start link self.assertEqual( Url(response['start']), Url(self._prepare_url( IMAGE_PATH, self._prepare_params(params) )) )
def test_sort_images_date_limit_descending(self): """Sort images by last update, descending, limit 1.""" params = { 'sort': 'updated_at:desc', 'limit': 1 } response = json.loads(services.aggregate(IMAGES, 'images', params, IMAGE_PATH)) # Check the id and size self.assertEqual(response['images'][0]['id'], LATEST_IMAGE) self.assertEqual(1, len(response['images'])) # Check the next link self.assertEqual( Url(response['next']), Url(self._prepare_url( IMAGE_PATH, self._prepare_params(params, marker=LATEST_IMAGE) )) )
def _aggregate_forward(self): if not CONF.proxy.aggregation: return self._local_forward() responses = {} for sp in CONF.proxy.service_providers: if sp == 'default': responses['default'] = self._do_request_on('default') else: for proj in auth.get_projects_at_sp(sp, self.local_token): responses[(sp, proj)] = self._do_request_on(sp, proj) return flask.Response( services.aggregate(responses, self.action[0], request.args.to_dict(), request.base_url, detailed=self.detailed), 200, content_type=responses['default'].headers['content-type'])
def test_aggregate_sort_images_limit(self): """Sort images by smallest size, ascending, limit to 1, alt format.""" params = { 'sort_key': 'size', 'sort_dir': 'asc', 'limit': 1 } response = json.loads(services.aggregate(IMAGES, 'images', params, IMAGE_PATH)) # Ensure the smallest is first and there is only 1 entry. self.assertEqual(response['images'][0]['id'], SMALLEST_IMAGE) self.assertEqual(1, len(response['images'])) # Ensure the 'next' url is correct. self.assertEqual( Url(response['next']), Url(self._prepare_url( IMAGE_PATH, self._prepare_params(params, marker=SMALLEST_IMAGE) )) )
def test_sort_images_date_limit_ascending(self): """Sort images by last update, ascending, limit to 2.""" params = { 'sort': 'updated_at:asc', 'limit': 2 } response = json.loads(services.aggregate(IMAGES, 'images', params, IMAGE_PATH)) # Check the first and second are the correct ids. self.assertEqual(response['images'][0]['id'], EARLIEST_IMAGE) self.assertEqual(response['images'][1]['id'], SECOND_EARLIEST_IMAGE) self.assertEqual(2, len(response['images'])) # Check the next link self.assertEqual( Url(response['next']), Url(self._prepare_url( IMAGE_PATH, self._prepare_params(params, marker=SECOND_EARLIEST_IMAGE) )) )
def _aggregate_forward(self): if not CONF.proxy.aggregation: return self._local_forward() responses = {} for sp in CONF.proxy.service_providers: if sp == 'default': responses['default'] = self._do_request_on('default') else: for proj in auth.get_projects_at_sp(sp, self.local_token): responses[(sp, proj)] = self._do_request_on(sp, proj) return flask.Response( services.aggregate(responses, self.action[0], request.args.to_dict(), request.base_url, detailed=self.detailed), 200, content_type=responses['default'].headers['content-type'] )
def test_marker_without_limit(self): """Test marker without limit.""" params = { 'sort': 'updated_at:asc', 'marker': EARLIEST_IMAGE } response = json.loads(services.aggregate(IMAGES, 'images', params, IMAGE_PATH)) # Ensure we skipped the first one self.assertEqual(response['images'][0]['id'], SECOND_EARLIEST_IMAGE) self.assertEqual(IMAGES_IN_SAMPLE - 1, len(response['images'])) # Start link self.assertEqual( Url(response['start']), Url(self._prepare_url( IMAGE_PATH, self._prepare_params(params) )) )