diff --git a/cinderclient/tests/test_utils.py b/cinderclient/tests/test_utils.py index da2b700d1..1ba9e5ff7 100644 --- a/cinderclient/tests/test_utils.py +++ b/cinderclient/tests/test_utils.py @@ -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): diff --git a/cinderclient/tests/v2/test_shell.py b/cinderclient/tests/v2/test_shell.py index 9aceed6ec..aa03844cc 100644 --- a/cinderclient/tests/v2/test_shell.py +++ b/cinderclient/tests/v2/test_shell.py @@ -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') diff --git a/cinderclient/utils.py b/cinderclient/utils.py index 25416c646..876ed77d2 100644 --- a/cinderclient/utils.py +++ b/cinderclient/utils.py @@ -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) diff --git a/cinderclient/v2/shell.py b/cinderclient/v2/shell.py index a0f697746..b52894776 100644 --- a/cinderclient/v2/shell.py +++ b/cinderclient/v2/shell.py @@ -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',