|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | +# not use this file except in compliance with the License. You may obtain |
| 3 | +# a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | +# License for the specific language governing permissions and limitations |
| 11 | +# under the License. |
| 12 | +# |
| 13 | + |
| 14 | +import mock |
| 15 | + |
| 16 | +from openstackclient.network.v2 import router |
| 17 | +from openstackclient.tests.network.v2 import fakes as network_fakes |
| 18 | + |
| 19 | + |
| 20 | +class TestRouter(network_fakes.TestNetworkV2): |
| 21 | + |
| 22 | + def setUp(self): |
| 23 | + super(TestRouter, self).setUp() |
| 24 | + |
| 25 | + # Get a shortcut to the network client |
| 26 | + self.network = self.app.client_manager.network |
| 27 | + |
| 28 | + |
| 29 | +class TestListRouter(TestRouter): |
| 30 | + |
| 31 | + # The routers going to be listed up. |
| 32 | + routers = network_fakes.FakeRouter.create_routers(count=3) |
| 33 | + |
| 34 | + columns = ( |
| 35 | + 'ID', |
| 36 | + 'Name', |
| 37 | + 'Status', |
| 38 | + 'State', |
| 39 | + 'Distributed', |
| 40 | + 'HA', |
| 41 | + 'Project', |
| 42 | + ) |
| 43 | + columns_long = columns + ( |
| 44 | + 'Routes', |
| 45 | + 'External gateway info', |
| 46 | + ) |
| 47 | + |
| 48 | + data = [] |
| 49 | + for r in routers: |
| 50 | + data.append(( |
| 51 | + r.id, |
| 52 | + r.name, |
| 53 | + r.status, |
| 54 | + router._format_admin_state(r.admin_state_up), |
| 55 | + r.distributed, |
| 56 | + r.ha, |
| 57 | + r.tenant_id, |
| 58 | + )) |
| 59 | + data_long = [] |
| 60 | + for i in range(0, len(routers)): |
| 61 | + r = routers[i] |
| 62 | + data_long.append( |
| 63 | + data[i] + ( |
| 64 | + r.routes, |
| 65 | + router._format_external_gateway_info(r.external_gateway_info), |
| 66 | + ) |
| 67 | + ) |
| 68 | + |
| 69 | + def setUp(self): |
| 70 | + super(TestListRouter, self).setUp() |
| 71 | + |
| 72 | + # Get the command object to test |
| 73 | + self.cmd = router.ListRouter(self.app, self.namespace) |
| 74 | + |
| 75 | + self.network.routers = mock.Mock(return_value=self.routers) |
| 76 | + |
| 77 | + def test_router_list_no_options(self): |
| 78 | + arglist = [] |
| 79 | + verifylist = [ |
| 80 | + ('long', False), |
| 81 | + ] |
| 82 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 83 | + |
| 84 | + # DisplayCommandBase.take_action() returns two tuples |
| 85 | + columns, data = self.cmd.take_action(parsed_args) |
| 86 | + |
| 87 | + self.network.routers.assert_called_with() |
| 88 | + self.assertEqual(self.columns, columns) |
| 89 | + self.assertEqual(self.data, list(data)) |
| 90 | + |
| 91 | + def test_router_list_long(self): |
| 92 | + arglist = [ |
| 93 | + '--long', |
| 94 | + ] |
| 95 | + verifylist = [ |
| 96 | + ('long', True), |
| 97 | + ] |
| 98 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 99 | + |
| 100 | + # DisplayCommandBase.take_action() returns two tuples |
| 101 | + columns, data = self.cmd.take_action(parsed_args) |
| 102 | + |
| 103 | + self.network.routers.assert_called_with() |
| 104 | + self.assertEqual(self.columns_long, columns) |
| 105 | + self.assertEqual(self.data_long, list(data)) |
0 commit comments