|
| 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 | +from mock import call |
| 16 | + |
| 17 | +from osc_lib import exceptions |
| 18 | + |
| 19 | +from openstackclient.network.v2 import floating_ip as fip |
| 20 | +from openstackclient.tests.unit.compute.v2 import fakes as compute_fakes |
| 21 | +from openstackclient.tests.unit import utils as tests_utils |
| 22 | + |
| 23 | + |
| 24 | +# Tests for Nova network |
| 25 | + |
| 26 | +class TestFloatingIPCompute(compute_fakes.TestComputev2): |
| 27 | + |
| 28 | + def setUp(self): |
| 29 | + super(TestFloatingIPCompute, self).setUp() |
| 30 | + |
| 31 | + # Get a shortcut to the compute client |
| 32 | + self.compute = self.app.client_manager.compute |
| 33 | + |
| 34 | + |
| 35 | +class TestCreateFloatingIPCompute(TestFloatingIPCompute): |
| 36 | + |
| 37 | + # The floating ip to be deleted. |
| 38 | + floating_ip = compute_fakes.FakeFloatingIP.create_one_floating_ip() |
| 39 | + |
| 40 | + columns = ( |
| 41 | + 'fixed_ip', |
| 42 | + 'id', |
| 43 | + 'instance_id', |
| 44 | + 'ip', |
| 45 | + 'pool', |
| 46 | + ) |
| 47 | + |
| 48 | + data = ( |
| 49 | + floating_ip.fixed_ip, |
| 50 | + floating_ip.id, |
| 51 | + floating_ip.instance_id, |
| 52 | + floating_ip.ip, |
| 53 | + floating_ip.pool, |
| 54 | + ) |
| 55 | + |
| 56 | + def setUp(self): |
| 57 | + super(TestCreateFloatingIPCompute, self).setUp() |
| 58 | + |
| 59 | + self.app.client_manager.network_endpoint_enabled = False |
| 60 | + |
| 61 | + self.compute.floating_ips.create.return_value = self.floating_ip |
| 62 | + |
| 63 | + # Get the command object to test |
| 64 | + self.cmd = fip.CreateFloatingIP(self.app, None) |
| 65 | + |
| 66 | + def test_create_no_options(self): |
| 67 | + arglist = [] |
| 68 | + verifylist = [] |
| 69 | + |
| 70 | + self.assertRaises(tests_utils.ParserException, self.check_parser, |
| 71 | + self.cmd, arglist, verifylist) |
| 72 | + |
| 73 | + def test_create_default_options(self): |
| 74 | + arglist = [ |
| 75 | + self.floating_ip.pool, |
| 76 | + ] |
| 77 | + verifylist = [ |
| 78 | + ('network', self.floating_ip.pool), |
| 79 | + ] |
| 80 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 81 | + |
| 82 | + columns, data = self.cmd.take_action(parsed_args) |
| 83 | + |
| 84 | + self.compute.floating_ips.create.assert_called_once_with( |
| 85 | + self.floating_ip.pool) |
| 86 | + self.assertEqual(self.columns, columns) |
| 87 | + self.assertEqual(self.data, data) |
| 88 | + |
| 89 | + |
| 90 | +class TestDeleteFloatingIPCompute(TestFloatingIPCompute): |
| 91 | + |
| 92 | + # The floating ips to be deleted. |
| 93 | + floating_ips = compute_fakes.FakeFloatingIP.create_floating_ips(count=2) |
| 94 | + |
| 95 | + def setUp(self): |
| 96 | + super(TestDeleteFloatingIPCompute, self).setUp() |
| 97 | + |
| 98 | + self.app.client_manager.network_endpoint_enabled = False |
| 99 | + |
| 100 | + self.compute.floating_ips.delete.return_value = None |
| 101 | + |
| 102 | + # Return value of utils.find_resource() |
| 103 | + self.compute.floating_ips.get = ( |
| 104 | + compute_fakes.FakeFloatingIP.get_floating_ips(self.floating_ips)) |
| 105 | + |
| 106 | + # Get the command object to test |
| 107 | + self.cmd = fip.DeleteFloatingIP(self.app, None) |
| 108 | + |
| 109 | + def test_floating_ip_delete(self): |
| 110 | + arglist = [ |
| 111 | + self.floating_ips[0].id, |
| 112 | + ] |
| 113 | + verifylist = [ |
| 114 | + ('floating_ip', [self.floating_ips[0].id]), |
| 115 | + ] |
| 116 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 117 | + |
| 118 | + result = self.cmd.take_action(parsed_args) |
| 119 | + |
| 120 | + self.compute.floating_ips.delete.assert_called_once_with( |
| 121 | + self.floating_ips[0].id |
| 122 | + ) |
| 123 | + self.assertIsNone(result) |
| 124 | + |
| 125 | + def test_multi_floating_ips_delete(self): |
| 126 | + arglist = [] |
| 127 | + verifylist = [] |
| 128 | + |
| 129 | + for f in self.floating_ips: |
| 130 | + arglist.append(f.id) |
| 131 | + verifylist = [ |
| 132 | + ('floating_ip', arglist), |
| 133 | + ] |
| 134 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 135 | + |
| 136 | + result = self.cmd.take_action(parsed_args) |
| 137 | + |
| 138 | + calls = [] |
| 139 | + for f in self.floating_ips: |
| 140 | + calls.append(call(f.id)) |
| 141 | + self.compute.floating_ips.delete.assert_has_calls(calls) |
| 142 | + self.assertIsNone(result) |
| 143 | + |
| 144 | + def test_multi_floating_ips_delete_with_exception(self): |
| 145 | + arglist = [ |
| 146 | + self.floating_ips[0].id, |
| 147 | + 'unexist_floating_ip', |
| 148 | + ] |
| 149 | + verifylist = [ |
| 150 | + ('floating_ip', |
| 151 | + [self.floating_ips[0].id, 'unexist_floating_ip']), |
| 152 | + ] |
| 153 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 154 | + |
| 155 | + find_mock_result = [self.floating_ips[0], exceptions.CommandError] |
| 156 | + self.compute.floating_ips.get = ( |
| 157 | + mock.Mock(side_effect=find_mock_result) |
| 158 | + ) |
| 159 | + self.compute.floating_ips.find.side_effect = exceptions.NotFound(None) |
| 160 | + |
| 161 | + try: |
| 162 | + self.cmd.take_action(parsed_args) |
| 163 | + self.fail('CommandError should be raised.') |
| 164 | + except exceptions.CommandError as e: |
| 165 | + self.assertEqual('1 of 2 floating_ips failed to delete.', str(e)) |
| 166 | + |
| 167 | + self.compute.floating_ips.get.assert_any_call( |
| 168 | + self.floating_ips[0].id) |
| 169 | + self.compute.floating_ips.get.assert_any_call( |
| 170 | + 'unexist_floating_ip') |
| 171 | + self.compute.floating_ips.delete.assert_called_once_with( |
| 172 | + self.floating_ips[0].id |
| 173 | + ) |
| 174 | + |
| 175 | + |
| 176 | +class TestListFloatingIPCompute(TestFloatingIPCompute): |
| 177 | + |
| 178 | + # The floating ips to be list up |
| 179 | + floating_ips = compute_fakes.FakeFloatingIP.create_floating_ips(count=3) |
| 180 | + |
| 181 | + columns = ( |
| 182 | + 'ID', |
| 183 | + 'Floating IP Address', |
| 184 | + 'Fixed IP Address', |
| 185 | + 'Server', |
| 186 | + 'Pool', |
| 187 | + ) |
| 188 | + |
| 189 | + data = [] |
| 190 | + for ip in floating_ips: |
| 191 | + data.append(( |
| 192 | + ip.id, |
| 193 | + ip.ip, |
| 194 | + ip.fixed_ip, |
| 195 | + ip.instance_id, |
| 196 | + ip.pool, |
| 197 | + )) |
| 198 | + |
| 199 | + def setUp(self): |
| 200 | + super(TestListFloatingIPCompute, self).setUp() |
| 201 | + |
| 202 | + self.app.client_manager.network_endpoint_enabled = False |
| 203 | + |
| 204 | + self.compute.floating_ips.list.return_value = self.floating_ips |
| 205 | + |
| 206 | + # Get the command object to test |
| 207 | + self.cmd = fip.ListFloatingIP(self.app, None) |
| 208 | + |
| 209 | + def test_floating_ip_list(self): |
| 210 | + arglist = [] |
| 211 | + verifylist = [] |
| 212 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 213 | + |
| 214 | + columns, data = self.cmd.take_action(parsed_args) |
| 215 | + |
| 216 | + self.compute.floating_ips.list.assert_called_once_with() |
| 217 | + self.assertEqual(self.columns, columns) |
| 218 | + self.assertEqual(self.data, list(data)) |
| 219 | + |
| 220 | + |
| 221 | +class TestShowFloatingIPCompute(TestFloatingIPCompute): |
| 222 | + |
| 223 | + # The floating ip to display. |
| 224 | + floating_ip = compute_fakes.FakeFloatingIP.create_one_floating_ip() |
| 225 | + |
| 226 | + columns = ( |
| 227 | + 'fixed_ip', |
| 228 | + 'id', |
| 229 | + 'instance_id', |
| 230 | + 'ip', |
| 231 | + 'pool', |
| 232 | + ) |
| 233 | + |
| 234 | + data = ( |
| 235 | + floating_ip.fixed_ip, |
| 236 | + floating_ip.id, |
| 237 | + floating_ip.instance_id, |
| 238 | + floating_ip.ip, |
| 239 | + floating_ip.pool, |
| 240 | + ) |
| 241 | + |
| 242 | + def setUp(self): |
| 243 | + super(TestShowFloatingIPCompute, self).setUp() |
| 244 | + |
| 245 | + self.app.client_manager.network_endpoint_enabled = False |
| 246 | + |
| 247 | + # Return value of utils.find_resource() |
| 248 | + self.compute.floating_ips.get.return_value = self.floating_ip |
| 249 | + |
| 250 | + # Get the command object to test |
| 251 | + self.cmd = fip.ShowFloatingIP(self.app, None) |
| 252 | + |
| 253 | + def test_floating_ip_show(self): |
| 254 | + arglist = [ |
| 255 | + self.floating_ip.id, |
| 256 | + ] |
| 257 | + verifylist = [ |
| 258 | + ('floating_ip', self.floating_ip.id), |
| 259 | + ] |
| 260 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 261 | + |
| 262 | + columns, data = self.cmd.take_action(parsed_args) |
| 263 | + |
| 264 | + self.assertEqual(self.columns, columns) |
| 265 | + self.assertEqual(self.data, data) |
0 commit comments