Ian Cordasco 683f342506 Make column ordering consistent and predictable
This changes how we sort columns in listings and how we display the
column headings. The default columns are now stored as lists so that
they are the same from run to run. This simplifies some of the logic in
the shell modules as well. Instead of keeping static mappings of
attributes to column headings, we now use some simple python logic, to
title case the columns.

This commit covers:

- cell-* commands
- cloud-* commands
- host-* commands
- project-* commands
- region-* commands

Finally, we noticed that the cloud-list and region-list commands were
behaving differently from the rest of the -list commands. This
unifies the interface to add the --detail flag.

Closes-bug: #1659103
Closes-bug: #1659427
Closes-bug: #1668221
Change-Id: If5906780e501c7b9ba93ecf54a7bcf6db5ddfa1c
2017-03-03 10:25:53 -06:00

107 lines
3.9 KiB
Python

# -*- coding: utf-8 -*-
# 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.
"""Base class for shell unit tests."""
import argparse
import mock
from cratonclient import exceptions
from cratonclient.tests import base
class TestShellCommand(base.TestCase):
"""Base class for shell command unit tests."""
def setUp(self):
"""Initialize test fixtures."""
super(TestShellCommand, self).setUp()
self.formatter = mock.Mock()
self.formatter.configure.return_value = self.formatter
self.craton_client = mock.Mock()
self.inventory = mock.Mock()
self.craton_client.inventory.return_value = self.inventory
def assertRaisesCommandErrorWith(self, func, args):
"""Assert the shell command raises CommandError."""
self.assertRaises(
exceptions.CommandError,
func, self.craton_client, args,
)
def args_for(self, **kwargs):
"""Return a Namespace object with the specified kwargs."""
kwargs.setdefault('formatter', self.formatter)
return argparse.Namespace(**kwargs)
class TestShellCommandUsingPrintDict(TestShellCommand):
"""Base class for shell commands using print_dict."""
def setUp(self):
"""Initialize test fixtures."""
super(TestShellCommandUsingPrintDict, self).setUp()
self.print_dict_patch = mock.patch(
'cratonclient.common.cliutils.print_dict'
)
self.print_dict = self.print_dict_patch.start()
def tearDown(self):
"""Clean-up test fixtures."""
super(TestShellCommandUsingPrintDict, self).tearDown()
self.print_dict_patch.stop()
def assertNothingWasCalled(self):
"""Assert inventory, list, and print_dict were not called."""
self.assertFalse(self.craton_client.inventory.called)
self.assertFalse(self.print_dict.called)
class TestShellCommandUsingPrintList(TestShellCommand):
"""Base class for shell commands using print_list."""
def setUp(self):
"""Initialize test fixtures."""
super(TestShellCommandUsingPrintList, self).setUp()
self.print_list_patch = mock.patch(
'cratonclient.common.cliutils.print_list'
)
self.print_list = self.print_list_patch.start()
def tearDown(self):
"""Clean-up test fixtures."""
super(TestShellCommandUsingPrintList, self).tearDown()
self.print_list_patch.stop()
def assertNothingWasCalled(self):
"""Assert inventory, list, and print_dict were not called."""
self.assertFalse(self.craton_client.inventory.called)
self.assertFalse(self.print_list.called)
def assertSortedPrintListFieldsEqualTo(self, expected_fields):
"""Assert the sorted fields parameter is equal expected_fields."""
kwargs = self.formatter.configure.call_args[1]
self.assertListEqual(expected_fields,
sorted(kwargs['fields']))
def assertSortedFieldsEqualTo(self, expected_fields):
"""Assert the sorted fields parameter is equal expected_fields."""
kwargs = self.formatter.configure.call_args[1]
self.assertListEqual(expected_fields,
sorted(kwargs['fields']))
def assertFieldsEqualTo(self, expected_fields):
"""Assert the sorted fields parameter is equal expected_fields."""
kwargs = self.formatter.configure.call_args[1]
self.assertListEqual(expected_fields, kwargs['fields'])