Merge "Make cluster name positional in ca-show"

This commit is contained in:
Jenkins 2017-07-26 16:09:51 +00:00 committed by Gerrit Code Review
commit 1665533ab4
5 changed files with 75 additions and 19 deletions

View File

@ -35,14 +35,20 @@ from six import moves
from magnumclient.i18n import _
NAME_DEPRECATION_BASE = ('%sThe --name parameter is deprecated and '
'will be removed in a future release. Use the '
'<name> positional parameter %s.')
DEPRECATION_BASE = ('%sThe --%s parameter is deprecated and '
'will be removed in a future release. Use the '
'<%s> positional parameter %s.')
NAME_DEPRECATION_HELP = NAME_DEPRECATION_BASE % ('', 'instead')
NAME_DEPRECATION_HELP = DEPRECATION_BASE % ('', 'name', 'name', 'instead')
NAME_DEPRECATION_WARNING = NAME_DEPRECATION_BASE % (
'WARNING: ', 'to avoid seeing this message')
NAME_DEPRECATION_WARNING = DEPRECATION_BASE % (
'WARNING: ', 'name', 'name', 'to avoid seeing this message')
CLUSTER_DEPRECATION_HELP = DEPRECATION_BASE % ('', 'cluster', 'cluster',
'instead')
CLUSTER_DEPRECATION_WARNING = DEPRECATION_BASE % (
'WARNING: ', 'cluster', 'cluster', 'to avoid seeing this message')
def deprecation_message(preamble, new_name):
@ -107,6 +113,14 @@ def validate_name_args(positional_name, optional_name):
raise DuplicateArgs("<name>", (positional_name, optional_name))
def validate_cluster_args(positional_cluster, optional_cluster):
if optional_cluster:
print(CLUSTER_DEPRECATION_WARNING)
if positional_cluster and optional_cluster:
raise DuplicateArgs("<cluster>", (positional_cluster,
optional_cluster))
def deprecated(message):
"""Decorator for marking a call as deprecated by printing a given message.

View File

@ -51,9 +51,9 @@ class TestCommandLineArgument(utils.TestCase):
".*?^Try 'magnum help ",
]
_duplicate_name_arg_error = [
_duplicate_arg_error = [
'.*?^usage: ',
'.*?^error: (Duplicate "<name>" arguments:)',
'.*?^error: (Duplicate "<.*>" arguments:)',
".*?^Try 'magnum help ",
]

View File

@ -14,7 +14,7 @@
import mock
from magnumclient.common import cliutils as utils
from magnumclient.common import cliutils
from magnumclient.tests.v1 import shell_test_base
from magnumclient.v1 import certificates_shell
@ -28,8 +28,7 @@ class ShellTest(shell_test_base.TestCommandLineArgument):
mockbay.status = "CREATE_COMPLETE"
mockbay.uuid = "xxx"
mock_bay_get.return_value = mockbay
self._test_arg_success('ca-show '
'--bay xxx')
self._test_arg_success('ca-show --bay xxx')
expected_args = {}
expected_args['cluster_uuid'] = mockbay.uuid
mock_cert_get.assert_called_once_with(**expected_args)
@ -41,12 +40,48 @@ class ShellTest(shell_test_base.TestCommandLineArgument):
mockcluster.status = "CREATE_COMPLETE"
mockcluster.uuid = "xxx"
mock_cluster_get.return_value = mockcluster
self._test_arg_success('ca-show '
'--cluster xxx')
self._test_arg_success('ca-show xxx')
expected_args = {}
expected_args['cluster_uuid'] = mockcluster.uuid
mock_cert_get.assert_called_once_with(**expected_args)
@mock.patch('magnumclient.v1.clusters.ClusterManager.get')
@mock.patch('magnumclient.v1.certificates.CertificateManager.get')
def test_positional_cluster_bay_ca_show_success(self, mock_cert_get,
mock_cluster_get):
mockcluster = mock.MagicMock()
mockcluster.status = "CREATE_COMPLETE"
mockcluster.uuid = "xxx"
mock_cluster_get.return_value = mockcluster
self._test_arg_success('ca-show xxx --bay not-found')
expected_args = {}
expected_args['cluster_uuid'] = mockcluster.uuid
mock_cert_get.assert_called_once_with(**expected_args)
@mock.patch('magnumclient.v1.clusters.ClusterManager.get')
@mock.patch('magnumclient.v1.certificates.CertificateManager.get')
def test_cluster_bay_ca_show_success(self, mock_cert_get,
mock_cluster_get):
mockcluster = mock.MagicMock()
mockcluster.status = "CREATE_COMPLETE"
mockcluster.uuid = "xxx"
mock_cluster_get.return_value = mockcluster
self._test_arg_success('ca-show --cluster xxx --bay not-found')
expected_args = {}
expected_args['cluster_uuid'] = mockcluster.uuid
mock_cert_get.assert_called_once_with(**expected_args)
@mock.patch('magnumclient.v1.clusters.ClusterManager.get')
@mock.patch('magnumclient.v1.certificates.CertificateManager.get')
def test_ca_show_failure_duplicate_arg(self, mock_cert_get,
mock_cluster_get):
self.assertRaises(cliutils.DuplicateArgs,
self._test_arg_failure,
'ca-show foo --cluster foo',
self._duplicate_arg_error)
mock_cert_get.assert_not_called()
mock_cluster_get.assert_not_called()
@mock.patch('os.path.isfile')
@mock.patch('magnumclient.v1.bays.BayManager.get')
@mock.patch('magnumclient.v1.certificates.CertificateManager.create')
@ -135,11 +170,10 @@ class ShellTest(shell_test_base.TestCommandLineArgument):
@mock.patch('magnumclient.v1.certificates.CertificateManager.get')
def test_ca_show_failure_with_invalid_field(self, mock_cert_get,
mock_cluster_get):
_error_msg = [".*?^--cluster or --bay"]
self.assertRaises(utils.MissingArgs,
self.assertRaises(cliutils.MissingArgs,
self._test_arg_failure,
'ca-show',
_error_msg)
self._few_argument_error)
mock_cert_get.assert_not_called()
mock_cluster_get.assert_not_called()

View File

@ -320,7 +320,7 @@ class ShellTest(shell_test_base.TestCommandLineArgument):
self._test_arg_failure,
'cluster-create foo --name bar '
'--cluster-template xxx',
self._duplicate_name_arg_error)
self._duplicate_arg_error)
mock_create.assert_not_called()
@mock.patch('magnumclient.v1.clusters.ClusterManager.delete')

View File

@ -43,12 +43,20 @@ def _get_target_uuid(cs, args):
required=False,
metavar='<bay>',
help=_('ID or name of the bay.'))
@utils.arg('--cluster',
required=False,
@utils.arg('postional_cluster',
metavar='<cluster>',
nargs='?',
default=None,
help=_('ID or name of the cluster.'))
@utils.arg('--cluster',
metavar='<cluster>',
default=None,
help=(_('ID or name of the cluster. %s') %
utils.CLUSTER_DEPRECATION_HELP))
def do_ca_show(cs, args):
"""Show details about the CA certificate for a bay or cluster."""
utils.validate_cluster_args(args.postional_cluster, args.cluster)
args.cluster = args.postional_cluster or args.cluster
opts = {
'cluster_uuid': _get_target_uuid(cs, args)
}