diff options
Diffstat (limited to 'openstackclient')
29 files changed, 1822 insertions, 455 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/client.py b/openstackclient/compute/client.py index 4cc3be98..c5b364b2 100644 --- a/openstackclient/compute/client.py +++ b/openstackclient/compute/client.py @@ -23,11 +23,12 @@ from openstackclient.i18n import _ LOG = logging.getLogger(__name__) -DEFAULT_API_VERSION = '2' +DEFAULT_API_VERSION = '2.1' API_VERSION_OPTION = 'os_compute_api_version' API_NAME = 'compute' API_VERSIONS = { "2": "novaclient.client", + "2.1": "novaclient.client", } # Save the microversion if in use diff --git a/openstackclient/compute/v2/aggregate.py b/openstackclient/compute/v2/aggregate.py index 2e2838c5..58d529e9 100644 --- a/openstackclient/compute/v2/aggregate.py +++ b/openstackclient/compute/v2/aggregate.py @@ -248,6 +248,14 @@ class SetAggregate(command.Command): help=_("Property to set on <aggregate> " "(repeat option to set multiple properties)") ) + parser.add_argument( + "--no-property", + dest="no_property", + action="store_true", + help=_("Remove all properties from <aggregate> " + "(specify both --property and --no-property to " + "overwrite the current properties)"), + ) return parser def take_action(self, parsed_args): @@ -269,10 +277,23 @@ class SetAggregate(command.Command): kwargs ) + set_property = {} + if parsed_args.no_property: + # NOTE(RuiChen): "availability_zone" is removed from response of + # aggregate show and create commands, don't see it + # anywhere, so pop it, avoid the unexpected server + # exception(can't unset the availability zone from + # aggregate metadata in nova). + set_property.update({key: None + for key in aggregate.metadata.keys() + if key != 'availability_zone'}) if parsed_args.property: + set_property.update(parsed_args.property) + + if set_property: compute_client.aggregates.set_metadata( aggregate, - parsed_args.property + set_property ) @@ -326,7 +347,6 @@ class UnsetAggregate(command.Command): "--property", metavar="<key>", action='append', - required=True, help=_("Property to remove from aggregate " "(repeat option to remove multiple properties)") ) @@ -338,6 +358,9 @@ class UnsetAggregate(command.Command): compute_client.aggregates, parsed_args.aggregate) - unset_property = {key: None for key in parsed_args.property} - compute_client.aggregates.set_metadata(aggregate, - unset_property) + unset_property = {} + if parsed_args.property: + unset_property.update({key: None for key in parsed_args.property}) + if unset_property: + compute_client.aggregates.set_metadata(aggregate, + unset_property) 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/network_segment.py b/openstackclient/network/v2/network_segment.py index 34cac0e0..94722f1e 100644 --- a/openstackclient/network/v2/network_segment.py +++ b/openstackclient/network/v2/network_segment.py @@ -26,12 +26,7 @@ LOG = logging.getLogger(__name__) class CreateNetworkSegment(command.ShowOne): - """Create new network segment - - (Caution: This is a beta command and subject to change. - Use global option --os-beta-command to enable - this command) - """ + """Create new network segment""" def get_parser(self, prog_name): parser = super(CreateNetworkSegment, self).get_parser(prog_name) @@ -76,7 +71,6 @@ class CreateNetworkSegment(command.ShowOne): return parser def take_action(self, parsed_args): - self.validate_os_beta_command_enabled() client = self.app.client_manager.network attrs = {} attrs['name'] = parsed_args.name @@ -96,12 +90,7 @@ class CreateNetworkSegment(command.ShowOne): class DeleteNetworkSegment(command.Command): - """Delete network segment(s) - - (Caution: This is a beta command and subject to change. - Use global option --os-beta-command to enable - this command) - """ + """Delete network segment(s)""" def get_parser(self, prog_name): parser = super(DeleteNetworkSegment, self).get_parser(prog_name) @@ -114,7 +103,6 @@ class DeleteNetworkSegment(command.Command): return parser def take_action(self, parsed_args): - self.validate_os_beta_command_enabled() client = self.app.client_manager.network result = 0 @@ -137,12 +125,7 @@ class DeleteNetworkSegment(command.Command): class ListNetworkSegment(command.Lister): - """List network segments - - (Caution: This is a beta command and subject to change. - Use global option --os-beta-command to enable - this command) - """ + """List network segments""" def get_parser(self, prog_name): parser = super(ListNetworkSegment, self).get_parser(prog_name) @@ -161,8 +144,6 @@ class ListNetworkSegment(command.Lister): return parser def take_action(self, parsed_args): - self.validate_os_beta_command_enabled() - network_client = self.app.client_manager.network filters = {} @@ -204,12 +185,7 @@ class ListNetworkSegment(command.Lister): class SetNetworkSegment(command.Command): - """Set network segment properties - - (Caution: This is a beta command and subject to change. - Use global option --os-beta-command to enable - this command) - """ + """Set network segment properties""" def get_parser(self, prog_name): parser = super(SetNetworkSegment, self).get_parser(prog_name) @@ -231,7 +207,6 @@ class SetNetworkSegment(command.Command): return parser def take_action(self, parsed_args): - self.validate_os_beta_command_enabled() client = self.app.client_manager.network obj = client.find_segment(parsed_args.network_segment, ignore_missing=False) @@ -244,12 +219,7 @@ class SetNetworkSegment(command.Command): class ShowNetworkSegment(command.ShowOne): - """Display network segment details - - (Caution: This is a beta command and subject to change. - Use global option --os-beta-command to enable - this command) - """ + """Display network segment details""" def get_parser(self, prog_name): parser = super(ShowNetworkSegment, self).get_parser(prog_name) @@ -261,8 +231,6 @@ class ShowNetworkSegment(command.ShowOne): return parser def take_action(self, parsed_args): - self.validate_os_beta_command_enabled() - client = self.app.client_manager.network obj = client.find_segment( parsed_args.network_segment, diff --git a/openstackclient/network/v2/router.py b/openstackclient/network/v2/router.py index cb40d774..64bb8819 100644 --- a/openstackclient/network/v2/router.py +++ b/openstackclient/network/v2/router.py @@ -94,7 +94,6 @@ def _get_attrs(client_manager, parsed_args): ).id attrs['tenant_id'] = project_id - # TODO(tangchen): Support getting 'ha' property. # TODO(tangchen): Support getting 'external_gateway_info' property. return attrs @@ -181,9 +180,14 @@ class CreateRouter(command.ShowOne): help=_("Create a distributed router") ) parser.add_argument( + '--ha', + action='store_true', + help=_("Create a highly available router") + ) + parser.add_argument( '--description', metavar='<description>', - help=_('Set router description') + help=_("Set router description") ) parser.add_argument( '--project', @@ -207,6 +211,8 @@ class CreateRouter(command.ShowOne): client = self.app.client_manager.network attrs = _get_attrs(self.app.client_manager, parsed_args) + if parsed_args.ha: + attrs['ha'] = parsed_args.ha obj = client.create_router(**attrs) columns = _get_columns(obj) @@ -514,12 +520,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 2021d9f0..1b778c91 100644 --- a/openstackclient/network/v2/subnet.py +++ b/openstackclient/network/v2/subnet.py @@ -179,7 +179,7 @@ def _get_attrs(client_manager, parsed_args, is_create=True): attrs['ipv6_ra_mode'] = parsed_args.ipv6_ra_mode if parsed_args.ipv6_address_mode is not None: attrs['ipv6_address_mode'] = parsed_args.ipv6_address_mode - if 'network_segment' in parsed_args: + if parsed_args.network_segment is not None: attrs['segment_id'] = client.find_segment( parsed_args.network_segment, ignore_missing=False).id @@ -299,13 +299,12 @@ class CreateSubnet(command.ShowOne): help=_("IPv6 address mode, " "valid modes: [dhcpv6-stateful, dhcpv6-stateless, slaac]") ) - if self.app.options.os_beta_command: - parser.add_argument( - '--network-segment', - metavar='<network-segment>', - help=_("Network segment to associate with this subnet " - "(ID only)") - ) + parser.add_argument( + '--network-segment', + metavar='<network-segment>', + help=_("Network segment to associate with this subnet " + "(name or ID)") + ) parser.add_argument( '--network', required=True, 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/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/functional/network/v2/test_network_segment.py b/openstackclient/tests/functional/network/v2/test_network_segment.py index de5aef96..b6f19ac4 100644 --- a/openstackclient/tests/functional/network/v2/test_network_segment.py +++ b/openstackclient/tests/functional/network/v2/test_network_segment.py @@ -39,8 +39,7 @@ class NetworkSegmentTests(base.TestCase): if cls.NETWORK_SEGMENT_EXTENSION: # Get the segment for the network. opts = cls.get_opts(['ID', 'Network']) - raw_output = cls.openstack('--os-beta-command ' - 'network segment list ' + raw_output = cls.openstack('network segment list ' ' --network ' + cls.NETWORK_NAME + ' ' + opts) raw_output_row = raw_output.split('\n')[0] @@ -55,14 +54,12 @@ class NetworkSegmentTests(base.TestCase): if self.NETWORK_SEGMENT_EXTENSION: opts = self.get_opts(['id']) raw_output = self.openstack( - '--os-beta-command' + ' network segment create --network ' + self.NETWORK_ID + ' --network-type geneve ' + ' --segment 2055 test_segment ' + opts ) network_segment_id = raw_output.strip('\n') - raw_output = self.openstack('--os-beta-command ' + - 'network segment delete ' + + raw_output = self.openstack('network segment delete ' + network_segment_id) self.assertOutput('', raw_output) else: @@ -71,8 +68,7 @@ class NetworkSegmentTests(base.TestCase): def test_network_segment_list(self): if self.NETWORK_SEGMENT_EXTENSION: opts = self.get_opts(['ID']) - raw_output = self.openstack('--os-beta-command ' - 'network segment list' + opts) + raw_output = self.openstack('network segment list' + opts) self.assertIn(self.NETWORK_SEGMENT_ID, raw_output) else: self.skipTest('Segment extension disabled') @@ -80,14 +76,12 @@ class NetworkSegmentTests(base.TestCase): def test_network_segment_set(self): if self.NETWORK_SEGMENT_EXTENSION: new_description = 'new_description' - raw_output = self.openstack('--os-beta-command ' - 'network segment set ' + + raw_output = self.openstack('network segment set ' + '--description ' + new_description + ' ' + self.NETWORK_SEGMENT_ID) self.assertOutput('', raw_output) opts = self.get_opts(['description']) - raw_output = self.openstack('--os-beta-command ' - 'network segment show ' + + raw_output = self.openstack('network segment show ' + self.NETWORK_SEGMENT_ID + opts) self.assertEqual(new_description + "\n", raw_output) else: @@ -96,8 +90,7 @@ class NetworkSegmentTests(base.TestCase): def test_network_segment_show(self): if self.NETWORK_SEGMENT_EXTENSION: opts = self.get_opts(['network_id']) - raw_output = self.openstack('--os-beta-command ' - 'network segment show ' + + raw_output = self.openstack('network segment show ' + self.NETWORK_SEGMENT_ID + opts) self.assertEqual(self.NETWORK_ID + "\n", raw_output) else: diff --git a/openstackclient/tests/unit/compute/v2/fakes.py b/openstackclient/tests/unit/compute/v2/fakes.py index 3c829773..985ce5e2 100644 --- a/openstackclient/tests/unit/compute/v2/fakes.py +++ b/openstackclient/tests/unit/compute/v2/fakes.py @@ -83,6 +83,7 @@ class FakeAggregate(object): "id": "aggregate-id-" + uuid.uuid4().hex, "metadata": { "availability_zone": "ag_zone", + "key1": "value1", } } diff --git a/openstackclient/tests/unit/compute/v2/test_aggregate.py b/openstackclient/tests/unit/compute/v2/test_aggregate.py index c636d3de..3efe0dbd 100644 --- a/openstackclient/tests/unit/compute/v2/test_aggregate.py +++ b/openstackclient/tests/unit/compute/v2/test_aggregate.py @@ -21,7 +21,6 @@ from osc_lib import utils from openstackclient.compute.v2 import aggregate from openstackclient.tests.unit.compute.v2 import fakes as compute_fakes -from openstackclient.tests.unit import utils as tests_utils class TestAggregate(compute_fakes.TestComputev2): @@ -235,7 +234,8 @@ class TestAggregateList(TestAggregate): TestAggregate.fake_ag.id, TestAggregate.fake_ag.name, TestAggregate.fake_ag.availability_zone, - {}, + {key: value for key, value in TestAggregate.fake_ag.metadata.items() + if key != 'availability_zone'}, ), ) def setUp(self): @@ -371,6 +371,62 @@ class TestAggregateSet(TestAggregate): self.fake_ag, parsed_args.property) self.assertIsNone(result) + def test_aggregate_set_with_no_property_and_property(self): + arglist = [ + '--no-property', + '--property', 'key2=value2', + 'ag1', + ] + verifylist = [ + ('no_property', True), + ('property', {'key2': 'value2'}), + ('aggregate', 'ag1'), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + result = self.cmd.take_action(parsed_args) + self.aggregate_mock.get.assert_called_once_with(parsed_args.aggregate) + self.assertNotCalled(self.aggregate_mock.update) + self.aggregate_mock.set_metadata.assert_called_once_with( + self.fake_ag, {'key1': None, 'key2': 'value2'}) + self.assertIsNone(result) + + def test_aggregate_set_with_no_property(self): + arglist = [ + '--no-property', + 'ag1', + ] + verifylist = [ + ('no_property', True), + ('aggregate', 'ag1'), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + result = self.cmd.take_action(parsed_args) + self.aggregate_mock.get.assert_called_once_with(parsed_args.aggregate) + self.assertNotCalled(self.aggregate_mock.update) + self.aggregate_mock.set_metadata.assert_called_once_with( + self.fake_ag, {'key1': None}) + self.assertIsNone(result) + + def test_aggregate_set_with_zone_and_no_property(self): + arglist = [ + '--zone', 'new_zone', + '--no-property', + 'ag1', + ] + verifylist = [ + ('zone', 'new_zone'), + ('no_property', True), + ('aggregate', 'ag1'), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + result = self.cmd.take_action(parsed_args) + self.aggregate_mock.get.assert_called_once_with(parsed_args.aggregate) + self.aggregate_mock.update.assert_called_once_with( + self.fake_ag, {'availability_zone': parsed_args.zone}) + self.aggregate_mock.set_metadata.assert_called_once_with( + self.fake_ag, {'key1': None}) + self.assertIsNone(result) + class TestAggregateShow(TestAggregate): @@ -387,7 +443,10 @@ class TestAggregateShow(TestAggregate): TestAggregate.fake_ag.hosts, TestAggregate.fake_ag.id, TestAggregate.fake_ag.name, - '', + utils.format_dict( + {key: value + for key, value in TestAggregate.fake_ag.metadata.items() + if key != 'availability_zone'}), ) def setUp(self): @@ -435,13 +494,32 @@ class TestAggregateUnset(TestAggregate): self.fake_ag, {'unset_key': None}) self.assertIsNone(result) - def test_aggregate_unset_no_property(self): + def test_aggregate_unset_multiple_properties(self): arglist = [ + '--property', 'unset_key1', + '--property', 'unset_key2', 'ag1', ] - verifylist = None - self.assertRaises(tests_utils.ParserException, - self.check_parser, - self.cmd, - arglist, - verifylist) + verifylist = [ + ('property', ['unset_key1', 'unset_key2']), + ('aggregate', 'ag1'), + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + result = self.cmd.take_action(parsed_args) + self.aggregate_mock.set_metadata.assert_called_once_with( + self.fake_ag, {'unset_key1': None, 'unset_key2': None}) + self.assertIsNone(result) + + def test_aggregate_unset_no_option(self): + arglist = [ + 'ag1', + ] + verifylist = [ + ('property', None), + ('aggregate', 'ag1'), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + result = self.cmd.take_action(parsed_args) + self.assertNotCalled(self.aggregate_mock.set_metadata) + self.assertIsNone(result) diff --git a/openstackclient/tests/unit/fakes.py b/openstackclient/tests/unit/fakes.py index f2598366..f7cb5676 100644 --- a/openstackclient/tests/unit/fakes.py +++ b/openstackclient/tests/unit/fakes.py @@ -38,7 +38,7 @@ _s.add_endpoint(AUTH_URL + ':5000/v2.0') _s = TEST_RESPONSE_DICT.add_service('network', name='neutron') _s.add_endpoint(AUTH_URL + ':9696') _s = TEST_RESPONSE_DICT.add_service('compute', name='nova') -_s.add_endpoint(AUTH_URL + ':8774/v2') +_s.add_endpoint(AUTH_URL + ':8774/v2.1') _s = TEST_RESPONSE_DICT.add_service('image', name='glance') _s.add_endpoint(AUTH_URL + ':9292') _s = TEST_RESPONSE_DICT.add_service('object', name='swift') 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 d7ebd0bc..94727ae3 100644 --- a/openstackclient/tests/unit/network/v2/fakes.py +++ b/openstackclient/tests/unit/network/v2/fakes.py @@ -122,7 +122,7 @@ class FakeAddressScope(object): @staticmethod def get_address_scopes(address_scopes=None, count=2): - """Get an iterable MagicMock object with a list of faked address scopes. + """Get an iterable Mock object with a list of faked address scopes. If address scopes list is provided, then initialize the Mock object with the list. Otherwise create one. @@ -331,7 +331,7 @@ class FakeNetwork(object): @staticmethod def get_networks(networks=None, count=2): - """Get an iterable MagicMock object with a list of faked networks. + """Get an iterable Mock object with a list of faked networks. If networks list is provided, then initialize the Mock object with the list. Otherwise create one. @@ -478,7 +478,7 @@ class FakePort(object): @staticmethod def get_ports(ports=None, count=2): - """Get an iterable MagicMock object with a list of faked ports. + """Get an iterable Mock object with a list of faked ports. If ports list is provided, then initialize the Mock object with the list. Otherwise create one. @@ -545,7 +545,7 @@ class FakeNetworkAgent(object): @staticmethod def get_network_agents(agents=None, count=2): - """Get an iterable MagicMock object with a list of faked network agents. + """Get an iterable Mock object with a list of faked network agents. If network agents list is provided, then initialize the Mock object with the list. Otherwise create one. @@ -615,7 +615,7 @@ class FakeNetworkRBAC(object): @staticmethod def get_network_rbacs(rbac_policies=None, count=2): - """Get an iterable MagicMock object with a list of faked rbac policies. + """Get an iterable Mock object with a list of faked rbac policies. If rbac policies list is provided, then initialize the Mock object with the list. Otherwise create one. @@ -633,6 +633,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.""" @@ -694,7 +844,7 @@ class FakeRouter(object): @staticmethod def get_routers(routers=None, count=2): - """Get an iterable MagicMock object with a list of faked routers. + """Get an iterable Mock object with a list of faked routers. If routers list is provided, then initialize the Mock object with the list. Otherwise create one. @@ -767,7 +917,7 @@ class FakeSecurityGroup(object): @staticmethod def get_security_groups(security_groups=None, count=2): - """Get an iterable MagicMock object with a list of faked security groups. + """Get an iterable Mock object with a list of faked security groups. If security groups list is provided, then initialize the Mock object with the list. Otherwise create one. @@ -845,7 +995,7 @@ class FakeSecurityGroupRule(object): @staticmethod def get_security_group_rules(security_group_rules=None, count=2): - """Get an iterable MagicMock object with a list of faked security group rules. + """Get an iterable Mock object with a list of faked security group rules. If security group rules list is provided, then initialize the Mock object with the list. Otherwise create one. @@ -930,7 +1080,7 @@ class FakeSubnet(object): @staticmethod def get_subnets(subnets=None, count=2): - """Get an iterable MagicMock object with a list of faked subnets. + """Get an iterable Mock object with a list of faked subnets. If subnets list is provided, then initialize the Mock object with the list. Otherwise create one. @@ -1008,7 +1158,7 @@ class FakeFloatingIP(object): @staticmethod def get_floating_ips(floating_ips=None, count=2): - """Get an iterable MagicMock object with a list of faked floating ips. + """Get an iterable Mock object with a list of faked floating ips. If floating_ips list is provided, then initialize the Mock object with the list. Otherwise create one. @@ -1091,7 +1241,7 @@ class FakeSubnetPool(object): @staticmethod def get_subnet_pools(subnet_pools=None, count=2): - """Get an iterable MagicMock object with a list of faked subnet pools. + """Get an iterable Mock object with a list of faked subnet pools. If subnet_pools list is provided, then initialize the Mock object with the list. Otherwise create one. 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_network_segment.py b/openstackclient/tests/unit/network/v2/test_network_segment.py index 3e755e07..0639766d 100644 --- a/openstackclient/tests/unit/network/v2/test_network_segment.py +++ b/openstackclient/tests/unit/network/v2/test_network_segment.py @@ -26,9 +26,6 @@ class TestNetworkSegment(network_fakes.TestNetworkV2): def setUp(self): super(TestNetworkSegment, self).setUp() - # Enable beta commands. - self.app.options.os_beta_command = True - # Get a shortcut to the network client self.network = self.app.client_manager.network @@ -81,22 +78,6 @@ class TestCreateNetworkSegment(TestNetworkSegment): self.assertRaises(tests_utils.ParserException, self.check_parser, self.cmd, [], []) - def test_create_no_beta_commands(self): - arglist = [ - '--network', self._network_segment.network_id, - '--network-type', self._network_segment.network_type, - self._network_segment.name, - ] - verifylist = [ - ('network', self._network_segment.network_id), - ('network_type', self._network_segment.network_type), - ('name', self._network_segment.name), - ] - self.app.options.os_beta_command = False - parsed_args = self.check_parser(self.cmd, arglist, verifylist) - self.assertRaises(exceptions.CommandError, self.cmd.take_action, - parsed_args) - def test_create_invalid_network_type(self): arglist = [ '--network', self._network_segment.network_id, @@ -182,7 +163,7 @@ class TestDeleteNetworkSegment(TestNetworkSegment): super(TestDeleteNetworkSegment, self).setUp() self.network.delete_segment = mock.Mock(return_value=None) - self.network.find_segment = mock.MagicMock( + self.network.find_segment = mock.Mock( side_effect=self._network_segments ) @@ -192,18 +173,6 @@ class TestDeleteNetworkSegment(TestNetworkSegment): self.namespace ) - def test_delete_no_beta_commands(self): - arglist = [ - self._network_segments[0].id, - ] - verifylist = [ - ('network_segment', [self._network_segments[0].id]), - ] - self.app.options.os_beta_command = False - parsed_args = self.check_parser(self.cmd, arglist, verifylist) - self.assertRaises(exceptions.CommandError, self.cmd.take_action, - parsed_args) - def test_delete(self): arglist = [ self._network_segments[0].id, @@ -251,7 +220,7 @@ class TestDeleteNetworkSegment(TestNetworkSegment): find_mock_result = [self._network_segments[0], exceptions.CommandError] self.network.find_segment = ( - mock.MagicMock(side_effect=find_mock_result) + mock.Mock(side_effect=find_mock_result) ) try: @@ -330,12 +299,6 @@ class TestListNetworkSegment(TestNetworkSegment): self.assertEqual(self.columns, columns) self.assertEqual(self.data, list(data)) - def test_list_no_beta_commands(self): - self.app.options.os_beta_command = False - parsed_args = self.check_parser(self.cmd, [], []) - self.assertRaises(exceptions.CommandError, self.cmd.take_action, - parsed_args) - def test_list_long(self): arglist = [ '--long', @@ -391,18 +354,6 @@ class TestSetNetworkSegment(TestNetworkSegment): # Get the command object to test self.cmd = network_segment.SetNetworkSegment(self.app, self.namespace) - def test_set_no_beta_commands(self): - arglist = [ - self._network_segment.id, - ] - verifylist = [ - ('network_segment', self._network_segment.id), - ] - self.app.options.os_beta_command = False - parsed_args = self.check_parser(self.cmd, arglist, verifylist) - self.assertRaises(exceptions.CommandError, self.cmd.take_action, - parsed_args) - def test_set_no_options(self): arglist = [ self._network_segment.id, @@ -485,18 +436,6 @@ class TestShowNetworkSegment(TestNetworkSegment): self.assertRaises(tests_utils.ParserException, self.check_parser, self.cmd, [], []) - def test_show_no_beta_commands(self): - arglist = [ - self._network_segment.id, - ] - verifylist = [ - ('network_segment', self._network_segment.id), - ] - self.app.options.os_beta_command = False - parsed_args = self.check_parser(self.cmd, arglist, verifylist) - self.assertRaises(exceptions.CommandError, self.cmd.take_action, - parsed_args) - def test_show_all_options(self): arglist = [ self._network_segment.id, diff --git a/openstackclient/tests/unit/network/v2/test_router.py b/openstackclient/tests/unit/network/v2/test_router.py index d12289e1..d85561bc 100644 --- a/openstackclient/tests/unit/network/v2/test_router.py +++ b/openstackclient/tests/unit/network/v2/test_router.py @@ -166,6 +166,7 @@ class TestCreateRouter(TestRouter): ('name', self.new_router.name), ('enable', True), ('distributed', False), + ('ha', False), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -178,6 +179,29 @@ class TestCreateRouter(TestRouter): self.assertEqual(self.columns, columns) self.assertEqual(self.data, data) + def test_create_with_ha_option(self): + arglist = [ + '--ha', + self.new_router.name, + ] + verifylist = [ + ('name', self.new_router.name), + ('enable', True), + ('distributed', False), + ('ha', True), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = (self.cmd.take_action(parsed_args)) + + self.network.create_router.assert_called_once_with(**{ + 'admin_state_up': True, + 'name': self.new_router.name, + 'ha': True, + }) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, data) + def test_create_with_AZ_hints(self): arglist = [ self.new_router.name, @@ -189,6 +213,7 @@ class TestCreateRouter(TestRouter): ('availability_zone_hints', ['fake-az', 'fake-az2']), ('enable', True), ('distributed', False), + ('ha', False) ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -748,9 +773,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/network/v2/test_subnet.py b/openstackclient/tests/unit/network/v2/test_subnet.py index 9c468f39..2d51aa4a 100644 --- a/openstackclient/tests/unit/network/v2/test_subnet.py +++ b/openstackclient/tests/unit/network/v2/test_subnet.py @@ -379,23 +379,6 @@ class TestCreateSubnet(TestSubnet): self.assertEqual(self.columns, columns) self.assertEqual(self.data_ipv6, data) - def test_create_no_beta_command_options(self): - arglist = [ - "--subnet-range", self._subnet.cidr, - "--network-segment", self._network_segment.id, - "--network", self._subnet.network_id, - self._subnet.name, - ] - verifylist = [ - ('name', self._subnet.name), - ('subnet_range', self._subnet.cidr), - ('network-segment', self._network_segment.id), - ('network', self._subnet.network_id), - ] - self.app.options.os_beta_command = False - self.assertRaises(tests_utils.ParserException, - self.check_parser, self.cmd, arglist, verifylist) - def test_create_with_network_segment(self): # Mock SDK calls for this test. self.network.create_subnet = mock.Mock(return_value=self._subnet) @@ -417,7 +400,6 @@ class TestCreateSubnet(TestSubnet): ] - self.app.options.os_beta_command = True parsed_args = self.check_parser(self.cmd, arglist, verifylist) columns, data = self.cmd.take_action(parsed_args) 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_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/v1/test_volume.py b/openstackclient/tests/unit/volume/v1/test_volume.py index 895f1f87..73c00844 100644 --- a/openstackclient/tests/unit/volume/v1/test_volume.py +++ b/openstackclient/tests/unit/volume/v1/test_volume.py @@ -23,6 +23,7 @@ from osc_lib import utils from openstackclient.tests.unit import fakes from openstackclient.tests.unit.identity.v2_0 import fakes as identity_fakes +from openstackclient.tests.unit import utils as tests_utils from openstackclient.tests.unit.volume.v1 import fakes as volume_fakes from openstackclient.volume.v1 import volume @@ -411,6 +412,67 @@ class TestVolumeCreate(TestVolume): self.assertEqual(self.columns, columns) self.assertEqual(self.datalist, data) + def test_volume_create_with_source(self): + self.volumes_mock.get.return_value = self.new_volume + arglist = [ + '--source', self.new_volume.id, + self.new_volume.display_name, + ] + verifylist = [ + ('source', self.new_volume.id), + ('name', self.new_volume.display_name), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + self.volumes_mock.create.assert_called_with( + None, + None, + self.new_volume.id, + self.new_volume.display_name, + None, + None, + None, + None, + None, + None, + None, + ) + self.assertEqual(self.columns, columns) + self.assertEqual(self.datalist, data) + + def test_volume_create_without_size(self): + arglist = [ + self.new_volume.display_name, + ] + verifylist = [ + ('name', self.new_volume.display_name), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.assertRaises(exceptions.CommandError, self.cmd.take_action, + parsed_args) + + def test_volume_create_with_multi_source(self): + arglist = [ + '--image', 'source_image', + '--source', 'source_volume', + '--snapshot', 'source_snapshot', + '--size', str(self.new_volume.size), + self.new_volume.display_name, + ] + verifylist = [ + ('image', 'source_image'), + ('source', 'source_volume'), + ('snapshot', 'source_snapshot'), + ('size', self.new_volume.size), + ('name', self.new_volume.display_name), + ] + + self.assertRaises(tests_utils.ParserException, self.check_parser, + self.cmd, arglist, verifylist) + class TestVolumeDelete(TestVolume): diff --git a/openstackclient/tests/unit/volume/v2/fakes.py b/openstackclient/tests/unit/volume/v2/fakes.py index 2aeea60a..5e1d16e1 100644 --- a/openstackclient/tests/unit/volume/v2/fakes.py +++ b/openstackclient/tests/unit/volume/v2/fakes.py @@ -222,6 +222,8 @@ class FakeVolumeClient(object): self.quotas.resource_class = fakes.FakeResource(None, {}) self.quota_classes = mock.Mock() self.quota_classes.resource_class = fakes.FakeResource(None, {}) + self.consistencygroups = mock.Mock() + self.consistencygroups.resource_class = fakes.FakeResource(None, {}) self.auth_token = kwargs['token'] self.management_url = kwargs['endpoint'] @@ -493,6 +495,59 @@ class FakeBackup(object): return mock.Mock(side_effect=backups) +class FakeConsistencyGroup(object): + """Fake one or more consistency group.""" + + @staticmethod + def create_one_consistency_group(attrs=None): + """Create a fake consistency group. + + :param Dictionary attrs: + A dictionary with all attributes + :return: + A FakeResource object with id, name, description, etc. + """ + attrs = attrs or {} + + # Set default attributes. + consistency_group_info = { + "id": 'backup-id-' + uuid.uuid4().hex, + "name": 'backup-name-' + uuid.uuid4().hex, + "description": 'description-' + uuid.uuid4().hex, + "status": "error", + "availability_zone": 'zone' + uuid.uuid4().hex, + "created_at": 'time-' + uuid.uuid4().hex, + "volume_types": ['volume-type1'], + } + + # Overwrite default attributes. + consistency_group_info.update(attrs) + + consistency_group = fakes.FakeResource( + info=copy.deepcopy(consistency_group_info), + loaded=True) + return consistency_group + + @staticmethod + def create_consistency_groups(attrs=None, count=2): + """Create multiple fake consistency groups. + + :param Dictionary attrs: + A dictionary with all attributes + :param int count: + The number of consistency groups to fake + :return: + A list of FakeResource objects faking the consistency groups + """ + consistency_groups = [] + for i in range(0, count): + consistency_group = ( + FakeConsistencyGroup.create_one_consistency_group(attrs)) + consistency_groups.append(consistency_group) + + return consistency_groups + + class FakeExtension(object): """Fake one or more extension.""" diff --git a/openstackclient/tests/unit/volume/v2/test_consistency_group.py b/openstackclient/tests/unit/volume/v2/test_consistency_group.py new file mode 100644 index 00000000..00e1b60e --- /dev/null +++ b/openstackclient/tests/unit/volume/v2/test_consistency_group.py @@ -0,0 +1,122 @@ +# +# 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. +# + +from osc_lib import utils + +from openstackclient.tests.unit.volume.v2 import fakes as volume_fakes +from openstackclient.volume.v2 import consistency_group + + +class TestConsistencyGroup(volume_fakes.TestVolume): + + def setUp(self): + super(TestConsistencyGroup, self).setUp() + + # Get a shortcut to the TransferManager Mock + self.consistencygroups_mock = ( + self.app.client_manager.volume.consistencygroups) + self.consistencygroups_mock.reset_mock() + + +class TestConsistencyGroupList(TestConsistencyGroup): + + consistency_groups = ( + volume_fakes.FakeConsistencyGroup.create_consistency_groups(count=2)) + + columns = [ + 'ID', + 'Status', + 'Name', + ] + columns_long = [ + 'ID', + 'Status', + 'Availability Zone', + 'Name', + 'Description', + 'Volume Types', + ] + data = [] + for c in consistency_groups: + data.append(( + c.id, + c.status, + c.name, + )) + data_long = [] + for c in consistency_groups: + data_long.append(( + c.id, + c.status, + c.availability_zone, + c.name, + c.description, + utils.format_list(c.volume_types) + )) + + def setUp(self): + super(TestConsistencyGroupList, self).setUp() + + self.consistencygroups_mock.list.return_value = self.consistency_groups + # Get the command to test + self.cmd = consistency_group.ListConsistencyGroup(self.app, None) + + def test_consistency_group_list_without_options(self): + arglist = [] + verifylist = [ + ("all_projects", False), + ("long", False), + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + columns, data = self.cmd.take_action(parsed_args) + + self.consistencygroups_mock.list.assert_called_once_with( + detailed=True, search_opts={'all_tenants': False}) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, list(data)) + + def test_consistency_group_list_with_all_project(self): + arglist = [ + "--all-projects" + ] + verifylist = [ + ("all_projects", True), + ("long", False), + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + columns, data = self.cmd.take_action(parsed_args) + + self.consistencygroups_mock.list.assert_called_once_with( + detailed=True, search_opts={'all_tenants': True}) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, list(data)) + + def test_consistency_group_list_with_long(self): + arglist = [ + "--long", + ] + verifylist = [ + ("all_projects", False), + ("long", True), + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + columns, data = self.cmd.take_action(parsed_args) + + self.consistencygroups_mock.list.assert_called_once_with( + detailed=True, search_opts={'all_tenants': False}) + self.assertEqual(self.columns_long, columns) + self.assertEqual(self.data_long, list(data)) diff --git a/openstackclient/tests/unit/volume/v2/test_volume.py b/openstackclient/tests/unit/volume/v2/test_volume.py index 5bdde9de..f4a7c142 100644 --- a/openstackclient/tests/unit/volume/v2/test_volume.py +++ b/openstackclient/tests/unit/volume/v2/test_volume.py @@ -21,6 +21,7 @@ from osc_lib import utils from openstackclient.tests.unit.identity.v3 import fakes as identity_fakes from openstackclient.tests.unit.image.v2 import fakes as image_fakes +from openstackclient.tests.unit import utils as tests_utils from openstackclient.tests.unit.volume.v2 import fakes as volume_fakes from openstackclient.volume.v2 import volume @@ -45,6 +46,10 @@ class TestVolume(volume_fakes.TestVolume): self.snapshots_mock = self.app.client_manager.volume.volume_snapshots self.snapshots_mock.reset_mock() + self.consistencygroups_mock = ( + self.app.client_manager.volume.consistencygroups) + self.consistencygroups_mock.reset_mock() + def setup_volumes_mock(self, count): volumes = volume_fakes.FakeVolume.create_volumes(count=count) @@ -123,18 +128,28 @@ class TestVolumeCreate(TestVolume): availability_zone=None, metadata=None, imageRef=None, - source_volid=None + source_volid=None, + consistencygroup_id=None, + source_replica=None, + multiattach=False, + scheduler_hints=None, ) self.assertEqual(self.columns, columns) self.assertEqual(self.datalist, data) def test_volume_create_options(self): + consistency_group = ( + volume_fakes.FakeConsistencyGroup.create_one_consistency_group()) + self.consistencygroups_mock.get.return_value = consistency_group arglist = [ '--size', str(self.new_volume.size), '--description', self.new_volume.description, '--type', self.new_volume.volume_type, '--availability-zone', self.new_volume.availability_zone, + '--consistency-group', consistency_group.id, + '--hint', 'k=v', + '--multi-attach', self.new_volume.name, ] verifylist = [ @@ -142,6 +157,9 @@ class TestVolumeCreate(TestVolume): ('description', self.new_volume.description), ('type', self.new_volume.volume_type), ('availability_zone', self.new_volume.availability_zone), + ('consistency_group', consistency_group.id), + ('hint', {'k': 'v'}), + ('multi_attach', True), ('name', self.new_volume.name), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -162,7 +180,11 @@ class TestVolumeCreate(TestVolume): availability_zone=self.new_volume.availability_zone, metadata=None, imageRef=None, - source_volid=None + source_volid=None, + consistencygroup_id=consistency_group.id, + source_replica=None, + multiattach=True, + scheduler_hints={'k': 'v'}, ) self.assertEqual(self.columns, columns) @@ -204,7 +226,11 @@ class TestVolumeCreate(TestVolume): availability_zone=None, metadata=None, imageRef=None, - source_volid=None + source_volid=None, + consistencygroup_id=None, + source_replica=None, + multiattach=False, + scheduler_hints=None, ) self.assertEqual(self.columns, columns) @@ -246,7 +272,11 @@ class TestVolumeCreate(TestVolume): availability_zone=None, metadata=None, imageRef=None, - source_volid=None + source_volid=None, + consistencygroup_id=None, + source_replica=None, + multiattach=False, + scheduler_hints=None, ) self.assertEqual(self.columns, columns) @@ -282,7 +312,11 @@ class TestVolumeCreate(TestVolume): availability_zone=None, metadata={'Alpha': 'a', 'Beta': 'b'}, imageRef=None, - source_volid=None + source_volid=None, + consistencygroup_id=None, + source_replica=None, + multiattach=False, + scheduler_hints=None, ) self.assertEqual(self.columns, columns) @@ -321,6 +355,10 @@ class TestVolumeCreate(TestVolume): metadata=None, imageRef=image.id, source_volid=None, + consistencygroup_id=None, + source_replica=None, + multiattach=False, + scheduler_hints=None, ) self.assertEqual(self.columns, columns) @@ -358,7 +396,11 @@ class TestVolumeCreate(TestVolume): availability_zone=None, metadata=None, imageRef=image.id, - source_volid=None + source_volid=None, + consistencygroup_id=None, + source_replica=None, + multiattach=False, + scheduler_hints=None, ) self.assertEqual(self.columns, columns) @@ -368,12 +410,10 @@ class TestVolumeCreate(TestVolume): snapshot = volume_fakes.FakeSnapshot.create_one_snapshot() self.new_volume.snapshot_id = snapshot.id arglist = [ - '--size', str(self.new_volume.size), '--snapshot', self.new_volume.snapshot_id, self.new_volume.name, ] verifylist = [ - ('size', self.new_volume.size), ('snapshot', self.new_volume.snapshot_id), ('name', self.new_volume.name), ] @@ -387,7 +427,7 @@ class TestVolumeCreate(TestVolume): columns, data = self.cmd.take_action(parsed_args) self.volumes_mock.create.assert_called_once_with( - size=self.new_volume.size, + size=None, snapshot_id=snapshot.id, name=self.new_volume.name, description=None, @@ -397,12 +437,83 @@ class TestVolumeCreate(TestVolume): availability_zone=None, metadata=None, imageRef=None, - source_volid=None + source_volid=None, + consistencygroup_id=None, + source_replica=None, + multiattach=False, + scheduler_hints=None, ) self.assertEqual(self.columns, columns) self.assertEqual(self.datalist, data) + def test_volume_create_with_source_replicated(self): + self.volumes_mock.get.return_value = self.new_volume + arglist = [ + '--source-replicated', self.new_volume.id, + self.new_volume.name, + ] + verifylist = [ + ('source_replicated', self.new_volume.id), + ('name', self.new_volume.name), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + self.volumes_mock.create.assert_called_once_with( + size=None, + snapshot_id=None, + name=self.new_volume.name, + description=None, + volume_type=None, + user_id=None, + project_id=None, + availability_zone=None, + metadata=None, + imageRef=None, + source_volid=None, + consistencygroup_id=None, + source_replica=self.new_volume.id, + multiattach=False, + scheduler_hints=None, + ) + + self.assertEqual(self.columns, columns) + self.assertEqual(self.datalist, data) + + def test_volume_create_without_size(self): + arglist = [ + self.new_volume.name, + ] + verifylist = [ + ('name', self.new_volume.name), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.assertRaises(exceptions.CommandError, self.cmd.take_action, + parsed_args) + + def test_volume_create_with_multi_source(self): + arglist = [ + '--image', 'source_image', + '--source', 'source_volume', + '--snapshot', 'source_snapshot', + '--source-replicated', 'source_replicated_volume', + '--size', str(self.new_volume.size), + self.new_volume.name, + ] + verifylist = [ + ('image', 'source_image'), + ('source', 'source_volume'), + ('snapshot', 'source_snapshot'), + ('source-replicated', 'source_replicated_volume'), + ('size', self.new_volume.size), + ('name', self.new_volume.name), + ] + + self.assertRaises(tests_utils.ParserException, self.check_parser, + self.cmd, arglist, verifylist) + class TestVolumeDelete(TestVolume): diff --git a/openstackclient/volume/v1/volume.py b/openstackclient/volume/v1/volume.py index 89fa2014..cafe8ce6 100644 --- a/openstackclient/volume/v1/volume.py +++ b/openstackclient/volume/v1/volume.py @@ -30,6 +30,20 @@ from openstackclient.i18n import _ LOG = logging.getLogger(__name__) +def _check_size_arg(args): + """Check whether --size option is required or not. + + Require size parameter only in case when snapshot or source + volume is not specified. + """ + + if ((args.snapshot or args.source) + is None and args.size is None): + msg = _("--size is a required option if snapshot " + "or source volume is not specified.") + raise exceptions.CommandError(msg) + + class CreateVolume(command.ShowOne): """Create new volume""" @@ -43,32 +57,32 @@ class CreateVolume(command.ShowOne): parser.add_argument( '--size', metavar='<size>', - required=True, type=int, - help=_('Volume size in GB'), + help=_("Volume size in GB (Required unless --snapshot or " + "--source is specified)"), ) parser.add_argument( '--type', metavar='<volume-type>', help=_("Set the type of volume"), ) - parser.add_argument( + source_group = parser.add_mutually_exclusive_group() + source_group.add_argument( '--image', metavar='<image>', help=_('Use <image> as source of volume (name or ID)'), ) - snapshot_group = parser.add_mutually_exclusive_group() - snapshot_group.add_argument( + source_group.add_argument( '--snapshot', metavar='<snapshot>', help=_('Use <snapshot> as source of volume (name or ID)'), ) - snapshot_group.add_argument( + source_group.add_argument( '--snapshot-id', metavar='<snapshot-id>', help=argparse.SUPPRESS, ) - parser.add_argument( + source_group.add_argument( '--source', metavar='<volume>', help=_('Volume to clone (name or ID)'), @@ -104,7 +118,7 @@ class CreateVolume(command.ShowOne): return parser def take_action(self, parsed_args): - + _check_size_arg(parsed_args) identity_client = self.app.client_manager.identity image_client = self.app.client_manager.image volume_client = self.app.client_manager.volume diff --git a/openstackclient/volume/v2/consistency_group.py b/openstackclient/volume/v2/consistency_group.py new file mode 100644 index 00000000..39f2d577 --- /dev/null +++ b/openstackclient/volume/v2/consistency_group.py @@ -0,0 +1,57 @@ +# +# 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. +# + +"""Volume v2 consistency group action implementations""" + +from osc_lib.command import command +from osc_lib import utils + +from openstackclient.i18n import _ + + +class ListConsistencyGroup(command.Lister): + """List consistency groups.""" + + def get_parser(self, prog_name): + parser = super(ListConsistencyGroup, self).get_parser(prog_name) + parser.add_argument( + '--all-projects', + action="store_true", + help=_('Show detail for all projects. Admin only. ' + '(defaults to False)') + ) + parser.add_argument( + '--long', + action="store_true", + help=_('List additional fields in output') + ) + return parser + + def take_action(self, parsed_args): + if parsed_args.long: + columns = ['ID', 'Status', 'Availability Zone', + 'Name', 'Description', 'Volume Types'] + else: + columns = ['ID', 'Status', 'Name'] + volume_client = self.app.client_manager.volume + consistency_groups = volume_client.consistencygroups.list( + detailed=True, + search_opts={'all_tenants': parsed_args.all_projects} + ) + + return (columns, ( + utils.get_item_properties( + s, columns, + formatters={'Volume Types': utils.format_list}) + for s in consistency_groups)) diff --git a/openstackclient/volume/v2/volume.py b/openstackclient/volume/v2/volume.py index b0067189..e7405114 100644 --- a/openstackclient/volume/v2/volume.py +++ b/openstackclient/volume/v2/volume.py @@ -30,6 +30,20 @@ from openstackclient.identity import common as identity_common LOG = logging.getLogger(__name__) +def _check_size_arg(args): + """Check whether --size option is required or not. + + Require size parameter only in case when snapshot or source + volume is not specified. + """ + + if ((args.snapshot or args.source or args.source_replicated) + is None and args.size is None): + msg = _("--size is a required option if snapshot " + "or source volume is not specified.") + raise exceptions.CommandError(msg) + + class CreateVolume(command.ShowOne): """Create new volume""" @@ -44,29 +58,35 @@ class CreateVolume(command.ShowOne): "--size", metavar="<size>", type=int, - required=True, - help=_("Volume size in GB"), + help=_("Volume size in GB (Required unless --snapshot or " + "--source or --source-replicated is specified)"), ) parser.add_argument( "--type", metavar="<volume-type>", help=_("Set the type of volume"), ) - parser.add_argument( + source_group = parser.add_mutually_exclusive_group() + source_group.add_argument( "--image", metavar="<image>", help=_("Use <image> as source of volume (name or ID)"), ) - parser.add_argument( + source_group.add_argument( "--snapshot", metavar="<snapshot>", help=_("Use <snapshot> as source of volume (name or ID)"), ) - parser.add_argument( + source_group.add_argument( "--source", metavar="<volume>", help=_("Volume to clone (name or ID)"), ) + source_group.add_argument( + "--source-replicated", + metavar="<replicated-volume>", + help=_("Replicated volume to clone (name or ID)"), + ) parser.add_argument( "--description", metavar="<description>", @@ -88,15 +108,34 @@ class CreateVolume(command.ShowOne): help=_("Create volume in <availability-zone>"), ) parser.add_argument( + "--consistency-group", + metavar="consistency-group>", + help=_("Consistency group where the new volume belongs to"), + ) + parser.add_argument( "--property", metavar="<key=value>", action=parseractions.KeyValueAction, help=_("Set a property to this volume " "(repeat option to set multiple properties)"), ) + parser.add_argument( + "--hint", + metavar="<key=value>", + action=parseractions.KeyValueAction, + help=_("Arbitrary scheduler hint key-value pairs to help boot " + "an instance (repeat option to set multiple hints)"), + ) + parser.add_argument( + "--multi-attach", + action="store_true", + help=_("Allow volume to be attached more than once " + "(default to False)") + ) return parser def take_action(self, parsed_args): + _check_size_arg(parsed_args) identity_client = self.app.client_manager.identity volume_client = self.app.client_manager.volume image_client = self.app.client_manager.image @@ -107,6 +146,18 @@ class CreateVolume(command.ShowOne): volume_client.volumes, parsed_args.source).id + replicated_source_volume = None + if parsed_args.source_replicated: + replicated_source_volume = utils.find_resource( + volume_client.volumes, + parsed_args.source_replicated).id + + consistency_group = None + if parsed_args.consistency_group: + consistency_group = utils.find_resource( + volume_client.consistencygroups, + parsed_args.consistency_group).id + image = None if parsed_args.image: image = utils.find_resource( @@ -142,7 +193,11 @@ class CreateVolume(command.ShowOne): availability_zone=parsed_args.availability_zone, metadata=parsed_args.property, imageRef=image, - source_volid=source_volume + source_volid=source_volume, + consistencygroup_id=consistency_group, + source_replica=replicated_source_volume, + multiattach=parsed_args.multi_attach, + scheduler_hints=parsed_args.hint, ) # Remove key links from being displayed volume._info.update( |
