Fix for Volumev1 Aggregation

Takes into account the differences between volumes details
in volume v1 and v2

Co-Authored-By: Kristi Nikolla <knikolla@bu.edu>

Change-Id: Ied7f6f1cca752eb300a284ca1267ad1ef79e6db5
Closes-bug: 1646520
This commit is contained in:
wjdan.alharthi 2016-12-06 04:34:42 +00:00 committed by Kristi Nikolla
parent cf40e932ef
commit 217f7c3a02
3 changed files with 65 additions and 18 deletions

View File

@ -298,6 +298,7 @@ class RequestHandler(object):
services.aggregate(responses,
self.details['action'][0],
self.details['service'],
version=self.details['version'],
params=request.args.to_dict(),
path=request.base_url,
strip_details=self.strip_details),

View File

@ -44,7 +44,7 @@ def construct_url(service_provider, service_type,
return url
def aggregate(responses, key, service_type,
def aggregate(responses, key, service_type, version=None,
params=None, path=None, strip_details=True):
"""Combine responses from several clusters into one response."""
if params:
@ -91,8 +91,9 @@ def aggregate(responses, key, service_type,
# we automatically make the call to /volumes/detail
# because we need sorting information. Here we
# remove the extra values /volumes/detail provides
if key == 'volumes' and strip_details:
resource_list[start:end] = _remove_details(resource_list[start:end])
if key == 'volumes' and strip_details and version:
resource_list[start:end] = _remove_details(resource_list[start:end],
version)
response = {key: resource_list[start:end]}
@ -195,9 +196,20 @@ def _is_reverse(order):
raise ValueError
def _remove_details(volumes):
def _remove_details(volumes, version):
"""Delete key, value pairs if key is not in keys"""
keys = ['id', 'links', 'name']
keys = {
'v1': [
'status', 'attachments', 'availability_zone',
'encrypted', 'source_volid', 'display_description',
'snapshot_id', 'id', 'size', 'display_name',
'bootable', 'created_at', 'multiattach',
'volume_type', 'metadata'
],
'v2': ['id', 'links', 'name']
}
for i in range(len(volumes)):
volumes[i] = {key: volumes[i][key] for key in keys}
volumes[i] = {key: volumes[i][key] for key in keys[version]}
return volumes

View File

@ -59,6 +59,15 @@ VOLUMES = {
))
}
VOLUMES_V1 = {
'default': Response(json.dumps(
samples.single_sp['/volume/v1/id/volumes/detail']
)),
'sp1': Response(json.dumps(
samples.single_sp['/volume/v1/id/volumes/detail']
))
}
IMAGES = {
'default': Response(json.dumps(
samples.multiple_sps['/image/v2/images'][0]
@ -118,11 +127,13 @@ class TestServices(testcase.TestCase):
'limit': 1
}
response = json.loads(services.aggregate(IMAGES, 'images', 'image',
params, IMAGE_PATH))
params=params,
path=IMAGE_PATH))
self.assertEqual(1, len(response['images']))
response = json.loads(services.aggregate(VOLUMES, 'volumes', 'volume',
params, VOLUME_PATH))
params=params,
path=IMAGE_PATH))
self.assertEqual(1, len(response['volumes']))
def test_aggregate_sort_images_ascending(self):
@ -131,7 +142,8 @@ class TestServices(testcase.TestCase):
'sort': 'size:asc'
}
response = json.loads(services.aggregate(IMAGES, 'images', 'image',
params, IMAGE_PATH))
params=params,
path=IMAGE_PATH))
self.assertEqual(response['images'][0]['id'], SMALLEST_IMAGE)
def test_aggregate_sort_images_limit(self):
@ -142,7 +154,8 @@ class TestServices(testcase.TestCase):
'limit': 1
}
response = json.loads(services.aggregate(IMAGES, 'images', 'image',
params, IMAGE_PATH))
params=params,
path=IMAGE_PATH))
# Ensure the smallest is first and there is only 1 entry.
self.assertEqual(response['images'][0]['id'], SMALLEST_IMAGE)
@ -164,7 +177,8 @@ class TestServices(testcase.TestCase):
'limit': 2
}
response = json.loads(services.aggregate(IMAGES, 'images', 'image',
params, IMAGE_PATH))
params=params,
path=IMAGE_PATH))
# Check the first and second are the correct ids.
self.assertEqual(response['images'][0]['id'], EARLIEST_IMAGE)
@ -187,7 +201,8 @@ class TestServices(testcase.TestCase):
'limit': 1
}
response = json.loads(services.aggregate(IMAGES, 'images', 'image',
params, IMAGE_PATH))
params=params,
path=IMAGE_PATH))
# Check the id and size
self.assertEqual(response['images'][0]['id'], LATEST_IMAGE)
@ -210,7 +225,8 @@ class TestServices(testcase.TestCase):
'marker': EARLIEST_IMAGE
}
response = json.loads(services.aggregate(IMAGES, 'images', 'image',
params, IMAGE_PATH))
params=params,
path=IMAGE_PATH))
# Ensure we skipped the first one
self.assertEqual(response['images'][0]['id'], SECOND_EARLIEST_IMAGE)
@ -242,7 +258,8 @@ class TestServices(testcase.TestCase):
}
response = json.loads(services.aggregate(IMAGES, 'images', 'image',
params, IMAGE_PATH))
params=params,
path=IMAGE_PATH))
# Ensure we skipped the first one
self.assertEqual(response['images'][0]['id'], SECOND_EARLIEST_IMAGE)
@ -265,7 +282,8 @@ class TestServices(testcase.TestCase):
}
response = json.loads(services.aggregate(IMAGES, 'images', 'image',
params, IMAGE_PATH))
params=params,
path=IMAGE_PATH))
# Ensure we skipped the first one
self.assertEqual(0, len(response['images']))
@ -312,10 +330,10 @@ class TestServices(testcase.TestCase):
Url(current_version_url),
Url(VOLUME_VERSIONED))
def test_remove_details(self):
"""Test aggregation on volumes with strip_details = True"""
def test_remove_details_v2(self):
"""Test aggregation on volumes v2 with strip_details = True"""
response = json.loads(services.aggregate(
VOLUMES, 'volumes', 'volume', strip_details=True
VOLUMES, 'volumes', 'volume', version='v2', strip_details=True
))
for v in response['volumes']:
self.assertEqual(
@ -323,6 +341,22 @@ class TestServices(testcase.TestCase):
{'id', 'links', 'name'}
)
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'}
)
@staticmethod
def _prepare_params(user_params, marker=None):
params = user_params.copy()