diff --git a/openstackclient/tests/unit/volume/v2/test_volume.py b/openstackclient/tests/unit/volume/v2/test_volume.py index ef9c2fab5..c930002f3 100644 --- a/openstackclient/tests/unit/volume/v2/test_volume.py +++ b/openstackclient/tests/unit/volume/v2/test_volume.py @@ -398,6 +398,43 @@ class TestVolumeCreate(TestVolume): parsed_args) self.assertIn("--os-volume-api-version 3.47 or greater", str(exc)) + def test_volume_create_with_source_volume(self): + source_vol = "source_vol" + arglist = [ + '--source', self.new_volume.id, + source_vol, + ] + verifylist = [ + ('source', self.new_volume.id), + ('name', source_vol), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.volumes_mock.get.return_value = self.new_volume + + # In base command class ShowOne in cliff, abstract method take_action() + # returns a two-part tuple with a tuple of column names and a tuple of + # data to be shown. + columns, data = self.cmd.take_action(parsed_args) + + self.volumes_mock.create.assert_called_once_with( + size=self.new_volume.size, + snapshot_id=None, + name=source_vol, + description=None, + volume_type=None, + availability_zone=None, + metadata=None, + imageRef=None, + source_volid=self.new_volume.id, + consistencygroup_id=None, + scheduler_hints=None, + backup_id=None, + ) + + self.assertEqual(self.columns, columns) + self.assertCountEqual(self.datalist, data) + def test_volume_create_with_bootable_and_readonly(self): arglist = [ '--bootable', diff --git a/openstackclient/volume/v2/volume.py b/openstackclient/volume/v2/volume.py index ffcbd573f..7905e0971 100644 --- a/openstackclient/volume/v2/volume.py +++ b/openstackclient/volume/v2/volume.py @@ -183,6 +183,11 @@ class CreateVolume(command.ShowOne): def take_action(self, parsed_args): _check_size_arg(parsed_args) + # size is validated in the above call to + # _check_size_arg where we check that size + # should be passed if we are not creating a + # volume from snapshot, backup or source volume + size = parsed_args.size volume_client = self.app.client_manager.volume image_client = self.app.client_manager.image @@ -195,9 +200,11 @@ class CreateVolume(command.ShowOne): source_volume = None if parsed_args.source: - source_volume = utils.find_resource( + source_volume_obj = utils.find_resource( volume_client.volumes, - parsed_args.source).id + parsed_args.source) + source_volume = source_volume_obj.id + size = max(size or 0, source_volume_obj.size) consistency_group = None if parsed_args.consistency_group: @@ -210,8 +217,6 @@ class CreateVolume(command.ShowOne): image = image_client.find_image(parsed_args.image, ignore_missing=False).id - size = parsed_args.size - snapshot = None if parsed_args.snapshot: snapshot_obj = utils.find_resource(