From e04e389da2e9d8ec96398356ceda2ab2388cdad5 Mon Sep 17 00:00:00 2001 From: Ukesh Kumar Vasudevan Date: Thu, 15 Sep 2016 12:51:23 +0530 Subject: [PATCH] router list if availability_zone ext not enabled when calling router list with --long OSC expects the availability_zone field in the response, even if this extension is not loaded: As per the document, http://developer.openstack.org/api-ref/networking/ v2/?expanded=list-routers-detail, The availability zone(s) for the router is available when router_availability_zone extension is enabled. Added testcase to test router list without availability zone Change-Id: Ic9abb06140eb310b797ade8b0463a876d8bea1b8 Closes-bug: #1622565 --- openstackclient/network/v2/router.py | 11 ++++-- .../tests/unit/network/v2/test_router.py | 36 +++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/openstackclient/network/v2/router.py b/openstackclient/network/v2/router.py index f6d96d03bb..d30197cc41 100644 --- a/openstackclient/network/v2/router.py +++ b/openstackclient/network/v2/router.py @@ -281,13 +281,20 @@ class ListRouter(command.Lister): columns = columns + ( 'routes', 'external_gateway_info', - 'availability_zones' ) column_headers = column_headers + ( 'Routes', 'External gateway info', - 'Availability zones' ) + # availability zone will be available only when + # router_availability_zone extension is enabled + if client.find_extension("router_availability_zone"): + columns = columns + ( + 'availability_zones', + ) + column_headers = column_headers + ( + 'Availability zones', + ) data = client.routers() return (column_headers, diff --git a/openstackclient/tests/unit/network/v2/test_router.py b/openstackclient/tests/unit/network/v2/test_router.py index 26fe655e30..9d0342228e 100644 --- a/openstackclient/tests/unit/network/v2/test_router.py +++ b/openstackclient/tests/unit/network/v2/test_router.py @@ -285,6 +285,7 @@ class TestListRouter(TestRouter): # The routers going to be listed up. routers = network_fakes.FakeRouter.create_routers(count=3) + _extensions = network_fakes.FakeExtension.create_one_extension() columns = ( 'ID', @@ -300,6 +301,10 @@ class TestListRouter(TestRouter): 'External gateway info', 'Availability zones' ) + columns_long_no_az = columns + ( + 'Routes', + 'External gateway info', + ) data = [] for r in routers: @@ -322,6 +327,15 @@ class TestListRouter(TestRouter): osc_utils.format_list(r.availability_zones), ) ) + data_long_no_az = [] + for i in range(0, len(routers)): + r = routers[i] + data_long_no_az.append( + data[i] + ( + router._format_routes(r.routes), + router._format_external_gateway_info(r.external_gateway_info), + ) + ) def setUp(self): super(TestListRouter, self).setUp() @@ -330,6 +344,7 @@ class TestListRouter(TestRouter): self.cmd = router.ListRouter(self.app, self.namespace) self.network.routers = mock.Mock(return_value=self.routers) + self.network.find_extension = mock.Mock(return_value=self._extensions) def test_router_list_no_options(self): arglist = [] @@ -365,6 +380,27 @@ class TestListRouter(TestRouter): self.assertEqual(self.columns_long, columns) self.assertEqual(self.data_long, list(data)) + def test_router_list_long_no_az(self): + arglist = [ + '--long', + ] + verifylist = [ + ('long', True), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + # to mock, that no availability zone + self.network.find_extension = mock.Mock(return_value=None) + + # In base command class Lister in cliff, abstract method take_action() + # returns a tuple containing the column names and an iterable + # containing the data to be listed. + columns, data = self.cmd.take_action(parsed_args) + + self.network.routers.assert_called_once_with() + self.assertEqual(self.columns_long_no_az, columns) + self.assertEqual(self.data_long_no_az, list(data)) + class TestRemovePortFromRouter(TestRouter): '''Remove port from a Router '''