
Modelled mostly after how Heat does it, but with some added power and also consistency between print_list and print_dict. We have print_list for printing of lists and print_dict for printing of show actions. Both functions take as arguments (aside from the data to print) also an optional dict of custom formatters (allowing to freely format the attributes before printing) and custom labels (allowing to tweak table headers and property names). The list is also sorted by one of the columns. The formatting is showcased on sample formatters and actions - rack-show and rack-list. Fixes bug 1213056 Change-Id: Ic14dbb930a5967e2634c1b4777e6705ab2a370ec
49 lines
1.7 KiB
Python
49 lines
1.7 KiB
Python
# Copyright 2013 OpenStack LLC.
|
|
# All Rights Reserved.
|
|
#
|
|
# 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.
|
|
|
|
|
|
import mock
|
|
|
|
from tuskarclient.common import utils
|
|
from tuskarclient.tests import utils as test_utils
|
|
|
|
|
|
class DefineCommandsTest(test_utils.TestCase):
|
|
|
|
def test_define_commands_from_module(self):
|
|
subparsers = mock.Mock()
|
|
subparser = mock.MagicMock()
|
|
subparsers.add_parser.return_value = subparser
|
|
dummy_module = self.dummy_command_module()
|
|
|
|
utils.define_commands_from_module(subparsers, dummy_module)
|
|
subparsers.add_parser.assert_called_with(
|
|
'dummy-list', help="Docstring.", description="Docstring.")
|
|
subparser.add_argument.assert_called_with(
|
|
'-a', metavar='<NUMBER>', help="Add a number.")
|
|
subparser.set_defaults.assert_called_with(
|
|
func=dummy_module.do_dummy_list)
|
|
|
|
def dummy_command_module(self):
|
|
@utils.arg('-a', metavar='<NUMBER>', help="Add a number.")
|
|
def do_dummy_list():
|
|
'''Docstring.'''
|
|
return 42
|
|
|
|
dummy = mock.Mock()
|
|
dummy.do_dummy_list = do_dummy_list
|
|
dummy.other_method = mock.Mock('other_method', return_value=43)
|
|
return dummy
|