1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2011 Rackspace
4
# Copyright (c) 2011 X.commerce, a business unit of eBay Inc.
7
# Licensed under the Apache License, Version 2.0 (the "License"); you may
8
# not use this file except in compliance with the License. You may obtain
9
# a copy of the License at
11
# http://www.apache.org/licenses/LICENSE-2.0
13
# Unless required by applicable law or agreed to in writing, software
14
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16
# License for the specific language governing permissions and limitations
23
from nova import context
25
from nova.db.sqlalchemy import models
26
from nova import exception
27
from nova.network import linux_net
28
from nova.network import manager as network_manager
29
from nova.openstack.common import importutils
30
from nova.openstack.common import log as logging
31
from nova.openstack.common import rpc
34
from nova.tests import fake_network
35
from nova import utils
38
LOG = logging.getLogger(__name__)
42
FAKEUUID = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
50
'cidr': '192.168.0.0/24',
51
'cidr_v6': '2001:db8::/64',
52
'gateway_v6': '2001:db8::1',
54
'netmask': '255.255.255.0',
56
'bridge_interface': 'fake_fa0',
57
'gateway': '192.168.0.1',
58
'broadcast': '192.168.0.255',
59
'dns1': '192.168.0.1',
60
'dns2': '192.168.0.2',
63
'project_id': 'fake_project',
64
'vpn_public_address': '192.168.0.2'},
66
'uuid': 'bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb',
70
'cidr': '192.168.1.0/24',
71
'cidr_v6': '2001:db9::/64',
72
'gateway_v6': '2001:db9::1',
74
'netmask': '255.255.255.0',
76
'bridge_interface': 'fake_fa1',
77
'gateway': '192.168.1.1',
78
'broadcast': '192.168.1.255',
79
'dns1': '192.168.0.1',
80
'dns2': '192.168.0.2',
83
'project_id': 'fake_project',
84
'vpn_public_address': '192.168.1.2'}]
86
fixed_ips = [{'id': 0,
87
'network_id': FAKEUUID,
88
'address': '192.168.0.100',
91
'virtual_interface_id': 0,
94
'network_id': 'bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb',
95
'address': '192.168.1.100',
98
'virtual_interface_id': 0,
106
floating_ip_fields = {'id': 0,
107
'address': '192.168.10.100',
112
'auto_assigned': False}
115
'address': 'DE:AD:BE:EF:00:00',
116
'uuid': '00000000-0000-0000-0000-0000000000000000',
120
'address': 'DE:AD:BE:EF:00:01',
121
'uuid': '00000000-0000-0000-0000-0000000000000001',
125
'address': 'DE:AD:BE:EF:00:02',
126
'uuid': '00000000-0000-0000-0000-0000000000000002',
131
class FlatNetworkTestCase(test.TestCase):
133
super(FlatNetworkTestCase, self).setUp()
134
self.tempdir = tempfile.mkdtemp()
135
self.flags(logdir=self.tempdir)
136
self.network = network_manager.FlatManager(host=HOST)
137
temp = importutils.import_object('nova.network.minidns.MiniDNS')
138
self.network.instance_dns_manager = temp
139
self.network.instance_dns_domain = ''
141
self.context = context.RequestContext('testuser', 'testproject',
145
shutil.rmtree(self.tempdir)
146
super(FlatNetworkTestCase, self).tearDown()
148
def test_get_instance_nw_info(self):
149
fake_get_instance_nw_info = fake_network.fake_get_instance_nw_info
151
nw_info = fake_get_instance_nw_info(self.stubs, 0, 2)
152
self.assertFalse(nw_info)
154
nw_info = fake_get_instance_nw_info(self.stubs, 1, 2)
156
for i, (nw, info) in enumerate(nw_info):
158
check = {'bridge': 'fake_br%d' % nid,
159
'cidr': '192.168.%s.0/24' % nid,
160
'cidr_v6': '2001:db8:0:%x::/64' % nid,
161
'id': '00000000-0000-0000-0000-00000000000000%02d' % nid,
164
'bridge_interface': None,
167
self.assertDictMatch(nw, check)
169
check = {'broadcast': '192.168.%d.255' % nid,
170
'dhcp_server': '192.168.1.1',
171
'dns': ['192.168.%d.3' % nid, '192.168.%d.4' % nid],
172
'gateway': '192.168.%d.1' % nid,
173
'gateway_v6': 'fe80::def',
176
'label': 'test%d' % nid,
177
'mac': 'DE:AD:BE:EF:00:%02x' % nid,
180
'00000000-0000-0000-0000-00000000000000%02d' % nid,
181
'should_create_vlan': False,
182
'should_create_bridge': False}
183
self.assertDictMatch(info, check)
185
check = [{'enabled': 'DONTCARE',
186
'ip': '2001:db8:0:1::%x' % nid,
188
'gateway': 'fe80::def'}]
189
self.assertDictListMatch(info['ip6s'], check)
191
num_fixed_ips = len(info['ips'])
192
check = [{'enabled': 'DONTCARE',
193
'ip': '192.168.%d.%03d' % (nid, ip_num + 99),
194
'netmask': '255.255.255.0',
195
'gateway': '192.168.%d.1' % nid}
196
for ip_num in xrange(1, num_fixed_ips + 1)]
197
self.assertDictListMatch(info['ips'], check)
199
def test_validate_networks(self):
200
self.mox.StubOutWithMock(db, 'network_get')
201
self.mox.StubOutWithMock(db, 'network_get_all_by_uuids')
202
self.mox.StubOutWithMock(db, 'fixed_ip_get_by_address')
204
requested_networks = [('bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb',
206
db.network_get_all_by_uuids(mox.IgnoreArg(), mox.IgnoreArg(),
207
project_only=mox.IgnoreArg()).AndReturn(networks)
208
db.network_get(mox.IgnoreArg(),
210
project_only=mox.IgnoreArg()).AndReturn(networks[1])
212
ip = fixed_ips[1].copy()
213
ip['instance_uuid'] = None
214
db.fixed_ip_get_by_address(mox.IgnoreArg(),
215
mox.IgnoreArg()).AndReturn(ip)
218
self.network.validate_networks(self.context, requested_networks)
220
def test_validate_reserved(self):
221
context_admin = context.RequestContext('testuser', 'testproject',
223
nets = self.network.create_networks(context_admin, 'fake',
224
'192.168.0.0/24', False, 1,
225
256, None, None, None, None, None)
226
self.assertEqual(1, len(nets))
228
self.assertEqual(3, db.network_count_reserved_ips(context_admin,
231
def test_validate_networks_none_requested_networks(self):
232
self.network.validate_networks(self.context, None)
234
def test_validate_networks_empty_requested_networks(self):
235
requested_networks = []
238
self.network.validate_networks(self.context, requested_networks)
240
def test_validate_networks_invalid_fixed_ip(self):
241
self.mox.StubOutWithMock(db, 'network_get_all_by_uuids')
242
requested_networks = [(1, "192.168.0.100.1")]
243
db.network_get_all_by_uuids(mox.IgnoreArg(), mox.IgnoreArg(),
244
project_only=mox.IgnoreArg()).AndReturn(networks)
247
self.assertRaises(exception.FixedIpInvalid,
248
self.network.validate_networks, self.context,
251
def test_validate_networks_empty_fixed_ip(self):
252
self.mox.StubOutWithMock(db, 'network_get_all_by_uuids')
254
requested_networks = [(1, "")]
255
db.network_get_all_by_uuids(mox.IgnoreArg(), mox.IgnoreArg(),
256
project_only=mox.IgnoreArg()).AndReturn(networks)
259
self.assertRaises(exception.FixedIpInvalid,
260
self.network.validate_networks,
261
self.context, requested_networks)
263
def test_validate_networks_none_fixed_ip(self):
264
self.mox.StubOutWithMock(db, 'network_get_all_by_uuids')
266
requested_networks = [(1, None)]
267
db.network_get_all_by_uuids(mox.IgnoreArg(), mox.IgnoreArg(),
268
project_only=mox.IgnoreArg()).AndReturn(networks)
271
self.network.validate_networks(self.context, requested_networks)
273
def test_add_fixed_ip_instance_using_id_without_vpn(self):
274
self.mox.StubOutWithMock(db, 'network_get')
275
self.mox.StubOutWithMock(db, 'network_update')
276
self.mox.StubOutWithMock(db, 'fixed_ip_associate_pool')
277
self.mox.StubOutWithMock(db, 'instance_get')
278
self.mox.StubOutWithMock(db,
279
'virtual_interface_get_by_instance_and_network')
280
self.mox.StubOutWithMock(db, 'fixed_ip_update')
282
db.fixed_ip_update(mox.IgnoreArg(),
285
db.virtual_interface_get_by_instance_and_network(mox.IgnoreArg(),
286
mox.IgnoreArg(), mox.IgnoreArg()).AndReturn({'id': 0})
288
db.instance_get(self.context,
289
1).AndReturn({'display_name': HOST,
290
'uuid': 'test-00001'})
291
db.instance_get(mox.IgnoreArg(),
292
mox.IgnoreArg()).AndReturn({'security_groups':
294
db.fixed_ip_associate_pool(mox.IgnoreArg(),
296
mox.IgnoreArg()).AndReturn('192.168.0.101')
297
db.network_get(mox.IgnoreArg(),
299
project_only=mox.IgnoreArg()).AndReturn(networks[0])
300
db.network_update(mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg())
302
self.network.add_fixed_ip_to_instance(self.context, 1, HOST,
305
def test_add_fixed_ip_instance_using_uuid_without_vpn(self):
306
self.mox.StubOutWithMock(db, 'network_get_by_uuid')
307
self.mox.StubOutWithMock(db, 'network_update')
308
self.mox.StubOutWithMock(db, 'fixed_ip_associate_pool')
309
self.mox.StubOutWithMock(db, 'instance_get')
310
self.mox.StubOutWithMock(db,
311
'virtual_interface_get_by_instance_and_network')
312
self.mox.StubOutWithMock(db, 'fixed_ip_update')
314
db.fixed_ip_update(mox.IgnoreArg(),
317
db.virtual_interface_get_by_instance_and_network(mox.IgnoreArg(),
318
mox.IgnoreArg(), mox.IgnoreArg()).AndReturn({'id': 0})
320
db.instance_get(self.context,
321
1).AndReturn({'display_name': HOST,
322
'uuid': 'test-00001'})
323
db.instance_get(mox.IgnoreArg(),
324
mox.IgnoreArg()).AndReturn({'security_groups':
326
db.fixed_ip_associate_pool(mox.IgnoreArg(),
328
mox.IgnoreArg()).AndReturn('192.168.0.101')
329
db.network_get_by_uuid(mox.IgnoreArg(),
330
mox.IgnoreArg()).AndReturn(networks[0])
331
db.network_update(mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg())
333
self.network.add_fixed_ip_to_instance(self.context, 1, HOST,
336
def test_mini_dns_driver(self):
337
zone1 = "example.org"
338
zone2 = "example.com"
339
driver = self.network.instance_dns_manager
340
driver.create_entry("hostone", "10.0.0.1", "A", zone1)
341
driver.create_entry("hosttwo", "10.0.0.2", "A", zone1)
342
driver.create_entry("hostthree", "10.0.0.3", "A", zone1)
343
driver.create_entry("hostfour", "10.0.0.4", "A", zone1)
344
driver.create_entry("hostfive", "10.0.0.5", "A", zone2)
346
driver.delete_entry("hostone", zone1)
347
driver.modify_address("hostfour", "10.0.0.1", zone1)
348
driver.modify_address("hostthree", "10.0.0.1", zone1)
349
names = driver.get_entries_by_address("10.0.0.1", zone1)
350
self.assertEqual(len(names), 2)
351
self.assertIn('hostthree', names)
352
self.assertIn('hostfour', names)
354
names = driver.get_entries_by_address("10.0.0.5", zone2)
355
self.assertEqual(len(names), 1)
356
self.assertIn('hostfive', names)
358
addresses = driver.get_entries_by_name("hosttwo", zone1)
359
self.assertEqual(len(addresses), 1)
360
self.assertIn('10.0.0.2', addresses)
362
self.assertRaises(exception.InvalidInput,
369
def test_instance_dns(self):
370
fixedip = '192.168.0.101'
371
self.mox.StubOutWithMock(db, 'network_get')
372
self.mox.StubOutWithMock(db, 'network_update')
373
self.mox.StubOutWithMock(db, 'fixed_ip_associate_pool')
374
self.mox.StubOutWithMock(db, 'instance_get')
375
self.mox.StubOutWithMock(db, 'instance_get_by_uuid')
376
self.mox.StubOutWithMock(db,
377
'virtual_interface_get_by_instance_and_network')
378
self.mox.StubOutWithMock(db, 'fixed_ip_update')
380
db.fixed_ip_update(mox.IgnoreArg(),
383
db.virtual_interface_get_by_instance_and_network(mox.IgnoreArg(),
384
mox.IgnoreArg(), mox.IgnoreArg()).AndReturn({'id': 0})
386
db.instance_get(self.context,
387
1).AndReturn({'display_name': HOST,
388
'uuid': 'test-00001'})
389
db.instance_get(mox.IgnoreArg(),
390
mox.IgnoreArg()).AndReturn({'security_groups':
393
db.fixed_ip_associate_pool(mox.IgnoreArg(),
395
mox.IgnoreArg()).AndReturn(fixedip)
396
db.network_get(mox.IgnoreArg(),
398
project_only=mox.IgnoreArg()).AndReturn(networks[0])
399
db.network_update(mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg())
402
self.network.add_fixed_ip_to_instance(self.context, 1, HOST,
404
instance_manager = self.network.instance_dns_manager
405
addresses = instance_manager.get_entries_by_name(HOST,
406
self.network.instance_dns_domain)
407
self.assertEqual(len(addresses), 1)
408
self.assertEqual(addresses[0], fixedip)
409
addresses = instance_manager.get_entries_by_name('test-00001',
410
self.network.instance_dns_domain)
411
self.assertEqual(len(addresses), 1)
412
self.assertEqual(addresses[0], fixedip)
415
class VlanNetworkTestCase(test.TestCase):
417
super(VlanNetworkTestCase, self).setUp()
418
self.network = network_manager.VlanManager(host=HOST)
420
self.context = context.RequestContext('testuser', 'testproject',
423
def test_vpn_allocate_fixed_ip(self):
424
self.mox.StubOutWithMock(db, 'instance_get')
425
self.mox.StubOutWithMock(db, 'fixed_ip_associate')
426
self.mox.StubOutWithMock(db, 'fixed_ip_update')
427
self.mox.StubOutWithMock(db,
428
'virtual_interface_get_by_instance_and_network')
430
db.instance_get(mox.IgnoreArg(),
431
mox.IgnoreArg()).AndReturn({'uuid': '42'})
432
db.fixed_ip_associate(mox.IgnoreArg(),
436
reserved=True).AndReturn('192.168.0.1')
437
db.fixed_ip_update(mox.IgnoreArg(),
440
db.virtual_interface_get_by_instance_and_network(mox.IgnoreArg(),
441
mox.IgnoreArg(), mox.IgnoreArg()).AndReturn({'id': 0})
444
network = dict(networks[0])
445
network['vpn_private_address'] = '192.168.0.2'
446
self.network.allocate_fixed_ip(None, 0, network, vpn=True)
448
def test_vpn_allocate_fixed_ip_no_network_id(self):
449
network = dict(networks[0])
450
network['vpn_private_address'] = '192.168.0.2'
452
instance = db.instance_create(self.context, {})
453
context_admin = context.RequestContext('testuser', 'testproject',
455
self.assertRaises(exception.FixedIpNotFoundForNetwork,
456
self.network.allocate_fixed_ip,
462
def test_allocate_fixed_ip(self):
463
self.mox.StubOutWithMock(db, 'fixed_ip_associate_pool')
464
self.mox.StubOutWithMock(db, 'fixed_ip_update')
465
self.mox.StubOutWithMock(db,
466
'virtual_interface_get_by_instance_and_network')
467
self.mox.StubOutWithMock(db, 'instance_get')
469
db.instance_get(mox.IgnoreArg(),
470
mox.IgnoreArg()).AndReturn({'uuid': FAKEUUID})
471
db.instance_get(mox.IgnoreArg(),
472
mox.IgnoreArg()).AndReturn({'security_groups':
474
db.fixed_ip_associate_pool(mox.IgnoreArg(),
476
mox.IgnoreArg()).AndReturn('192.168.0.1')
477
db.fixed_ip_update(mox.IgnoreArg(),
480
db.virtual_interface_get_by_instance_and_network(mox.IgnoreArg(),
481
mox.IgnoreArg(), mox.IgnoreArg()).AndReturn({'id': 0})
484
network = dict(networks[0])
485
network['vpn_private_address'] = '192.168.0.2'
486
self.network.allocate_fixed_ip(self.context, 0, network)
488
def test_create_networks_too_big(self):
489
self.assertRaises(ValueError, self.network.create_networks, None,
490
num_networks=4094, vlan_start=1)
492
def test_create_networks_too_many(self):
493
self.assertRaises(ValueError, self.network.create_networks, None,
494
num_networks=100, vlan_start=1,
495
cidr='192.168.0.1/24', network_size=100)
497
def test_validate_networks(self):
498
def network_get(_context, network_id, project_only='allow_none'):
499
return networks[network_id]
501
self.stubs.Set(db, 'network_get', network_get)
502
self.mox.StubOutWithMock(db, 'network_get_all_by_uuids')
503
self.mox.StubOutWithMock(db, "fixed_ip_get_by_address")
505
requested_networks = [("bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb",
507
db.network_get_all_by_uuids(mox.IgnoreArg(), mox.IgnoreArg(),
508
project_only=mox.IgnoreArg()).AndReturn(networks)
510
fixed_ips[1]['network_id'] = networks[1]['id']
511
fixed_ips[1]['instance_uuid'] = None
512
db.fixed_ip_get_by_address(mox.IgnoreArg(),
513
mox.IgnoreArg()).AndReturn(fixed_ips[1])
516
self.network.validate_networks(self.context, requested_networks)
518
def test_validate_networks_none_requested_networks(self):
519
self.network.validate_networks(self.context, None)
521
def test_validate_networks_empty_requested_networks(self):
522
requested_networks = []
525
self.network.validate_networks(self.context, requested_networks)
527
def test_validate_networks_invalid_fixed_ip(self):
528
self.mox.StubOutWithMock(db, 'network_get_all_by_uuids')
529
requested_networks = [(1, "192.168.0.100.1")]
530
db.network_get_all_by_uuids(mox.IgnoreArg(), mox.IgnoreArg(),
531
project_only=mox.IgnoreArg()).AndReturn(networks)
534
self.assertRaises(exception.FixedIpInvalid,
535
self.network.validate_networks, self.context,
538
def test_validate_networks_empty_fixed_ip(self):
539
self.mox.StubOutWithMock(db, 'network_get_all_by_uuids')
541
requested_networks = [(1, "")]
542
db.network_get_all_by_uuids(mox.IgnoreArg(), mox.IgnoreArg(),
543
project_only=mox.IgnoreArg()).AndReturn(networks)
546
self.assertRaises(exception.FixedIpInvalid,
547
self.network.validate_networks,
548
self.context, requested_networks)
550
def test_validate_networks_none_fixed_ip(self):
551
self.mox.StubOutWithMock(db, 'network_get_all_by_uuids')
553
requested_networks = [(1, None)]
554
db.network_get_all_by_uuids(mox.IgnoreArg(), mox.IgnoreArg(),
555
project_only=mox.IgnoreArg()).AndReturn(networks)
557
self.network.validate_networks(self.context, requested_networks)
559
def test_floating_ip_owned_by_project(self):
560
ctxt = context.RequestContext('testuser', 'testproject',
563
# raises because floating_ip project_id is None
564
floating_ip = {'address': '10.0.0.1',
566
self.assertRaises(exception.NotAuthorized,
567
self.network._floating_ip_owned_by_project,
571
# raises because floating_ip project_id is not equal to ctxt project_id
572
floating_ip = {'address': '10.0.0.1',
573
'project_id': ctxt.project_id + '1'}
574
self.assertRaises(exception.NotAuthorized,
575
self.network._floating_ip_owned_by_project,
579
# does not raise (floating ip is owned by ctxt project)
580
floating_ip = {'address': '10.0.0.1',
581
'project_id': ctxt.project_id}
582
self.network._floating_ip_owned_by_project(ctxt, floating_ip)
584
ctxt = context.RequestContext(None, None,
587
# does not raise (ctxt is admin)
588
floating_ip = {'address': '10.0.0.1',
590
self.network._floating_ip_owned_by_project(ctxt, floating_ip)
592
# does not raise (ctxt is admin)
593
floating_ip = {'address': '10.0.0.1',
594
'project_id': 'testproject'}
595
self.network._floating_ip_owned_by_project(ctxt, floating_ip)
597
def test_allocate_floating_ip(self):
598
ctxt = context.RequestContext('testuser', 'testproject',
601
def fake_allocate_address(*args, **kwargs):
602
return {'address': '10.0.0.1', 'project_id': ctxt.project_id}
604
self.stubs.Set(self.network.db, 'floating_ip_allocate_address',
605
fake_allocate_address)
607
self.network.allocate_floating_ip(ctxt, ctxt.project_id)
609
def test_deallocate_floating_ip(self):
610
ctxt = context.RequestContext('testuser', 'testproject',
613
def fake1(*args, **kwargs):
616
def fake2(*args, **kwargs):
617
return {'address': '10.0.0.1', 'fixed_ip_id': 1}
619
def fake3(*args, **kwargs):
620
return {'address': '10.0.0.1', 'fixed_ip_id': None,
621
'project_id': ctxt.project_id}
623
self.stubs.Set(self.network.db, 'floating_ip_deallocate', fake1)
624
self.stubs.Set(self.network, '_floating_ip_owned_by_project', fake1)
626
# this time should raise because floating ip is associated to fixed_ip
627
self.stubs.Set(self.network.db, 'floating_ip_get_by_address', fake2)
628
self.assertRaises(exception.FloatingIpAssociated,
629
self.network.deallocate_floating_ip,
633
# this time should not raise
634
self.stubs.Set(self.network.db, 'floating_ip_get_by_address', fake3)
635
self.network.deallocate_floating_ip(ctxt, ctxt.project_id)
637
def test_associate_floating_ip(self):
638
ctxt = context.RequestContext('testuser', 'testproject',
641
def fake1(*args, **kwargs):
644
# floating ip that's already associated
645
def fake2(*args, **kwargs):
646
return {'address': '10.0.0.1',
651
# floating ip that isn't associated
652
def fake3(*args, **kwargs):
653
return {'address': '10.0.0.1',
658
# fixed ip with remote host
659
def fake4(*args, **kwargs):
660
return {'address': '10.0.0.1',
663
'network_id': 'blah'}
665
def fake4_network(*args, **kwargs):
666
return {'multi_host': False, 'host': 'jibberjabber'}
668
# fixed ip with local host
669
def fake5(*args, **kwargs):
670
return {'address': '10.0.0.1',
673
'network_id': 'blahblah'}
675
def fake5_network(*args, **kwargs):
676
return {'multi_host': False, 'host': 'testhost'}
678
def fake6(*args, **kwargs):
681
def fake7(*args, **kwargs):
684
def fake8(*args, **kwargs):
685
raise exception.ProcessExecutionError('',
686
'Cannot find device "em0"\n')
688
def fake9(*args, **kwargs):
689
raise test.TestingException()
691
# raises because interface doesn't exist
692
self.stubs.Set(self.network.db,
693
'floating_ip_fixed_ip_associate',
695
self.stubs.Set(self.network.db, 'floating_ip_disassociate', fake1)
696
self.stubs.Set(self.network.driver, 'bind_floating_ip', fake8)
697
self.assertRaises(exception.NoFloatingIpInterface,
698
self.network._associate_floating_ip,
704
self.stubs.Set(self.network, '_floating_ip_owned_by_project', fake1)
706
# raises because floating_ip is already associated to a fixed_ip
707
self.stubs.Set(self.network.db, 'floating_ip_get_by_address', fake2)
708
self.stubs.Set(self.network, 'disassociate_floating_ip', fake9)
710
def fake_fixed_ip_get(context, fixed_ip_id):
711
return {'instance_uuid': 'fake_uuid'}
713
self.stubs.Set(self.network.db, 'fixed_ip_get', fake_fixed_ip_get)
715
self.assertRaises(test.TestingException,
716
self.network.associate_floating_ip,
721
self.stubs.Set(self.network.db, 'floating_ip_get_by_address', fake3)
723
# does not raise and makes call remotely
725
self.stubs.Set(self.network.db, 'fixed_ip_get_by_address', fake4)
726
self.stubs.Set(self.network.db, 'network_get', fake4_network)
727
self.stubs.Set(rpc, 'call', fake6)
728
self.network.associate_floating_ip(ctxt, mox.IgnoreArg(),
730
self.assertFalse(self.local)
732
# does not raise and makes call locally
734
self.stubs.Set(self.network.db, 'fixed_ip_get_by_address', fake5)
735
self.stubs.Set(self.network.db, 'network_get', fake5_network)
736
self.stubs.Set(self.network, '_associate_floating_ip', fake7)
737
self.network.associate_floating_ip(ctxt, mox.IgnoreArg(),
739
self.assertTrue(self.local)
741
def test_floating_ip_init_host(self):
743
def get_all_by_host(_context, _host):
744
return [{'interface': 'foo',
746
{'interface': 'fakeiface',
747
'address': 'fakefloat',
752
self.stubs.Set(self.network.db, 'floating_ip_get_all_by_host',
755
def fixed_ip_get(_context, fixed_ip_id):
757
return {'address': 'fakefixed'}
758
raise exception.FixedIpNotFound()
759
self.stubs.Set(self.network.db, 'fixed_ip_get', fixed_ip_get)
761
self.mox.StubOutWithMock(self.network.l3driver, 'add_floating_ip')
762
self.flags(public_interface=False)
763
self.network.l3driver.add_floating_ip('fakefloat',
767
self.network.init_host_floating_ips()
768
self.mox.UnsetStubs()
771
self.mox.StubOutWithMock(self.network.l3driver, 'add_floating_ip')
772
self.flags(public_interface='fooiface')
773
self.network.l3driver.add_floating_ip('fakefloat',
777
self.network.init_host_floating_ips()
778
self.mox.UnsetStubs()
781
def test_disassociate_floating_ip(self):
782
ctxt = context.RequestContext('testuser', 'testproject',
785
def fake1(*args, **kwargs):
788
# floating ip that isn't associated
789
def fake2(*args, **kwargs):
790
return {'address': '10.0.0.1',
795
# floating ip that is associated
796
def fake3(*args, **kwargs):
797
return {'address': '10.0.0.1',
801
'project_id': ctxt.project_id}
803
# fixed ip with remote host
804
def fake4(*args, **kwargs):
805
return {'address': '10.0.0.1',
808
'network_id': 'blah'}
810
def fake4_network(*args, **kwargs):
811
return {'multi_host': False,
812
'host': 'jibberjabber'}
814
# fixed ip with local host
815
def fake5(*args, **kwargs):
816
return {'address': '10.0.0.1',
819
'network_id': 'blahblah'}
821
def fake5_network(*args, **kwargs):
822
return {'multi_host': False, 'host': 'testhost'}
824
def fake6(*args, **kwargs):
827
def fake7(*args, **kwargs):
830
def fake8(*args, **kwargs):
831
return {'address': '10.0.0.1',
835
'auto_assigned': True,
836
'project_id': ctxt.project_id}
838
self.stubs.Set(self.network, '_floating_ip_owned_by_project', fake1)
840
# raises because floating_ip is not associated to a fixed_ip
841
self.stubs.Set(self.network.db, 'floating_ip_get_by_address', fake2)
842
self.assertRaises(exception.FloatingIpNotAssociated,
843
self.network.disassociate_floating_ip,
847
self.stubs.Set(self.network.db, 'floating_ip_get_by_address', fake3)
849
# does not raise and makes call remotely
851
self.stubs.Set(self.network.db, 'fixed_ip_get', fake4)
852
self.stubs.Set(self.network.db, 'network_get', fake4_network)
853
self.stubs.Set(rpc, 'call', fake6)
854
self.network.disassociate_floating_ip(ctxt, mox.IgnoreArg())
855
self.assertFalse(self.local)
857
# does not raise and makes call locally
859
self.stubs.Set(self.network.db, 'fixed_ip_get', fake5)
860
self.stubs.Set(self.network.db, 'network_get', fake5_network)
861
self.stubs.Set(self.network, '_disassociate_floating_ip', fake7)
862
self.network.disassociate_floating_ip(ctxt, mox.IgnoreArg())
863
self.assertTrue(self.local)
865
# raises because auto_assigned floating IP cannot be disassociated
866
self.stubs.Set(self.network.db, 'floating_ip_get_by_address', fake8)
867
self.assertRaises(exception.CannotDisassociateAutoAssignedFloatingIP,
868
self.network.disassociate_floating_ip,
872
def test_add_fixed_ip_instance_without_vpn_requested_networks(self):
873
self.mox.StubOutWithMock(db, 'network_get')
874
self.mox.StubOutWithMock(db, 'fixed_ip_associate_pool')
875
self.mox.StubOutWithMock(db, 'instance_get')
876
self.mox.StubOutWithMock(db,
877
'virtual_interface_get_by_instance_and_network')
878
self.mox.StubOutWithMock(db, 'fixed_ip_update')
880
db.instance_get(mox.IgnoreArg(),
881
mox.IgnoreArg()).AndReturn({'uuid': FAKEUUID})
882
db.fixed_ip_update(mox.IgnoreArg(),
885
db.virtual_interface_get_by_instance_and_network(mox.IgnoreArg(),
886
mox.IgnoreArg(), mox.IgnoreArg()).AndReturn({'id': 0})
888
db.instance_get(mox.IgnoreArg(),
889
mox.IgnoreArg()).AndReturn({'security_groups':
891
'availability_zone': '',
893
db.fixed_ip_associate_pool(mox.IgnoreArg(),
895
mox.IgnoreArg()).AndReturn('192.168.0.101')
896
db.network_get(mox.IgnoreArg(),
898
project_only=mox.IgnoreArg()).AndReturn(networks[0])
900
self.network.add_fixed_ip_to_instance(self.context, 1, HOST,
903
def test_ip_association_and_allocation_of_other_project(self):
904
"""Makes sure that we cannot deallocaate or disassociate
905
a public ip of other project"""
907
def network_get(_context, network_id, project_only="allow_none"):
908
return networks[network_id]
910
self.stubs.Set(db, 'network_get', network_get)
912
context1 = context.RequestContext('user', 'project1')
913
context2 = context.RequestContext('user', 'project2')
916
float_addr = db.floating_ip_create(context1.elevated(),
918
'project_id': context1.project_id})
920
instance = db.instance_create(context1,
921
{'project_id': 'project1'})
923
fix_addr = db.fixed_ip_associate_pool(context1.elevated(),
926
# Associate the IP with non-admin user context
927
self.assertRaises(exception.NotAuthorized,
928
self.network.associate_floating_ip,
933
# Deallocate address from other project
934
self.assertRaises(exception.NotAuthorized,
935
self.network.deallocate_floating_ip,
939
# Now Associates the address to the actual project
940
self.network.associate_floating_ip(context1, float_addr, fix_addr)
942
# Now try dis-associating from other project
943
self.assertRaises(exception.NotAuthorized,
944
self.network.disassociate_floating_ip,
948
# Clean up the ip addresses
949
self.network.disassociate_floating_ip(context1, float_addr)
950
self.network.deallocate_floating_ip(context1, float_addr)
951
self.network.deallocate_fixed_ip(context1, fix_addr, 'fake')
952
db.floating_ip_destroy(context1.elevated(), float_addr)
953
db.fixed_ip_disassociate(context1.elevated(), fix_addr)
955
def test_deallocate_fixed(self):
956
"""Verify that release is called properly.
958
Ensures https://bugs.launchpad.net/nova/+bug/973442 doesn't return"""
960
def network_get(_context, network_id, project_only="allow_none"):
961
return networks[network_id]
963
self.stubs.Set(db, 'network_get', network_get)
965
def vif_get(_context, _vif_id):
966
return {'address': 'fake_mac'}
968
self.stubs.Set(db, 'virtual_interface_get', vif_get)
969
context1 = context.RequestContext('user', 'project1')
971
instance = db.instance_create(context1,
972
{'project_id': 'project1'})
974
elevated = context1.elevated()
975
fix_addr = db.fixed_ip_associate_pool(elevated, 1, instance['uuid'])
976
values = {'allocated': True,
977
'virtual_interface_id': 3}
978
db.fixed_ip_update(elevated, fix_addr, values)
979
fixed = db.fixed_ip_get_by_address(elevated, fix_addr)
980
network = db.network_get(elevated, fixed['network_id'])
982
self.flags(force_dhcp_release=True)
983
self.mox.StubOutWithMock(linux_net, 'release_dhcp')
984
linux_net.release_dhcp(network['bridge'], fixed['address'], 'fake_mac')
986
self.network.deallocate_fixed_ip(context1, fix_addr, 'fake')
987
fixed = db.fixed_ip_get_by_address(elevated, fix_addr)
988
self.assertFalse(fixed['allocated'])
990
def test_deallocate_fixed_deleted(self):
991
"""Verify doesn't deallocate deleted fixed_ip from deleted network"""
993
def network_get(_context, network_id, project_only="allow_none"):
994
return networks[network_id]
996
def teardown_network_on_host(_context, network):
997
if network['id'] == 0:
998
raise test.TestingException()
1000
self.stubs.Set(db, 'network_get', network_get)
1001
self.stubs.Set(self.network, '_teardown_network_on_host',
1002
teardown_network_on_host)
1004
context1 = context.RequestContext('user', 'project1')
1006
instance = db.instance_create(context1,
1007
{'project_id': 'project1'})
1009
elevated = context1.elevated()
1010
fix_addr = db.fixed_ip_associate_pool(elevated, 1, instance['uuid'])
1011
db.fixed_ip_update(elevated, fix_addr, {'deleted': 1})
1012
elevated.read_deleted = 'yes'
1013
delfixed = db.fixed_ip_get_by_address(elevated, fix_addr)
1014
values = {'address': fix_addr,
1016
'instance_uuid': delfixed['instance_uuid']}
1017
db.fixed_ip_create(elevated, values)
1018
elevated.read_deleted = 'no'
1019
newfixed = db.fixed_ip_get_by_address(elevated, fix_addr)
1020
elevated.read_deleted = 'yes'
1022
deallocate = self.network.deallocate_fixed_ip
1023
self.assertRaises(test.TestingException, deallocate, context1,
1026
def test_deallocate_fixed_no_vif(self):
1027
"""Verify that deallocate doesn't raise when no vif is returned.
1029
Ensures https://bugs.launchpad.net/nova/+bug/968457 doesn't return"""
1031
def network_get(_context, network_id, project_only="allow_none"):
1032
return networks[network_id]
1034
self.stubs.Set(db, 'network_get', network_get)
1036
def vif_get(_context, _vif_id):
1039
self.stubs.Set(db, 'virtual_interface_get', vif_get)
1040
context1 = context.RequestContext('user', 'project1')
1042
instance = db.instance_create(context1,
1043
{'project_id': 'project1'})
1045
elevated = context1.elevated()
1046
fix_addr = db.fixed_ip_associate_pool(elevated, 1, instance['uuid'])
1047
values = {'allocated': True,
1048
'virtual_interface_id': 3}
1049
db.fixed_ip_update(elevated, fix_addr, values)
1051
self.flags(force_dhcp_release=True)
1052
self.network.deallocate_fixed_ip(context1, fix_addr, 'fake')
1054
def test_fixed_ip_cleanup_fail(self):
1055
"""Verify IP is not deallocated if the security group refresh fails."""
1056
def network_get(_context, network_id, project_only="allow_none"):
1057
return networks[network_id]
1059
self.stubs.Set(db, 'network_get', network_get)
1061
context1 = context.RequestContext('user', 'project1')
1063
instance = db.instance_create(context1,
1064
{'project_id': 'project1'})
1066
elevated = context1.elevated()
1067
fix_addr = db.fixed_ip_associate_pool(elevated, 1, instance['uuid'])
1068
values = {'allocated': True,
1069
'virtual_interface_id': 3}
1070
db.fixed_ip_update(elevated, fix_addr, values)
1071
fixed = db.fixed_ip_get_by_address(elevated, fix_addr)
1072
network = db.network_get(elevated, fixed['network_id'])
1074
def fake_refresh(instance_uuid):
1075
raise test.TestingException()
1076
self.stubs.Set(self.network,
1077
'_do_trigger_security_group_members_refresh_for_instance',
1079
self.assertRaises(test.TestingException,
1080
self.network.deallocate_fixed_ip,
1081
context1, fix_addr, 'fake')
1082
fixed = db.fixed_ip_get_by_address(elevated, fix_addr)
1083
self.assertTrue(fixed['allocated'])
1086
class CommonNetworkTestCase(test.TestCase):
1089
super(CommonNetworkTestCase, self).setUp()
1090
self.context = context.RequestContext('fake', 'fake')
1092
def fake_create_fixed_ips(self, context, network_id, fixed_cidr=None):
1095
def test_deallocate_for_instance_passes_host_info(self):
1096
manager = fake_network.FakeNetworkManager()
1098
db.instance_get = lambda _x, _y: dict(uuid='ignoreduuid')
1099
db.virtual_interface_delete_by_instance = lambda _x, _y: None
1100
ctx = context.RequestContext('igonre', 'igonre')
1102
db.fixed_ip_get_by_instance = lambda x, y: [dict(address='1.2.3.4')]
1104
manager.deallocate_for_instance(
1105
ctx, instance_id='ignore', host='somehost')
1108
(ctx, '1.2.3.4', 'somehost')
1109
], manager.deallocate_fixed_ip_calls)
1111
def test_remove_fixed_ip_from_instance(self):
1112
manager = fake_network.FakeNetworkManager()
1113
manager.remove_fixed_ip_from_instance(self.context, 99, HOST,
1116
self.assertEquals(manager.deallocate_called, '10.0.0.1')
1118
def test_remove_fixed_ip_from_instance_bad_input(self):
1119
manager = fake_network.FakeNetworkManager()
1120
self.assertRaises(exception.FixedIpNotFoundForSpecificInstance,
1121
manager.remove_fixed_ip_from_instance,
1122
self.context, 99, HOST, 'bad input')
1124
def test_validate_cidrs(self):
1125
manager = fake_network.FakeNetworkManager()
1126
nets = manager.create_networks(None, 'fake', '192.168.0.0/24',
1127
False, 1, 256, None, None, None,
1129
self.assertEqual(1, len(nets))
1130
cidrs = [str(net['cidr']) for net in nets]
1131
self.assertTrue('192.168.0.0/24' in cidrs)
1133
def test_validate_cidrs_split_exact_in_half(self):
1134
manager = fake_network.FakeNetworkManager()
1135
nets = manager.create_networks(None, 'fake', '192.168.0.0/24',
1136
False, 2, 128, None, None, None,
1138
self.assertEqual(2, len(nets))
1139
cidrs = [str(net['cidr']) for net in nets]
1140
self.assertTrue('192.168.0.0/25' in cidrs)
1141
self.assertTrue('192.168.0.128/25' in cidrs)
1143
def test_validate_cidrs_split_cidr_in_use_middle_of_range(self):
1144
manager = fake_network.FakeNetworkManager()
1145
self.mox.StubOutWithMock(manager.db, 'network_get_all')
1146
ctxt = mox.IgnoreArg()
1147
manager.db.network_get_all(ctxt).AndReturn([{'id': 1,
1148
'cidr': '192.168.2.0/24'}])
1149
self.mox.ReplayAll()
1150
nets = manager.create_networks(None, 'fake', '192.168.0.0/16',
1151
False, 4, 256, None, None, None,
1153
self.assertEqual(4, len(nets))
1154
cidrs = [str(net['cidr']) for net in nets]
1155
exp_cidrs = ['192.168.0.0/24', '192.168.1.0/24', '192.168.3.0/24',
1157
for exp_cidr in exp_cidrs:
1158
self.assertTrue(exp_cidr in cidrs)
1159
self.assertFalse('192.168.2.0/24' in cidrs)
1161
def test_validate_cidrs_smaller_subnet_in_use(self):
1162
manager = fake_network.FakeNetworkManager()
1163
self.mox.StubOutWithMock(manager.db, 'network_get_all')
1164
ctxt = mox.IgnoreArg()
1165
manager.db.network_get_all(ctxt).AndReturn([{'id': 1,
1166
'cidr': '192.168.2.9/25'}])
1167
self.mox.ReplayAll()
1168
# ValueError: requested cidr (192.168.2.0/24) conflicts with
1169
# existing smaller cidr
1170
args = (None, 'fake', '192.168.2.0/24', False, 1, 256, None, None,
1172
self.assertRaises(ValueError, manager.create_networks, *args)
1174
def test_validate_cidrs_split_smaller_cidr_in_use(self):
1175
manager = fake_network.FakeNetworkManager()
1176
self.mox.StubOutWithMock(manager.db, 'network_get_all')
1177
ctxt = mox.IgnoreArg()
1178
manager.db.network_get_all(ctxt).AndReturn([{'id': 1,
1179
'cidr': '192.168.2.0/25'}])
1180
self.mox.ReplayAll()
1181
nets = manager.create_networks(None, 'fake', '192.168.0.0/16',
1182
False, 4, 256, None, None, None, None,
1184
self.assertEqual(4, len(nets))
1185
cidrs = [str(net['cidr']) for net in nets]
1186
exp_cidrs = ['192.168.0.0/24', '192.168.1.0/24', '192.168.3.0/24',
1188
for exp_cidr in exp_cidrs:
1189
self.assertTrue(exp_cidr in cidrs)
1190
self.assertFalse('192.168.2.0/24' in cidrs)
1192
def test_validate_cidrs_split_smaller_cidr_in_use2(self):
1193
manager = fake_network.FakeNetworkManager()
1194
self.mox.StubOutWithMock(manager.db, 'network_get_all')
1195
ctxt = mox.IgnoreArg()
1196
manager.db.network_get_all(ctxt).AndReturn([{'id': 1,
1197
'cidr': '192.168.2.9/29'}])
1198
self.mox.ReplayAll()
1199
nets = manager.create_networks(None, 'fake', '192.168.2.0/24',
1200
False, 3, 32, None, None, None, None,
1202
self.assertEqual(3, len(nets))
1203
cidrs = [str(net['cidr']) for net in nets]
1204
exp_cidrs = ['192.168.2.32/27', '192.168.2.64/27', '192.168.2.96/27']
1205
for exp_cidr in exp_cidrs:
1206
self.assertTrue(exp_cidr in cidrs)
1207
self.assertFalse('192.168.2.0/27' in cidrs)
1209
def test_validate_cidrs_split_all_in_use(self):
1210
manager = fake_network.FakeNetworkManager()
1211
self.mox.StubOutWithMock(manager.db, 'network_get_all')
1212
ctxt = mox.IgnoreArg()
1213
in_use = [{'id': 1, 'cidr': '192.168.2.9/29'},
1214
{'id': 2, 'cidr': '192.168.2.64/26'},
1215
{'id': 3, 'cidr': '192.168.2.128/26'}]
1216
manager.db.network_get_all(ctxt).AndReturn(in_use)
1217
self.mox.ReplayAll()
1218
args = (None, 'fake', '192.168.2.0/24', False, 3, 64, None, None,
1220
# ValueError: Not enough subnets avail to satisfy requested num_
1221
# networks - some subnets in requested range already
1223
self.assertRaises(ValueError, manager.create_networks, *args)
1225
def test_validate_cidrs_one_in_use(self):
1226
manager = fake_network.FakeNetworkManager()
1227
args = (None, 'fake', '192.168.0.0/24', False, 2, 256, None, None,
1229
# ValueError: network_size * num_networks exceeds cidr size
1230
self.assertRaises(ValueError, manager.create_networks, *args)
1232
def test_validate_cidrs_already_used(self):
1233
manager = fake_network.FakeNetworkManager()
1234
self.mox.StubOutWithMock(manager.db, 'network_get_all')
1235
ctxt = mox.IgnoreArg()
1236
manager.db.network_get_all(ctxt).AndReturn([{'id': 1,
1237
'cidr': '192.168.0.0/24'}])
1238
self.mox.ReplayAll()
1239
# ValueError: cidr already in use
1240
args = (None, 'fake', '192.168.0.0/24', False, 1, 256, None, None,
1242
self.assertRaises(ValueError, manager.create_networks, *args)
1244
def test_validate_cidrs_too_many(self):
1245
manager = fake_network.FakeNetworkManager()
1246
args = (None, 'fake', '192.168.0.0/24', False, 200, 256, None, None,
1248
# ValueError: Not enough subnets avail to satisfy requested
1250
self.assertRaises(ValueError, manager.create_networks, *args)
1252
def test_validate_cidrs_split_partial(self):
1253
manager = fake_network.FakeNetworkManager()
1254
nets = manager.create_networks(None, 'fake', '192.168.0.0/16',
1255
False, 2, 256, None, None, None, None,
1257
returned_cidrs = [str(net['cidr']) for net in nets]
1258
self.assertTrue('192.168.0.0/24' in returned_cidrs)
1259
self.assertTrue('192.168.1.0/24' in returned_cidrs)
1261
def test_validate_cidrs_conflict_existing_supernet(self):
1262
manager = fake_network.FakeNetworkManager()
1263
self.mox.StubOutWithMock(manager.db, 'network_get_all')
1264
ctxt = mox.IgnoreArg()
1265
fakecidr = [{'id': 1, 'cidr': '192.168.0.0/8'}]
1266
manager.db.network_get_all(ctxt).AndReturn(fakecidr)
1267
self.mox.ReplayAll()
1268
args = (None, 'fake', '192.168.0.0/24', False, 1, 256, None, None,
1270
# ValueError: requested cidr (192.168.0.0/24) conflicts
1271
# with existing supernet
1272
self.assertRaises(ValueError, manager.create_networks, *args)
1274
def test_create_networks(self):
1275
cidr = '192.168.0.0/24'
1276
manager = fake_network.FakeNetworkManager()
1277
self.stubs.Set(manager, '_create_fixed_ips',
1278
self.fake_create_fixed_ips)
1279
args = [None, 'foo', cidr, None, 1, 256, 'fd00::/48', None, None,
1281
self.assertTrue(manager.create_networks(*args))
1283
def test_create_networks_cidr_already_used(self):
1284
manager = fake_network.FakeNetworkManager()
1285
self.mox.StubOutWithMock(manager.db, 'network_get_all')
1286
ctxt = mox.IgnoreArg()
1287
fakecidr = [{'id': 1, 'cidr': '192.168.0.0/24'}]
1288
manager.db.network_get_all(ctxt).AndReturn(fakecidr)
1289
self.mox.ReplayAll()
1290
args = [None, 'foo', '192.168.0.0/24', None, 1, 256,
1291
'fd00::/48', None, None, None, None, None]
1292
self.assertRaises(ValueError, manager.create_networks, *args)
1294
def test_create_networks_many(self):
1295
cidr = '192.168.0.0/16'
1296
manager = fake_network.FakeNetworkManager()
1297
self.stubs.Set(manager, '_create_fixed_ips',
1298
self.fake_create_fixed_ips)
1299
args = [None, 'foo', cidr, None, 10, 256, 'fd00::/48', None, None,
1301
self.assertTrue(manager.create_networks(*args))
1303
def test_get_instance_uuids_by_ip_regex(self):
1304
manager = fake_network.FakeNetworkManager()
1305
_vifs = manager.db.virtual_interface_get_all(None)
1306
fake_context = context.RequestContext('user', 'project')
1308
# Greedy get eveything
1309
res = manager.get_instance_uuids_by_ip_filter(fake_context,
1311
self.assertEqual(len(res), len(_vifs))
1314
res = manager.get_instance_uuids_by_ip_filter(fake_context,
1316
self.assertFalse(res)
1319
res = manager.get_instance_uuids_by_ip_filter(fake_context,
1320
{'ip': '172.16.0.2'})
1321
self.assertTrue(res)
1322
self.assertEqual(len(res), 1)
1323
self.assertEqual(res[0]['instance_uuid'], _vifs[1]['instance_uuid'])
1326
res = manager.get_instance_uuids_by_ip_filter(fake_context,
1327
{'ip': '173.16.0.2'})
1328
self.assertTrue(res)
1329
self.assertEqual(len(res), 1)
1330
self.assertEqual(res[0]['instance_uuid'], _vifs[2]['instance_uuid'])
1332
# Get instance 0 and 1
1333
res = manager.get_instance_uuids_by_ip_filter(fake_context,
1334
{'ip': '172.16.0.*'})
1335
self.assertTrue(res)
1336
self.assertEqual(len(res), 2)
1337
self.assertEqual(res[0]['instance_uuid'], _vifs[0]['instance_uuid'])
1338
self.assertEqual(res[1]['instance_uuid'], _vifs[1]['instance_uuid'])
1340
# Get instance 1 and 2
1341
res = manager.get_instance_uuids_by_ip_filter(fake_context,
1342
{'ip': '17..16.0.2'})
1343
self.assertTrue(res)
1344
self.assertEqual(len(res), 2)
1345
self.assertEqual(res[0]['instance_uuid'], _vifs[1]['instance_uuid'])
1346
self.assertEqual(res[1]['instance_uuid'], _vifs[2]['instance_uuid'])
1348
def test_get_instance_uuids_by_ipv6_regex(self):
1349
manager = fake_network.FakeNetworkManager()
1350
_vifs = manager.db.virtual_interface_get_all(None)
1351
fake_context = context.RequestContext('user', 'project')
1353
# Greedy get eveything
1354
res = manager.get_instance_uuids_by_ip_filter(fake_context,
1356
self.assertEqual(len(res), len(_vifs))
1359
res = manager.get_instance_uuids_by_ip_filter(fake_context,
1360
{'ip6': '.*1034.*'})
1361
self.assertFalse(res)
1364
res = manager.get_instance_uuids_by_ip_filter(fake_context,
1365
{'ip6': '2001:.*2'})
1366
self.assertTrue(res)
1367
self.assertEqual(len(res), 1)
1368
self.assertEqual(res[0]['instance_uuid'], _vifs[1]['instance_uuid'])
1371
ip6 = '2001:db8:69:1f:dead:beff:feff:ef03'
1372
res = manager.get_instance_uuids_by_ip_filter(fake_context,
1374
self.assertTrue(res)
1375
self.assertEqual(len(res), 1)
1376
self.assertEqual(res[0]['instance_uuid'], _vifs[2]['instance_uuid'])
1378
# Get instance 0 and 1
1379
res = manager.get_instance_uuids_by_ip_filter(fake_context,
1380
{'ip6': '.*ef0[1,2]'})
1381
self.assertTrue(res)
1382
self.assertEqual(len(res), 2)
1383
self.assertEqual(res[0]['instance_uuid'], _vifs[0]['instance_uuid'])
1384
self.assertEqual(res[1]['instance_uuid'], _vifs[1]['instance_uuid'])
1386
# Get instance 1 and 2
1387
ip6 = '2001:db8:69:1.:dead:beff:feff:ef0.'
1388
res = manager.get_instance_uuids_by_ip_filter(fake_context,
1390
self.assertTrue(res)
1391
self.assertEqual(len(res), 2)
1392
self.assertEqual(res[0]['instance_uuid'], _vifs[1]['instance_uuid'])
1393
self.assertEqual(res[1]['instance_uuid'], _vifs[2]['instance_uuid'])
1395
def test_get_instance_uuids_by_ip(self):
1396
manager = fake_network.FakeNetworkManager()
1397
_vifs = manager.db.virtual_interface_get_all(None)
1398
fake_context = context.RequestContext('user', 'project')
1401
res = manager.get_instance_uuids_by_ip_filter(fake_context,
1403
self.assertFalse(res)
1407
res = manager.get_instance_uuids_by_ip_filter(fake_context,
1409
self.assertFalse(res)
1413
res = manager.get_instance_uuids_by_ip_filter(fake_context,
1415
self.assertTrue(res)
1416
self.assertEqual(len(res), 1)
1417
self.assertEqual(res[0]['instance_uuid'], _vifs[1]['instance_uuid'])
1421
res = manager.get_instance_uuids_by_ip_filter(fake_context,
1423
self.assertTrue(res)
1424
self.assertEqual(len(res), 1)
1425
self.assertEqual(res[0]['instance_uuid'], _vifs[2]['instance_uuid'])
1427
def test_get_network(self):
1428
manager = fake_network.FakeNetworkManager()
1429
fake_context = context.RequestContext('user', 'project')
1430
self.mox.StubOutWithMock(manager.db, 'network_get_by_uuid')
1431
manager.db.network_get_by_uuid(mox.IgnoreArg(),
1432
mox.IgnoreArg()).AndReturn(networks[0])
1433
self.mox.ReplayAll()
1434
uuid = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'
1435
network = manager.get_network(fake_context, uuid)
1436
self.assertEqual(network['uuid'], uuid)
1438
def test_get_network_not_found(self):
1439
manager = fake_network.FakeNetworkManager()
1440
fake_context = context.RequestContext('user', 'project')
1441
self.mox.StubOutWithMock(manager.db, 'network_get_by_uuid')
1442
manager.db.network_get_by_uuid(
1444
mox.IgnoreArg()).AndRaise(exception.NetworkNotFoundForUUID)
1445
self.mox.ReplayAll()
1446
uuid = 'eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee'
1447
self.assertRaises(exception.NetworkNotFound,
1448
manager.get_network, fake_context, uuid)
1450
def test_get_all_networks(self):
1451
manager = fake_network.FakeNetworkManager()
1452
fake_context = context.RequestContext('user', 'project')
1453
self.mox.StubOutWithMock(manager.db, 'network_get_all')
1454
manager.db.network_get_all(mox.IgnoreArg()).AndReturn(networks)
1455
self.mox.ReplayAll()
1456
output = manager.get_all_networks(fake_context)
1457
self.assertEqual(len(networks), 2)
1458
self.assertEqual(output[0]['uuid'],
1459
'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa')
1460
self.assertEqual(output[1]['uuid'],
1461
'bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb')
1463
def test_disassociate_network(self):
1464
manager = fake_network.FakeNetworkManager()
1465
fake_context = context.RequestContext('user', 'project')
1466
self.mox.StubOutWithMock(manager.db, 'network_get_by_uuid')
1467
manager.db.network_get_by_uuid(mox.IgnoreArg(),
1468
mox.IgnoreArg()).AndReturn(networks[0])
1469
self.mox.ReplayAll()
1470
uuid = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'
1471
manager.disassociate_network(fake_context, uuid)
1473
def test_disassociate_network_not_found(self):
1474
manager = fake_network.FakeNetworkManager()
1475
fake_context = context.RequestContext('user', 'project')
1476
self.mox.StubOutWithMock(manager.db, 'network_get_by_uuid')
1477
manager.db.network_get_by_uuid(
1479
mox.IgnoreArg()).AndRaise(exception.NetworkNotFoundForUUID)
1480
self.mox.ReplayAll()
1481
uuid = 'eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee'
1482
self.assertRaises(exception.NetworkNotFound,
1483
manager.disassociate_network, fake_context, uuid)
1486
class TestRPCFixedManager(network_manager.RPCAllocateFixedIP,
1487
network_manager.NetworkManager):
1488
"""Dummy manager that implements RPCAllocateFixedIP"""
1491
class RPCAllocateTestCase(test.TestCase):
1492
"""Tests nova.network.manager.RPCAllocateFixedIP"""
1494
super(RPCAllocateTestCase, self).setUp()
1495
self.rpc_fixed = TestRPCFixedManager()
1496
self.context = context.RequestContext('fake', 'fake')
1498
def test_rpc_allocate(self):
1499
"""Test to verify bug 855030 doesn't resurface.
1501
Mekes sure _rpc_allocate_fixed_ip returns a value so the call
1502
returns properly and the greenpool completes."""
1503
address = '10.10.10.10'
1505
def fake_allocate(*args, **kwargs):
1508
def fake_network_get(*args, **kwargs):
1511
self.stubs.Set(self.rpc_fixed, 'allocate_fixed_ip', fake_allocate)
1512
self.stubs.Set(self.rpc_fixed.db, 'network_get', fake_network_get)
1513
rval = self.rpc_fixed._rpc_allocate_fixed_ip(self.context,
1516
self.assertEqual(rval, address)
1519
class TestFloatingIPManager(network_manager.FloatingIP,
1520
network_manager.NetworkManager):
1521
"""Dummy manager that implements FloatingIP"""
1524
class AllocateTestCase(test.TestCase):
1525
def test_allocate_for_instance(self):
1526
address = "10.10.10.10"
1527
self.flags(auto_assign_floating_ip=True)
1528
self.compute = self.start_service('compute')
1529
self.network = self.start_service('network')
1531
self.user_id = 'fake'
1532
self.project_id = 'fake'
1533
self.context = context.RequestContext(self.user_id,
1537
db.floating_ip_create(self.context,
1538
{'address': address,
1540
inst = db.instance_create(self.context, {'host': self.compute.host,
1541
'instance_type_id': 1})
1542
networks = db.network_get_all(self.context)
1543
for network in networks:
1544
db.network_update(self.context, network['id'],
1545
{'host': self.network.host})
1546
project_id = self.context.project_id
1547
nw_info = self.network.allocate_for_instance(self.context,
1548
instance_id=inst['id'], instance_uuid=inst['uuid'],
1549
host=inst['host'], vpn=None, rxtx_factor=3,
1550
project_id=project_id)
1551
self.assertEquals(1, len(nw_info))
1552
fixed_ip = nw_info.fixed_ips()[0]['address']
1553
self.assertTrue(utils.is_valid_ipv4(fixed_ip))
1554
self.network.deallocate_for_instance(self.context,
1555
instance_id=inst['id'],
1557
host=self.network.host,
1558
project_id=project_id)
1561
class FloatingIPTestCase(test.TestCase):
1562
"""Tests nova.network.manager.FloatingIP"""
1564
super(FloatingIPTestCase, self).setUp()
1565
self.tempdir = tempfile.mkdtemp()
1566
self.flags(logdir=self.tempdir)
1567
self.network = TestFloatingIPManager()
1568
temp = importutils.import_object('nova.network.minidns.MiniDNS')
1569
self.network.floating_dns_manager = temp
1570
self.network.db = db
1571
self.project_id = 'testproject'
1572
self.context = context.RequestContext('testuser', self.project_id,
1576
shutil.rmtree(self.tempdir)
1577
super(FloatingIPTestCase, self).tearDown()
1579
def test_double_deallocation(self):
1580
instance_ref = db.api.instance_create(self.context,
1581
{"project_id": self.project_id})
1582
# Run it twice to make it fault if it does not handle
1583
# instances without fixed networks
1584
# If this fails in either, it does not handle having no addresses
1585
self.network.deallocate_for_instance(self.context,
1586
instance_id=instance_ref['id'])
1587
self.network.deallocate_for_instance(self.context,
1588
instance_id=instance_ref['id'])
1590
def test_deallocation_deleted_instance(self):
1591
self.stubs.Set(self.network, '_teardown_network_on_host',
1592
lambda *args, **kwargs: None)
1593
instance = db.api.instance_create(self.context, {
1594
'project_id': self.project_id, 'deleted': True})
1595
network = db.api.network_create_safe(self.context.elevated(), {
1596
'project_id': self.project_id})
1597
addr = db.fixed_ip_create(self.context, {'allocated': True,
1598
'instance_uuid': instance['uuid'], 'address': '10.1.1.1',
1599
'network_id': network['id']})
1600
fixed = db.fixed_ip_get_by_address(
1601
self.context.elevated(read_deleted='yes'), addr)
1602
db.api.floating_ip_create(self.context, {
1603
'address': '10.10.10.10', 'instance_uuid': instance['uuid'],
1604
'fixed_ip_id': fixed['id'],
1605
'project_id': self.project_id})
1606
self.network.deallocate_for_instance(self.context,
1607
instance_id=instance['id'])
1609
def test_deallocation_duplicate_floating_ip(self):
1610
self.stubs.Set(self.network, '_teardown_network_on_host',
1611
lambda *args, **kwargs: None)
1612
instance = db.api.instance_create(self.context, {
1613
'project_id': self.project_id})
1614
network = db.api.network_create_safe(self.context.elevated(), {
1615
'project_id': self.project_id})
1616
addr = db.fixed_ip_create(self.context, {'allocated': True,
1617
'instance_uuid': instance['uuid'], 'address': '10.1.1.1',
1618
'network_id': network['id']})
1619
fixed = db.fixed_ip_get_by_address(
1620
self.context.elevated(read_deleted='yes'), addr)
1621
db.api.floating_ip_create(self.context, {
1622
'address': '10.10.10.10',
1624
db.api.floating_ip_create(self.context, {
1625
'address': '10.10.10.10', 'instance_uuid': instance['uuid'],
1626
'fixed_ip_id': fixed['id'],
1627
'project_id': self.project_id})
1628
self.network.deallocate_for_instance(self.context,
1629
instance_id=instance['id'])
1631
def test_floating_dns_create_conflict(self):
1632
zone = "example.org"
1633
address1 = "10.10.10.11"
1637
self.network.add_dns_entry(self.context, address1, name1, "A", zone)
1639
self.assertRaises(exception.FloatingIpDNSExists,
1640
self.network.add_dns_entry, self.context,
1641
address1, name1, "A", zone)
1643
def test_floating_create_and_get(self):
1644
zone = "example.org"
1645
address1 = "10.10.10.11"
1648
entries = self.network.get_dns_entries_by_address(self.context,
1650
self.assertFalse(entries)
1652
self.network.add_dns_entry(self.context, address1, name1, "A", zone)
1653
self.network.add_dns_entry(self.context, address1, name2, "A", zone)
1654
entries = self.network.get_dns_entries_by_address(self.context,
1656
self.assertEquals(len(entries), 2)
1657
self.assertEquals(entries[0], name1)
1658
self.assertEquals(entries[1], name2)
1660
entries = self.network.get_dns_entries_by_name(self.context,
1662
self.assertEquals(len(entries), 1)
1663
self.assertEquals(entries[0], address1)
1665
def test_floating_dns_delete(self):
1666
zone = "example.org"
1667
address1 = "10.10.10.11"
1671
self.network.add_dns_entry(self.context, address1, name1, "A", zone)
1672
self.network.add_dns_entry(self.context, address1, name2, "A", zone)
1673
self.network.delete_dns_entry(self.context, name1, zone)
1675
entries = self.network.get_dns_entries_by_address(self.context,
1677
self.assertEquals(len(entries), 1)
1678
self.assertEquals(entries[0], name2)
1680
self.assertRaises(exception.NotFound,
1681
self.network.delete_dns_entry, self.context,
1684
def test_floating_dns_domains_public(self):
1686
domain1 = "example.org"
1687
domain2 = "example.com"
1688
address1 = '10.10.10.10'
1689
entryname = 'testentry'
1691
context_admin = context.RequestContext('testuser', 'testproject',
1694
self.assertRaises(exception.AdminRequired,
1695
self.network.create_public_dns_domain, self.context,
1697
self.network.create_public_dns_domain(context_admin, domain1,
1699
self.network.create_public_dns_domain(context_admin, domain2,
1702
domains = self.network.get_dns_domains(self.context)
1703
self.assertEquals(len(domains), 2)
1704
self.assertEquals(domains[0]['domain'], domain1)
1705
self.assertEquals(domains[1]['domain'], domain2)
1706
self.assertEquals(domains[0]['project'], 'testproject')
1707
self.assertEquals(domains[1]['project'], 'fakeproject')
1709
self.network.add_dns_entry(self.context, address1, entryname,
1711
entries = self.network.get_dns_entries_by_name(self.context,
1713
self.assertEquals(len(entries), 1)
1714
self.assertEquals(entries[0], address1)
1716
self.assertRaises(exception.AdminRequired,
1717
self.network.delete_dns_domain, self.context,
1719
self.network.delete_dns_domain(context_admin, domain1)
1720
self.network.delete_dns_domain(context_admin, domain2)
1722
# Verify that deleting the domain deleted the associated entry
1723
entries = self.network.get_dns_entries_by_name(self.context,
1725
self.assertFalse(entries)
1727
def test_delete_all_by_ip(self):
1728
domain1 = "example.org"
1729
domain2 = "example.com"
1730
address = "10.10.10.10"
1734
def fake_domains(context):
1735
return [{'domain': 'example.org', 'scope': 'public'},
1736
{'domain': 'example.com', 'scope': 'public'},
1737
{'domain': 'test.example.org', 'scope': 'public'}]
1739
self.stubs.Set(self.network, 'get_dns_domains', fake_domains)
1741
context_admin = context.RequestContext('testuser', 'testproject',
1744
self.network.create_public_dns_domain(context_admin, domain1,
1746
self.network.create_public_dns_domain(context_admin, domain2,
1749
domains = self.network.get_dns_domains(self.context)
1750
for domain in domains:
1751
self.network.add_dns_entry(self.context, address,
1752
name1, "A", domain['domain'])
1753
self.network.add_dns_entry(self.context, address,
1754
name2, "A", domain['domain'])
1755
entries = self.network.get_dns_entries_by_address(self.context,
1758
self.assertEquals(len(entries), 2)
1760
self.network._delete_all_entries_for_ip(self.context, address)
1762
for domain in domains:
1763
entries = self.network.get_dns_entries_by_address(self.context,
1766
self.assertFalse(entries)
1768
self.network.delete_dns_domain(context_admin, domain1)
1769
self.network.delete_dns_domain(context_admin, domain2)
1771
def test_mac_conflicts(self):
1772
"""Make sure MAC collisions are retried"""
1773
self.flags(create_unique_mac_address_attempts=3)
1774
ctxt = context.RequestContext('testuser', 'testproject', is_admin=True)
1775
macs = ['bb:bb:bb:bb:bb:bb', 'aa:aa:aa:aa:aa:aa']
1777
# Create a VIF with aa:aa:aa:aa:aa:aa
1778
crash_test_dummy_vif = {
1780
'instance_uuid': 'fake_uuid',
1781
'network_id': 'fake_net',
1782
'uuid': 'fake_uuid',
1784
self.network.db.virtual_interface_create(ctxt, crash_test_dummy_vif)
1786
# Hand out a collision first, then a legit MAC
1789
self.stubs.Set(utils, 'generate_mac_address', fake_gen_mac)
1791
# SQLite doesn't seem to honor the uniqueness constraint on the
1792
# address column, so fake the collision-avoidance here
1793
def fake_vif_save(vif):
1794
if vif.address == crash_test_dummy_vif['address']:
1795
raise exception.DBError("If you're smart, you'll retry!")
1796
self.stubs.Set(models.VirtualInterface, 'save', fake_vif_save)
1798
# Attempt to add another and make sure that both MACs are consumed
1800
self.network.add_virtual_interface(ctxt, 'fake_uuid', 'fake_net')
1801
self.assertEqual(macs, [])
1804
class NetworkPolicyTestCase(test.TestCase):
1806
super(NetworkPolicyTestCase, self).setUp()
1811
self.context = context.get_admin_context()
1814
super(NetworkPolicyTestCase, self).tearDown()
1817
def _set_rules(self, rules):
1818
nova.common.policy.set_brain(nova.common.policy.HttpBrain(rules))
1820
def test_check_policy(self):
1821
self.mox.StubOutWithMock(nova.policy, 'enforce')
1823
'project_id': self.context.project_id,
1824
'user_id': self.context.user_id,
1826
nova.policy.enforce(self.context, 'network:get_all', target)
1827
self.mox.ReplayAll()
1828
network_manager.check_policy(self.context, 'get_all')
1831
class InstanceDNSTestCase(test.TestCase):
1832
"""Tests nova.network.manager instance DNS"""
1834
super(InstanceDNSTestCase, self).setUp()
1835
self.tempdir = tempfile.mkdtemp()
1836
self.flags(logdir=self.tempdir)
1837
self.network = TestFloatingIPManager()
1838
temp = importutils.import_object('nova.network.minidns.MiniDNS')
1839
self.network.instance_dns_manager = temp
1840
temp = importutils.import_object('nova.network.dns_driver.DNSDriver')
1841
self.network.floating_dns_manager = temp
1842
self.network.db = db
1843
self.project_id = 'testproject'
1844
self.context = context.RequestContext('testuser', self.project_id,
1848
shutil.rmtree(self.tempdir)
1849
super(InstanceDNSTestCase, self).tearDown()
1851
def test_dns_domains_private(self):
1853
domain1 = 'example.org'
1855
context_admin = context.RequestContext('testuser', 'testproject',
1858
self.assertRaises(exception.AdminRequired,
1859
self.network.create_private_dns_domain, self.context,
1862
self.network.create_private_dns_domain(context_admin, domain1, zone1)
1863
domains = self.network.get_dns_domains(self.context)
1864
self.assertEquals(len(domains), 1)
1865
self.assertEquals(domains[0]['domain'], domain1)
1866
self.assertEquals(domains[0]['availability_zone'], zone1)
1868
self.assertRaises(exception.AdminRequired,
1869
self.network.delete_dns_domain, self.context,
1871
self.network.delete_dns_domain(context_admin, domain1)
1874
domain1 = "example.org"
1875
domain2 = "example.com"
1878
class LdapDNSTestCase(test.TestCase):
1879
"""Tests nova.network.ldapdns.LdapDNS"""
1881
super(LdapDNSTestCase, self).setUp()
1883
self.saved_ldap = sys.modules.get('ldap')
1884
import nova.auth.fakeldap
1885
sys.modules['ldap'] = nova.auth.fakeldap
1887
temp = importutils.import_object('nova.network.ldapdns.FakeLdapDNS')
1889
self.driver.create_domain(domain1)
1890
self.driver.create_domain(domain2)
1893
self.driver.delete_domain(domain1)
1894
self.driver.delete_domain(domain2)
1895
sys.modules['ldap'] = self.saved_ldap
1896
super(LdapDNSTestCase, self).tearDown()
1898
def test_ldap_dns_domains(self):
1899
domains = self.driver.get_domains()
1900
self.assertEqual(len(domains), 2)
1901
self.assertIn(domain1, domains)
1902
self.assertIn(domain2, domains)
1904
def test_ldap_dns_create_conflict(self):
1905
address1 = "10.10.10.11"
1909
self.driver.create_entry(name1, address1, "A", domain1)
1911
self.assertRaises(exception.FloatingIpDNSExists,
1912
self.driver.create_entry,
1913
name1, address1, "A", domain1)
1915
def test_ldap_dns_create_and_get(self):
1916
address1 = "10.10.10.11"
1919
entries = self.driver.get_entries_by_address(address1, domain1)
1920
self.assertFalse(entries)
1922
self.driver.create_entry(name1, address1, "A", domain1)
1923
self.driver.create_entry(name2, address1, "A", domain1)
1924
entries = self.driver.get_entries_by_address(address1, domain1)
1925
self.assertEquals(len(entries), 2)
1926
self.assertEquals(entries[0], name1)
1927
self.assertEquals(entries[1], name2)
1929
entries = self.driver.get_entries_by_name(name1, domain1)
1930
self.assertEquals(len(entries), 1)
1931
self.assertEquals(entries[0], address1)
1933
def test_ldap_dns_delete(self):
1934
address1 = "10.10.10.11"
1938
self.driver.create_entry(name1, address1, "A", domain1)
1939
self.driver.create_entry(name2, address1, "A", domain1)
1940
entries = self.driver.get_entries_by_address(address1, domain1)
1941
self.assertEquals(len(entries), 2)
1943
self.driver.delete_entry(name1, domain1)
1944
entries = self.driver.get_entries_by_address(address1, domain1)
1945
LOG.debug("entries: %s" % entries)
1946
self.assertEquals(len(entries), 1)
1947
self.assertEquals(entries[0], name2)
1949
self.assertRaises(exception.NotFound,
1950
self.driver.delete_entry,