Merge "Client output is not sorted by --sort_key"

This commit is contained in:
Jenkins 2015-01-07 23:15:39 +00:00 committed by Gerrit Code Review
commit 0d93c3b76d
4 changed files with 70 additions and 5 deletions

@ -110,9 +110,10 @@ class PrintListTestCase(test_utils.TestCase):
def test_print_list_with_list(self):
Row = collections.namedtuple('Row', ['a', 'b'])
to_print = [Row(a=1, b=2), Row(a=3, b=4)]
to_print = [Row(a=3, b=4), Row(a=1, b=2)]
with CaptureStdout() as cso:
utils.print_list(to_print, ['a', 'b'])
# Output should be sorted by the first key (a)
self.assertEqual("""\
+---+---+
| a | b |
@ -120,6 +121,36 @@ class PrintListTestCase(test_utils.TestCase):
| 1 | 2 |
| 3 | 4 |
+---+---+
""", cso.read())
def test_print_list_with_list_sortby(self):
Row = collections.namedtuple('Row', ['a', 'b'])
to_print = [Row(a=4, b=3), Row(a=2, b=1)]
with CaptureStdout() as cso:
utils.print_list(to_print, ['a', 'b'], sortby_index=1)
# Output should be sorted by the second key (b)
self.assertEqual("""\
+---+---+
| a | b |
+---+---+
| 2 | 1 |
| 4 | 3 |
+---+---+
""", cso.read())
def test_print_list_with_list_no_sort(self):
Row = collections.namedtuple('Row', ['a', 'b'])
to_print = [Row(a=3, b=4), Row(a=1, b=2)]
with CaptureStdout() as cso:
utils.print_list(to_print, ['a', 'b'], sortby_index=None)
# Output should be in the order given
self.assertEqual("""\
+---+---+
| a | b |
+---+---+
| 3 | 4 |
| 1 | 2 |
+---+---+
""", cso.read())
def test_print_list_with_generator(self):

@ -14,6 +14,7 @@
# under the License.
import fixtures
import mock
from requests_mock.contrib import fixture as requests_mock_fixture
from cinderclient import client
@ -107,6 +108,24 @@ class ShellTest(utils.TestCase):
self.run_command('list --sort_key=name --sort_dir=asc')
self.assert_called('GET', '/volumes/detail?sort_dir=asc&sort_key=name')
def test_list_reorder_with_sort(self):
# sortby_index is None if there is sort information
for cmd in ['list --sort_key=name',
'list --sort_dir=asc',
'list --sort_key=name --sort_dir=asc']:
with mock.patch('cinderclient.utils.print_list') as mock_print:
self.run_command(cmd)
mock_print.assert_called_once_with(
mock.ANY, mock.ANY, sortby_index=None)
def test_list_reorder_without_sort(self):
# sortby_index is 0 without sort information
for cmd in ['list', 'list --all-tenants']:
with mock.patch('cinderclient.utils.print_list') as mock_print:
self.run_command(cmd)
mock_print.assert_called_once_with(
mock.ANY, mock.ANY, sortby_index=0)
def test_list_availability_zone(self):
self.run_command('availability-zone-list')
self.assert_called('GET', '/os-availability-zone')

@ -110,7 +110,16 @@ def _print(pt, order):
print(strutils.safe_encode(pt.get_string(sortby=order)))
def print_list(objs, fields, formatters=None, order_by=None):
def print_list(objs, fields, formatters=None, sortby_index=0):
'''Prints a list of objects.
@param objs: Objects to print
@param fields: Fields on each object to be printed
@param formatters: Custom field formatters
@param sortby_index: Results sorted against the key in the fields list at
this index; if None then the object order is not
altered
'''
formatters = formatters or {}
mixed_case_fields = ['serverId']
pt = prettytable.PrettyTable([f for f in fields], caching=False)
@ -133,8 +142,10 @@ def print_list(objs, fields, formatters=None, order_by=None):
row.append(data)
pt.add_row(row)
if order_by is None:
order_by = fields[0]
if sortby_index is None:
order_by = None
else:
order_by = fields[sortby_index]
_print(pt, order_by)

@ -217,7 +217,11 @@ def do_list(cs, args):
else:
key_list = ['ID', 'Status', 'Name',
'Size', 'Volume Type', 'Bootable', 'Attached to']
utils.print_list(volumes, key_list)
if args.sort_key or args.sort_dir:
sortby_index = None
else:
sortby_index = 0
utils.print_list(volumes, key_list, sortby_index=sortby_index)
@utils.arg('volume',