Merge "Migrate 'volume summary' command to SDK"

This commit is contained in:
Zuul 2023-07-24 12:29:14 +00:00 committed by Gerrit Code Review
commit 7ea78b6ef6
4 changed files with 63 additions and 57 deletions

View File

@ -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

View File

@ -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,

View File

@ -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},
),

View File

@ -0,0 +1,4 @@
---
features:
- |
The ``volume summary`` command has been migrated to SDK.