Fix volume create --image

'volume create --image' should allow an image name to be used.

Closes-Bug: #1383333
Change-Id: I996d46db321eef2d75c3d19b480319f8a78c09b3
This commit is contained in:
Dean Troyer 2014-11-18 09:02:04 -06:00
parent 57364804f6
commit 39116bf594
3 changed files with 174 additions and 3 deletions

View File

@ -65,6 +65,24 @@ EXTENSION = {
'links': extension_links,
}
# NOTE(dtroyer): duplicating here the minimum image info needed to test
# volume create --image until circular references can be
# avoided by refactoring the test fakes.
image_id = 'im1'
image_name = 'graven'
IMAGE = {
'id': image_id,
'name': image_name,
}
class FakeImagev1Client(object):
def __init__(self, **kwargs):
self.images = mock.Mock()
class FakeVolumev1Client(object):
def __init__(self, **kwargs):
@ -91,3 +109,8 @@ class TestVolumev1(utils.TestCommand):
endpoint=fakes.AUTH_URL,
token=fakes.AUTH_TOKEN,
)
self.app.client_manager.image = FakeImagev1Client(
endpoint=fakes.AUTH_URL,
token=fakes.AUTH_TOKEN,
)

View File

@ -38,6 +38,10 @@ class TestVolume(volume_fakes.TestVolumev1):
self.users_mock = self.app.client_manager.identity.users
self.users_mock.reset_mock()
# Get a shortcut to the ImageManager Mock
self.images_mock = self.app.client_manager.image.images
self.images_mock.reset_mock()
# TODO(dtroyer): The volume create tests are incomplete, only the minimal
# options and the options that require additional processing
@ -389,3 +393,135 @@ class TestVolumeCreate(TestVolume):
volume_fakes.volume_type,
)
self.assertEqual(datalist, data)
def test_volume_create_image_id(self):
self.images_mock.get.return_value = fakes.FakeResource(
None,
copy.deepcopy(volume_fakes.IMAGE),
loaded=True,
)
arglist = [
'--image', volume_fakes.image_id,
'--size', str(volume_fakes.volume_size),
volume_fakes.volume_name,
]
verifylist = [
('image', volume_fakes.image_id),
('size', volume_fakes.volume_size),
('name', volume_fakes.volume_name),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
# DisplayCommandBase.take_action() returns two tuples
columns, data = self.cmd.take_action(parsed_args)
# VolumeManager.create(size, snapshot_id=, source_volid=,
# display_name=, display_description=,
# volume_type=, user_id=,
# project_id=, availability_zone=,
# metadata=, imageRef=)
self.volumes_mock.create.assert_called_with(
volume_fakes.volume_size,
None,
None,
volume_fakes.volume_name,
None,
None,
None,
None,
None,
None,
volume_fakes.image_id,
)
collist = (
'attach_status',
'availability_zone',
'display_description',
'display_name',
'id',
'properties',
'size',
'status',
'type',
)
self.assertEqual(collist, columns)
datalist = (
'detached',
volume_fakes.volume_zone,
volume_fakes.volume_description,
volume_fakes.volume_name,
volume_fakes.volume_id,
volume_fakes.volume_metadata_str,
volume_fakes.volume_size,
'',
volume_fakes.volume_type,
)
self.assertEqual(datalist, data)
def test_volume_create_image_name(self):
self.images_mock.get.return_value = fakes.FakeResource(
None,
copy.deepcopy(volume_fakes.IMAGE),
loaded=True,
)
arglist = [
'--image', volume_fakes.image_name,
'--size', str(volume_fakes.volume_size),
volume_fakes.volume_name,
]
verifylist = [
('image', volume_fakes.image_name),
('size', volume_fakes.volume_size),
('name', volume_fakes.volume_name),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
# DisplayCommandBase.take_action() returns two tuples
columns, data = self.cmd.take_action(parsed_args)
# VolumeManager.create(size, snapshot_id=, source_volid=,
# display_name=, display_description=,
# volume_type=, user_id=,
# project_id=, availability_zone=,
# metadata=, imageRef=)
self.volumes_mock.create.assert_called_with(
volume_fakes.volume_size,
None,
None,
volume_fakes.volume_name,
None,
None,
None,
None,
None,
None,
volume_fakes.image_id,
)
collist = (
'attach_status',
'availability_zone',
'display_description',
'display_name',
'id',
'properties',
'size',
'status',
'type',
)
self.assertEqual(collist, columns)
datalist = (
'detached',
volume_fakes.volume_zone,
volume_fakes.volume_description,
volume_fakes.volume_name,
volume_fakes.volume_id,
volume_fakes.volume_metadata_str,
volume_fakes.volume_size,
'',
volume_fakes.volume_type,
)
self.assertEqual(datalist, data)

View File

@ -99,6 +99,7 @@ class CreateVolume(show.ShowOne):
self.log.debug('take_action(%s)', parsed_args)
identity_client = self.app.client_manager.identity
image_client = self.app.client_manager.image
volume_client = self.app.client_manager.volume
source_volume = None
@ -111,12 +112,23 @@ class CreateVolume(show.ShowOne):
project = None
if parsed_args.project:
project = utils.find_resource(
identity_client.tenants, parsed_args.project).id
identity_client.tenants,
parsed_args.project,
).id
user = None
if parsed_args.user:
user = utils.find_resource(
identity_client.users, parsed_args.user).id
identity_client.users,
parsed_args.user,
).id
image = None
if parsed_args.image:
image = utils.find_resource(
image_client.images,
parsed_args.image,
).id
volume = volume_client.volumes.create(
parsed_args.size,
@ -129,7 +141,7 @@ class CreateVolume(show.ShowOne):
project,
parsed_args.availability_zone,
parsed_args.property,
parsed_args.image
image,
)
# Map 'metadata' column to 'properties'
volume._info.update(