Introduce helper for checking args deprecation
This patch introduces `_check_arguments` methods for processing deprecation workflow of novaclient.client.Client entry-point. Also, this patch adds a proper warning messages for 'auth_plugin', 'auth_system' arguments, which were removed previously Change-Id: I7ee210c08f4126b267572c5428511451aeb17260
This commit is contained in:
parent
330516543b
commit
bf09ad844e
@ -825,6 +825,32 @@ def get_client_class(version):
|
||||
return client_class
|
||||
|
||||
|
||||
def _check_arguments(kwargs, release, deprecated_name, right_name=None):
|
||||
"""Process deprecation of arguments.
|
||||
|
||||
Checks presence of deprecated argument in kwargs, prints proper warning
|
||||
message, renames key to right one it needed.
|
||||
"""
|
||||
if deprecated_name in kwargs:
|
||||
msg = _LW("The '%(old)s' argument is deprecated in %(release)s and "
|
||||
"its use may result in errors in future releases.") % {
|
||||
"old": deprecated_name, "release": release}
|
||||
if right_name:
|
||||
if right_name in kwargs:
|
||||
msg += _LW(" As '%(new)s' is provided, the '%(old)s' argument "
|
||||
"will be ignored.") % {"old": deprecated_name,
|
||||
"new": right_name}
|
||||
kwargs.pop(deprecated_name)
|
||||
else:
|
||||
msg += _LW(" Use '%s' instead.") % right_name
|
||||
kwargs[right_name] = kwargs.pop(deprecated_name)
|
||||
else:
|
||||
# just ignore it
|
||||
kwargs.pop(deprecated_name)
|
||||
|
||||
warnings.warn(msg)
|
||||
|
||||
|
||||
def Client(version, username=None, api_key=None, project_id=None,
|
||||
auth_url=None, **kwargs):
|
||||
"""Initialize client object based on given version.
|
||||
@ -847,6 +873,10 @@ def Client(version, username=None, api_key=None, project_id=None,
|
||||
session API. See "The novaclient Python API" page at
|
||||
python-novaclient's doc.
|
||||
"""
|
||||
|
||||
_check_arguments(kwargs, "Ocata", "auth_plugin")
|
||||
_check_arguments(kwargs, "Ocata", "auth_system")
|
||||
|
||||
api_version, client_class = _get_client_class_and_version(version)
|
||||
kwargs.pop("direct_use", None)
|
||||
return client_class(username=username, api_key=api_key,
|
||||
|
@ -13,7 +13,7 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
|
||||
import copy
|
||||
import logging
|
||||
|
||||
import fixtures
|
||||
@ -478,13 +478,13 @@ class SessionClientTest(utils.TestCase):
|
||||
'compute')
|
||||
|
||||
|
||||
class DiscoverExtensionTest(utils.TestCase):
|
||||
class ClientsUtilsTest(utils.TestCase):
|
||||
|
||||
@mock.patch("novaclient.client._discover_via_entry_points")
|
||||
@mock.patch("novaclient.client._discover_via_contrib_path")
|
||||
@mock.patch("novaclient.client._discover_via_python_path")
|
||||
@mock.patch("novaclient.extension.Extension")
|
||||
def test_discover_all(self, mock_extension,
|
||||
def test_discover_extensions_all(self, mock_extension,
|
||||
mock_discover_via_python_path,
|
||||
mock_discover_via_contrib_path,
|
||||
mock_discover_via_entry_points):
|
||||
@ -513,10 +513,9 @@ class DiscoverExtensionTest(utils.TestCase):
|
||||
@mock.patch("novaclient.client._discover_via_contrib_path")
|
||||
@mock.patch("novaclient.client._discover_via_python_path")
|
||||
@mock.patch("novaclient.extension.Extension")
|
||||
def test_discover_only_contrib(self, mock_extension,
|
||||
mock_discover_via_python_path,
|
||||
mock_discover_via_contrib_path,
|
||||
mock_discover_via_entry_points):
|
||||
def test_discover_extensions_only_contrib(
|
||||
self, mock_extension, mock_discover_via_python_path,
|
||||
mock_discover_via_contrib_path, mock_discover_via_entry_points):
|
||||
mock_discover_via_contrib_path.return_value = [("name", "module")]
|
||||
|
||||
version = novaclient.api_versions.APIVersion("2.0")
|
||||
@ -526,3 +525,39 @@ class DiscoverExtensionTest(utils.TestCase):
|
||||
self.assertFalse(mock_discover_via_python_path.called)
|
||||
self.assertFalse(mock_discover_via_entry_points.called)
|
||||
mock_extension.assert_called_once_with("name", "module")
|
||||
|
||||
@mock.patch("novaclient.client.warnings")
|
||||
def test__check_arguments(self, mock_warnings):
|
||||
release = "Coolest"
|
||||
|
||||
# no reference
|
||||
novaclient.client._check_arguments({}, release=release,
|
||||
deprecated_name="foo")
|
||||
self.assertFalse(mock_warnings.warn.called)
|
||||
novaclient.client._check_arguments({}, release=release,
|
||||
deprecated_name="foo",
|
||||
right_name="bar")
|
||||
self.assertFalse(mock_warnings.warn.called)
|
||||
|
||||
# with alternative
|
||||
original_kwargs = {"foo": "text"}
|
||||
actual_kwargs = copy.copy(original_kwargs)
|
||||
self.assertEqual(original_kwargs, actual_kwargs)
|
||||
novaclient.client._check_arguments(actual_kwargs, release=release,
|
||||
deprecated_name="foo",
|
||||
right_name="bar")
|
||||
self.assertNotEqual(original_kwargs, actual_kwargs)
|
||||
self.assertEqual({"bar": original_kwargs["foo"]}, actual_kwargs)
|
||||
self.assertTrue(mock_warnings.warn.called)
|
||||
|
||||
mock_warnings.warn.reset_mock()
|
||||
|
||||
# without alternative
|
||||
original_kwargs = {"foo": "text"}
|
||||
actual_kwargs = copy.copy(original_kwargs)
|
||||
self.assertEqual(original_kwargs, actual_kwargs)
|
||||
novaclient.client._check_arguments(actual_kwargs, release=release,
|
||||
deprecated_name="foo")
|
||||
self.assertNotEqual(original_kwargs, actual_kwargs)
|
||||
self.assertEqual({}, actual_kwargs)
|
||||
self.assertTrue(mock_warnings.warn.called)
|
||||
|
Loading…
Reference in New Issue
Block a user