def test_type_show_with_encryption(self): encryption_type = volume_fakes.FakeType.create_one_encryption_type() encryption_info = { 'provider': 'LuksEncryptor', 'cipher': None, 'key_size': None, 'control_location': 'front-end', } self.volume_type = volume_fakes.FakeType.create_one_type( attrs={'encryption': encryption_info}) self.types_mock.get.return_value = self.volume_type self.encryption_types_mock.get.return_value = encryption_type encryption_columns = ( 'description', 'encryption', 'id', 'is_public', 'name', 'properties', ) encryption_data = (self.volume_type.description, format_columns.DictColumn(encryption_info), self.volume_type.id, True, self.volume_type.name, format_columns.DictColumn( self.volume_type.extra_specs)) arglist = ['--encryption-type', self.volume_type.id] verifylist = [('encryption_type', True), ("volume_type", self.volume_type.id)] parsed_args = self.check_parser(self.cmd, arglist, verifylist) columns, data = self.cmd.take_action(parsed_args) self.types_mock.get.assert_called_with(self.volume_type.id) self.encryption_types_mock.get.assert_called_with(self.volume_type.id) self.assertEqual(encryption_columns, columns) self.assertItemEqual(encryption_data, data)
def take_action(self, parsed_args): volume_client = self.app.client_manager.volume volume_type = volume_client.volume_types.create(parsed_args.name) volume_type._info.pop('extra_specs') if parsed_args.property: result = volume_type.set_keys(parsed_args.property) volume_type._info.update( {'properties': format_columns.DictColumn(result)}) if (parsed_args.encryption_provider or parsed_args.encryption_cipher or parsed_args.encryption_key_size or parsed_args.encryption_control_location): try: # create new encryption encryption = _create_encryption_type(volume_client, volume_type, parsed_args) except Exception as e: LOG.error( _("Failed to set encryption information for this " "volume type: %s"), e) # add encryption info in result encryption._info.pop("volume_type_id", None) volume_type._info.update( {'encryption': format_columns.DictColumn(encryption._info)}) volume_type._info.pop("os-volume-type-access:is_public", None) return zip(*sorted(volume_type._info.items()))
def test_assert_list_item(self): expected = [ ['a', 'b', 'c'], [format_columns.DictColumn({'a': 1, 'b': 2}), format_columns.ListColumn(['x', 'y', 'z'])] ] actual = [ ['a', 'b', 'c'], [format_columns.DictColumn({'a': 1, 'b': 2}), format_columns.ListColumn(['x', 'y', 'z'])] ] self.assertListItemEqual(expected, actual)
def take_action(self, parsed_args): identity_client = self.app.client_manager.identity volume_client = self.app.client_manager.volume if parsed_args.project and not parsed_args.private: msg = _("--project is only allowed with --private") raise exceptions.CommandError(msg) kwargs = {} if parsed_args.public: kwargs['is_public'] = True if parsed_args.private: kwargs['is_public'] = False volume_type = volume_client.volume_types.create( parsed_args.name, description=parsed_args.description, **kwargs) volume_type._info.pop('extra_specs') if parsed_args.project: try: project_id = identity_common.find_project( identity_client, parsed_args.project, parsed_args.project_domain, ).id volume_client.volume_type_access.add_project_access( volume_type.id, project_id) except Exception as e: msg = _("Failed to add project %(project)s access to " "type: %(e)s") LOG.error(msg % {'project': parsed_args.project, 'e': e}) if parsed_args.property: result = volume_type.set_keys(parsed_args.property) volume_type._info.update( {'properties': format_columns.DictColumn(result)}) if (parsed_args.encryption_provider or parsed_args.encryption_cipher or parsed_args.encryption_key_size or parsed_args.encryption_control_location): try: # create new encryption encryption = _create_encryption_type(volume_client, volume_type, parsed_args) except Exception as e: LOG.error( _("Failed to set encryption information for this " "volume type: %s"), e) # add encryption info in result encryption._info.pop("volume_type_id", None) volume_type._info.update( {'encryption': format_columns.DictColumn(encryption._info)}) volume_type._info.pop("os-volume-type-access:is_public", None) return zip(*sorted(six.iteritems(volume_type._info)))
def test_type_show_with_access(self): arglist = [self.volume_type.id] verifylist = [("volume_type", self.volume_type.id)] parsed_args = self.check_parser(self.cmd, arglist, verifylist) private_type = volume_fakes.FakeType.create_one_type( attrs={'is_public': False}) type_access_list = volume_fakes.FakeTypeAccess.create_one_type_access() with mock.patch.object(self.types_mock, 'get', return_value=private_type): with mock.patch.object(self.types_access_mock, 'list', return_value=[type_access_list]): columns, data = self.cmd.take_action(parsed_args) self.types_mock.get.assert_called_once_with( self.volume_type.id) self.types_access_mock.list.assert_called_once_with( private_type.id) self.assertEqual(self.columns, columns) private_type_data = (format_columns.ListColumn([ type_access_list.project_id ]), private_type.description, private_type.id, private_type.is_public, private_type.name, format_columns.DictColumn( private_type.extra_specs)) self.assertItemEqual(private_type_data, data)
class TestAggregate(compute_fakes.TestComputev2): fake_ag = compute_fakes.FakeAggregate.create_one_aggregate() columns = ( 'availability_zone', 'hosts', 'id', 'name', 'properties', ) data = ( fake_ag.availability_zone, format_columns.ListColumn(fake_ag.hosts), fake_ag.id, fake_ag.name, format_columns.DictColumn(fake_ag.metadata), ) def setUp(self): super(TestAggregate, self).setUp() # Get a shortcut to the AggregateManager Mock self.app.client_manager.sdk_connection = mock.Mock() self.app.client_manager.sdk_connection.compute = mock.Mock() self.sdk_client = self.app.client_manager.sdk_connection.compute self.sdk_client.aggregates = mock.Mock() self.sdk_client.find_aggregate = mock.Mock() self.sdk_client.create_aggregate = mock.Mock() self.sdk_client.update_aggregate = mock.Mock() self.sdk_client.update_aggregate = mock.Mock() self.sdk_client.set_aggregate_metadata = mock.Mock() self.sdk_client.add_host_to_aggregate = mock.Mock() self.sdk_client.remove_host_from_aggregate = mock.Mock()
def test_assert_item_formattable_columns_vs_legacy_formatter(self): expected = [format_columns.DictColumn({'a': 1, 'b': 2}), format_columns.ListColumn(['x', 'y', 'z'])] actual = [utils.format_dict({'a': 1, 'b': 2}), utils.format_list(['x', 'y', 'z'])] self.assertRaises(AssertionError, self.assertItemEqual, expected, actual)
def test_project_show(self): arglist = [ self.fake_proj_show.id, ] verifylist = [ ('project', self.fake_proj_show.id), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) # 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.projects_mock.get.assert_called_with(self.fake_proj_show.id, ) collist = ('description', 'enabled', 'id', 'name', 'properties') self.assertEqual(collist, columns) datalist = ( self.fake_proj_show.description, True, self.fake_proj_show.id, self.fake_proj_show.name, format_columns.DictColumn({}), ) self.assertCountEqual(datalist, data)
def take_action(self, parsed_args): share_client = self.app.client_manager.share share_obj = apiutils.find_resource(share_client.shares, parsed_args.share) export_locations = share_client.share_export_locations.list(share_obj) export_locations = cliutils.convert_dict_list_to_string( export_locations, ignored_keys=[ 'replica_state', 'availability_zone', 'share_replica_id' ]) data = share_obj._info data['export_locations'] = export_locations # Special mapping for columns to make the output easier to read: # 'metadata' --> 'properties' data.update( { 'properties': format_columns.DictColumn( data.pop('metadata', {})), }, ) # Remove key links from being displayed data.pop("links", None) data.pop("shares_type", None) return self.dict2columns(data)
class TestQosShow(TestQos): qos_spec = volume_fakes.FakeQos.create_one_qos() qos_association = volume_fakes.FakeQos.create_one_qos_association() columns = ('associations', 'consumer', 'id', 'name', 'properties') data = ( format_columns.ListColumn([qos_association.name]), qos_spec.consumer, qos_spec.id, qos_spec.name, format_columns.DictColumn(qos_spec.specs), ) def setUp(self): super(TestQosShow, self).setUp() self.qos_mock.get.return_value = self.qos_spec self.qos_mock.get_associations.return_value = [self.qos_association] # Get the command object to test self.cmd = qos_specs.ShowQos(self.app, None) def test_qos_show(self): arglist = [self.qos_spec.id] verifylist = [('qos_spec', self.qos_spec.id)] parsed_args = self.check_parser(self.cmd, arglist, verifylist) columns, data = self.cmd.take_action(parsed_args) self.qos_mock.get.assert_called_with(self.qos_spec.id) self.assertEqual(self.columns, columns) self.assertCountEqual(self.data, tuple(data))
def take_action(self, parsed_args): compute_client = self.app.client_manager.compute info = {} data = compute_client.aggregates.create( parsed_args.name, parsed_args.zone, ) info.update(data._info) if parsed_args.property: info.update( compute_client.aggregates.set_metadata( data, parsed_args.property, )._info) # Special mapping for columns to make the output easier to read: # 'metadata' --> 'properties' hosts = None properties = None if 'hosts' in info.keys(): hosts = format_columns.ListColumn(info.pop('hosts')) if 'metadata' in info.keys(): properties = format_columns.DictColumn(info.pop('metadata')) info.update({ 'hosts': hosts, 'properties': properties, }, ) return zip(*sorted(info.items()))
def test_qos_list_no_association(self): self.qos_mock.reset_mock() self.qos_mock.get_associations.side_effect = [ [self.qos_association], exceptions.NotFound("NotFound"), ] arglist = [] verifylist = [] parsed_args = self.check_parser(self.cmd, arglist, verifylist) columns, data = self.cmd.take_action(parsed_args) self.qos_mock.list.assert_called_with() self.assertEqual(self.columns, columns) ex_data = copy.deepcopy(self.data) ex_data[1] = ( self.qos_specs[1].id, self.qos_specs[1].name, self.qos_specs[1].consumer, format_columns.ListColumn(None), format_columns.DictColumn(self.qos_specs[1].specs), ) self.assertCountEqual(ex_data, list(data))
def take_action(self, parsed_args): share_client = self.app.client_manager.share share_obj = apiutils.find_resource(share_client.shares, parsed_args.share) export_locations = share_client.share_export_locations.list(share_obj) export_locations = (cliutils.transform_export_locations_to_string_view( export_locations)) data = share_obj._info data['export_locations'] = export_locations # Special mapping for columns to make the output easier to read: # 'metadata' --> 'properties' data.update( { 'properties': format_columns.DictColumn( data.pop('metadata', {})), }, ) # Remove key links from being displayed data.pop("links", None) data.pop("shares_type", None) return self.dict2columns(data)
def _format_image(image): """Format an image to make it more consistent with OSC operations.""" info = {} properties = {} # the only fields we're not including is "links", "tags" and the properties fields_to_show = [ 'status', 'name', 'container_format', 'created_at', 'size', 'disk_format', 'updated_at', 'visibility', 'min_disk', 'protected', 'id', 'file', 'checksum', 'owner', 'virtual_size', 'min_ram', 'schema' ] # split out the usual key and the properties which are top-level for key in six.iterkeys(image): if key in fields_to_show: info[key] = image.get(key) elif key == 'tags': continue # handle this later else: properties[key] = image.get(key) # format the tags if they are there info['tags'] = format_columns.ListColumn(image.get('tags')) # add properties back into the dictionary as a top-level key if properties: info['properties'] = format_columns.DictColumn(properties) return info
def take_action(self, parsed_args): volume_client = self.app.client_manager.volume volume = utils.find_resource(volume_client.volumes, parsed_args.volume) # Map 'metadata' column to 'properties' volume._info.update( { 'properties': format_columns.DictColumn(volume._info.pop('metadata')), 'type': volume._info.pop('volume_type'), }, ) if 'os-vol-tenant-attr:tenant_id' in volume._info: volume._info.update( {'project_id': volume._info.pop( 'os-vol-tenant-attr:tenant_id')} ) # Replace "display_name" by "name", keep consistent in v1 and v2 if 'display_name' in volume._info: volume._info.update({'name': volume._info.pop('display_name')}) volume_info = utils.backward_compat_col_showone( volume._info, parsed_args.columns, {'display_name': 'name'} ) return zip(*sorted(six.iteritems(volume_info)))
class TestAggregate(compute_fakes.TestComputev2): fake_ag = compute_fakes.FakeAggregate.create_one_aggregate() columns = ( 'availability_zone', 'hosts', 'id', 'name', 'properties', ) data = ( fake_ag.availability_zone, format_columns.ListColumn(fake_ag.hosts), fake_ag.id, fake_ag.name, format_columns.DictColumn(fake_ag.metadata), ) def setUp(self): super(TestAggregate, self).setUp() # Get a shortcut to the AggregateManager Mock self.aggregate_mock = self.app.client_manager.compute.aggregates self.aggregate_mock.reset_mock()
def test_type_show_with_list_access_exec(self): arglist = [self.volume_type.id] verifylist = [("volume_type", self.volume_type.id)] parsed_args = self.check_parser(self.cmd, arglist, verifylist) private_type = volume_fakes.FakeVolumeType.create_one_volume_type( attrs={'is_public': False}) with mock.patch.object(self.types_mock, 'get', return_value=private_type): with mock.patch.object(self.types_access_mock, 'list', side_effect=Exception()): columns, data = self.cmd.take_action(parsed_args) self.types_mock.get.assert_called_once_with( self.volume_type.id) self.types_access_mock.list.assert_called_once_with( private_type.id) self.assertEqual(self.columns, columns) private_type_data = (None, private_type.description, private_type.id, private_type.is_public, private_type.name, format_columns.DictColumn( private_type.extra_specs)) self.assertCountEqual(private_type_data, data)
class TestAggregateList(TestAggregate): list_columns = ( "ID", "Name", "Availability Zone", ) list_columns_long = ( "ID", "Name", "Availability Zone", "Properties", "Hosts", ) list_data = (( TestAggregate.fake_ag.id, TestAggregate.fake_ag.name, TestAggregate.fake_ag.availability_zone, ), ) list_data_long = (( TestAggregate.fake_ag.id, TestAggregate.fake_ag.name, TestAggregate.fake_ag.availability_zone, format_columns.DictColumn({ key: value for key, value in TestAggregate.fake_ag.metadata.items() if key != 'availability_zone' }), format_columns.ListColumn(TestAggregate.fake_ag.hosts), ), ) def setUp(self): super(TestAggregateList, self).setUp() self.sdk_client.aggregates.return_value = [self.fake_ag] self.cmd = aggregate.ListAggregate(self.app, None) def test_aggregate_list(self): parsed_args = self.check_parser(self.cmd, [], []) columns, data = self.cmd.take_action(parsed_args) self.assertEqual(self.list_columns, columns) self.assertCountEqual(self.list_data, tuple(data)) def test_aggregate_list_with_long(self): arglist = [ '--long', ] vertifylist = [ ('long', True), ] parsed_args = self.check_parser(self.cmd, arglist, vertifylist) columns, data = self.cmd.take_action(parsed_args) self.assertEqual(self.list_columns_long, columns) self.assertCountEqual(self.list_data_long, tuple(data))
def take_action(self, parsed_args): volume_client = self.app.client_manager.volume volume = parsed_args.volume if not parsed_args.volume: volume = parsed_args.snapshot_name volume_id = utils.find_resource(volume_client.volumes, volume).id if parsed_args.remote_source: # Create a new snapshot from an existing remote snapshot source if parsed_args.force: msg = (_("'--force' option will not work when you create " "new volume snapshot from an existing remote " "volume snapshot")) LOG.warning(msg) snapshot = volume_client.volume_snapshots.manage( volume_id=volume_id, ref=parsed_args.remote_source, name=parsed_args.snapshot_name, description=parsed_args.description, metadata=parsed_args.property, ) else: # create a new snapshot from scratch snapshot = volume_client.volume_snapshots.create( volume_id, force=parsed_args.force, name=parsed_args.snapshot_name, description=parsed_args.description, metadata=parsed_args.property, ) snapshot._info.update({ 'properties': format_columns.DictColumn(snapshot._info.pop('metadata')) }) return zip(*sorted(snapshot._info.items()))
def test_assert_item_with_formattable_columns(self): expected = [ format_columns.DictColumn({ 'a': 1, 'b': 2 }), format_columns.ListColumn(['x', 'y', 'z']) ] actual = [ format_columns.DictColumn({ 'a': 1, 'b': 2 }), format_columns.ListColumn(['x', 'y', 'z']) ] self.assertItemEqual(expected, actual)
def test_dict_column(self): dict_content = { 'key1': 'value1', 'key2': 'value2', } col = format_columns.DictColumn(dict_content) self.assertEqual(dict_content, col.machine_readable()) self.assertEqual("key1='value1', key2='value2'", col.human_readable())
def take_action(self, parsed_args): data = self.app.client_manager.object_store.container_show( container=parsed_args.container, ) if 'properties' in data: data['properties'] = format_columns.DictColumn(data['properties']) return zip(*sorted(data.items()))
class TestShowNetworkAgent(TestNetworkAgent): _network_agent = ( network_fakes.FakeNetworkAgent.create_one_network_agent()) columns = ( 'admin_state_up', 'agent_type', 'alive', 'availability_zone', 'binary', 'configurations', 'host', 'id', ) data = ( network_agent.AdminStateColumn(_network_agent.admin_state_up), _network_agent.agent_type, network_agent.AliveColumn(_network_agent.is_alive), _network_agent.availability_zone, _network_agent.binary, format_columns.DictColumn(_network_agent.configurations), _network_agent.host, _network_agent.id, ) def setUp(self): super(TestShowNetworkAgent, self).setUp() self.network.get_agent = mock.Mock( return_value=self._network_agent) # Get the command object to test self.cmd = network_agent.ShowNetworkAgent(self.app, self.namespace) def test_show_no_options(self): arglist = [] verifylist = [] # Missing required args should bail here self.assertRaises(tests_utils.ParserException, self.check_parser, self.cmd, arglist, verifylist) def test_show_all_options(self): arglist = [ self._network_agent.id, ] verifylist = [ ('network_agent', self._network_agent.id), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) columns, data = self.cmd.take_action(parsed_args) self.network.get_agent.assert_called_once_with( self._network_agent.id) self.assertEqual(self.columns, columns) self.assertItemEqual(list(self.data), list(data))
def setUp(self): super(TestQosCreate, self).setUp() self.new_qos_spec = volume_fakes.FakeQos.create_one_qos() self.datalist = (self.new_qos_spec.consumer, self.new_qos_spec.id, self.new_qos_spec.name, format_columns.DictColumn(self.new_qos_spec.specs)) self.qos_mock.create.return_value = self.new_qos_spec # Get the command object to test self.cmd = qos_specs.CreateQos(self.app, None)
def take_action(self, parsed_args): volume_client = self.app.client_manager.volume snapshot = utils.find_resource(volume_client.volume_snapshots, parsed_args.snapshot) snapshot._info.update({ 'properties': format_columns.DictColumn(snapshot._info.pop('metadata')) }) return zip(*sorted(snapshot._info.items()))
def test_volume_list_long(self): arglist = [ '--long', ] verifylist = [ ('long', True), ('all_projects', False), ('name', None), ('status', None), ('marker', None), ('limit', None), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) columns, data = self.cmd.take_action(parsed_args) search_opts = { 'all_tenants': False, 'project_id': None, 'user_id': None, 'name': None, 'status': None, } self.volumes_mock.list.assert_called_once_with( search_opts=search_opts, marker=None, limit=None, ) collist = [ 'ID', 'Name', 'Status', 'Size', 'Type', 'Bootable', 'Attached to', 'Properties', ] self.assertEqual(collist, columns) datalist = (( self.mock_volume.id, self.mock_volume.name, self.mock_volume.status, self.mock_volume.size, self.mock_volume.volume_type, self.mock_volume.bootable, volume.AttachmentsColumn(self.mock_volume.attachments), format_columns.DictColumn(self.mock_volume.metadata), ), ) self.assertCountEqual(datalist, tuple(data))
def setUp(self): super(TestTypeShow, self).setUp() self.volume_type = volume_fakes.FakeVolumeType.create_one_volume_type() self.data = (None, self.volume_type.description, self.volume_type.id, True, self.volume_type.name, format_columns.DictColumn(self.volume_type.extra_specs)) self.types_mock.get.return_value = self.volume_type # Get the command object to test self.cmd = volume_type.ShowVolumeType(self.app, None)
def test_assert_item_different_formattable_columns(self): class ExceptionColumn(cliff_columns.FormattableColumn): def human_readable(self): raise Exception('always fail') expected = [format_columns.DictColumn({'a': 1, 'b': 2})] actual = [ExceptionColumn({'a': 1, 'b': 2})] # AssertionError is a subclass of Exception # so raising AssertionError ensures ExceptionColumn.human_readable() # is not called. self.assertRaises(AssertionError, self.assertItemEqual, expected, actual)
def take_action(self, parsed_args): volume_client = self.app.client_manager.volume volume_type = utils.find_resource(volume_client.volume_types, parsed_args.volume_type) properties = format_columns.DictColumn( volume_type._info.pop('extra_specs', {})) volume_type._info.update({'properties': properties}) access_project_ids = None if not volume_type.is_public: try: volume_type_access = volume_client.volume_type_access.list( volume_type.id) project_ids = [ utils.get_field(item, 'project_id') for item in volume_type_access ] # TODO(Rui Chen): This format list case can be removed after # patch https://review.opendev.org/#/c/330223/ merged. access_project_ids = format_columns.ListColumn(project_ids) except Exception as e: msg = _('Failed to get access project list for volume type ' '%(type)s: %(e)s') LOG.error(msg % {'type': volume_type.id, 'e': e}) volume_type._info.update({'access_project_ids': access_project_ids}) if parsed_args.encryption_type: # show encryption type information for this volume type try: encryption = volume_client.volume_encryption_types.get( volume_type.id) encryption._info.pop("volume_type_id", None) volume_type._info.update({ 'encryption': format_columns.DictColumn(encryption._info) }) except Exception as e: LOG.error( _("Failed to display the encryption information " "of this volume type: %s"), e) volume_type._info.pop("os-volume-type-access:is_public", None) return zip(*sorted(six.iteritems(volume_type._info)))
def test_image_list_long_option(self): arglist = [ '--long', ] verifylist = [ ('long', True), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) # In base command class Lister in cliff, abstract method take_action() # returns a tuple containing the column names and an iterable # containing the data to be listed. columns, data = self.cmd.take_action(parsed_args) self.api_mock.image_list.assert_called_with( detailed=True, marker=self._image.id, ) collist = ( 'ID', 'Name', 'Disk Format', 'Container Format', 'Size', 'Checksum', 'Status', 'Visibility', 'Protected', 'Project', 'Properties', ) self.assertEqual(collist, columns) datalist = (( self._image.id, self._image.name, '', '', '', '', '', image.VisibilityColumn(True), False, self._image.owner, format_columns.DictColumn({ 'Alpha': 'a', 'Beta': 'b', 'Gamma': 'g' }), ), ) self.assertListItemEqual(datalist, tuple(data))