Skip to content

Commit

Permalink
Return empty list when volume not attached
Browse files Browse the repository at this point in the history
* The api used to return a list of a single empty dict
* Fixes bug 942990

Change-Id: I9926515acfcedf711e81615aa13cec0bbf968086
  • Loading branch information
bcwaldon committed Feb 29, 2012
1 parent 9a6dfec commit 36100f6
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 22 deletions.
6 changes: 3 additions & 3 deletions nova/api/openstack/volume/volumes.py
Expand Up @@ -84,10 +84,10 @@ def _translate_volume_summary_view(context, vol):
d['availabilityZone'] = vol['availability_zone']
d['createdAt'] = vol['created_at']

d['attachments'] = []
if vol['attach_status'] == 'attached':
d['attachments'] = [_translate_attachment_detail_view(context, vol)]
else:
d['attachments'] = [{}]
attachment = _translate_attachment_detail_view(context, vol)
d['attachments'].append(attachment)

d['displayName'] = vol['display_name']
d['displayDescription'] = vol['display_description']
Expand Down
42 changes: 23 additions & 19 deletions nova/tests/api/openstack/fakes.py
Expand Up @@ -591,25 +591,29 @@ def stub_instance(id, user_id=None, project_id=None, host=None,
return instance


def stub_volume(id):
return {'id': id,
'user_id': 'fakeuser',
'project_id': 'fakeproject',
'host': 'fakehost',
'size': 1,
'availability_zone': 'fakeaz',
'instance': {'uuid': 'fakeuuid'},
'mountpoint': '/',
'status': 'fakestatus',
'attach_status': 'attached',
'name': 'vol name',
'display_name': 'displayname',
'display_description': 'displaydesc',
'created_at': datetime.datetime(1, 1, 1, 1, 1, 1),
'snapshot_id': None,
'volume_type_id': 'fakevoltype',
'volume_metadata': [],
'volume_type': {'name': 'vol_type_name'}}
def stub_volume(id, **kwargs):
volume = {
'id': id,
'user_id': 'fakeuser',
'project_id': 'fakeproject',
'host': 'fakehost',
'size': 1,
'availability_zone': 'fakeaz',
'instance': {'uuid': 'fakeuuid'},
'mountpoint': '/',
'status': 'fakestatus',
'attach_status': 'attached',
'name': 'vol name',
'display_name': 'displayname',
'display_description': 'displaydesc',
'created_at': datetime.datetime(1, 1, 1, 1, 1, 1),
'snapshot_id': None,
'volume_type_id': 'fakevoltype',
'volume_metadata': [],
'volume_type': {'name': 'vol_type_name'}}

volume.update(kwargs)
return volume


def stub_volume_create(self, context, size, name, description, snapshot,
Expand Down
22 changes: 22 additions & 0 deletions nova/tests/api/openstack/volume/test_volumes.py
Expand Up @@ -135,6 +135,28 @@ def test_volume_show(self):
'size': 1}}
self.assertEqual(res_dict, expected)

def test_volume_show_no_attachments(self):
def stub_volume_get(self, context, volume_id):
return fakes.stub_volume(volume_id, attach_status='detached')

self.stubs.Set(volume_api.API, 'get', stub_volume_get)

req = fakes.HTTPRequest.blank('/v1/volumes/1')
res_dict = self.controller.show(req, 1)
expected = {'volume': {'status': 'fakestatus',
'displayDescription': 'displaydesc',
'availabilityZone': 'fakeaz',
'displayName': 'displayname',
'attachments': [],
'volumeType': 'vol_type_name',
'snapshotId': None,
'metadata': {},
'id': '1',
'createdAt': datetime.datetime(1, 1, 1,
1, 1, 1),
'size': 1}}
self.assertEqual(res_dict, expected)

def test_volume_show_no_volume(self):
self.stubs.Set(volume_api.API, "get", fakes.stub_volume_get_notfound)

Expand Down

0 comments on commit 36100f6

Please sign in to comment.