From 4dfbc5011efe6b5cd30d5eac20b8cf29003004b2 Mon Sep 17 00:00:00 2001 From: whoami-rajat Date: Thu, 25 May 2023 19:03:16 +0530 Subject: [PATCH] Migrate 'volume summary' command to SDK This patch modifies the existing volume summary call from cinderclient to SDK. Depends-On: https://review.opendev.org/c/openstack/openstacksdk/+/884397 Change-Id: I364211a597b6ec155b154fa9d3c67239e750b5ad --- openstackclient/tests/unit/volume/v3/fakes.py | 12 +++ .../tests/unit/volume/v3/test_volume.py | 89 +++++++++---------- openstackclient/volume/v3/volume.py | 15 ++-- ...olume-summary-to-sdk-96ff58f653e0feaa.yaml | 4 + 4 files changed, 63 insertions(+), 57 deletions(-) create mode 100644 releasenotes/notes/migrate-volume-summary-to-sdk-96ff58f653e0feaa.yaml diff --git a/openstackclient/tests/unit/volume/v3/fakes.py b/openstackclient/tests/unit/volume/v3/fakes.py index c8b8e9f445..eb7b7f9ab7 100644 --- a/openstackclient/tests/unit/volume/v3/fakes.py +++ b/openstackclient/tests/unit/volume/v3/fakes.py @@ -15,6 +15,7 @@ from unittest import mock import uuid from cinderclient import api_versions +from openstack.block_storage.v3 import block_storage_summary as _summary from openstackclient.tests.unit.compute.v2 import fakes as compute_fakes from openstackclient.tests.unit import fakes @@ -531,3 +532,14 @@ def create_snapshot_manage_list_records(count=2): ) return snapshot_manage_list + + +def get_one_block_storage_summary(total_size, metadata=None): + summary_dict = { + 'total_count': 2, + 'total_size': total_size, + } + if metadata: + summary_dict['metadata'] = metadata + block_storage_summary = _summary.BlockStorageSummary(**summary_dict) + return block_storage_summary diff --git a/openstackclient/tests/unit/volume/v3/test_volume.py b/openstackclient/tests/unit/volume/v3/test_volume.py index 1b29867886..93b7a018e8 100644 --- a/openstackclient/tests/unit/volume/v3/test_volume.py +++ b/openstackclient/tests/unit/volume/v3/test_volume.py @@ -21,10 +21,37 @@ from osc_lib.cli import format_columns from osc_lib import exceptions from openstackclient.tests.unit.volume.v2 import fakes as volume_fakes +from openstackclient.tests.unit.volume.v3 import fakes as v3_fakes from openstackclient.volume.v3 import volume -class TestVolumeSummary(volume_fakes.TestVolume): +class BaseVolumeTest(volume_fakes.TestVolume): + def setUp(self): + super().setUp() + + self.app.client_manager.sdk_connection = mock.Mock() + self.app.client_manager.sdk_connection.volume = mock.Mock() + self.sdk_client = self.app.client_manager.sdk_connection.volume + + patcher = mock.patch.object( + sdk_utils, 'supports_microversion', return_value=True + ) + self.addCleanup(patcher.stop) + self.supports_microversion_mock = patcher.start() + self._set_mock_microversion( + self.app.client_manager.volume.api_version.get_string() + ) + + def _set_mock_microversion(self, mock_v): + """Set a specific microversion for the mock supports_microversion().""" + self.supports_microversion_mock.reset_mock(return_value=True) + self.supports_microversion_mock.side_effect = ( + lambda _, v: api_versions.APIVersion(v) + <= api_versions.APIVersion(mock_v) + ) + + +class TestVolumeSummary(BaseVolumeTest): columns = [ 'Total Count', 'Total Size', @@ -33,25 +60,18 @@ class TestVolumeSummary(volume_fakes.TestVolume): def setUp(self): super().setUp() - self.volumes_mock = self.app.client_manager.volume.volumes - self.volumes_mock.reset_mock() self.mock_vol_1 = volume_fakes.create_one_volume() self.mock_vol_2 = volume_fakes.create_one_volume() - self.return_dict = { - 'volume-summary': { - 'total_count': 2, - 'total_size': self.mock_vol_1.size + self.mock_vol_2.size, - } - } - self.volumes_mock.summary.return_value = self.return_dict + block_storage_summary = v3_fakes.get_one_block_storage_summary( + self.mock_vol_1.size + self.mock_vol_2.size + ) + self.sdk_client.summary.return_value = block_storage_summary # Get the command object to test self.cmd = volume.VolumeSummary(self.app, None) def test_volume_summary(self): - self.app.client_manager.volume.api_version = api_versions.APIVersion( - '3.12' - ) + self._set_mock_microversion('3.12') arglist = [ '--all-projects', ] @@ -62,9 +82,7 @@ class TestVolumeSummary(volume_fakes.TestVolume): columns, data = self.cmd.take_action(parsed_args) - self.volumes_mock.summary.assert_called_once_with( - all_tenants=True, - ) + self.sdk_client.summary.assert_called_once_with(True) self.assertEqual(self.columns, columns) @@ -88,14 +106,13 @@ class TestVolumeSummary(volume_fakes.TestVolume): ) def test_volume_summary_with_metadata(self): - self.app.client_manager.volume.api_version = api_versions.APIVersion( - '3.36' - ) + self._set_mock_microversion('3.36') combine_meta = {**self.mock_vol_1.metadata, **self.mock_vol_2.metadata} - meta_dict = copy.deepcopy(self.return_dict) - meta_dict['volume-summary']['metadata'] = combine_meta - self.volumes_mock.summary.return_value = meta_dict + block_storage_summary = v3_fakes.get_one_block_storage_summary( + self.mock_vol_1.size + self.mock_vol_2.size, metadata=combine_meta + ) + self.sdk_client.summary.return_value = block_storage_summary new_cols = copy.deepcopy(self.columns) new_cols.extend(['Metadata']) @@ -110,9 +127,7 @@ class TestVolumeSummary(volume_fakes.TestVolume): columns, data = self.cmd.take_action(parsed_args) - self.volumes_mock.summary.assert_called_once_with( - all_tenants=True, - ) + self.sdk_client.summary.assert_called_once_with(True) self.assertEqual(new_cols, columns) @@ -124,24 +139,10 @@ class TestVolumeSummary(volume_fakes.TestVolume): self.assertCountEqual(datalist, tuple(data)) -class TestVolumeRevertToSnapshot(volume_fakes.TestVolume): +class TestVolumeRevertToSnapshot(BaseVolumeTest): def setUp(self): super().setUp() - self.app.client_manager.sdk_connection = mock.Mock() - self.app.client_manager.sdk_connection.volume = mock.Mock() - self.sdk_client = self.app.client_manager.sdk_connection.volume - self.sdk_client.reset_mock() - - patcher = mock.patch.object( - sdk_utils, 'supports_microversion', return_value=True - ) - self.addCleanup(patcher.stop) - self.supports_microversion_mock = patcher.start() - self._set_mock_microversion( - self.app.client_manager.volume.api_version.get_string() - ) - self.mock_volume = volume_fakes.create_one_volume() self.mock_snapshot = volume_fakes.create_one_snapshot( attrs={'volume_id': self.mock_volume.id} @@ -150,14 +151,6 @@ class TestVolumeRevertToSnapshot(volume_fakes.TestVolume): # Get the command object to test self.cmd = volume.VolumeRevertToSnapshot(self.app, None) - def _set_mock_microversion(self, mock_v): - """Set a specific microversion for the mock supports_microversion().""" - self.supports_microversion_mock.reset_mock(return_value=True) - self.supports_microversion_mock.side_effect = ( - lambda _, v: api_versions.APIVersion(v) - <= api_versions.APIVersion(mock_v) - ) - def test_volume_revert_to_snapshot_pre_340(self): arglist = [ self.mock_snapshot.id, diff --git a/openstackclient/volume/v3/volume.py b/openstackclient/volume/v3/volume.py index 0f2d3c1d5a..91d58c3073 100644 --- a/openstackclient/volume/v3/volume.py +++ b/openstackclient/volume/v3/volume.py @@ -16,7 +16,6 @@ import logging -from cinderclient import api_versions from openstack import utils as sdk_utils from osc_lib.cli import format_columns from osc_lib.command import command @@ -43,9 +42,9 @@ class VolumeSummary(command.ShowOne): return parser def take_action(self, parsed_args): - volume_client = self.app.client_manager.volume + volume_client = self.app.client_manager.sdk_connection.volume - if volume_client.api_version < api_versions.APIVersion('3.12'): + if not sdk_utils.supports_microversion(volume_client, '3.12'): msg = _( "--os-volume-api-version 3.12 or greater is required to " "support the 'volume summary' command" @@ -60,21 +59,19 @@ class VolumeSummary(command.ShowOne): 'Total Count', 'Total Size', ] - if volume_client.api_version.matches('3.36'): + if sdk_utils.supports_microversion(volume_client, '3.36'): columns.append('metadata') column_headers.append('Metadata') # set value of 'all_tenants' when using project option all_projects = parsed_args.all_projects - vol_summary = volume_client.volumes.summary( - all_tenants=all_projects, - ) + vol_summary = volume_client.summary(all_projects) return ( column_headers, - utils.get_dict_properties( - vol_summary['volume-summary'], + utils.get_item_properties( + vol_summary, columns, formatters={'metadata': format_columns.DictColumn}, ), diff --git a/releasenotes/notes/migrate-volume-summary-to-sdk-96ff58f653e0feaa.yaml b/releasenotes/notes/migrate-volume-summary-to-sdk-96ff58f653e0feaa.yaml new file mode 100644 index 0000000000..9c495f8289 --- /dev/null +++ b/releasenotes/notes/migrate-volume-summary-to-sdk-96ff58f653e0feaa.yaml @@ -0,0 +1,4 @@ +--- +features: + - | + The ``volume summary`` command has been migrated to SDK.