|
| 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 six |
| 15 | + |
| 16 | +from openstackclient.common import availability_zone |
| 17 | +from openstackclient.tests.compute.v2 import fakes as compute_fakes |
| 18 | +from openstackclient.tests import fakes |
| 19 | +from openstackclient.tests import utils |
| 20 | + |
| 21 | + |
| 22 | +def _build_compute_az_datalist(compute_az, long_datalist=False): |
| 23 | + datalist = () |
| 24 | + if not long_datalist: |
| 25 | + datalist = ( |
| 26 | + compute_az.zoneName, |
| 27 | + 'available', |
| 28 | + ) |
| 29 | + else: |
| 30 | + for host, services in six.iteritems(compute_az.hosts): |
| 31 | + for service, state in six.iteritems(services): |
| 32 | + datalist += ( |
| 33 | + compute_az.zoneName, |
| 34 | + 'available', |
| 35 | + host, |
| 36 | + service, |
| 37 | + 'enabled :-) ' + state['updated_at'], |
| 38 | + ) |
| 39 | + return (datalist,) |
| 40 | + |
| 41 | + |
| 42 | +class TestAvailabilityZone(utils.TestCommand): |
| 43 | + |
| 44 | + def setUp(self): |
| 45 | + super(TestAvailabilityZone, self).setUp() |
| 46 | + |
| 47 | + compute_client = compute_fakes.FakeComputev2Client( |
| 48 | + endpoint=fakes.AUTH_URL, |
| 49 | + token=fakes.AUTH_TOKEN, |
| 50 | + ) |
| 51 | + self.app.client_manager.compute = compute_client |
| 52 | + |
| 53 | + self.compute_azs_mock = compute_client.availability_zones |
| 54 | + self.compute_azs_mock.reset_mock() |
| 55 | + |
| 56 | + |
| 57 | +class TestAvailabilityZoneList(TestAvailabilityZone): |
| 58 | + |
| 59 | + compute_azs = \ |
| 60 | + compute_fakes.FakeAvailabilityZone.create_availability_zones() |
| 61 | + |
| 62 | + def setUp(self): |
| 63 | + super(TestAvailabilityZoneList, self).setUp() |
| 64 | + |
| 65 | + self.compute_azs_mock.list.return_value = self.compute_azs |
| 66 | + |
| 67 | + # Get the command object to test |
| 68 | + self.cmd = availability_zone.ListAvailabilityZone(self.app, None) |
| 69 | + |
| 70 | + def test_availability_zone_list_no_options(self): |
| 71 | + arglist = [] |
| 72 | + verifylist = [] |
| 73 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 74 | + |
| 75 | + # DisplayCommandBase.take_action() returns two tuples |
| 76 | + columns, data = self.cmd.take_action(parsed_args) |
| 77 | + |
| 78 | + self.compute_azs_mock.list.assert_called_with() |
| 79 | + |
| 80 | + columnslist = ('Zone Name', 'Zone Status') |
| 81 | + self.assertEqual(columnslist, columns) |
| 82 | + datalist = () |
| 83 | + for compute_az in self.compute_azs: |
| 84 | + datalist += _build_compute_az_datalist(compute_az) |
| 85 | + self.assertEqual(datalist, tuple(data)) |
| 86 | + |
| 87 | + def test_availability_zone_list_long(self): |
| 88 | + arglist = [ |
| 89 | + '--long', |
| 90 | + ] |
| 91 | + verifylist = [ |
| 92 | + ('long', True), |
| 93 | + ] |
| 94 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 95 | + |
| 96 | + # DisplayCommandBase.take_action() returns two tuples |
| 97 | + columns, data = self.cmd.take_action(parsed_args) |
| 98 | + |
| 99 | + self.compute_azs_mock.list.assert_called_with() |
| 100 | + |
| 101 | + columnslist = ( |
| 102 | + 'Zone Name', |
| 103 | + 'Zone Status', |
| 104 | + 'Host Name', |
| 105 | + 'Service Name', |
| 106 | + 'Service Status', |
| 107 | + ) |
| 108 | + self.assertEqual(columnslist, columns) |
| 109 | + datalist = () |
| 110 | + for compute_az in self.compute_azs: |
| 111 | + datalist += _build_compute_az_datalist(compute_az, |
| 112 | + long_datalist=True) |
| 113 | + self.assertEqual(datalist, tuple(data)) |
0 commit comments