36
36
def network_api_get_fixed_ip(self, context, id):
39
return {'address': '10.0.0.1', 'id': id, 'instance_id': 1}
39
return {'address': '10.0.0.1', 'id': id, 'instance_uuid': 1}
42
42
def network_api_get_floating_ip(self, context, id):
99
99
return get_nw_info_for_instance
102
def get_instance_by_floating_ip_addr(self, context, address):
102
106
class FloatingIpTest(test.TestCase):
103
107
floating_ip = "10.10.10.10"
108
floating_ip_2 = "10.10.10.11"
105
def _create_floating_ip(self):
110
def _create_floating_ips(self, floating_ips=None):
106
111
"""Create a floating ip object."""
108
return db.floating_ip_create(self.context,
109
{'address': self.floating_ip,
112
if floating_ips is None:
113
floating_ips = [self.floating_ip]
114
elif not isinstance(floating_ips, (list, tuple)):
115
floating_ips = [floating_ips]
117
def make_ip_dict(ip):
118
"""Shortcut for creating floating ip dict."""
121
dict_ = {'pool': 'nova', 'host': 'fake_host'}
122
return db.floating_ip_bulk_create(
123
self.context, [dict(address=ip, **dict_) for ip in floating_ips],
113
126
def _delete_floating_ip(self):
114
127
db.floating_ip_destroy(self.context, self.floating_ip)
129
142
network_api_release)
130
143
self.stubs.Set(network.api.API, "disassociate_floating_ip",
131
144
network_api_disassociate)
145
self.stubs.Set(network.api.API, "get_instance_id_by_floating_address",
146
get_instance_by_floating_ip_addr)
132
147
self.stubs.Set(compute_utils, "get_nw_info_for_instance",
133
148
stub_nw_info(self.stubs))
138
153
fake_instance_get)
140
155
self.context = context.get_admin_context()
141
self._create_floating_ip()
156
self._create_floating_ips()
143
158
self.controller = floating_ips.FloatingIPController()
144
159
self.manager = floating_ips.FloatingIPActionController()
148
163
super(FloatingIpTest, self).tearDown()
150
165
def test_translate_floating_ip_view(self):
151
floating_ip_address = self._create_floating_ip()
166
floating_ip_address = self.floating_ip
152
167
floating_ip = db.floating_ip_get_by_address(self.context,
153
168
floating_ip_address)
154
169
floating_ip['fixed_ip'] = None
208
223
'fixed_ip_id': 11}
210
225
def get_fixed_ip(self, context, id):
211
return {'address': '10.0.0.1', 'instance_id': 1}
226
return {'address': '10.0.0.1', 'instance_uuid': 1}
213
228
self.stubs.Set(network.api.API, "get_floating_ip", get_floating_ip)
214
229
self.stubs.Set(network.api.API, "get_fixed_ip", get_fixed_ip)
220
235
self.assertEqual(res_dict['floating_ip']['ip'], '10.10.10.10')
221
236
self.assertEqual(res_dict['floating_ip']['instance_id'], FAKE_UUID)
238
def test_recreation_of_floating_ip(self):
239
self._delete_floating_ip()
240
self._create_floating_ips()
242
def test_floating_ip_in_bulk_creation(self):
243
self._delete_floating_ip()
245
self._create_floating_ips([self.floating_ip, self.floating_ip_2])
246
all_ips = db.floating_ip_get_all(self.context)
247
ip_list = [ip['address'] for ip in all_ips]
248
self.assertIn(self.floating_ip, ip_list)
249
self.assertIn(self.floating_ip_2, ip_list)
251
def test_fail_floating_ip_in_bulk_creation(self):
252
self.assertRaises(exception.FloatingIpExists,
253
self._create_floating_ips,
254
[self.floating_ip, self.floating_ip_2])
255
all_ips = db.floating_ip_get_all(self.context)
256
ip_list = [ip['address'] for ip in all_ips]
257
self.assertIn(self.floating_ip, ip_list)
258
self.assertNotIn(self.floating_ip_2, ip_list)
223
260
# test floating ip allocate/release(deallocate)
224
261
def test_floating_ip_allocate_no_free_ips(self):
225
262
def fake_call(*args, **kwargs):
228
265
self.stubs.Set(rpc, "call", fake_call)
230
267
req = fakes.HTTPRequest.blank('/v2/fake/os-floating-ips')
231
self.assertRaises(webob.exc.HTTPBadRequest, self.controller.create,
268
self.assertRaises(exception.NoMoreFloatingIps,
269
self.controller.create,
234
272
def test_floating_ip_allocate(self):
274
312
req = fakes.HTTPRequest.blank('/v2/fake/servers/test_inst/action')
275
313
self.manager._remove_floating_ip(req, 'test_inst', body)
315
def test_floating_ip_disassociate_missing(self):
316
body = dict(removeFloatingIp=dict(address='10.10.10.10'))
318
req = fakes.HTTPRequest.blank('/v2/fake/servers/test_inst/action')
319
rsp = self.manager._remove_floating_ip(req, 'test_inst', body)
320
self.assertTrue(rsp.status_int == 404)
277
322
# these are a few bad param tests
279
324
def test_bad_address_param_in_remove_floating_ip(self):