summaryrefslogtreecommitdiff
path: root/openstackclient
diff options
context:
space:
mode:
Diffstat (limited to 'openstackclient')
-rw-r--r--openstackclient/tests/unit/volume/v2/fakes.py55
-rw-r--r--openstackclient/tests/unit/volume/v2/test_consistency_group.py122
-rw-r--r--openstackclient/volume/v2/consistency_group.py57
3 files changed, 234 insertions, 0 deletions
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/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))