diff options
| author | Zuul <zuul@review.opendev.org> | 2022-05-13 14:13:07 +0000 |
|---|---|---|
| committer | Gerrit Code Review <review@openstack.org> | 2022-05-13 14:13:07 +0000 |
| commit | 7fcfce486b5140d1a40a50f78de7cf831e184442 (patch) | |
| tree | 392a85427f9e5ed8f240c23c1ccc5a87c49154fe /openstackclient/tests | |
| parent | 4b14c400ff96240548b3fe6c242899a04a70a2cb (diff) | |
| parent | d727a6502369a6a7244ef1c07c1c483dbe0a8dab (diff) | |
| download | python-openstackclient-7fcfce486b5140d1a40a50f78de7cf831e184442.tar.gz | |
Merge "volume: Add 'block storage cluster *' commands"
Diffstat (limited to 'openstackclient/tests')
| -rw-r--r-- | openstackclient/tests/unit/volume/v3/fakes.py | 54 | ||||
| -rw-r--r-- | openstackclient/tests/unit/volume/v3/test_block_storage_cluster.py | 434 |
2 files changed, 488 insertions, 0 deletions
diff --git a/openstackclient/tests/unit/volume/v3/fakes.py b/openstackclient/tests/unit/volume/v3/fakes.py index 9040b2be..81ff0a98 100644 --- a/openstackclient/tests/unit/volume/v3/fakes.py +++ b/openstackclient/tests/unit/volume/v3/fakes.py @@ -32,6 +32,8 @@ class FakeVolumeClient(object): self.attachments = mock.Mock() self.attachments.resource_class = fakes.FakeResource(None, {}) + self.clusters = mock.Mock() + self.clusters.resource_class = fakes.FakeResource(None, {}) self.groups = mock.Mock() self.groups.resource_class = fakes.FakeResource(None, {}) self.group_snapshots = mock.Mock() @@ -70,6 +72,58 @@ FakeVolume = volume_v2_fakes.FakeVolume FakeVolumeType = volume_v2_fakes.FakeVolumeType +class FakeCluster: + """Fake one or more clusters.""" + + @staticmethod + def create_one_cluster(attrs=None): + """Create a fake service cluster. + + :param attrs: A dictionary with all attributes of service cluster + :return: A FakeResource object with id, name, status, etc. + """ + attrs = attrs or {} + + # Set default attribute + cluster_info = { + 'name': f'cluster-{uuid.uuid4().hex}', + 'binary': f'binary-{uuid.uuid4().hex}', + 'state': random.choice(['up', 'down']), + 'status': random.choice(['enabled', 'disabled']), + 'disabled_reason': None, + 'num_hosts': random.randint(1, 64), + 'num_down_hosts': random.randint(1, 64), + 'last_heartbeat': '2015-09-16T09:28:52.000000', + 'created_at': '2015-09-16T09:28:52.000000', + 'updated_at': '2015-09-16T09:28:52.000000', + 'replication_status': None, + 'frozen': False, + 'active_backend_id': None, + } + + # Overwrite default attributes if there are some attributes set + cluster_info.update(attrs) + + return fakes.FakeResource( + None, + cluster_info, + loaded=True) + + @staticmethod + def create_clusters(attrs=None, count=2): + """Create multiple fake service clusters. + + :param attrs: A dictionary with all attributes of service cluster + :param count: The number of service clusters to be faked + :return: A list of FakeResource objects + """ + clusters = [] + for n in range(0, count): + clusters.append(FakeCluster.create_one_cluster(attrs)) + + return clusters + + class FakeVolumeGroup: """Fake one or more volume groups.""" diff --git a/openstackclient/tests/unit/volume/v3/test_block_storage_cluster.py b/openstackclient/tests/unit/volume/v3/test_block_storage_cluster.py new file mode 100644 index 00000000..d87a946b --- /dev/null +++ b/openstackclient/tests/unit/volume/v3/test_block_storage_cluster.py @@ -0,0 +1,434 @@ +# 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 cinderclient import api_versions +from osc_lib import exceptions + +from openstackclient.tests.unit.volume.v3 import fakes as volume_fakes +from openstackclient.volume.v3 import block_storage_cluster + + +class TestBlockStorageCluster(volume_fakes.TestVolume): + + def setUp(self): + super().setUp() + + # Get a shortcut to the BlockStorageClusterManager Mock + self.cluster_mock = self.app.client_manager.volume.clusters + self.cluster_mock.reset_mock() + + +class TestBlockStorageClusterList(TestBlockStorageCluster): + + # The cluster to be listed + fake_clusters = volume_fakes.FakeCluster.create_clusters() + + def setUp(self): + super().setUp() + + self.cluster_mock.list.return_value = self.fake_clusters + + # Get the command object to test + self.cmd = \ + block_storage_cluster.ListBlockStorageCluster(self.app, None) + + def test_cluster_list(self): + self.app.client_manager.volume.api_version = \ + api_versions.APIVersion('3.7') + + arglist = [ + ] + verifylist = [ + ('cluster', None), + ('binary', None), + ('is_up', None), + ('is_disabled', None), + ('num_hosts', None), + ('num_down_hosts', None), + ('long', False), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + expected_columns = ('Name', 'Binary', 'State', 'Status') + expected_data = tuple( + ( + cluster.name, + cluster.binary, + cluster.state, + cluster.status, + ) for cluster in self.fake_clusters + ) + columns, data = self.cmd.take_action(parsed_args) + + self.assertEqual(expected_columns, columns) + self.assertEqual(expected_data, tuple(data)) + + # checking if proper call was made to list clusters + self.cluster_mock.list.assert_called_with( + name=None, + binary=None, + is_up=None, + disabled=None, + num_hosts=None, + num_down_hosts=None, + detailed=False, + ) + + def test_cluster_list_with_full_options(self): + self.app.client_manager.volume.api_version = \ + api_versions.APIVersion('3.7') + + arglist = [ + '--cluster', 'foo', + '--binary', 'bar', + '--up', + '--disabled', + '--num-hosts', '5', + '--num-down-hosts', '0', + '--long', + ] + verifylist = [ + ('cluster', 'foo'), + ('binary', 'bar'), + ('is_up', True), + ('is_disabled', True), + ('num_hosts', 5), + ('num_down_hosts', 0), + ('long', True), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + expected_columns = ( + 'Name', + 'Binary', + 'State', + 'Status', + 'Num Hosts', + 'Num Down Hosts', + 'Last Heartbeat', + 'Disabled Reason', + 'Created At', + 'Updated At', + ) + expected_data = tuple( + ( + cluster.name, + cluster.binary, + cluster.state, + cluster.status, + cluster.num_hosts, + cluster.num_down_hosts, + cluster.last_heartbeat, + cluster.disabled_reason, + cluster.created_at, + cluster.updated_at, + ) for cluster in self.fake_clusters + ) + columns, data = self.cmd.take_action(parsed_args) + + self.assertEqual(expected_columns, columns) + self.assertEqual(expected_data, tuple(data)) + + # checking if proper call was made to list clusters + self.cluster_mock.list.assert_called_with( + name='foo', + binary='bar', + is_up=True, + disabled=True, + num_hosts=5, + num_down_hosts=0, + detailed=True, + ) + + def test_cluster_list_pre_v37(self): + self.app.client_manager.volume.api_version = \ + api_versions.APIVersion('3.6') + + arglist = [ + ] + verifylist = [ + ('cluster', None), + ('binary', None), + ('is_up', None), + ('is_disabled', None), + ('num_hosts', None), + ('num_down_hosts', None), + ('long', False), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + exc = self.assertRaises( + exceptions.CommandError, + self.cmd.take_action, + parsed_args) + self.assertIn( + '--os-volume-api-version 3.7 or greater is required', str(exc)) + + +class TestBlockStorageClusterSet(TestBlockStorageCluster): + + cluster = volume_fakes.FakeCluster.create_one_cluster() + columns = ( + 'Name', + 'Binary', + 'State', + 'Status', + 'Disabled Reason', + 'Hosts', + 'Down Hosts', + 'Last Heartbeat', + 'Created At', + 'Updated At', + 'Replication Status', + 'Frozen', + 'Active Backend ID', + ) + data = ( + cluster.name, + cluster.binary, + cluster.state, + cluster.status, + cluster.disabled_reason, + cluster.num_hosts, + cluster.num_down_hosts, + cluster.last_heartbeat, + cluster.created_at, + cluster.updated_at, + cluster.replication_status, + cluster.frozen, + cluster.active_backend_id, + ) + + def setUp(self): + super().setUp() + + self.cluster_mock.update.return_value = self.cluster + + self.cmd = \ + block_storage_cluster.SetBlockStorageCluster(self.app, None) + + def test_cluster_set(self): + self.app.client_manager.volume.api_version = \ + api_versions.APIVersion('3.7') + + arglist = [ + '--enable', + self.cluster.name, + ] + verifylist = [ + ('cluster', self.cluster.name), + ('binary', 'cinder-volume'), + ('disabled', False), + ('disabled_reason', None), + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, tuple(data)) + + self.cluster_mock.update.assert_called_once_with( + self.cluster.name, + 'cinder-volume', + disabled=False, + disabled_reason=None, + ) + + def test_cluster_set_disable_with_reason(self): + self.app.client_manager.volume.api_version = \ + api_versions.APIVersion('3.7') + + arglist = [ + '--binary', self.cluster.binary, + '--disable', + '--disable-reason', 'foo', + self.cluster.name, + ] + verifylist = [ + ('cluster', self.cluster.name), + ('binary', self.cluster.binary), + ('disabled', True), + ('disabled_reason', 'foo'), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, tuple(data)) + + self.cluster_mock.update.assert_called_once_with( + self.cluster.name, + self.cluster.binary, + disabled=True, + disabled_reason='foo', + ) + + def test_cluster_set_only_with_disable_reason(self): + self.app.client_manager.volume.api_version = \ + api_versions.APIVersion('3.7') + + arglist = [ + '--disable-reason', 'foo', + self.cluster.name, + ] + verifylist = [ + ('cluster', self.cluster.name), + ('binary', 'cinder-volume'), + ('disabled', None), + ('disabled_reason', 'foo'), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + exc = self.assertRaises( + exceptions.CommandError, + self.cmd.take_action, + parsed_args) + self.assertIn( + "Cannot specify --disable-reason without --disable", str(exc)) + + def test_cluster_set_enable_with_disable_reason(self): + self.app.client_manager.volume.api_version = \ + api_versions.APIVersion('3.7') + + arglist = [ + '--enable', + '--disable-reason', 'foo', + self.cluster.name, + ] + verifylist = [ + ('cluster', self.cluster.name), + ('binary', 'cinder-volume'), + ('disabled', False), + ('disabled_reason', 'foo'), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + exc = self.assertRaises( + exceptions.CommandError, + self.cmd.take_action, + parsed_args) + self.assertIn( + "Cannot specify --disable-reason without --disable", str(exc)) + + def test_cluster_set_pre_v37(self): + self.app.client_manager.volume.api_version = \ + api_versions.APIVersion('3.6') + + arglist = [ + '--enable', + self.cluster.name, + ] + verifylist = [ + ('cluster', self.cluster.name), + ('binary', 'cinder-volume'), + ('disabled', False), + ('disabled_reason', None), + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + exc = self.assertRaises( + exceptions.CommandError, + self.cmd.take_action, + parsed_args) + self.assertIn( + '--os-volume-api-version 3.7 or greater is required', str(exc)) + + +class TestBlockStorageClusterShow(TestBlockStorageCluster): + + cluster = volume_fakes.FakeCluster.create_one_cluster() + columns = ( + 'Name', + 'Binary', + 'State', + 'Status', + 'Disabled Reason', + 'Hosts', + 'Down Hosts', + 'Last Heartbeat', + 'Created At', + 'Updated At', + 'Replication Status', + 'Frozen', + 'Active Backend ID', + ) + data = ( + cluster.name, + cluster.binary, + cluster.state, + cluster.status, + cluster.disabled_reason, + cluster.num_hosts, + cluster.num_down_hosts, + cluster.last_heartbeat, + cluster.created_at, + cluster.updated_at, + cluster.replication_status, + cluster.frozen, + cluster.active_backend_id, + ) + + def setUp(self): + super().setUp() + + self.cluster_mock.show.return_value = self.cluster + + self.cmd = \ + block_storage_cluster.ShowBlockStorageCluster(self.app, None) + + def test_cluster_show(self): + self.app.client_manager.volume.api_version = \ + api_versions.APIVersion('3.7') + + arglist = [ + '--binary', self.cluster.binary, + self.cluster.name, + ] + verifylist = [ + ('cluster', self.cluster.name), + ('binary', self.cluster.binary), + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, tuple(data)) + + self.cluster_mock.show.assert_called_once_with( + self.cluster.name, + binary=self.cluster.binary, + ) + + def test_cluster_show_pre_v37(self): + self.app.client_manager.volume.api_version = \ + api_versions.APIVersion('3.6') + + arglist = [ + '--binary', self.cluster.binary, + self.cluster.name, + ] + verifylist = [ + ('cluster', self.cluster.name), + ('binary', self.cluster.binary), + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + exc = self.assertRaises( + exceptions.CommandError, + self.cmd.take_action, + parsed_args) + self.assertIn( + '--os-volume-api-version 3.7 or greater is required', str(exc)) |
