diff options
Diffstat (limited to 'openstackclient')
36 files changed, 1979 insertions, 364 deletions
diff --git a/openstackclient/common/client_config.py b/openstackclient/common/client_config.py index 30286df8..d6297753 100644 --- a/openstackclient/common/client_config.py +++ b/openstackclient/common/client_config.py @@ -17,6 +17,8 @@ import logging from os_client_config import config from os_client_config import exceptions as occ_exceptions +from oslo_utils import strutils +import six LOG = logging.getLogger(__name__) @@ -180,7 +182,9 @@ class OSC_Config(config.OpenStackConfig): config = self._auth_v2_ignore_v3(config) config = self._auth_default_domain(config) - LOG.debug("auth_config_hook(): %s" % config) + if LOG.isEnabledFor(logging.DEBUG): + LOG.debug("auth_config_hook(): %s", + strutils.mask_password(six.text_type(config))) return config def load_auth_plugin(self, config): diff --git a/openstackclient/compute/v2/hypervisor.py b/openstackclient/compute/v2/hypervisor.py index 0222e899..69b5d137 100644 --- a/openstackclient/compute/v2/hypervisor.py +++ b/openstackclient/compute/v2/hypervisor.py @@ -35,14 +35,24 @@ class ListHypervisor(command.Lister): metavar="<hostname>", help=_("Filter hypervisors using <hostname> substring") ) + parser.add_argument( + '--long', + action='store_true', + help=_("List additional fields in output") + ) return parser def take_action(self, parsed_args): compute_client = self.app.client_manager.compute columns = ( "ID", - "Hypervisor Hostname" + "Hypervisor Hostname", + "Hypervisor Type", + "Host IP", + "State" ) + if parsed_args.long: + columns += ("vCPUs Used", "vCPUs", "Memory MB Used", "Memory MB") if parsed_args.matching: data = compute_client.hypervisors.search(parsed_args.matching) diff --git a/openstackclient/compute/v2/keypair.py b/openstackclient/compute/v2/keypair.py index d30fd429..d5c682f4 100644 --- a/openstackclient/compute/v2/keypair.py +++ b/openstackclient/compute/v2/keypair.py @@ -32,19 +32,20 @@ LOG = logging.getLogger(__name__) class CreateKeypair(command.ShowOne): - """Create new public key""" + """Create new public or private key for server ssh access""" def get_parser(self, prog_name): parser = super(CreateKeypair, self).get_parser(prog_name) parser.add_argument( 'name', metavar='<name>', - help=_("New public key name") + help=_("New public or private key name") ) parser.add_argument( '--public-key', metavar='<file>', - help=_("Filename for public key to add") + help=_("Filename for public key to add. If not used, " + "creates a private key.") ) return parser @@ -82,7 +83,7 @@ class CreateKeypair(command.ShowOne): class DeleteKeypair(command.Command): - """Delete public key(s)""" + """Delete public or private key(s)""" def get_parser(self, prog_name): parser = super(DeleteKeypair, self).get_parser(prog_name) @@ -90,7 +91,7 @@ class DeleteKeypair(command.Command): 'name', metavar='<key>', nargs='+', - help=_("Public key(s) to delete (name only)") + help=_("Name of key(s) to delete (name only)") ) return parser @@ -104,19 +105,19 @@ class DeleteKeypair(command.Command): compute_client.keypairs.delete(data.name) except Exception as e: result += 1 - LOG.error(_("Failed to delete public key with name " + LOG.error(_("Failed to delete key with name " "'%(name)s': %(e)s") % {'name': n, 'e': e}) if result > 0: total = len(parsed_args.name) - msg = (_("%(result)s of %(total)s public keys failed " + msg = (_("%(result)s of %(total)s keys failed " "to delete.") % {'result': result, 'total': total}) raise exceptions.CommandError(msg) class ListKeypair(command.Lister): - """List public key fingerprints""" + """List key fingerprints""" def take_action(self, parsed_args): compute_client = self.app.client_manager.compute @@ -133,20 +134,20 @@ class ListKeypair(command.Lister): class ShowKeypair(command.ShowOne): - """Display public key details""" + """Display key details""" def get_parser(self, prog_name): parser = super(ShowKeypair, self).get_parser(prog_name) parser.add_argument( 'name', metavar='<key>', - help=_("Public key to display (name only)") + help=_("Public or private key to display (name only)") ) parser.add_argument( '--public-key', action='store_true', default=False, - help=_("Show only bare public key (name only)") + help=_("Show only bare public key paired with the generated key") ) return parser diff --git a/openstackclient/compute/v2/server.py b/openstackclient/compute/v2/server.py index 1ca31497..df46c7df 100644 --- a/openstackclient/compute/v2/server.py +++ b/openstackclient/compute/v2/server.py @@ -973,15 +973,15 @@ class MigrateServer(command.Command): migration_group = parser.add_mutually_exclusive_group() migration_group.add_argument( '--shared-migration', - dest='shared_migration', - action='store_true', - default=True, + dest='block_migration', + action='store_false', + default=False, help=_('Perform a shared live migration (default)'), ) migration_group.add_argument( '--block-migration', - dest='shared_migration', - action='store_false', + dest='block_migration', + action='store_true', help=_('Perform a block live migration'), ) disk_group = parser.add_mutually_exclusive_group() @@ -1016,9 +1016,9 @@ class MigrateServer(command.Command): ) if parsed_args.live: server.live_migrate( - parsed_args.live, - parsed_args.shared_migration, - parsed_args.disk_overcommit, + host=parsed_args.live, + block_migration=parsed_args.block_migration, + disk_over_commit=parsed_args.disk_overcommit, ) else: server.migrate() diff --git a/openstackclient/identity/v2_0/user.py b/openstackclient/identity/v2_0/user.py index d2075150..bc091ce7 100644 --- a/openstackclient/identity/v2_0/user.py +++ b/openstackclient/identity/v2_0/user.py @@ -94,6 +94,10 @@ class CreateUser(command.ShowOne): if parsed_args.password_prompt: parsed_args.password = utils.get_password(self.app.stdin) + if not parsed_args.password: + LOG.warning(_("No password was supplied, authentication will fail " + "when a user does not have a password.")) + try: user = identity_client.users.create( parsed_args.name, @@ -292,6 +296,10 @@ class SetUser(command.Command): if parsed_args.password_prompt: parsed_args.password = utils.get_password(self.app.stdin) + if '' == parsed_args.password: + LOG.warning(_("No password was supplied, authentication will fail " + "when a user does not have a password.")) + user = utils.find_resource( identity_client.users, parsed_args.user, diff --git a/openstackclient/identity/v3/user.py b/openstackclient/identity/v3/user.py index dc47ef8d..1e086fb6 100644 --- a/openstackclient/identity/v3/user.py +++ b/openstackclient/identity/v3/user.py @@ -110,6 +110,10 @@ class CreateUser(command.ShowOne): if parsed_args.password_prompt: parsed_args.password = utils.get_password(self.app.stdin) + if not parsed_args.password: + LOG.warning(_("No password was supplied, authentication will fail " + "when a user does not have a password.")) + try: user = identity_client.users.create( name=parsed_args.name, @@ -329,6 +333,10 @@ class SetUser(command.Command): if parsed_args.password_prompt: parsed_args.password = utils.get_password(self.app.stdin) + if '' == parsed_args.password: + LOG.warning(_("No password was supplied, authentication will fail " + "when a user does not have a password.")) + user = utils.find_resource( identity_client.users, parsed_args.user, @@ -408,6 +416,10 @@ class SetPasswordUser(command.Command): password = utils.get_password( self.app.stdin, prompt="New Password:") + if '' == password: + LOG.warning(_("No password was supplied, authentication will fail " + "when a user does not have a password.")) + identity_client.users.update_password(current_password, password) diff --git a/openstackclient/network/v2/floating_ip.py b/openstackclient/network/v2/floating_ip.py index bb75540c..e8057628 100644 --- a/openstackclient/network/v2/floating_ip.py +++ b/openstackclient/network/v2/floating_ip.py @@ -203,12 +203,16 @@ class ListFloatingIP(common.NetworkAndComputeLister): 'floating_ip_address', 'fixed_ip_address', 'port_id', + 'floating_network_id', + 'project_id', ) headers = ( 'ID', 'Floating IP Address', 'Fixed IP Address', 'Port', + 'Floating Network', + 'Project', ) query = {} diff --git a/openstackclient/network/v2/network.py b/openstackclient/network/v2/network.py index dbf1b601..40183b73 100644 --- a/openstackclient/network/v2/network.py +++ b/openstackclient/network/v2/network.py @@ -42,6 +42,7 @@ def _get_columns(item): columns = list(item.keys()) if 'tenant_id' in columns: columns.remove('tenant_id') + if 'project_id' not in columns: columns.append('project_id') return tuple(sorted(columns)) diff --git a/openstackclient/network/v2/network_qos_policy.py b/openstackclient/network/v2/network_qos_policy.py new file mode 100644 index 00000000..a8fcfc59 --- /dev/null +++ b/openstackclient/network/v2/network_qos_policy.py @@ -0,0 +1,231 @@ +# Copyright (c) 2016, Intel Corporation. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import logging + +from osc_lib.command import command +from osc_lib import exceptions +from osc_lib import utils + +from openstackclient.i18n import _ +from openstackclient.identity import common as identity_common + + +LOG = logging.getLogger(__name__) + + +def _get_columns(item): + columns = list(item.keys()) + if 'tenant_id' in columns: + columns.remove('tenant_id') + columns.append('project_id') + return tuple(sorted(columns)) + + +def _get_attrs(client_manager, parsed_args): + attrs = {} + if parsed_args.name is not None: + attrs['name'] = str(parsed_args.name) + if parsed_args.description is not None: + attrs['description'] = parsed_args.description + if parsed_args.share: + attrs['shared'] = True + if parsed_args.no_share: + attrs['shared'] = False + if parsed_args.project is not None: + identity_client = client_manager.identity + project_id = identity_common.find_project( + identity_client, + parsed_args.project, + parsed_args.project_domain, + ).id + attrs['tenant_id'] = project_id + + return attrs + + +class CreateNetworkQosPolicy(command.ShowOne): + """Create a QoS policy""" + + def get_parser(self, prog_name): + parser = super(CreateNetworkQosPolicy, self).get_parser(prog_name) + parser.add_argument( + 'name', + metavar='<name>', + help=_("Name of QoS policy to create") + ) + parser.add_argument( + '--description', + metavar='<description>', + help=_("Description of the QoS policy") + ) + share_group = parser.add_mutually_exclusive_group() + share_group.add_argument( + '--share', + action='store_true', + default=None, + help=_("Make the QoS policy accessible by other projects") + ) + share_group.add_argument( + '--no-share', + action='store_true', + help=_("Make the QoS policy not accessible by other projects " + "(default)") + ) + parser.add_argument( + '--project', + metavar='<project>', + help=_("Owner's project (name or ID)") + ) + identity_common.add_project_domain_option_to_parser(parser) + return parser + + def take_action(self, parsed_args): + client = self.app.client_manager.network + attrs = _get_attrs(self.app.client_manager, parsed_args) + obj = client.create_qos_policy(**attrs) + columns = _get_columns(obj) + data = utils.get_item_properties(obj, columns, formatters={}) + return columns, data + + +class DeleteNetworkQosPolicy(command.Command): + """Delete Qos Policy(s)""" + + def get_parser(self, prog_name): + parser = super(DeleteNetworkQosPolicy, self).get_parser(prog_name) + parser.add_argument( + 'policy', + metavar="<qos-policy>", + nargs="+", + help=_("QoS policy(s) to delete (name or ID)") + ) + return parser + + def take_action(self, parsed_args): + client = self.app.client_manager.network + result = 0 + + for policy in parsed_args.policy: + try: + obj = client.find_qos_policy(policy, ignore_missing=False) + client.delete_qos_policy(obj) + except Exception as e: + result += 1 + LOG.error(_("Failed to delete QoS policy " + "name or ID '%(qos_policy)s': %(e)s"), + {'qos_policy': policy, 'e': e}) + + if result > 0: + total = len(parsed_args.policy) + msg = (_("%(result)s of %(total)s QoS policies failed " + "to delete.") % {'result': result, 'total': total}) + raise exceptions.CommandError(msg) + + +class ListNetworkQosPolicy(command.Lister): + """List QoS policies""" + + def take_action(self, parsed_args): + client = self.app.client_manager.network + columns = ( + 'id', + 'name', + 'shared', + 'tenant_id', + ) + column_headers = ( + 'ID', + 'Name', + 'Shared', + 'Project', + ) + data = client.qos_policies() + + return (column_headers, + (utils.get_item_properties( + s, columns, formatters={}, + ) for s in data)) + + +class SetNetworkQosPolicy(command.Command): + """Set QoS policy properties""" + + def get_parser(self, prog_name): + parser = super(SetNetworkQosPolicy, self).get_parser(prog_name) + parser.add_argument( + 'policy', + metavar="<qos-policy>", + help=_("QoS policy to modify (name or ID)") + ) + parser.add_argument( + '--name', + metavar="<name>", + help=_('Set QoS policy name') + ) + parser.add_argument( + '--description', + metavar='<description>', + help=_("Description of the QoS policy") + ) + enable_group = parser.add_mutually_exclusive_group() + enable_group.add_argument( + '--share', + action='store_true', + help=_('Make the QoS policy accessible by other projects'), + ) + enable_group.add_argument( + '--no-share', + action='store_true', + help=_('Make the QoS policy not accessible by other projects'), + ) + return parser + + def take_action(self, parsed_args): + client = self.app.client_manager.network + obj = client.find_qos_policy( + parsed_args.policy, + ignore_missing=False) + attrs = {} + if parsed_args.name is not None: + attrs['name'] = parsed_args.name + if parsed_args.share: + attrs['shared'] = True + if parsed_args.no_share: + attrs['shared'] = False + if parsed_args.description is not None: + attrs['description'] = parsed_args.description + client.update_qos_policy(obj, **attrs) + + +class ShowNetworkQosPolicy(command.ShowOne): + """Display QoS policy details""" + + def get_parser(self, prog_name): + parser = super(ShowNetworkQosPolicy, self).get_parser(prog_name) + parser.add_argument( + 'policy', + metavar="<qos-policy>", + help=_("QoS policy to display (name or ID)") + ) + return parser + + def take_action(self, parsed_args): + client = self.app.client_manager.network + obj = client.find_qos_policy(parsed_args.policy, + ignore_missing=False) + columns = _get_columns(obj) + data = utils.get_item_properties(obj, columns) + return columns, data diff --git a/openstackclient/network/v2/port.py b/openstackclient/network/v2/port.py index 92b286a9..86174d53 100644 --- a/openstackclient/network/v2/port.py +++ b/openstackclient/network/v2/port.py @@ -281,7 +281,23 @@ class CreatePort(command.ShowOne): help=_("Name of this port") ) # TODO(singhj): Add support for extended options: - # qos,security groups,dhcp, address pairs + # qos,dhcp, address pairs + secgroups = parser.add_mutually_exclusive_group() + secgroups.add_argument( + '--security-group', + metavar='<security-group>', + action='append', + dest='security_groups', + help=_("Security group to associate with this port (name or ID) " + "(repeat option to set multiple security groups)") + ) + secgroups.add_argument( + '--no-security-group', + dest='no_security_group', + action='store_true', + help=_("Associate no security groups with this port") + ) + return parser def take_action(self, parsed_args): @@ -291,6 +307,14 @@ class CreatePort(command.ShowOne): parsed_args.network = _network.id _prepare_fixed_ips(self.app.client_manager, parsed_args) attrs = _get_attrs(self.app.client_manager, parsed_args) + + if parsed_args.security_groups: + attrs['security_groups'] = [client.find_security_group( + sg, ignore_missing=False).id + for sg in parsed_args.security_groups] + if parsed_args.no_security_group: + attrs['security_groups'] = [] + obj = client.create_port(**attrs) columns = _get_columns(obj) data = utils.get_item_properties(obj, columns, formatters=_formatters) @@ -360,6 +384,12 @@ class ListPort(command.Lister): metavar='<server>', help=_("List only ports attached to this server (name or ID)"), ) + parser.add_argument( + '--long', + action='store_true', + default=False, + help=_("List additional fields in output") + ) return parser def take_action(self, parsed_args): @@ -371,15 +401,20 @@ class ListPort(command.Lister): 'name', 'mac_address', 'fixed_ips', + 'status', ) column_headers = ( 'ID', 'Name', 'MAC Address', 'Fixed IP Addresses', + 'Status', ) filters = {} + if parsed_args.long: + columns += ('security_groups', 'device_owner',) + column_headers += ('Security Groups', 'Device Owner',) if parsed_args.device_owner is not None: filters['device_owner'] = parsed_args.device_owner if parsed_args.router: @@ -463,6 +498,21 @@ class SetPort(command.Command): metavar="<port>", help=_("Port to modify (name or ID)") ) + parser.add_argument( + '--security-group', + metavar='<security-group>', + action='append', + dest='security_groups', + help=_("Security group to associate with this port (name or ID) " + "(repeat option to set multiple security groups)") + ) + parser.add_argument( + '--no-security-group', + dest='no_security_group', + action='store_true', + help=_("Clear existing security groups associated with this port") + ) + return parser def take_action(self, parsed_args): @@ -490,6 +540,17 @@ class SetPort(command.Command): attrs['fixed_ips'] += [ip for ip in obj.fixed_ips if ip] elif parsed_args.no_fixed_ip: attrs['fixed_ips'] = [] + if parsed_args.security_groups and parsed_args.no_security_group: + attrs['security_groups'] = [client.find_security_group(sg, + ignore_missing=False).id + for sg in parsed_args.security_groups] + elif parsed_args.security_groups: + attrs['security_groups'] = obj.security_groups + for sg in parsed_args.security_groups: + sg_id = client.find_security_group(sg, ignore_missing=False).id + attrs['security_groups'].append(sg_id) + elif parsed_args.no_security_group: + attrs['security_groups'] = [] client.update_port(obj, **attrs) @@ -536,6 +597,15 @@ class UnsetPort(command.Command): help=_("Desired key which should be removed from binding:profile" "(repeat option to unset multiple binding:profile data)")) parser.add_argument( + '--security-group', + metavar='<security-group>', + action='append', + dest='security_groups', + help=_("Security group which should be removed this port (name " + "or ID) (repeat option to unset multiple security groups)") + ) + + parser.add_argument( 'port', metavar="<port>", help=_("Port to modify (name or ID)") @@ -550,6 +620,7 @@ class UnsetPort(command.Command): # Unset* classes tmp_fixed_ips = copy.deepcopy(obj.fixed_ips) tmp_binding_profile = copy.deepcopy(obj.binding_profile) + tmp_secgroups = copy.deepcopy(obj.security_groups) _prepare_fixed_ips(self.app.client_manager, parsed_args) attrs = {} if parsed_args.fixed_ip: @@ -568,5 +639,16 @@ class UnsetPort(command.Command): msg = _("Port does not contain binding-profile %s") % key raise exceptions.CommandError(msg) attrs['binding:profile'] = tmp_binding_profile + if parsed_args.security_groups: + try: + for sg in parsed_args.security_groups: + sg_id = client.find_security_group( + sg, ignore_missing=False).id + tmp_secgroups.remove(sg_id) + except ValueError: + msg = _("Port does not contain security group %s") % sg + raise exceptions.CommandError(msg) + attrs['security_groups'] = tmp_secgroups + if attrs: client.update_port(obj, **attrs) diff --git a/openstackclient/network/v2/router.py b/openstackclient/network/v2/router.py index 48a3a92c..d96c314a 100644 --- a/openstackclient/network/v2/router.py +++ b/openstackclient/network/v2/router.py @@ -261,6 +261,22 @@ class ListRouter(command.Lister): def get_parser(self, prog_name): parser = super(ListRouter, self).get_parser(prog_name) parser.add_argument( + '--name', + metavar='<name>', + help=_("List routers according to their name") + ) + admin_state_group = parser.add_mutually_exclusive_group() + admin_state_group.add_argument( + '--enable', + action='store_true', + help=_("List enabled routers") + ) + admin_state_group.add_argument( + '--disable', + action='store_true', + help=_("List disabled routers") + ) + parser.add_argument( '--long', action='store_true', default=False, @@ -289,6 +305,17 @@ class ListRouter(command.Lister): 'HA', 'Project', ) + + args = {} + + if parsed_args.name is not None: + args['name'] = parsed_args.name + + if parsed_args.enable: + args['admin_state_up'] = True + elif parsed_args.disable: + args['admin_state_up'] = False + if parsed_args.long: columns = columns + ( 'routes', @@ -308,7 +335,7 @@ class ListRouter(command.Lister): 'Availability zones', ) - data = client.routers() + data = client.routers(**args) return (column_headers, (utils.get_item_properties( s, columns, @@ -433,11 +460,19 @@ class SetRouter(command.Command): action='store_true', help=argparse.SUPPRESS, ) - - # TODO(tangchen): Support setting 'ha' property in 'router set' - # command. It appears that changing the ha state is supported by - # neutron under certain conditions. - + routes_ha = parser.add_mutually_exclusive_group() + routes_ha.add_argument( + '--ha', + action='store_true', + help=_("Set the router as highly available " + "(disabled router only)") + ) + routes_ha.add_argument( + '--no-ha', + action='store_true', + help=_("Clear high availablability attribute of the router " + "(disabled router only)") + ) # TODO(tangchen): Support setting 'external_gateway_info' property in # 'router set' command. @@ -451,6 +486,10 @@ class SetRouter(command.Command): attrs = _get_attrs(self.app.client_manager, parsed_args) # Get the route attributes. + if parsed_args.ha: + attrs['ha'] = True + elif parsed_args.no_ha: + attrs['ha'] = False if parsed_args.no_route: attrs['routes'] = [] elif parsed_args.clear_routes: @@ -520,12 +559,11 @@ class UnsetRouter(command.Command): if parsed_args.routes: try: for route in parsed_args.routes: + route['nexthop'] = route.pop('gateway') tmp_routes.remove(route) except ValueError: msg = (_("Router does not contain route %s") % route) raise exceptions.CommandError(msg) - for route in tmp_routes: - route['nexthop'] = route.pop('gateway') attrs['routes'] = tmp_routes if attrs: client.update_router(obj, **attrs) diff --git a/openstackclient/network/v2/subnet.py b/openstackclient/network/v2/subnet.py index 1b778c91..f1ecb5a7 100644 --- a/openstackclient/network/v2/subnet.py +++ b/openstackclient/network/v2/subnet.py @@ -542,7 +542,7 @@ class SetSubnet(command.Command): if not parsed_args.no_allocation_pool: attrs['allocation_pools'] += obj.allocation_pools elif parsed_args.no_allocation_pool: - attrs['allocation_pools'] = '' + attrs['allocation_pools'] = [] if 'service_types' in attrs: attrs['service_types'] += obj.service_types client.update_subnet(obj, **attrs) diff --git a/openstackclient/network/v2/subnet_pool.py b/openstackclient/network/v2/subnet_pool.py index a01d2f7b..a29c4518 100644 --- a/openstackclient/network/v2/subnet_pool.py +++ b/openstackclient/network/v2/subnet_pool.py @@ -238,40 +238,42 @@ class ListSubnetPool(command.Lister): shared_group.add_argument( '--share', action='store_true', - help=_("List subnets shared between projects"), + help=_("List subnet pools shared between projects"), ) shared_group.add_argument( '--no-share', action='store_true', - help=_("List subnets not shared between projects"), + help=_("List subnet pools not shared between projects"), ) default_group = parser.add_mutually_exclusive_group() default_group.add_argument( '--default', action='store_true', - help=_("List subnets used as the default external subnet pool"), + help=_("List subnet pools used as the default external " + "subnet pool"), ) default_group.add_argument( '--no-default', action='store_true', - help=_("List subnets not used as the default external subnet pool") + help=_("List subnet pools not used as the default external " + "subnet pool") ) parser.add_argument( '--project', metavar='<project>', - help=_("List subnets according to their project (name or ID)") + help=_("List subnet pools according to their project (name or ID)") ) identity_common.add_project_domain_option_to_parser(parser) parser.add_argument( '--name', metavar='<name>', - help=_("List only subnets of given name in output") + help=_("List only subnet pools of given name in output") ) parser.add_argument( '--address-scope', metavar='<address-scope>', - help=_("List only subnets of given address scope (name or ID) " - "in output") + help=_("List only subnet pools of given address scope " + "(name or ID) in output") ) return parser diff --git a/openstackclient/object/v1/account.py b/openstackclient/object/v1/account.py index 801fe450..2fe00ecb 100644 --- a/openstackclient/object/v1/account.py +++ b/openstackclient/object/v1/account.py @@ -18,6 +18,8 @@ from osc_lib.command import command from osc_lib import utils import six +from openstackclient.i18n import _ + class SetAccount(command.Command): """Set account properties""" @@ -29,8 +31,8 @@ class SetAccount(command.Command): metavar="<key=value>", required=True, action=parseractions.KeyValueAction, - help="Set a property on this account " - "(repeat option to set multiple properties)" + help=_("Set a property on this account " + "(repeat option to set multiple properties)") ) return parser @@ -61,8 +63,8 @@ class UnsetAccount(command.Command): required=True, action='append', default=[], - help='Property to remove from account ' - '(repeat option to remove multiple properties)', + help=_('Property to remove from account ' + '(repeat option to remove multiple properties)'), ) return parser diff --git a/openstackclient/object/v1/container.py b/openstackclient/object/v1/container.py index 2f0d4ac2..01964d0c 100644 --- a/openstackclient/object/v1/container.py +++ b/openstackclient/object/v1/container.py @@ -37,7 +37,7 @@ class CreateContainer(command.Lister): 'containers', metavar='<container-name>', nargs="+", - help='New container name(s)', + help=_('New container name(s)'), ) return parser @@ -71,13 +71,13 @@ class DeleteContainer(command.Command): '--recursive', '-r', action='store_true', default=False, - help='Recursively delete objects and container', + help=_('Recursively delete objects and container'), ) parser.add_argument( 'containers', metavar='<container>', nargs="+", - help='Container(s) to delete', + help=_('Container(s) to delete'), ) return parser @@ -105,35 +105,35 @@ class ListContainer(command.Lister): parser.add_argument( "--prefix", metavar="<prefix>", - help="Filter list using <prefix>", + help=_("Filter list using <prefix>"), ) parser.add_argument( "--marker", metavar="<marker>", - help="Anchor for paging", + help=_("Anchor for paging"), ) parser.add_argument( "--end-marker", metavar="<end-marker>", - help="End anchor for paging", + help=_("End anchor for paging"), ) parser.add_argument( "--limit", metavar="<limit>", type=int, - help="Limit the number of containers returned", + help=_("Limit the number of containers returned"), ) parser.add_argument( '--long', action='store_true', default=False, - help='List additional fields in output', + help=_('List additional fields in output'), ) parser.add_argument( '--all', action='store_true', default=False, - help='List all containers (default is 10000)', + help=_('List all containers (default is 10000)'), ) return parser @@ -175,7 +175,7 @@ class SaveContainer(command.Command): parser.add_argument( 'container', metavar='<container>', - help='Container to save', + help=_('Container to save'), ) return parser @@ -193,15 +193,15 @@ class SetContainer(command.Command): parser.add_argument( 'container', metavar='<container>', - help='Container to modify', + help=_('Container to modify'), ) parser.add_argument( "--property", metavar="<key=value>", required=True, action=parseractions.KeyValueAction, - help="Set a property on this container " - "(repeat option to set multiple properties)" + help=_("Set a property on this container " + "(repeat option to set multiple properties)") ) return parser @@ -220,7 +220,7 @@ class ShowContainer(command.ShowOne): parser.add_argument( 'container', metavar='<container>', - help='Container to display', + help=_('Container to display'), ) return parser @@ -243,7 +243,7 @@ class UnsetContainer(command.Command): parser.add_argument( 'container', metavar='<container>', - help='Container to modify', + help=_('Container to modify'), ) parser.add_argument( '--property', @@ -251,8 +251,8 @@ class UnsetContainer(command.Command): required=True, action='append', default=[], - help='Property to remove from container ' - '(repeat option to remove multiple properties)', + help=_('Property to remove from container ' + '(repeat option to remove multiple properties)'), ) return parser diff --git a/openstackclient/object/v1/object.py b/openstackclient/object/v1/object.py index 88f6815e..3c47ee04 100644 --- a/openstackclient/object/v1/object.py +++ b/openstackclient/object/v1/object.py @@ -37,19 +37,19 @@ class CreateObject(command.Lister): parser.add_argument( 'container', metavar='<container>', - help='Container for new object', + help=_('Container for new object'), ) parser.add_argument( 'objects', metavar='<filename>', nargs="+", - help='Local filename(s) to upload', + help=_('Local filename(s) to upload'), ) parser.add_argument( '--name', metavar='<name>', - help='Upload a file and rename it. ' - 'Can only be used when uploading a single object' + help=_('Upload a file and rename it. ' + 'Can only be used when uploading a single object') ) return parser @@ -88,13 +88,13 @@ class DeleteObject(command.Command): parser.add_argument( 'container', metavar='<container>', - help='Delete object(s) from <container>', + help=_('Delete object(s) from <container>'), ) parser.add_argument( 'objects', metavar='<object>', nargs="+", - help='Object(s) to delete', + help=_('Object(s) to delete'), ) return parser @@ -115,45 +115,45 @@ class ListObject(command.Lister): parser.add_argument( "container", metavar="<container>", - help="Container to list", + help=_("Container to list"), ) parser.add_argument( "--prefix", metavar="<prefix>", - help="Filter list using <prefix>", + help=_("Filter list using <prefix>"), ) parser.add_argument( "--delimiter", metavar="<delimiter>", - help="Roll up items with <delimiter>", + help=_("Roll up items with <delimiter>"), ) parser.add_argument( "--marker", metavar="<marker>", - help="Anchor for paging", + help=_("Anchor for paging"), ) parser.add_argument( "--end-marker", metavar="<end-marker>", - help="End anchor for paging", + help=_("End anchor for paging"), ) parser.add_argument( "--limit", metavar="<limit>", type=int, - help="Limit the number of objects returned", + help=_("Limit the number of objects returned"), ) parser.add_argument( '--long', action='store_true', default=False, - help='List additional fields in output', + help=_('List additional fields in output'), ) parser.add_argument( '--all', action='store_true', default=False, - help='List all objects in container (default is 10000)', + help=_('List all objects in container (default is 10000)'), ) return parser @@ -204,17 +204,17 @@ class SaveObject(command.Command): parser.add_argument( "--file", metavar="<filename>", - help="Destination filename (defaults to object name)", + help=_("Destination filename (defaults to object name)"), ) parser.add_argument( 'container', metavar='<container>', - help='Download <object> from <container>', + help=_('Download <object> from <container>'), ) parser.add_argument( "object", metavar="<object>", - help="Object to save", + help=_("Object to save"), ) return parser @@ -234,20 +234,20 @@ class SetObject(command.Command): parser.add_argument( 'container', metavar='<container>', - help='Modify <object> from <container>', + help=_('Modify <object> from <container>'), ) parser.add_argument( 'object', metavar='<object>', - help='Object to modify', + help=_('Object to modify'), ) parser.add_argument( "--property", metavar="<key=value>", required=True, action=parseractions.KeyValueAction, - help="Set a property on this object " - "(repeat option to set multiple properties)" + help=_("Set a property on this object " + "(repeat option to set multiple properties)") ) return parser @@ -267,12 +267,12 @@ class ShowObject(command.ShowOne): parser.add_argument( 'container', metavar='<container>', - help='Display <object> from <container>', + help=_('Display <object> from <container>'), ) parser.add_argument( 'object', metavar='<object>', - help='Object to display', + help=_('Object to display'), ) return parser @@ -296,12 +296,12 @@ class UnsetObject(command.Command): parser.add_argument( 'container', metavar='<container>', - help='Modify <object> from <container>', + help=_('Modify <object> from <container>'), ) parser.add_argument( 'object', metavar='<object>', - help='Object to modify', + help=_('Object to modify'), ) parser.add_argument( '--property', @@ -309,8 +309,8 @@ class UnsetObject(command.Command): required=True, action='append', default=[], - help='Property to remove from object ' - '(repeat option to remove multiple properties)', + help=_('Property to remove from object ' + '(repeat option to remove multiple properties)'), ) return parser diff --git a/openstackclient/shell.py b/openstackclient/shell.py index 3971b6ef..be4b5283 100644 --- a/openstackclient/shell.py +++ b/openstackclient/shell.py @@ -93,10 +93,12 @@ class OpenStackShell(shell.OpenStackShell): mod_versions = getattr(mod, 'API_VERSIONS', None) if not skip_old_check and mod_versions: if version_opt not in mod_versions: + sorted_versions = sorted( + mod.API_VERSIONS.keys(), + key=lambda s: list(map(int, s.split('.')))) self.log.warning( - "%s version %s is not in supported versions %s" - % (api, version_opt, - ', '.join(list(mod.API_VERSIONS.keys())))) + "%s version %s is not in supported versions: %s" + % (api, version_opt, ', '.join(sorted_versions))) # Command groups deal only with major versions version = '.v' + version_opt.replace('.', '_').split('_')[0] diff --git a/openstackclient/tests/functional/base.py b/openstackclient/tests/functional/base.py index 298b2454..885abc02 100644 --- a/openstackclient/tests/functional/base.py +++ b/openstackclient/tests/functional/base.py @@ -63,8 +63,8 @@ class TestCase(testtools.TestCase): return cls.openstack('extension list ' + opts) @classmethod - def get_opts(cls, fields, format='value'): - return ' -f {0} {1}'.format(format, + def get_opts(cls, fields, output_format='value'): + return ' -f {0} {1}'.format(output_format, ' '.join(['-c ' + it for it in fields])) @classmethod diff --git a/openstackclient/tests/functional/network/v2/test_network_qos_policy.py b/openstackclient/tests/functional/network/v2/test_network_qos_policy.py new file mode 100644 index 00000000..07dea31b --- /dev/null +++ b/openstackclient/tests/functional/network/v2/test_network_qos_policy.py @@ -0,0 +1,55 @@ +# Copyright (c) 2016, Intel Corporation. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import uuid + +from openstackclient.tests.functional import base + + +class QosPolicyTests(base.TestCase): + """Functional tests for QoS policy. """ + NAME = uuid.uuid4().hex + HEADERS = ['Name'] + FIELDS = ['name'] + + @classmethod + def setUpClass(cls): + opts = cls.get_opts(cls.FIELDS) + raw_output = cls.openstack('network qos policy create ' + cls.NAME + + opts) + cls.assertOutput(cls.NAME + "\n", raw_output) + + @classmethod + def tearDownClass(cls): + raw_output = cls.openstack('network qos policy delete ' + cls.NAME) + cls.assertOutput('', raw_output) + + def test_qos_policy_list(self): + opts = self.get_opts(self.HEADERS) + raw_output = self.openstack('network qos policy list' + opts) + self.assertIn(self.NAME, raw_output) + + def test_qos_policy_show(self): + opts = self.get_opts(self.FIELDS) + raw_output = self.openstack('network qos policy show ' + self.NAME + + opts) + self.assertEqual(self.NAME + "\n", raw_output) + + def test_qos_policy_set(self): + self.openstack('network qos policy set --share ' + self.NAME) + opts = self.get_opts(['shared']) + raw_output = self.openstack('network qos policy show ' + self.NAME + + opts) + self.assertEqual("True\n", raw_output) diff --git a/openstackclient/tests/unit/compute/v2/test_hypervisor.py b/openstackclient/tests/unit/compute/v2/test_hypervisor.py index e39570af..7200d04e 100644 --- a/openstackclient/tests/unit/compute/v2/test_hypervisor.py +++ b/openstackclient/tests/unit/compute/v2/test_hypervisor.py @@ -48,19 +48,63 @@ class TestHypervisorList(TestHypervisor): self.columns = ( "ID", - "Hypervisor Hostname" + "Hypervisor Hostname", + "Hypervisor Type", + "Host IP", + "State" + ) + self.columns_long = ( + "ID", + "Hypervisor Hostname", + "Hypervisor Type", + "Host IP", + "State", + "vCPUs Used", + "vCPUs", + "Memory MB Used", + "Memory MB" ) self.data = ( ( self.hypervisors[0].id, self.hypervisors[0].hypervisor_hostname, + self.hypervisors[0].hypervisor_type, + self.hypervisors[0].host_ip, + self.hypervisors[0].state ), ( self.hypervisors[1].id, self.hypervisors[1].hypervisor_hostname, + self.hypervisors[1].hypervisor_type, + self.hypervisors[1].host_ip, + self.hypervisors[1].state ), ) + self.data_long = ( + ( + self.hypervisors[0].id, + self.hypervisors[0].hypervisor_hostname, + self.hypervisors[0].hypervisor_type, + self.hypervisors[0].host_ip, + self.hypervisors[0].state, + self.hypervisors[0].vcpus_used, + self.hypervisors[0].vcpus, + self.hypervisors[0].memory_mb_used, + self.hypervisors[0].memory_mb + ), + ( + self.hypervisors[1].id, + self.hypervisors[1].hypervisor_hostname, + self.hypervisors[1].hypervisor_type, + self.hypervisors[1].host_ip, + self.hypervisors[1].state, + self.hypervisors[1].vcpus_used, + self.hypervisors[1].vcpus, + self.hypervisors[1].memory_mb_used, + self.hypervisors[1].memory_mb + ), + ) # Get the command object to test self.cmd = hypervisor.ListHypervisor(self.app, None) @@ -93,6 +137,9 @@ class TestHypervisorList(TestHypervisor): ( self.hypervisors[0].id, self.hypervisors[0].hypervisor_hostname, + self.hypervisors[1].hypervisor_type, + self.hypervisors[1].host_ip, + self.hypervisors[1].state, ), ) @@ -123,6 +170,24 @@ class TestHypervisorList(TestHypervisor): self.cmd.take_action, parsed_args) + def test_hypervisor_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.hypervisors_mock.list.assert_called_with() + self.assertEqual(self.columns_long, columns) + self.assertEqual(self.data_long, tuple(data)) + class TestHypervisorShow(TestHypervisor): diff --git a/openstackclient/tests/unit/compute/v2/test_keypair.py b/openstackclient/tests/unit/compute/v2/test_keypair.py index cb008545..efc5463c 100644 --- a/openstackclient/tests/unit/compute/v2/test_keypair.py +++ b/openstackclient/tests/unit/compute/v2/test_keypair.py @@ -179,8 +179,7 @@ class TestKeypairDelete(TestKeypair): self.cmd.take_action(parsed_args) self.fail('CommandError should be raised.') except exceptions.CommandError as e: - self.assertEqual('1 of 2 public keys failed to delete.', - str(e)) + self.assertEqual('1 of 2 keys failed to delete.', str(e)) find_mock.assert_any_call( self.keypairs_mock, self.keypairs[0].name) diff --git a/openstackclient/tests/unit/image/v1/fakes.py b/openstackclient/tests/unit/image/v1/fakes.py index a8e52fa3..080356ee 100644 --- a/openstackclient/tests/unit/image/v1/fakes.py +++ b/openstackclient/tests/unit/image/v1/fakes.py @@ -13,7 +13,9 @@ # under the License. # +import copy import mock +import uuid from openstackclient.tests.unit import fakes from openstackclient.tests.unit import utils @@ -74,3 +76,45 @@ class TestImagev1(utils.TestCommand): endpoint=fakes.AUTH_URL, token=fakes.AUTH_TOKEN, ) + + +class FakeImage(object): + """Fake one or more images.""" + + @staticmethod + def create_one_image(attrs=None): + """Create a fake image. + + :param Dictionary attrs: + A dictionary with all attrbutes of image + :return: + A FakeResource object with id, name, owner, protected, + visibility and tags attrs + """ + attrs = attrs or {} + + # Set default attribute + image_info = { + 'id': str(uuid.uuid4()), + 'name': 'image-name' + uuid.uuid4().hex, + 'owner': 'image-owner' + uuid.uuid4().hex, + 'container_format': '', + 'disk_format': '', + 'min_disk': 0, + 'min_ram': 0, + 'is_public': True, + 'protected': False, + 'properties': { + 'Alpha': 'a', + 'Beta': 'b', + 'Gamma': 'g'}, + } + + # Overwrite default attributes if there are some attributes set + image_info.update(attrs) + + image = fakes.FakeResource( + info=copy.deepcopy(image_info), + loaded=True) + + return image diff --git a/openstackclient/tests/unit/image/v1/test_image.py b/openstackclient/tests/unit/image/v1/test_image.py index a6bc80a0..aef74f04 100644 --- a/openstackclient/tests/unit/image/v1/test_image.py +++ b/openstackclient/tests/unit/image/v1/test_image.py @@ -17,6 +17,7 @@ import copy import mock from osc_lib import exceptions +from osc_lib import utils from openstackclient.image.v1 import image from openstackclient.tests.unit import fakes @@ -35,25 +36,39 @@ class TestImage(image_fakes.TestImagev1): class TestImageCreate(TestImage): + new_image = image_fakes.FakeImage.create_one_image() + columns = ( + 'container_format', + 'disk_format', + 'id', + 'is_public', + 'min_disk', + 'min_ram', + 'name', + 'owner', + 'properties', + 'protected', + ) + data = ( + new_image.container_format, + new_image.disk_format, + new_image.id, + new_image.is_public, + new_image.min_disk, + new_image.min_ram, + new_image.name, + new_image.owner, + utils.format_dict(new_image.properties), + new_image.protected, + ) + def setUp(self): super(TestImageCreate, self).setUp() - self.images_mock.create.return_value = fakes.FakeResource( - None, - copy.deepcopy(image_fakes.IMAGE), - loaded=True, - ) + self.images_mock.create.return_value = self.new_image # This is the return value for utils.find_resource() - self.images_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(image_fakes.IMAGE), - loaded=True, - ) - self.images_mock.update.return_value = fakes.FakeResource( - None, - copy.deepcopy(image_fakes.IMAGE), - loaded=True, - ) + self.images_mock.get.return_value = self.new_image + self.images_mock.update.return_value = self.new_image # Get the command object to test self.cmd = image.CreateImage(self.app, None) @@ -65,12 +80,12 @@ class TestImageCreate(TestImage): } self.images_mock.configure_mock(**mock_exception) arglist = [ - image_fakes.image_name, + self.new_image.name, ] verifylist = [ ('container_format', image.DEFAULT_CONTAINER_FORMAT), ('disk_format', image.DEFAULT_DISK_FORMAT), - ('name', image_fakes.image_name), + ('name', self.new_image.name), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -81,7 +96,7 @@ class TestImageCreate(TestImage): # ImageManager.create(name=, **) self.images_mock.create.assert_called_with( - name=image_fakes.image_name, + name=self.new_image.name, container_format=image.DEFAULT_CONTAINER_FORMAT, disk_format=image.DEFAULT_DISK_FORMAT, data=mock.ANY, @@ -90,8 +105,8 @@ class TestImageCreate(TestImage): # Verify update() was not called, if it was show the args self.assertEqual(self.images_mock.update.call_args_list, []) - self.assertEqual(image_fakes.IMAGE_columns, columns) - self.assertEqual(image_fakes.IMAGE_data, data) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, data) def test_image_reserve_options(self): mock_exception = { @@ -107,7 +122,7 @@ class TestImageCreate(TestImage): '--protected', '--private', '--project', 'q', - image_fakes.image_name, + self.new_image.name, ] verifylist = [ ('container_format', 'ovf'), @@ -119,7 +134,7 @@ class TestImageCreate(TestImage): ('public', False), ('private', True), ('project', 'q'), - ('name', image_fakes.image_name), + ('name', self.new_image.name), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -130,7 +145,7 @@ class TestImageCreate(TestImage): # ImageManager.create(name=, **) self.images_mock.create.assert_called_with( - name=image_fakes.image_name, + name=self.new_image.name, container_format='ovf', disk_format='fs', min_disk=10, @@ -144,14 +159,14 @@ class TestImageCreate(TestImage): # Verify update() was not called, if it was show the args self.assertEqual(self.images_mock.update.call_args_list, []) - self.assertEqual(image_fakes.IMAGE_columns, columns) - self.assertEqual(image_fakes.IMAGE_data, data) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, data) @mock.patch('openstackclient.image.v1.image.io.open', name='Open') def test_image_create_file(self, mock_open): mock_file = mock.Mock(name='File') mock_open.return_value = mock_file - mock_open.read.return_value = image_fakes.image_data + mock_open.read.return_value = self.data mock_exception = { 'find.side_effect': exceptions.CommandError('x'), 'get.side_effect': exceptions.CommandError('x'), @@ -164,7 +179,7 @@ class TestImageCreate(TestImage): '--public', '--property', 'Alpha=1', '--property', 'Beta=2', - image_fakes.image_name, + self.new_image.name, ] verifylist = [ ('file', 'filer'), @@ -173,7 +188,7 @@ class TestImageCreate(TestImage): ('public', True), ('private', False), ('properties', {'Alpha': '1', 'Beta': '2'}), - ('name', image_fakes.image_name), + ('name', self.new_image.name), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -193,7 +208,7 @@ class TestImageCreate(TestImage): # ImageManager.create(name=, **) self.images_mock.create.assert_called_with( - name=image_fakes.image_name, + name=self.new_image.name, container_format=image.DEFAULT_CONTAINER_FORMAT, disk_format=image.DEFAULT_DISK_FORMAT, protected=False, @@ -208,21 +223,19 @@ class TestImageCreate(TestImage): # Verify update() was not called, if it was show the args self.assertEqual(self.images_mock.update.call_args_list, []) - self.assertEqual(image_fakes.IMAGE_columns, columns) - self.assertEqual(image_fakes.IMAGE_data, data) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, data) class TestImageDelete(TestImage): + _image = image_fakes.FakeImage.create_one_image() + def setUp(self): super(TestImageDelete, self).setUp() # This is the return value for utils.find_resource() - self.images_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(image_fakes.IMAGE), - loaded=True, - ) + self.images_mock.get.return_value = self._image self.images_mock.delete.return_value = None # Get the command object to test @@ -230,21 +243,23 @@ class TestImageDelete(TestImage): def test_image_delete_no_options(self): arglist = [ - image_fakes.image_id, + self._image.id, ] verifylist = [ - ('images', [image_fakes.image_id]), + ('images', [self._image.id]), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) result = self.cmd.take_action(parsed_args) - self.images_mock.delete.assert_called_with(image_fakes.image_id) + self.images_mock.delete.assert_called_with(self._image.id) self.assertIsNone(result) class TestImageList(TestImage): + _image = image_fakes.FakeImage.create_one_image() + columns = ( 'ID', 'Name', @@ -252,18 +267,33 @@ class TestImageList(TestImage): ) datalist = ( ( - image_fakes.image_id, - image_fakes.image_name, + _image.id, + _image.name, '', ), ) + # create a image_info as the side_effect of the fake image_list() + info = { + 'id': _image.id, + 'name': _image.name, + 'owner': _image.owner, + 'container_format': _image.container_format, + 'disk_format': _image.disk_format, + 'min_disk': _image.min_disk, + 'min_ram': _image.min_ram, + 'is_public': _image.is_public, + 'protected': _image.protected, + 'properties': _image.properties, + } + image_info = copy.deepcopy(info) + def setUp(self): super(TestImageList, self).setUp() self.api_mock = mock.Mock() self.api_mock.image_list.side_effect = [ - [copy.deepcopy(image_fakes.IMAGE)], [], + [self.image_info], [], ] self.app.client_manager.image.api = self.api_mock @@ -285,7 +315,7 @@ class TestImageList(TestImage): columns, data = self.cmd.take_action(parsed_args) self.api_mock.image_list.assert_called_with( detailed=True, - marker=image_fakes.image_id, + marker=self._image.id, ) self.assertEqual(self.columns, columns) @@ -309,7 +339,7 @@ class TestImageList(TestImage): self.api_mock.image_list.assert_called_with( detailed=True, public=True, - marker=image_fakes.image_id, + marker=self._image.id, ) self.assertEqual(self.columns, columns) @@ -333,7 +363,7 @@ class TestImageList(TestImage): self.api_mock.image_list.assert_called_with( detailed=True, private=True, - marker=image_fakes.image_id, + marker=self._image.id, ) self.assertEqual(self.columns, columns) @@ -354,7 +384,7 @@ class TestImageList(TestImage): columns, data = self.cmd.take_action(parsed_args) self.api_mock.image_list.assert_called_with( detailed=True, - marker=image_fakes.image_id, + marker=self._image.id, ) collist = ( @@ -373,8 +403,8 @@ class TestImageList(TestImage): self.assertEqual(collist, columns) datalist = (( - image_fakes.image_id, - image_fakes.image_name, + self._image.id, + self._image.name, '', '', '', @@ -382,7 +412,7 @@ class TestImageList(TestImage): '', 'public', False, - image_fakes.image_owner, + self._image.owner, "Alpha='a', Beta='b', Gamma='g'", ), ) self.assertEqual(datalist, tuple(data)) @@ -390,7 +420,7 @@ class TestImageList(TestImage): @mock.patch('openstackclient.api.utils.simple_filter') def test_image_list_property_option(self, sf_mock): sf_mock.side_effect = [ - [copy.deepcopy(image_fakes.IMAGE)], [], + [self.image_info], [], ] arglist = [ @@ -407,10 +437,10 @@ class TestImageList(TestImage): columns, data = self.cmd.take_action(parsed_args) self.api_mock.image_list.assert_called_with( detailed=True, - marker=image_fakes.image_id, + marker=self._image.id, ) sf_mock.assert_called_with( - [image_fakes.IMAGE], + [self.image_info], attr='a', value='1', property_field='properties', @@ -422,7 +452,7 @@ class TestImageList(TestImage): @mock.patch('osc_lib.utils.sort_items') def test_image_list_sort_option(self, si_mock): si_mock.side_effect = [ - [copy.deepcopy(image_fakes.IMAGE)], [], + [self.image_info], [], ] arglist = ['--sort', 'name:asc'] @@ -435,10 +465,10 @@ class TestImageList(TestImage): columns, data = self.cmd.take_action(parsed_args) self.api_mock.image_list.assert_called_with( detailed=True, - marker=image_fakes.image_id, + marker=self._image.id, ) si_mock.assert_called_with( - [image_fakes.IMAGE], + [self.image_info], 'name:asc' ) @@ -448,36 +478,30 @@ class TestImageList(TestImage): class TestImageSet(TestImage): + _image = image_fakes.FakeImage.create_one_image() + def setUp(self): super(TestImageSet, self).setUp() # This is the return value for utils.find_resource() - self.images_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(image_fakes.IMAGE), - loaded=True, - ) - self.images_mock.update.return_value = fakes.FakeResource( - None, - copy.deepcopy(image_fakes.IMAGE), - loaded=True, - ) + self.images_mock.get.return_value = self._image + self.images_mock.update.return_value = self._image # Get the command object to test self.cmd = image.SetImage(self.app, None) def test_image_set_no_options(self): arglist = [ - image_fakes.image_name, + self._image.name, ] verifylist = [ - ('image', image_fakes.image_name), + ('image', self._image.name), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) result = self.cmd.take_action(parsed_args) - self.images_mock.update.assert_called_with(image_fakes.image_id, + self.images_mock.update.assert_called_with(self._image.id, **{}) self.assertIsNone(result) @@ -490,7 +514,7 @@ class TestImageSet(TestImage): '--disk-format', 'vmdk', '--size', '35165824', '--project', 'new-owner', - image_fakes.image_name, + self._image.name, ] verifylist = [ ('name', 'new-name'), @@ -500,7 +524,7 @@ class TestImageSet(TestImage): ('disk_format', 'vmdk'), ('size', 35165824), ('project', 'new-owner'), - ('image', image_fakes.image_name), + ('image', self._image.name), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -517,7 +541,7 @@ class TestImageSet(TestImage): } # ImageManager.update(image, **kwargs) self.images_mock.update.assert_called_with( - image_fakes.image_id, + self._image.id, **kwargs ) self.assertIsNone(result) @@ -526,14 +550,14 @@ class TestImageSet(TestImage): arglist = [ '--protected', '--private', - image_fakes.image_name, + self._image.name, ] verifylist = [ ('protected', True), ('unprotected', False), ('public', False), ('private', True), - ('image', image_fakes.image_name), + ('image', self._image.name), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -545,7 +569,7 @@ class TestImageSet(TestImage): } # ImageManager.update(image, **kwargs) self.images_mock.update.assert_called_with( - image_fakes.image_id, + self._image.id, **kwargs ) self.assertIsNone(result) @@ -554,14 +578,14 @@ class TestImageSet(TestImage): arglist = [ '--unprotected', '--public', - image_fakes.image_name, + self._image.name, ] verifylist = [ ('protected', False), ('unprotected', True), ('public', True), ('private', False), - ('image', image_fakes.image_name), + ('image', self._image.name), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -573,7 +597,7 @@ class TestImageSet(TestImage): } # ImageManager.update(image, **kwargs) self.images_mock.update.assert_called_with( - image_fakes.image_id, + self._image.id, **kwargs ) self.assertIsNone(result) @@ -582,11 +606,11 @@ class TestImageSet(TestImage): arglist = [ '--property', 'Alpha=1', '--property', 'Beta=2', - image_fakes.image_name, + self._image.name, ] verifylist = [ ('properties', {'Alpha': '1', 'Beta': '2'}), - ('image', image_fakes.image_name), + ('image', self._image.name), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -601,7 +625,7 @@ class TestImageSet(TestImage): } # ImageManager.update(image, **kwargs) self.images_mock.update.assert_called_with( - image_fakes.image_id, + self._image.id, **kwargs ) self.assertIsNone(result) @@ -624,7 +648,7 @@ class TestImageSet(TestImage): "volume_type": 'volume_type', "container_format": image.DEFAULT_CONTAINER_FORMAT, "disk_format": image.DEFAULT_DISK_FORMAT, - "image": image_fakes.image_name, + "image": self._image.name, } full_response = {"os-volume_upload_image": response} volumes_mock.upload_to_image.return_value = (201, full_response) @@ -632,7 +656,7 @@ class TestImageSet(TestImage): arglist = [ '--volume', 'volly', '--name', 'updated_image', - image_fakes.image_name, + self._image.name, ] verifylist = [ ('private', False), @@ -642,7 +666,7 @@ class TestImageSet(TestImage): ('volume', 'volly'), ('force', False), ('name', 'updated_image'), - ('image', image_fakes.image_name), + ('image', self._image.name), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -653,13 +677,13 @@ class TestImageSet(TestImage): volumes_mock.upload_to_image.assert_called_with( 'vol1', False, - image_fakes.image_name, + self._image.name, '', '', ) # ImageManager.update(image_id, remove_props=, **) self.images_mock.update.assert_called_with( - image_fakes.image_id, + self._image.id, name='updated_image', volume='volly', ) @@ -668,24 +692,46 @@ class TestImageSet(TestImage): class TestImageShow(TestImage): + _image = image_fakes.FakeImage.create_one_image() + columns = ( + 'container_format', + 'disk_format', + 'id', + 'is_public', + 'min_disk', + 'min_ram', + 'name', + 'owner', + 'properties', + 'protected', + ) + data = ( + _image.container_format, + _image.disk_format, + _image.id, + _image.is_public, + _image.min_disk, + _image.min_ram, + _image.name, + _image.owner, + utils.format_dict(_image.properties), + _image.protected, + ) + def setUp(self): super(TestImageShow, self).setUp() - self.images_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(image_fakes.IMAGE), - loaded=True, - ) + self.images_mock.get.return_value = self._image # Get the command object to test self.cmd = image.ShowImage(self.app, None) def test_image_show(self): arglist = [ - image_fakes.image_id, + self._image.id, ] verifylist = [ - ('image', image_fakes.image_id), + ('image', self._image.id), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -694,8 +740,8 @@ class TestImageShow(TestImage): # data to be shown. columns, data = self.cmd.take_action(parsed_args) self.images_mock.get.assert_called_with( - image_fakes.image_id, + self._image.id, ) - self.assertEqual(image_fakes.IMAGE_columns, columns) - self.assertEqual(image_fakes.IMAGE_data, data) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, data) diff --git a/openstackclient/tests/unit/network/v2/fakes.py b/openstackclient/tests/unit/network/v2/fakes.py index ed3579b7..90dd0892 100644 --- a/openstackclient/tests/unit/network/v2/fakes.py +++ b/openstackclient/tests/unit/network/v2/fakes.py @@ -634,6 +634,156 @@ class FakeNetworkRBAC(object): return mock.Mock(side_effect=rbac_policies) +class FakeNetworkQosBandwidthLimitRule(object): + """Fake one or more QoS bandwidth limit rules.""" + + @staticmethod + def create_one_qos_bandwidth_limit_rule(attrs=None): + """Create a fake QoS bandwidth limit rule. + + :param Dictionary attrs: + A dictionary with all attributes + :return: + A FakeResource object with id, qos_policy_id, max_kbps and + max_burst_kbps attributes. + """ + attrs = attrs or {} + + # Set default attributes. + qos_bandwidth_limit_rule_attrs = { + 'id': 'qos-bandwidth-limit-rule-id-' + uuid.uuid4().hex, + 'qos_policy_id': 'qos-policy-id-' + uuid.uuid4().hex, + 'max_kbps': 1500, + 'max_burst_kbps': 1200, + } + + # Overwrite default attributes. + qos_bandwidth_limit_rule_attrs.update(attrs) + + qos_bandwidth_limit_rule = fakes.FakeResource( + info=copy.deepcopy(qos_bandwidth_limit_rule_attrs), + loaded=True) + + return qos_bandwidth_limit_rule + + @staticmethod + def create_qos_bandwidth_limit_rules(attrs=None, count=2): + """Create multiple fake QoS bandwidth limit rules. + + :param Dictionary attrs: + A dictionary with all attributes + :param int count: + The number of QoS bandwidth limit rules to fake + :return: + A list of FakeResource objects faking the QoS bandwidth limit rules + """ + qos_policies = [] + for i in range(0, count): + qos_policies.append(FakeNetworkQosBandwidthLimitRule. + create_one_qos_bandwidth_limit_rule(attrs)) + + return qos_policies + + @staticmethod + def get_qos_bandwidth_limit_rules(qos_rules=None, count=2): + """Get a list of faked QoS bandwidth limit rules. + + If QoS bandwidth limit rules list is provided, then initialize the + Mock object with the list. Otherwise create one. + + :param List address scopes: + A list of FakeResource objects faking QoS bandwidth limit rules + :param int count: + The number of QoS bandwidth limit rules to fake + :return: + An iterable Mock object with side_effect set to a list of faked + qos bandwidth limit rules + """ + if qos_rules is None: + qos_rules = (FakeNetworkQosBandwidthLimitRule. + create_qos_bandwidth_limit_rules(count)) + return mock.Mock(side_effect=qos_rules) + + +class FakeNetworkQosPolicy(object): + """Fake one or more QoS policies.""" + + @staticmethod + def create_one_qos_policy(attrs=None): + """Create a fake QoS policy. + + :param Dictionary attrs: + A dictionary with all attributes + :return: + A FakeResource object with name, id, etc. + """ + attrs = attrs or {} + qos_id = attrs.get('id') or 'qos-policy-id-' + uuid.uuid4().hex + rule_attrs = {'qos_policy_id': qos_id} + rules = [ + FakeNetworkQosBandwidthLimitRule. + create_one_qos_bandwidth_limit_rule(rule_attrs)] + + # Set default attributes. + qos_policy_attrs = { + 'name': 'qos-policy-name-' + uuid.uuid4().hex, + 'id': qos_id, + 'tenant_id': 'project-id-' + uuid.uuid4().hex, + 'shared': False, + 'description': 'qos-policy-description-' + uuid.uuid4().hex, + 'rules': rules, + } + + # Overwrite default attributes. + qos_policy_attrs.update(attrs) + + qos_policy = fakes.FakeResource( + info=copy.deepcopy(qos_policy_attrs), + loaded=True) + + # Set attributes with special mapping in OpenStack SDK. + qos_policy.project_id = qos_policy_attrs['tenant_id'] + + return qos_policy + + @staticmethod + def create_qos_policies(attrs=None, count=2): + """Create multiple fake QoS policies. + + :param Dictionary attrs: + A dictionary with all attributes + :param int count: + The number of QoS policies to fake + :return: + A list of FakeResource objects faking the QoS policies + """ + qos_policies = [] + for i in range(0, count): + qos_policies.append( + FakeNetworkQosPolicy.create_one_qos_policy(attrs)) + + return qos_policies + + @staticmethod + def get_qos_policies(qos_policies=None, count=2): + """Get an iterable MagicMock object with a list of faked QoS policies. + + If qos policies list is provided, then initialize the Mock object + with the list. Otherwise create one. + + :param List address scopes: + A list of FakeResource objects faking qos policies + :param int count: + The number of QoS policies to fake + :return: + An iterable Mock object with side_effect set to a list of faked + QoS policies + """ + if qos_policies is None: + qos_policies = FakeNetworkQosPolicy.create_qos_policies(count) + return mock.Mock(side_effect=qos_policies) + + class FakeRouter(object): """Fake one or more routers.""" diff --git a/openstackclient/tests/unit/network/v2/test_floating_ip.py b/openstackclient/tests/unit/network/v2/test_floating_ip.py index 1f30f2e9..578c6154 100644 --- a/openstackclient/tests/unit/network/v2/test_floating_ip.py +++ b/openstackclient/tests/unit/network/v2/test_floating_ip.py @@ -237,6 +237,8 @@ class TestListFloatingIPNetwork(TestFloatingIPNetwork): 'Floating IP Address', 'Fixed IP Address', 'Port', + 'Floating Network', + 'Project', ) data = [] @@ -246,6 +248,8 @@ class TestListFloatingIPNetwork(TestFloatingIPNetwork): ip.floating_ip_address, ip.fixed_ip_address, ip.port_id, + ip.floating_network_id, + ip.tenant_id, )) def setUp(self): diff --git a/openstackclient/tests/unit/network/v2/test_network.py b/openstackclient/tests/unit/network/v2/test_network.py index 50a60c2d..828da4a2 100644 --- a/openstackclient/tests/unit/network/v2/test_network.py +++ b/openstackclient/tests/unit/network/v2/test_network.py @@ -609,7 +609,7 @@ class TestListNetwork(TestNetwork): self.assertEqual(self.columns, columns) self.assertEqual(self.data, list(data)) - def test_networ_list_project_domain(self): + def test_network_list_project_domain(self): project = identity_fakes_v3.FakeProject.create_one_project() self.projects_mock.get.return_value = project arglist = [ @@ -625,6 +625,8 @@ class TestListNetwork(TestNetwork): filters = {'tenant_id': project.id} self.network.networks.assert_called_once_with(**filters) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, list(data)) def test_network_list_share(self): arglist = [ diff --git a/openstackclient/tests/unit/network/v2/test_network_qos_policy.py b/openstackclient/tests/unit/network/v2/test_network_qos_policy.py new file mode 100644 index 00000000..bd30579a --- /dev/null +++ b/openstackclient/tests/unit/network/v2/test_network_qos_policy.py @@ -0,0 +1,380 @@ +# Copyright (c) 2016, Intel Corporation. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import mock +from mock import call + +from osc_lib import exceptions + +from openstackclient.network.v2 import network_qos_policy +from openstackclient.tests.unit.identity.v3 import fakes as identity_fakes_v3 +from openstackclient.tests.unit.network.v2 import fakes as network_fakes +from openstackclient.tests.unit import utils as tests_utils + + +class TestQosPolicy(network_fakes.TestNetworkV2): + + def setUp(self): + super(TestQosPolicy, self).setUp() + # Get a shortcut to the network client + self.network = self.app.client_manager.network + # Get a shortcut to the ProjectManager Mock + self.projects_mock = self.app.client_manager.identity.projects + + +class TestCreateNetworkQosPolicy(TestQosPolicy): + + project = identity_fakes_v3.FakeProject.create_one_project() + + # The new qos policy created. + new_qos_policy = ( + network_fakes.FakeNetworkQosPolicy.create_one_qos_policy( + attrs={ + 'tenant_id': project.id, + } + )) + columns = ( + 'description', + 'id', + 'name', + 'project_id', + 'rules', + 'shared', + ) + + data = ( + new_qos_policy.description, + new_qos_policy.id, + new_qos_policy.name, + new_qos_policy.project_id, + new_qos_policy.rules, + new_qos_policy.shared, + ) + + def setUp(self): + super(TestCreateNetworkQosPolicy, self).setUp() + self.network.create_qos_policy = mock.Mock( + return_value=self.new_qos_policy) + + # Get the command object to test + self.cmd = network_qos_policy.CreateNetworkQosPolicy( + self.app, self.namespace) + + self.projects_mock.get.return_value = self.project + + def test_create_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_create_default_options(self): + arglist = [ + self.new_qos_policy.name, + ] + verifylist = [ + ('project', None), + ('name', self.new_qos_policy.name), + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + columns, data = (self.cmd.take_action(parsed_args)) + + self.network.create_qos_policy.assert_called_once_with(**{ + 'name': self.new_qos_policy.name + }) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, data) + + def test_create_all_options(self): + arglist = [ + '--share', + '--project', self.project.name, + self.new_qos_policy.name, + '--description', 'QoS policy description', + ] + verifylist = [ + ('share', True), + ('project', self.project.name), + ('name', self.new_qos_policy.name), + ('description', 'QoS policy description'), + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + columns, data = self.cmd.take_action(parsed_args) + + self.network.create_qos_policy.assert_called_once_with(**{ + 'shared': True, + 'tenant_id': self.project.id, + 'name': self.new_qos_policy.name, + 'description': 'QoS policy description', + }) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, data) + + +class TestDeleteNetworkQosPolicy(TestQosPolicy): + + # The address scope to delete. + _qos_policies = ( + network_fakes.FakeNetworkQosPolicy.create_qos_policies(count=2)) + + def setUp(self): + super(TestDeleteNetworkQosPolicy, self).setUp() + self.network.delete_qos_policy = mock.Mock(return_value=None) + self.network.find_qos_policy = ( + network_fakes.FakeNetworkQosPolicy.get_qos_policies( + qos_policies=self._qos_policies) + ) + + # Get the command object to test + self.cmd = network_qos_policy.DeleteNetworkQosPolicy( + self.app, self.namespace) + + def test_qos_policy_delete(self): + arglist = [ + self._qos_policies[0].name, + ] + verifylist = [ + ('policy', [self._qos_policies[0].name]), + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + result = self.cmd.take_action(parsed_args) + self.network.find_qos_policy.assert_called_once_with( + self._qos_policies[0].name, ignore_missing=False) + self.network.delete_qos_policy.assert_called_once_with( + self._qos_policies[0]) + self.assertIsNone(result) + + def test_multi_qos_policies_delete(self): + arglist = [] + + for a in self._qos_policies: + arglist.append(a.name) + verifylist = [ + ('policy', arglist), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + result = self.cmd.take_action(parsed_args) + + calls = [] + for a in self._qos_policies: + calls.append(call(a)) + self.network.delete_qos_policy.assert_has_calls(calls) + self.assertIsNone(result) + + def test_multi_qos_policies_delete_with_exception(self): + arglist = [ + self._qos_policies[0].name, + 'unexist_qos_policy', + ] + verifylist = [ + ('policy', + [self._qos_policies[0].name, 'unexist_qos_policy']), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + find_mock_result = [self._qos_policies[0], exceptions.CommandError] + self.network.find_qos_policy = ( + mock.MagicMock(side_effect=find_mock_result) + ) + + try: + self.cmd.take_action(parsed_args) + self.fail('CommandError should be raised.') + except exceptions.CommandError as e: + self.assertEqual('1 of 2 QoS policies failed to delete.', str(e)) + + self.network.find_qos_policy.assert_any_call( + self._qos_policies[0].name, ignore_missing=False) + self.network.find_qos_policy.assert_any_call( + 'unexist_qos_policy', ignore_missing=False) + self.network.delete_qos_policy.assert_called_once_with( + self._qos_policies[0] + ) + + +class TestListNetworkQosPolicy(TestQosPolicy): + + # The QoS policies to list up. + qos_policies = ( + network_fakes.FakeNetworkQosPolicy.create_qos_policies(count=3)) + columns = ( + 'ID', + 'Name', + 'Shared', + 'Project', + ) + data = [] + for qos_policy in qos_policies: + data.append(( + qos_policy.id, + qos_policy.name, + qos_policy.shared, + qos_policy.project_id, + )) + + def setUp(self): + super(TestListNetworkQosPolicy, self).setUp() + self.network.qos_policies = mock.Mock(return_value=self.qos_policies) + + # Get the command object to test + self.cmd = network_qos_policy.ListNetworkQosPolicy(self.app, + self.namespace) + + def test_qos_policy_list(self): + arglist = [] + verifylist = [] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + columns, data = self.cmd.take_action(parsed_args) + + self.network.qos_policies.assert_called_once_with(**{}) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, list(data)) + + +class TestSetNetworkQosPolicy(TestQosPolicy): + + # The QoS policy to set. + _qos_policy = network_fakes.FakeNetworkQosPolicy.create_one_qos_policy() + + def setUp(self): + super(TestSetNetworkQosPolicy, self).setUp() + self.network.update_qos_policy = mock.Mock(return_value=None) + self.network.find_qos_policy = mock.Mock( + return_value=self._qos_policy) + + # Get the command object to test + self.cmd = network_qos_policy.SetNetworkQosPolicy(self.app, + self.namespace) + + def test_set_nothing(self): + arglist = [self._qos_policy.name, ] + verifylist = [ + ('policy', self._qos_policy.name), + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + result = self.cmd.take_action(parsed_args) + + attrs = {} + self.network.update_qos_policy.assert_called_with( + self._qos_policy, **attrs) + self.assertIsNone(result) + + def test_set_name_share_description(self): + arglist = [ + '--name', 'new_qos_policy', + '--share', + '--description', 'QoS policy description', + self._qos_policy.name, + ] + verifylist = [ + ('name', 'new_qos_policy'), + ('share', True), + ('description', 'QoS policy description'), + ('policy', self._qos_policy.name), + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + result = self.cmd.take_action(parsed_args) + attrs = { + 'name': 'new_qos_policy', + 'description': 'QoS policy description', + 'shared': True, + } + self.network.update_qos_policy.assert_called_with( + self._qos_policy, **attrs) + self.assertIsNone(result) + + def test_set_no_share(self): + arglist = [ + '--no-share', + self._qos_policy.name, + ] + verifylist = [ + ('no_share', True), + ('policy', self._qos_policy.name), + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + result = self.cmd.take_action(parsed_args) + attrs = { + 'shared': False + } + self.network.update_qos_policy.assert_called_with( + self._qos_policy, **attrs) + self.assertIsNone(result) + + +class TestShowNetworkQosPolicy(TestQosPolicy): + + # The QoS policy to show. + _qos_policy = ( + network_fakes.FakeNetworkQosPolicy.create_one_qos_policy()) + columns = ( + 'description', + 'id', + 'name', + 'project_id', + 'rules', + 'shared', + ) + data = ( + _qos_policy.description, + _qos_policy.id, + _qos_policy.name, + _qos_policy.project_id, + _qos_policy.rules, + _qos_policy.shared, + ) + + def setUp(self): + super(TestShowNetworkQosPolicy, self).setUp() + self.network.find_qos_policy = mock.Mock(return_value=self._qos_policy) + + # Get the command object to test + self.cmd = network_qos_policy.ShowNetworkQosPolicy(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._qos_policy.name, + ] + verifylist = [ + ('policy', self._qos_policy.name), + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + columns, data = self.cmd.take_action(parsed_args) + + self.network.find_qos_policy.assert_called_once_with( + self._qos_policy.name, ignore_missing=False) + self.assertEqual(self.columns, columns) + self.assertEqual(list(self.data), list(data)) diff --git a/openstackclient/tests/unit/network/v2/test_port.py b/openstackclient/tests/unit/network/v2/test_port.py index a2aceab1..4ff278a9 100644 --- a/openstackclient/tests/unit/network/v2/test_port.py +++ b/openstackclient/tests/unit/network/v2/test_port.py @@ -228,6 +228,93 @@ class TestCreatePort(TestPort): self.assertEqual(ref_columns, columns) self.assertEqual(ref_data, data) + def test_create_with_security_group(self): + secgroup = network_fakes.FakeSecurityGroup.create_one_security_group() + self.network.find_security_group = mock.Mock(return_value=secgroup) + arglist = [ + '--network', self._port.network_id, + '--security-group', secgroup.id, + 'test-port', + ] + verifylist = [ + ('network', self._port.network_id,), + ('enable', True), + ('security_groups', [secgroup.id]), + ('name', 'test-port'), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = (self.cmd.take_action(parsed_args)) + + self.network.create_port.assert_called_once_with(**{ + 'admin_state_up': True, + 'network_id': self._port.network_id, + 'security_groups': [secgroup.id], + 'name': 'test-port', + }) + + ref_columns, ref_data = self._get_common_cols_data(self._port) + self.assertEqual(ref_columns, columns) + self.assertEqual(ref_data, data) + + def test_create_with_security_groups(self): + sg_1 = network_fakes.FakeSecurityGroup.create_one_security_group() + sg_2 = network_fakes.FakeSecurityGroup.create_one_security_group() + self.network.find_security_group = mock.Mock(side_effect=[sg_1, sg_2]) + arglist = [ + '--network', self._port.network_id, + '--security-group', sg_1.id, + '--security-group', sg_2.id, + 'test-port', + ] + verifylist = [ + ('network', self._port.network_id,), + ('enable', True), + ('security_groups', [sg_1.id, sg_2.id]), + ('name', 'test-port'), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = (self.cmd.take_action(parsed_args)) + + self.network.create_port.assert_called_once_with(**{ + 'admin_state_up': True, + 'network_id': self._port.network_id, + 'security_groups': [sg_1.id, sg_2.id], + 'name': 'test-port', + }) + + ref_columns, ref_data = self._get_common_cols_data(self._port) + self.assertEqual(ref_columns, columns) + self.assertEqual(ref_data, data) + + def test_create_with_no_secuirty_groups(self): + arglist = [ + '--network', self._port.network_id, + '--no-security-group', + 'test-port', + ] + verifylist = [ + ('network', self._port.network_id), + ('enable', True), + ('no_security_group', True), + ('name', 'test-port'), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = (self.cmd.take_action(parsed_args)) + + self.network.create_port.assert_called_once_with(**{ + 'admin_state_up': True, + 'network_id': self._port.network_id, + 'security_groups': [], + 'name': 'test-port', + }) + + ref_columns, ref_data = self._get_common_cols_data(self._port) + self.assertEqual(ref_columns, columns) + self.assertEqual(ref_data, data) + class TestDeletePort(TestPort): @@ -317,6 +404,17 @@ class TestListPort(TestPort): 'Name', 'MAC Address', 'Fixed IP Addresses', + 'Status', + ) + + columns_long = ( + 'ID', + 'Name', + 'MAC Address', + 'Fixed IP Addresses', + 'Status', + 'Security Groups', + 'Device Owner', ) data = [] @@ -326,6 +424,19 @@ class TestListPort(TestPort): prt.name, prt.mac_address, utils.format_list_of_dicts(prt.fixed_ips), + prt.status, + )) + + data_long = [] + for prt in _ports: + data_long.append(( + prt.id, + prt.name, + prt.mac_address, + utils.format_list_of_dicts(prt.fixed_ips), + prt.status, + utils.format_list(prt.security_groups), + prt.device_owner, )) def setUp(self): @@ -439,6 +550,23 @@ class TestListPort(TestPort): self.assertEqual(self.columns, columns) self.assertEqual(self.data, list(data)) + def test_list_port_with_long(self): + arglist = [ + '--long', + ] + + verifylist = [ + ('long', True), + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + self.network.ports.assert_called_once_with() + self.assertEqual(self.columns_long, columns) + self.assertEqual(self.data_long, list(data)) + class TestSetPort(TestPort): @@ -651,6 +779,95 @@ class TestSetPort(TestPort): self.network.update_port.assert_called_once_with(self._port, **attrs) self.assertIsNone(result) + def test_set_security_group(self): + sg = network_fakes.FakeSecurityGroup.create_one_security_group() + self.network.find_security_group = mock.Mock(return_value=sg) + arglist = [ + '--security-group', sg.id, + self._port.name, + ] + verifylist = [ + ('security_groups', [sg.id]), + ('port', self._port.name), + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + result = self.cmd.take_action(parsed_args) + + attrs = { + 'security_groups': [sg.id], + } + self.network.update_port.assert_called_once_with(self._port, **attrs) + self.assertIsNone(result) + + def test_append_security_group(self): + sg_1 = network_fakes.FakeSecurityGroup.create_one_security_group() + sg_2 = network_fakes.FakeSecurityGroup.create_one_security_group() + sg_3 = network_fakes.FakeSecurityGroup.create_one_security_group() + self.network.find_security_group = mock.Mock(side_effect=[sg_2, sg_3]) + _testport = network_fakes.FakePort.create_one_port( + {'security_groups': [sg_1.id]}) + self.network.find_port = mock.Mock(return_value=_testport) + arglist = [ + '--security-group', sg_2.id, + '--security-group', sg_3.id, + _testport.name, + ] + verifylist = [ + ('security_groups', [sg_2.id, sg_3.id]), + ('port', _testport.name), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + result = self.cmd.take_action(parsed_args) + attrs = { + 'security_groups': [sg_1.id, sg_2.id, sg_3.id], + } + self.network.update_port.assert_called_once_with(_testport, **attrs) + self.assertIsNone(result) + + def test_set_no_security_groups(self): + arglist = [ + '--no-security-group', + self._port.name, + ] + verifylist = [ + ('no_security_group', True), + ('port', self._port.name), + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + result = self.cmd.take_action(parsed_args) + + attrs = { + 'security_groups': [], + } + self.network.update_port.assert_called_once_with(self._port, **attrs) + self.assertIsNone(result) + + def test_overwrite_security_group(self): + sg1 = network_fakes.FakeSecurityGroup.create_one_security_group() + sg2 = network_fakes.FakeSecurityGroup.create_one_security_group() + _testport = network_fakes.FakePort.create_one_port( + {'security_groups': [sg1.id]}) + self.network.find_port = mock.Mock(return_value=_testport) + self.network.find_security_group = mock.Mock(return_value=sg2) + arglist = [ + '--security-group', sg2.id, + '--no-security-group', + _testport.name, + ] + verifylist = [ + ('security_groups', [sg2.id]), + ('no_security_group', True) + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + result = self.cmd.take_action(parsed_args) + attrs = { + 'security_groups': [sg2.id], + } + self.network.update_port.assert_called_once_with(_testport, **attrs) + self.assertIsNone(result) + class TestShowPort(TestPort): @@ -767,3 +984,47 @@ class TestUnsetPort(TestPort): self.assertRaises(exceptions.CommandError, self.cmd.take_action, parsed_args) + + def test_unset_security_group(self): + _fake_sg1 = network_fakes.FakeSecurityGroup.create_one_security_group() + _fake_sg2 = network_fakes.FakeSecurityGroup.create_one_security_group() + _fake_port = network_fakes.FakePort.create_one_port( + {'security_groups': [_fake_sg1.id, _fake_sg2.id]}) + self.network.find_port = mock.Mock(return_value=_fake_port) + self.network.find_security_group = mock.Mock(return_value=_fake_sg2) + arglist = [ + '--security-group', _fake_sg2.id, + _fake_port.name, + ] + verifylist = [ + ('security_groups', [_fake_sg2.id]), + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + result = self.cmd.take_action(parsed_args) + + attrs = { + 'security_groups': [_fake_sg1.id] + } + self.network.update_port.assert_called_once_with( + _fake_port, **attrs) + self.assertIsNone(result) + + def test_unset_port_security_group_not_existent(self): + _fake_sg1 = network_fakes.FakeSecurityGroup.create_one_security_group() + _fake_sg2 = network_fakes.FakeSecurityGroup.create_one_security_group() + _fake_port = network_fakes.FakePort.create_one_port( + {'security_groups': [_fake_sg1.id]}) + self.network.find_security_group = mock.Mock(return_value=_fake_sg2) + arglist = [ + '--security-group', _fake_sg2.id, + _fake_port.name, + ] + verifylist = [ + ('security_groups', [_fake_sg2.id]), + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + self.assertRaises(exceptions.CommandError, + self.cmd.take_action, + parsed_args) diff --git a/openstackclient/tests/unit/network/v2/test_router.py b/openstackclient/tests/unit/network/v2/test_router.py index 6a445862..24984e47 100644 --- a/openstackclient/tests/unit/network/v2/test_router.py +++ b/openstackclient/tests/unit/network/v2/test_router.py @@ -427,6 +427,58 @@ class TestListRouter(TestRouter): self.assertEqual(self.columns_long_no_az, columns) self.assertEqual(self.data_long_no_az, list(data)) + def test_list_name(self): + test_name = "fakename" + arglist = [ + '--name', test_name, + ] + verifylist = [ + ('long', False), + ('name', test_name), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + columns, data = self.cmd.take_action(parsed_args) + + self.network.routers.assert_called_once_with( + **{'name': test_name} + ) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, list(data)) + + def test_router_list_enable(self): + arglist = [ + '--enable', + ] + verifylist = [ + ('long', False), + ('enable', True), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + columns, data = self.cmd.take_action(parsed_args) + + self.network.routers.assert_called_once_with( + **{'admin_state_up': True} + ) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, list(data)) + + def test_router_list_disable(self): + arglist = [ + '--disable', + ] + verifylist = [ + ('long', False), + ('disable', True) + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + columns, data = self.cmd.take_action(parsed_args) + + self.network.routers.assert_called_once_with( + **{'admin_state_up': False} + ) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, list(data)) + class TestRemovePortFromRouter(TestRouter): '''Remove port from a Router ''' @@ -529,6 +581,7 @@ class TestSetRouter(TestRouter): '--enable', '--distributed', '--name', 'noob', + '--no-ha', '--description', 'router', ] verifylist = [ @@ -537,6 +590,7 @@ class TestSetRouter(TestRouter): ('distributed', True), ('name', 'noob'), ('description', 'router'), + ('no_ha', True), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -546,6 +600,7 @@ class TestSetRouter(TestRouter): 'admin_state_up': True, 'distributed': True, 'name': 'noob', + 'ha': False, 'description': 'router', } self.network.update_router.assert_called_once_with( @@ -557,11 +612,13 @@ class TestSetRouter(TestRouter): self._router.name, '--disable', '--centralized', + '--ha', ] verifylist = [ ('router', self._router.name), ('disable', True), ('centralized', True), + ('ha', True), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -570,6 +627,7 @@ class TestSetRouter(TestRouter): attrs = { 'admin_state_up': False, 'distributed': False, + 'ha': True, } self.network.update_router.assert_called_once_with( self._router, **attrs) @@ -773,9 +831,9 @@ class TestUnsetRouter(TestRouter): super(TestUnsetRouter, self).setUp() self._testrouter = network_fakes.FakeRouter.create_one_router( {'routes': [{"destination": "192.168.101.1/24", - "gateway": "172.24.4.3"}, + "nexthop": "172.24.4.3"}, {"destination": "192.168.101.2/24", - "gateway": "172.24.4.3"}], }) + "nexthop": "172.24.4.3"}], }) self.fake_subnet = network_fakes.FakeSubnet.create_one_subnet() self.network.find_router = mock.Mock(return_value=self._testrouter) self.network.update_router = mock.Mock(return_value=None) diff --git a/openstackclient/tests/unit/volume/v1/fakes.py b/openstackclient/tests/unit/volume/v1/fakes.py index 3999543c..a11ea491 100644 --- a/openstackclient/tests/unit/volume/v1/fakes.py +++ b/openstackclient/tests/unit/volume/v1/fakes.py @@ -306,6 +306,32 @@ class FakeQos(object): return qos @staticmethod + def create_one_qos_association(attrs=None): + """Create a fake Qos specification association. + + :param Dictionary attrs: + A dictionary with all attributes + :return: + A FakeResource object with id, name, association_type, etc. + """ + attrs = attrs or {} + + # Set default attributes. + qos_association_info = { + "id": 'type-id-' + uuid.uuid4().hex, + "name": 'type-name-' + uuid.uuid4().hex, + "association_type": 'volume_type', + } + + # Overwrite default attributes. + qos_association_info.update(attrs) + + qos_association = fakes.FakeResource( + info=copy.deepcopy(qos_association_info), + loaded=True) + return qos_association + + @staticmethod def create_qoses(attrs=None, count=2): """Create multiple fake Qos specifications. diff --git a/openstackclient/tests/unit/volume/v1/test_backup.py b/openstackclient/tests/unit/volume/v1/test_backup.py index 32c2fd22..1097d3f1 100644 --- a/openstackclient/tests/unit/volume/v1/test_backup.py +++ b/openstackclient/tests/unit/volume/v1/test_backup.py @@ -249,26 +249,65 @@ class TestBackupList(TestBackup): self.volumes_mock.list.return_value = [self.volume] self.backups_mock.list.return_value = self.backups + self.volumes_mock.get.return_value = self.volume # Get the command to test self.cmd = backup.ListVolumeBackup(self.app, None) def test_backup_list_without_options(self): arglist = [] - verifylist = [("long", False)] + verifylist = [ + ("long", False), + ("name", None), + ("status", None), + ("volume", None), + ('all_projects', False), + ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) columns, data = self.cmd.take_action(parsed_args) + search_opts = { + "name": None, + "status": None, + "volume_id": None, + "all_tenants": False, + } + self.volumes_mock.get.assert_not_called() + self.backups_mock.list.assert_called_with( + search_opts=search_opts, + ) self.assertEqual(self.columns, columns) self.assertEqual(self.data, list(data)) def test_backup_list_with_options(self): - arglist = ["--long"] - verifylist = [("long", True)] + arglist = [ + "--long", + "--name", self.backups[0].name, + "--status", "error", + "--volume", self.volume.id, + "--all-projects" + ] + verifylist = [ + ("long", True), + ("name", self.backups[0].name), + ("status", "error"), + ("volume", self.volume.id), + ('all_projects', True), + ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) columns, data = self.cmd.take_action(parsed_args) + search_opts = { + "name": self.backups[0].name, + "status": "error", + "volume_id": self.volume.id, + "all_tenants": True, + } + self.volumes_mock.get.assert_called_once_with(self.volume.id) + self.backups_mock.list.assert_called_with( + search_opts=search_opts, + ) self.assertEqual(self.columns_long, columns) self.assertEqual(self.data_long, list(data)) diff --git a/openstackclient/tests/unit/volume/v1/test_qos_specs.py b/openstackclient/tests/unit/volume/v1/test_qos_specs.py index 1982980a..464038e7 100644 --- a/openstackclient/tests/unit/volume/v1/test_qos_specs.py +++ b/openstackclient/tests/unit/volume/v1/test_qos_specs.py @@ -13,14 +13,12 @@ # under the License. # -import copy import mock from mock import call from osc_lib import exceptions from osc_lib import utils -from openstackclient.tests.unit import fakes from openstackclient.tests.unit.volume.v1 import fakes as volume_fakes from openstackclient.volume.v1 import qos_specs @@ -39,154 +37,124 @@ class TestQos(volume_fakes.TestVolumev1): class TestQosAssociate(TestQos): + volume_type = volume_fakes.FakeType.create_one_type() + qos_spec = volume_fakes.FakeQos.create_one_qos() + def setUp(self): super(TestQosAssociate, self).setUp() + self.qos_mock.get.return_value = self.qos_spec + self.types_mock.get.return_value = self.volume_type # Get the command object to test self.cmd = qos_specs.AssociateQos(self.app, None) def test_qos_associate(self): - self.qos_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(volume_fakes.QOS), - loaded=True - ) - self.types_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(volume_fakes.TYPE), - loaded=True - ) arglist = [ - volume_fakes.qos_id, - volume_fakes.type_id + self.qos_spec.id, + self.volume_type.id ] verifylist = [ - ('qos_spec', volume_fakes.qos_id), - ('volume_type', volume_fakes.type_id) + ('qos_spec', self.qos_spec.id), + ('volume_type', self.volume_type.id) ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) result = self.cmd.take_action(parsed_args) self.qos_mock.associate.assert_called_with( - volume_fakes.qos_id, - volume_fakes.type_id + self.qos_spec.id, + self.volume_type.id ) self.assertIsNone(result) class TestQosCreate(TestQos): + new_qos_spec = volume_fakes.FakeQos.create_one_qos() columns = ( 'consumer', 'id', 'name', + 'specs' ) datalist = ( - volume_fakes.qos_consumer, - volume_fakes.qos_id, - volume_fakes.qos_name + new_qos_spec.consumer, + new_qos_spec.id, + new_qos_spec.name, + new_qos_spec.specs ) def setUp(self): super(TestQosCreate, self).setUp() + 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 test_qos_create_without_properties(self): - self.qos_mock.create.return_value = fakes.FakeResource( - None, - copy.deepcopy(volume_fakes.QOS_DEFAULT_CONSUMER), - loaded=True - ) - arglist = [ - volume_fakes.qos_name, + self.new_qos_spec.name, ] verifylist = [ - ('name', volume_fakes.qos_name), + ('name', self.new_qos_spec.name), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) columns, data = self.cmd.take_action(parsed_args) self.qos_mock.create.assert_called_with( - volume_fakes.qos_name, - {'consumer': volume_fakes.qos_default_consumer} + self.new_qos_spec.name, + {'consumer': 'both'} ) self.assertEqual(self.columns, columns) - datalist = ( - volume_fakes.qos_default_consumer, - volume_fakes.qos_id, - volume_fakes.qos_name - ) - self.assertEqual(datalist, data) + self.assertEqual(self.datalist, data) def test_qos_create_with_consumer(self): - self.qos_mock.create.return_value = fakes.FakeResource( - None, - copy.deepcopy(volume_fakes.QOS), - loaded=True - ) - arglist = [ - volume_fakes.qos_name, - '--consumer', volume_fakes.qos_consumer + '--consumer', self.new_qos_spec.consumer, + self.new_qos_spec.name, ] verifylist = [ - ('name', volume_fakes.qos_name), - ('consumer', volume_fakes.qos_consumer) + ('consumer', self.new_qos_spec.consumer), + ('name', self.new_qos_spec.name), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) columns, data = self.cmd.take_action(parsed_args) self.qos_mock.create.assert_called_with( - volume_fakes.qos_name, - {'consumer': volume_fakes.qos_consumer} + self.new_qos_spec.name, + {'consumer': self.new_qos_spec.consumer} ) self.assertEqual(self.columns, columns) self.assertEqual(self.datalist, data) def test_qos_create_with_properties(self): - self.qos_mock.create.return_value = fakes.FakeResource( - None, - copy.deepcopy(volume_fakes.QOS_WITH_SPECS), - loaded=True - ) - arglist = [ - volume_fakes.qos_name, - '--consumer', volume_fakes.qos_consumer, + '--consumer', self.new_qos_spec.consumer, '--property', 'foo=bar', - '--property', 'iops=9001' + '--property', 'iops=9001', + self.new_qos_spec.name, ] verifylist = [ - ('name', volume_fakes.qos_name), - ('consumer', volume_fakes.qos_consumer), - ('property', volume_fakes.qos_specs) + ('consumer', self.new_qos_spec.consumer), + ('property', self.new_qos_spec.specs), + ('name', self.new_qos_spec.name), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) columns, data = self.cmd.take_action(parsed_args) - specs = volume_fakes.qos_specs.copy() - specs.update({'consumer': volume_fakes.qos_consumer}) + self.new_qos_spec.specs.update( + {'consumer': self.new_qos_spec.consumer}) self.qos_mock.create.assert_called_with( - volume_fakes.qos_name, - specs + self.new_qos_spec.name, + self.new_qos_spec.specs ) - columns = self.columns + ( - 'specs', - ) - self.assertEqual(columns, columns) - datalist = self.datalist + ( - volume_fakes.qos_specs, - ) - self.assertEqual(datalist, data) + self.assertEqual(self.columns, columns) + self.assertEqual(self.datalist, data) class TestQosDelete(TestQos): @@ -294,79 +262,62 @@ class TestQosDelete(TestQos): class TestQosDisassociate(TestQos): + volume_type = volume_fakes.FakeType.create_one_type() + qos_spec = volume_fakes.FakeQos.create_one_qos() + def setUp(self): super(TestQosDisassociate, self).setUp() + self.qos_mock.get.return_value = self.qos_spec + self.types_mock.get.return_value = self.volume_type # Get the command object to test self.cmd = qos_specs.DisassociateQos(self.app, None) def test_qos_disassociate_with_volume_type(self): - self.qos_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(volume_fakes.QOS), - loaded=True - ) - self.types_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(volume_fakes.TYPE), - loaded=True - ) arglist = [ - volume_fakes.qos_id, - '--volume-type', volume_fakes.type_id + '--volume-type', self.volume_type.id, + self.qos_spec.id, ] verifylist = [ - ('qos_spec', volume_fakes.qos_id), - ('volume_type', volume_fakes.type_id) + ('volume_type', self.volume_type.id), + ('qos_spec', self.qos_spec.id), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) result = self.cmd.take_action(parsed_args) self.qos_mock.disassociate.assert_called_with( - volume_fakes.qos_id, - volume_fakes.type_id + self.qos_spec.id, + self.volume_type.id ) self.assertIsNone(result) def test_qos_disassociate_with_all_volume_types(self): - self.qos_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(volume_fakes.QOS), - loaded=True - ) - arglist = [ - volume_fakes.qos_id, - '--all' + '--all', + self.qos_spec.id, ] verifylist = [ - ('qos_spec', volume_fakes.qos_id) + ('qos_spec', self.qos_spec.id) ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) result = self.cmd.take_action(parsed_args) - self.qos_mock.disassociate_all.assert_called_with(volume_fakes.qos_id) + self.qos_mock.disassociate_all.assert_called_with(self.qos_spec.id) self.assertIsNone(result) class TestQosList(TestQos): + qos_spec = volume_fakes.FakeQos.create_one_qos() + qos_association = volume_fakes.FakeQos.create_one_qos_association() + def setUp(self): super(TestQosList, self).setUp() - self.qos_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(volume_fakes.QOS_WITH_ASSOCIATIONS), - loaded=True, - ) - self.qos_mock.list.return_value = [self.qos_mock.get.return_value] - self.qos_mock.get_associations.return_value = [fakes.FakeResource( - None, - copy.deepcopy(volume_fakes.qos_association), - loaded=True, - )] + self.qos_mock.list.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.ListQos(self.app, None) @@ -389,81 +340,72 @@ class TestQosList(TestQos): ) self.assertEqual(collist, columns) datalist = (( - volume_fakes.qos_id, - volume_fakes.qos_name, - volume_fakes.qos_consumer, - volume_fakes.type_name, - utils.format_dict(volume_fakes.qos_specs), + self.qos_spec.id, + self.qos_spec.name, + self.qos_spec.consumer, + self.qos_association.name, + utils.format_dict(self.qos_spec.specs), ), ) self.assertEqual(datalist, tuple(data)) class TestQosSet(TestQos): + qos_spec = volume_fakes.FakeQos.create_one_qos() + def setUp(self): super(TestQosSet, self).setUp() + self.qos_mock.get.return_value = self.qos_spec # Get the command object to test self.cmd = qos_specs.SetQos(self.app, None) def test_qos_set_with_properties_with_id(self): - self.qos_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(volume_fakes.QOS_WITH_SPECS), - loaded=True - ) arglist = [ - volume_fakes.qos_id, '--property', 'foo=bar', - '--property', 'iops=9001' + '--property', 'iops=9001', + self.qos_spec.id, ] verifylist = [ - ('qos_spec', volume_fakes.qos_id), - ('property', volume_fakes.qos_specs) + ('property', self.qos_spec.specs), + ('qos_spec', self.qos_spec.id), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) result = self.cmd.take_action(parsed_args) self.qos_mock.set_keys.assert_called_with( - volume_fakes.qos_id, - volume_fakes.qos_specs + self.qos_spec.id, + self.qos_spec.specs ) self.assertIsNone(result) class TestQosShow(TestQos): + qos_spec = volume_fakes.FakeQos.create_one_qos() + qos_association = volume_fakes.FakeQos.create_one_qos_association() + def setUp(self): super(TestQosShow, self).setUp() - - self.qos_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(volume_fakes.QOS_WITH_ASSOCIATIONS), - loaded=True, - ) - self.qos_mock.get_associations.return_value = [fakes.FakeResource( - None, - copy.deepcopy(volume_fakes.qos_association), - loaded=True, - )] - + 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 = [ - volume_fakes.qos_id + self.qos_spec.id ] verifylist = [ - ('qos_spec', volume_fakes.qos_id) + ('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( - volume_fakes.qos_id + self.qos_spec.id ) collist = ( @@ -475,56 +417,53 @@ class TestQosShow(TestQos): ) self.assertEqual(collist, columns) datalist = ( - volume_fakes.type_name, - volume_fakes.qos_consumer, - volume_fakes.qos_id, - volume_fakes.qos_name, - utils.format_dict(volume_fakes.qos_specs), + self.qos_association.name, + self.qos_spec.consumer, + self.qos_spec.id, + self.qos_spec.name, + utils.format_dict(self.qos_spec.specs), ) self.assertEqual(datalist, tuple(data)) class TestQosUnset(TestQos): + qos_spec = volume_fakes.FakeQos.create_one_qos() + def setUp(self): super(TestQosUnset, self).setUp() + self.qos_mock.get.return_value = self.qos_spec # Get the command object to test self.cmd = qos_specs.UnsetQos(self.app, None) def test_qos_unset_with_properties(self): - self.qos_mock.get.return_value = fakes.FakeResource( - None, - copy.deepcopy(volume_fakes.QOS), - loaded=True - ) arglist = [ - volume_fakes.qos_id, '--property', 'iops', - '--property', 'foo' + '--property', 'foo', + self.qos_spec.id, ] - verifylist = [ - ('qos_spec', volume_fakes.qos_id), - ('property', ['iops', 'foo']) + ('property', ['iops', 'foo']), + ('qos_spec', self.qos_spec.id), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) result = self.cmd.take_action(parsed_args) self.qos_mock.unset_keys.assert_called_with( - volume_fakes.qos_id, + self.qos_spec.id, ['iops', 'foo'] ) self.assertIsNone(result) def test_qos_unset_nothing(self): arglist = [ - volume_fakes.qos_id, + self.qos_spec.id, ] verifylist = [ - ('qos_spec', volume_fakes.qos_id), + ('qos_spec', self.qos_spec.id), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) diff --git a/openstackclient/tests/unit/volume/v2/test_backup.py b/openstackclient/tests/unit/volume/v2/test_backup.py index 306c9eb3..10e7aac5 100644 --- a/openstackclient/tests/unit/volume/v2/test_backup.py +++ b/openstackclient/tests/unit/volume/v2/test_backup.py @@ -280,26 +280,78 @@ class TestBackupList(TestBackup): self.volumes_mock.list.return_value = [self.volume] self.backups_mock.list.return_value = self.backups + self.volumes_mock.get.return_value = self.volume + self.backups_mock.get.return_value = self.backups[0] # Get the command to test self.cmd = backup.ListVolumeBackup(self.app, None) def test_backup_list_without_options(self): arglist = [] - verifylist = [("long", False)] + verifylist = [ + ("long", False), + ("name", None), + ("status", None), + ("volume", None), + ("marker", None), + ("limit", None), + ('all_projects', False), + ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) columns, data = self.cmd.take_action(parsed_args) + search_opts = { + "name": None, + "status": None, + "volume_id": None, + 'all_tenants': False, + } + self.volumes_mock.get.assert_not_called() + self.backups_mock.get.assert_not_called() + self.backups_mock.list.assert_called_with( + search_opts=search_opts, + marker=None, + limit=None, + ) self.assertEqual(self.columns, columns) self.assertEqual(self.data, list(data)) def test_backup_list_with_options(self): - arglist = ["--long"] - verifylist = [("long", True)] + arglist = [ + "--long", + "--name", self.backups[0].name, + "--status", "error", + "--volume", self.volume.id, + "--marker", self.backups[0].id, + "--all-projects", + "--limit", "3", + ] + verifylist = [ + ("long", True), + ("name", self.backups[0].name), + ("status", "error"), + ("volume", self.volume.id), + ("marker", self.backups[0].id), + ('all_projects', True), + ("limit", 3), + ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) columns, data = self.cmd.take_action(parsed_args) + search_opts = { + "name": self.backups[0].name, + "status": "error", + "volume_id": self.volume.id, + 'all_tenants': True, + } + self.volumes_mock.get.assert_called_once_with(self.volume.id) + self.backups_mock.get.assert_called_once_with(self.backups[0].id) + self.backups_mock.list.assert_called_with( + search_opts=search_opts, + marker=self.backups[0].id, + limit=3, + ) self.assertEqual(self.columns_long, columns) self.assertEqual(self.data_long, list(data)) diff --git a/openstackclient/volume/v1/backup.py b/openstackclient/volume/v1/backup.py index c9d0ca0d..a02cdccb 100644 --- a/openstackclient/volume/v1/backup.py +++ b/openstackclient/volume/v1/backup.py @@ -152,9 +152,36 @@ class ListVolumeBackup(command.Lister): default=False, help=_('List additional fields in output'), ) + parser.add_argument( + "--name", + metavar="<name>", + help=_("Filters results by the backup name") + ) + parser.add_argument( + "--status", + metavar="<status>", + choices=['creating', 'available', 'deleting', + 'error', 'restoring', 'error_restoring'], + help=_("Filters results by the backup status " + "('creating', 'available', 'deleting', " + "'error', 'restoring' or 'error_restoring')") + ) + parser.add_argument( + "--volume", + metavar="<volume>", + help=_("Filters results by the volume which they " + "backup (name or ID)") + ) + parser.add_argument( + '--all-projects', + action='store_true', + default=False, + help=_('Include all projects (admin only)'), + ) return parser def take_action(self, parsed_args): + volume_client = self.app.client_manager.volume def _format_volume_id(volume_id): """Return a volume name if available @@ -180,13 +207,25 @@ class ListVolumeBackup(command.Lister): # Cache the volume list volume_cache = {} try: - for s in self.app.client_manager.volume.volumes.list(): + for s in volume_client.volumes.list(): volume_cache[s.id] = s except Exception: # Just forget it if there's any trouble pass - data = self.app.client_manager.volume.backups.list() + filter_volume_id = None + if parsed_args.volume: + filter_volume_id = utils.find_resource(volume_client.volumes, + parsed_args.volume).id + search_opts = { + 'name': parsed_args.name, + 'status': parsed_args.status, + 'volume_id': filter_volume_id, + 'all_tenants': parsed_args.all_projects, + } + data = volume_client.backups.list( + search_opts=search_opts, + ) return (column_headers, (utils.get_item_properties( diff --git a/openstackclient/volume/v2/backup.py b/openstackclient/volume/v2/backup.py index 2ca35b24..c41cffda 100644 --- a/openstackclient/volume/v2/backup.py +++ b/openstackclient/volume/v2/backup.py @@ -17,6 +17,7 @@ import copy import logging +from osc_lib.cli import parseractions from osc_lib.command import command from osc_lib import exceptions from osc_lib import utils @@ -179,9 +180,48 @@ class ListVolumeBackup(command.Lister): default=False, help=_("List additional fields in output") ) + parser.add_argument( + "--name", + metavar="<name>", + help=_("Filters results by the backup name") + ) + parser.add_argument( + "--status", + metavar="<status>", + choices=['creating', 'available', 'deleting', + 'error', 'restoring', 'error_restoring'], + help=_("Filters results by the backup status " + "('creating', 'available', 'deleting', " + "'error', 'restoring' or 'error_restoring')") + ) + parser.add_argument( + "--volume", + metavar="<volume>", + help=_("Filters results by the volume which they " + "backup (name or ID)") + ) + parser.add_argument( + '--marker', + metavar='<marker>', + help=_('The last backup of the previous page (name or ID)'), + ) + parser.add_argument( + '--limit', + type=int, + action=parseractions.NonNegativeAction, + metavar='<limit>', + help=_('Maximum number of backups to display'), + ) + parser.add_argument( + '--all-projects', + action='store_true', + default=False, + help=_('Include all projects (admin only)'), + ) return parser def take_action(self, parsed_args): + volume_client = self.app.client_manager.volume def _format_volume_id(volume_id): """Return a volume name if available @@ -207,13 +247,31 @@ class ListVolumeBackup(command.Lister): # Cache the volume list volume_cache = {} try: - for s in self.app.client_manager.volume.volumes.list(): + for s in volume_client.volumes.list(): volume_cache[s.id] = s except Exception: # Just forget it if there's any trouble pass - data = self.app.client_manager.volume.backups.list() + filter_volume_id = None + if parsed_args.volume: + filter_volume_id = utils.find_resource(volume_client.volumes, + parsed_args.volume).id + marker_backup_id = None + if parsed_args.marker: + marker_backup_id = utils.find_resource(volume_client.backups, + parsed_args.marker).id + search_opts = { + 'name': parsed_args.name, + 'status': parsed_args.status, + 'volume_id': filter_volume_id, + 'all_tenants': parsed_args.all_projects, + } + data = volume_client.backups.list( + search_opts=search_opts, + marker=marker_backup_id, + limit=parsed_args.limit, + ) return (column_headers, (utils.get_item_properties( diff --git a/openstackclient/volume/v2/volume.py b/openstackclient/volume/v2/volume.py index e7405114..cb409711 100644 --- a/openstackclient/volume/v2/volume.py +++ b/openstackclient/volume/v2/volume.py @@ -484,10 +484,11 @@ class SetVolume(command.Command): try: if volume.status != 'available': msg = (_("Volume is in %s state, it must be available " - "before size can be extended"), volume.status) + "before size can be extended") % volume.status) raise exceptions.CommandError(msg) if parsed_args.size <= volume.size: - msg = _("New size must be greater than %s GB"), volume.size + msg = (_("New size must be greater than %s GB") + % volume.size) raise exceptions.CommandError(msg) volume_client.volumes.extend(volume.id, parsed_args.size) except Exception as e: |
