~ubuntu-branches/ubuntu/raring/nova/raring-proposed

« back to all changes in this revision

Viewing changes to nova/tests/network/test_manager.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short, Chuck Short, Adam Gandelman, Yolanda Robla, James Page
  • Date: 2013-01-11 13:06:56 UTC
  • mfrom: (1.1.67)
  • Revision ID: package-import@ubuntu.com-20130111130656-7n7fkevy03stm3mv
Tags: 2013.1~g2-0ubuntu1
[ Chuck Short ]
* New upstream release.
* debian/patches/ubuntu-show-tests.patch: Dropped no longer needed.
* debian/nova-xcp-plugins.install: Fix xcp-plugins empty packages
* debian/control: Drop python-nose in favor or testrepository
* debian/control: Add python-coverage as a build dep.
* debian/rules, debian/control: Run pep8 tests.
* debian/*.init: Remove they are not needed and take up space
* debian/control, debian/nova-cells.{install, logrotate, upstart}: Add
  cells support.
* debian/patches/fix-ubuntu-tests.patch: temporarily disable failing tests.
* debian/control, debian/nova-baremetal.{install, logrotate, upstart}: Add
  nova baremetal support.
* debian/control: Remove python-support.

[ Adam Gandelman ]
* debian/*.manpages: Install Sphinx-generated manpages instead of
  our own.
* debian/nova-compute-*.conf: Specify the newly required compute_driver
  flag in addition to libvirt_type.
* debian/control:  Specify required python-webob and python-stevedore
  versions.

[ Yolanda Robla ]
* debian/*.upstart: Use start-stop-daemon instead of su for chuid
  (LP: #1086833).
* debian/rules: Remove override of dh_installinit for discriminating
  between Debian and Ubuntu.
* debian/nova-common.docs: Installing changelogs from rules
* debian/rules: Replacing perms in /etc/nova/logging.conf for 0644
* debian/control: adduser dependency on nova-compute.
* debian/control: added section oldlibs and priority extra on
  nova-ajax-console-proxy.
* debian/nova-xvpvncproxy.postrm: removing because of duplicates.

[ James Page ]
* d/control: Add ~ to python-sqlalchemy-ext versioned dependencies to
  make backporting easier.
* d/control: Updated nova-volume description and depdendencies to
  mark it as a transitional package, moved to oldlibs/extra.
* d/p/fix-libvirt-tests.patch: Dropped; accepted upstream.
* d/control: Added python-stevedore to BD's.
* d/*.postrm: Dropped postrm's that just run update-rc.d; this is not
  required when deploying upstart configurations only.
* d/nova-scheduler.manpages: Add man page for nova-rpc-zmq-receiver.
* d/rules: Install upstream changelog with a policy compliant name.
* d/control: Mark nova-compute-xcp as virtual package.
* d/control: nova-api-os-volume; Depend on cinder-api and mark as
  transitional package.
* d/nova-api-os-volume.lintian-overrides: Dropped - no longer required.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16
16
# License for the specific language governing permissions and limitations
17
17
# under the License.
 
18
import shutil
 
19
import tempfile
18
20
 
19
 
import fixtures
20
21
import mox
21
 
from oslo.config import cfg
22
22
 
23
23
from nova import context
24
24
from nova import db
25
25
from nova.db.sqlalchemy import models
26
26
from nova import exception
27
27
from nova import ipv6
28
 
from nova.network import floating_ips
29
28
from nova.network import linux_net
30
29
from nova.network import manager as network_manager
31
 
from nova.network import model as net_model
32
 
from nova.openstack.common.db import exception as db_exc
 
30
from nova.openstack.common import cfg
33
31
from nova.openstack.common import importutils
34
32
from nova.openstack.common import log as logging
35
33
from nova.openstack.common import rpc
36
34
from nova.openstack.common.rpc import common as rpc_common
37
 
from nova import quota
 
35
import nova.policy
38
36
from nova import test
39
37
from nova.tests import fake_ldap
40
38
from nova.tests import fake_network
41
39
from nova.tests import matchers
42
40
from nova import utils
43
41
 
 
42
 
44
43
CONF = cfg.CONF
45
44
LOG = logging.getLogger(__name__)
46
45
 
142
141
class FlatNetworkTestCase(test.TestCase):
143
142
    def setUp(self):
144
143
        super(FlatNetworkTestCase, self).setUp()
145
 
        self.tempdir = self.useFixture(fixtures.TempDir()).path
 
144
        self.tempdir = tempfile.mkdtemp()
146
145
        self.flags(log_dir=self.tempdir)
147
146
        self.network = network_manager.FlatManager(host=HOST)
148
147
        self.network.instance_dns_domain = ''
150
149
        self.context = context.RequestContext('testuser', 'testproject',
151
150
                                              is_admin=False)
152
151
 
 
152
    def tearDown(self):
 
153
        shutil.rmtree(self.tempdir)
 
154
        super(FlatNetworkTestCase, self).tearDown()
 
155
 
153
156
    def test_get_instance_nw_info(self):
154
157
        fake_get_instance_nw_info = fake_network.fake_get_instance_nw_info
155
158
 
181
184
                     'label': 'test%d' % nid,
182
185
                     'mac': 'DE:AD:BE:EF:00:%02x' % nid,
183
186
                     'rxtx_cap': 30,
184
 
                     'vif_type': net_model.VIF_TYPE_BRIDGE,
185
 
                     'vif_devname': None,
 
187
                     'vif_type': None,
186
188
                     'vif_uuid':
187
189
                        '00000000-0000-0000-0000-00000000000000%02d' % nid,
188
 
                     'ovs_interfaceid': None,
189
 
                     'qbh_params': None,
190
 
                     'qbg_params': None,
191
190
                     'should_create_vlan': False,
192
191
                     'should_create_bridge': False}
193
192
            self.assertThat(info, matchers.DictMatches(check))
284
283
        self.mox.StubOutWithMock(db, 'network_get')
285
284
        self.mox.StubOutWithMock(db, 'network_update')
286
285
        self.mox.StubOutWithMock(db, 'fixed_ip_associate_pool')
 
286
        self.mox.StubOutWithMock(db, 'instance_get')
287
287
        self.mox.StubOutWithMock(db,
288
288
                              'virtual_interface_get_by_instance_and_network')
289
289
        self.mox.StubOutWithMock(db, 'fixed_ip_update')
290
 
        self.mox.StubOutWithMock(quota.QUOTAS, 'reserve')
291
 
        self.mox.StubOutWithMock(db, 'instance_get_by_uuid')
292
 
        self.mox.StubOutWithMock(self.network, 'get_instance_nw_info')
293
 
 
 
290
 
 
291
        db.fixed_ip_update(mox.IgnoreArg(),
 
292
                           mox.IgnoreArg(),
 
293
                           mox.IgnoreArg())
 
294
        db.virtual_interface_get_by_instance_and_network(mox.IgnoreArg(),
 
295
                mox.IgnoreArg(), mox.IgnoreArg()).AndReturn({'id': 0})
 
296
 
 
297
        db.instance_get(self.context,
 
298
                        1).AndReturn({'display_name': HOST,
 
299
                                      'uuid': 'test-00001'})
 
300
        db.instance_get(mox.IgnoreArg(),
 
301
                        mox.IgnoreArg()).AndReturn({'security_groups':
 
302
                                                             [{'id': 0}]})
 
303
        db.instance_get(mox.IgnoreArg(),
 
304
                mox.IgnoreArg()).AndReturn({'security_groups':
 
305
                                            [{'id':0, 'name':'test'}]})
294
306
        db.fixed_ip_associate_pool(mox.IgnoreArg(),
295
307
                                   mox.IgnoreArg(),
296
308
                                   mox.IgnoreArg()).AndReturn('192.168.0.101')
297
 
 
298
 
        db.instance_get_by_uuid(mox.IgnoreArg(),
299
 
                mox.IgnoreArg()).AndReturn({'security_groups':
300
 
                                            [{'id': 0}]})
301
 
        db.instance_get_by_uuid(mox.IgnoreArg(),
302
 
                mox.IgnoreArg()).AndReturn({'security_groups':
303
 
                                            [{'id': 0, 'name': 'test'}]})
304
 
 
305
 
        db.virtual_interface_get_by_instance_and_network(mox.IgnoreArg(),
306
 
                mox.IgnoreArg(), mox.IgnoreArg()).AndReturn({'id': 0})
307
 
 
308
 
        db.fixed_ip_update(mox.IgnoreArg(),
309
 
                           mox.IgnoreArg(),
310
 
                           mox.IgnoreArg())
311
 
 
312
 
        quota.QUOTAS.reserve(mox.IgnoreArg(),
313
 
                             fixed_ips=mox.IgnoreArg()).AndReturn(None)
314
 
 
315
 
        db.instance_get_by_uuid(self.context,
316
 
                        mox.IgnoreArg()).AndReturn({'display_name': HOST})
317
 
 
318
309
        db.network_get(mox.IgnoreArg(),
319
310
                       mox.IgnoreArg(),
320
311
                       project_only=mox.IgnoreArg()).AndReturn(networks[0])
321
312
        db.network_update(mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg())
322
 
 
323
 
        self.network.get_instance_nw_info(mox.IgnoreArg(), mox.IgnoreArg(),
324
 
                                          mox.IgnoreArg(), mox.IgnoreArg())
325
313
        self.mox.ReplayAll()
326
 
        self.network.add_fixed_ip_to_instance(self.context, FAKEUUID, HOST,
 
314
        self.network.add_fixed_ip_to_instance(self.context, 1, HOST,
327
315
                                              networks[0]['id'])
328
316
 
329
317
    def test_add_fixed_ip_instance_using_uuid_without_vpn(self):
330
318
        self.mox.StubOutWithMock(db, 'network_get_by_uuid')
331
319
        self.mox.StubOutWithMock(db, 'network_update')
332
320
        self.mox.StubOutWithMock(db, 'fixed_ip_associate_pool')
 
321
        self.mox.StubOutWithMock(db, 'instance_get')
333
322
        self.mox.StubOutWithMock(db,
334
323
                              'virtual_interface_get_by_instance_and_network')
335
324
        self.mox.StubOutWithMock(db, 'fixed_ip_update')
336
 
        self.mox.StubOutWithMock(quota.QUOTAS, 'reserve')
337
 
        self.mox.StubOutWithMock(db, 'instance_get_by_uuid')
338
 
        self.mox.StubOutWithMock(self.network, 'get_instance_nw_info')
 
325
 
 
326
        db.fixed_ip_update(mox.IgnoreArg(),
 
327
                           mox.IgnoreArg(),
 
328
                           mox.IgnoreArg())
 
329
        db.virtual_interface_get_by_instance_and_network(mox.IgnoreArg(),
 
330
                mox.IgnoreArg(), mox.IgnoreArg()).AndReturn({'id': 0})
 
331
 
 
332
        db.instance_get(self.context,
 
333
                        1).AndReturn({'display_name': HOST,
 
334
                                      'uuid': 'test-00001'})
 
335
        db.instance_get(mox.IgnoreArg(),
 
336
                        mox.IgnoreArg()).AndReturn({'security_groups':
 
337
                                                             [{'id': 0}]})
 
338
        db.instance_get(mox.IgnoreArg(),
 
339
                mox.IgnoreArg()).AndReturn({'security_groups':
 
340
                                            [{'id':0, 'name':'test'}]})
339
341
 
340
342
        db.fixed_ip_associate_pool(mox.IgnoreArg(),
341
343
                                   mox.IgnoreArg(),
342
344
                                   mox.IgnoreArg()).AndReturn('192.168.0.101')
343
 
 
344
 
        db.instance_get_by_uuid(mox.IgnoreArg(),
345
 
                mox.IgnoreArg()).AndReturn({'security_groups':
346
 
                                            [{'id': 0}]})
347
 
        db.instance_get_by_uuid(mox.IgnoreArg(),
348
 
                mox.IgnoreArg()).AndReturn({'security_groups':
349
 
                                            [{'id': 0, 'name': 'test'}]})
350
 
 
351
 
        db.virtual_interface_get_by_instance_and_network(mox.IgnoreArg(),
352
 
                mox.IgnoreArg(), mox.IgnoreArg()).AndReturn({'id': 0})
353
 
 
354
 
        db.fixed_ip_update(mox.IgnoreArg(),
355
 
                           mox.IgnoreArg(),
356
 
                           mox.IgnoreArg())
357
 
 
358
 
        quota.QUOTAS.reserve(mox.IgnoreArg(),
359
 
                             fixed_ips=mox.IgnoreArg()).AndReturn(None)
360
 
 
361
 
        db.instance_get_by_uuid(self.context,
362
 
                        mox.IgnoreArg()).AndReturn({'display_name': HOST})
363
 
 
364
345
        db.network_get_by_uuid(mox.IgnoreArg(),
365
346
                               mox.IgnoreArg()).AndReturn(networks[0])
366
347
        db.network_update(mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg())
367
 
 
368
 
        self.network.get_instance_nw_info(mox.IgnoreArg(), mox.IgnoreArg(),
369
 
                                          mox.IgnoreArg(), mox.IgnoreArg())
370
348
        self.mox.ReplayAll()
371
 
        self.network.add_fixed_ip_to_instance(self.context, FAKEUUID, HOST,
 
349
        self.network.add_fixed_ip_to_instance(self.context, 1, HOST,
372
350
                                              networks[0]['uuid'])
373
351
 
374
352
    def test_mini_dns_driver(self):
417
395
 
418
396
    def test_instance_dns(self):
419
397
        fixedip = '192.168.0.101'
420
 
        self.mox.StubOutWithMock(db, 'network_get_by_uuid')
 
398
        self.mox.StubOutWithMock(db, 'network_get')
421
399
        self.mox.StubOutWithMock(db, 'network_update')
422
400
        self.mox.StubOutWithMock(db, 'fixed_ip_associate_pool')
 
401
        self.mox.StubOutWithMock(db, 'instance_get')
 
402
        self.mox.StubOutWithMock(db, 'instance_get_by_uuid')
423
403
        self.mox.StubOutWithMock(db,
424
404
                              'virtual_interface_get_by_instance_and_network')
425
405
        self.mox.StubOutWithMock(db, 'fixed_ip_update')
426
 
        self.mox.StubOutWithMock(db, 'instance_get_by_uuid')
427
 
        self.mox.StubOutWithMock(self.network, 'get_instance_nw_info')
428
 
        self.mox.StubOutWithMock(quota.QUOTAS, 'reserve')
 
406
 
 
407
        db.fixed_ip_update(mox.IgnoreArg(),
 
408
                           mox.IgnoreArg(),
 
409
                           mox.IgnoreArg())
 
410
        db.virtual_interface_get_by_instance_and_network(mox.IgnoreArg(),
 
411
                mox.IgnoreArg(), mox.IgnoreArg()).AndReturn({'id': 0})
 
412
 
 
413
        db.instance_get(self.context,
 
414
                        1).AndReturn({'display_name': HOST,
 
415
                                      'uuid': 'test-00001'})
 
416
        db.instance_get(mox.IgnoreArg(),
 
417
                        mox.IgnoreArg()).AndReturn({'security_groups':
 
418
                                                             [{'id': 0}]})
 
419
        db.instance_get(mox.IgnoreArg(),
 
420
                mox.IgnoreArg()).AndReturn({'security_groups':
 
421
                                            [{'id':0, 'name':'test'}]})
429
422
 
430
423
        db.fixed_ip_associate_pool(mox.IgnoreArg(),
431
424
                                   mox.IgnoreArg(),
432
425
                                   mox.IgnoreArg()).AndReturn(fixedip)
433
 
 
434
 
        db.instance_get_by_uuid(mox.IgnoreArg(),
435
 
                mox.IgnoreArg()).AndReturn({'security_groups':
436
 
                                            [{'id': 0}]})
437
 
        db.instance_get_by_uuid(mox.IgnoreArg(),
438
 
                mox.IgnoreArg()).AndReturn({'security_groups':
439
 
                                            [{'id': 0, 'name': 'test'}]})
440
 
 
441
 
        db.virtual_interface_get_by_instance_and_network(mox.IgnoreArg(),
442
 
                mox.IgnoreArg(), mox.IgnoreArg()).AndReturn({'id': 0})
443
 
 
444
 
        db.fixed_ip_update(mox.IgnoreArg(),
445
 
                           mox.IgnoreArg(),
446
 
                           mox.IgnoreArg())
447
 
 
448
 
        quota.QUOTAS.reserve(mox.IgnoreArg(),
449
 
                             fixed_ips=mox.IgnoreArg()).AndReturn(None)
450
 
 
451
 
        db.instance_get_by_uuid(self.context,
452
 
                        mox.IgnoreArg()).AndReturn({'display_name': HOST})
453
 
 
454
 
        db.network_get_by_uuid(mox.IgnoreArg(),
455
 
                               mox.IgnoreArg()).AndReturn(networks[0])
 
426
        db.network_get(mox.IgnoreArg(),
 
427
                       mox.IgnoreArg(),
 
428
                       project_only=mox.IgnoreArg()).AndReturn(networks[0])
456
429
        db.network_update(mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg())
457
430
 
458
 
        self.network.get_instance_nw_info(mox.IgnoreArg(), mox.IgnoreArg(),
459
 
                                          mox.IgnoreArg(), mox.IgnoreArg())
460
431
        self.mox.ReplayAll()
461
 
        self.network.add_fixed_ip_to_instance(self.context, FAKEUUID, HOST,
462
 
                                              networks[0]['uuid'])
463
 
 
 
432
        self.network.add_fixed_ip_to_instance(self.context, 1, HOST,
 
433
                                              networks[0]['id'])
464
434
        instance_manager = self.network.instance_dns_manager
465
435
        addresses = instance_manager.get_entries_by_name(HOST,
466
436
                                             self.network.instance_dns_domain)
467
437
        self.assertEqual(len(addresses), 1)
468
438
        self.assertEqual(addresses[0], fixedip)
469
 
        addresses = instance_manager.get_entries_by_name(FAKEUUID,
 
439
        addresses = instance_manager.get_entries_by_name('test-00001',
470
440
                                              self.network.instance_dns_domain)
471
441
        self.assertEqual(len(addresses), 1)
472
442
        self.assertEqual(addresses[0], fixedip)
473
443
 
474
 
    def test_allocate_floating_ip(self):
475
 
        self.assertEqual(self.network.allocate_floating_ip(self.context,
476
 
                                                           1, None), None)
477
 
 
478
 
    def test_deallocate_floating_ip(self):
479
 
        self.assertEqual(self.network.deallocate_floating_ip(self.context,
480
 
                                                             1, None), None)
481
 
 
482
 
    def test_associate_floating_ip(self):
483
 
        self.assertEqual(self.network.associate_floating_ip(self.context,
484
 
                                                            None, None), None)
485
 
 
486
 
    def test_disassociate_floating_ip(self):
487
 
        self.assertEqual(self.network.disassociate_floating_ip(self.context,
488
 
                                                               None, None),
489
 
                         None)
490
 
 
491
444
 
492
445
class VlanNetworkTestCase(test.TestCase):
493
446
    def setUp(self):
498
451
                                              is_admin=False)
499
452
 
500
453
    def test_vpn_allocate_fixed_ip(self):
 
454
        self.mox.StubOutWithMock(db, 'instance_get')
501
455
        self.mox.StubOutWithMock(db, 'fixed_ip_associate')
502
456
        self.mox.StubOutWithMock(db, 'fixed_ip_update')
503
457
        self.mox.StubOutWithMock(db,
504
458
                              'virtual_interface_get_by_instance_and_network')
505
 
        self.mox.StubOutWithMock(db, 'instance_get_by_uuid')
506
459
 
 
460
        db.instance_get(mox.IgnoreArg(),
 
461
                        mox.IgnoreArg()).AndReturn({'uuid': '42',
 
462
                                                    'display_name': HOST})
507
463
        db.fixed_ip_associate(mox.IgnoreArg(),
508
464
                              mox.IgnoreArg(),
509
465
                              mox.IgnoreArg(),
514
470
                           mox.IgnoreArg())
515
471
        db.virtual_interface_get_by_instance_and_network(mox.IgnoreArg(),
516
472
                mox.IgnoreArg(), mox.IgnoreArg()).AndReturn({'id': 0})
517
 
        db.instance_get_by_uuid(mox.IgnoreArg(),
518
 
                        mox.IgnoreArg()).AndReturn({'display_name': HOST})
519
473
        self.mox.ReplayAll()
520
474
 
521
475
        network = dict(networks[0])
522
476
        network['vpn_private_address'] = '192.168.0.2'
523
 
        self.network.allocate_fixed_ip(self.context, FAKEUUID, network,
524
 
                                       vpn=True)
 
477
        self.network.allocate_fixed_ip(self.context, 0, network, vpn=True)
525
478
 
526
479
    def test_vpn_allocate_fixed_ip_no_network_id(self):
527
480
        network = dict(networks[0])
533
486
        self.assertRaises(exception.FixedIpNotFoundForNetwork,
534
487
                self.network.allocate_fixed_ip,
535
488
                context_admin,
536
 
                instance['uuid'],
 
489
                instance['id'],
537
490
                network,
538
491
                vpn=True)
539
492
 
542
495
        self.mox.StubOutWithMock(db, 'fixed_ip_update')
543
496
        self.mox.StubOutWithMock(db,
544
497
                              'virtual_interface_get_by_instance_and_network')
545
 
        self.mox.StubOutWithMock(db, 'instance_get_by_uuid')
 
498
        self.mox.StubOutWithMock(db, 'instance_get')
546
499
 
547
 
        db.instance_get_by_uuid(mox.IgnoreArg(),
 
500
        db.instance_get(mox.IgnoreArg(),
 
501
                    mox.IgnoreArg()).AndReturn({'display_name': HOST,
 
502
                                                'uuid': FAKEUUID})
 
503
        db.instance_get(mox.IgnoreArg(),
548
504
                        mox.IgnoreArg()).AndReturn({'security_groups':
549
505
                                                             [{'id': 0}]})
550
 
 
551
506
        db.fixed_ip_associate_pool(mox.IgnoreArg(),
552
507
                                   mox.IgnoreArg(),
553
508
                                   mox.IgnoreArg()).AndReturn('192.168.0.1')
556
511
                           mox.IgnoreArg())
557
512
        db.virtual_interface_get_by_instance_and_network(mox.IgnoreArg(),
558
513
                mox.IgnoreArg(), mox.IgnoreArg()).AndReturn({'id': 0})
559
 
        db.instance_get_by_uuid(mox.IgnoreArg(),
560
 
                    mox.IgnoreArg()).AndReturn({'display_name': HOST})
561
514
        self.mox.ReplayAll()
562
515
 
563
516
        network = dict(networks[0])
564
517
        network['vpn_private_address'] = '192.168.0.2'
565
 
        self.network.allocate_fixed_ip(self.context, FAKEUUID, network)
 
518
        self.network.allocate_fixed_ip(self.context, 0, network)
566
519
 
567
520
    def test_create_networks_too_big(self):
568
521
        self.assertRaises(ValueError, self.network.create_networks, None,
718
671
                                      is_admin=False)
719
672
 
720
673
        def fake1(*args, **kwargs):
721
 
            return {'address': '10.0.0.1', 'network': 'fakenet'}
 
674
            return '10.0.0.1'
722
675
 
723
676
        # floating ip that's already associated
724
677
        def fake2(*args, **kwargs):
774
727
                       'floating_ip_fixed_ip_associate',
775
728
                       fake1)
776
729
        self.stubs.Set(self.network.db, 'floating_ip_disassociate', fake1)
777
 
        self.stubs.Set(self.network.driver, 'ensure_floating_forward', fake8)
 
730
        self.stubs.Set(self.network.driver, 'bind_floating_ip', fake8)
778
731
        self.assertRaises(exception.NoFloatingIpInterface,
779
732
                          self.network._associate_floating_ip,
780
733
                          ctxt,
824
777
                                                 mox.IgnoreArg())
825
778
        self.assertTrue(self.local)
826
779
 
827
 
    def test_add_floating_ip_nat_before_bind(self):
828
 
        # Tried to verify order with documented mox record/verify
829
 
        # functionality, but it doesn't seem to work since I can't make it
830
 
        # fail.  I'm using stubs and a flag for now, but if this mox feature
831
 
        # can be made to work, it would be a better way to test this.
832
 
        #
833
 
        # self.mox.StubOutWithMock(self.network.driver,
834
 
        #                          'ensure_floating_forward')
835
 
        # self.mox.StubOutWithMock(self.network.driver, 'bind_floating_ip')
836
 
        #
837
 
        # self.network.driver.ensure_floating_forward(mox.IgnoreArg(),
838
 
        #                                             mox.IgnoreArg(),
839
 
        #                                             mox.IgnoreArg(),
840
 
        #                                             mox.IgnoreArg())
841
 
        # self.network.driver.bind_floating_ip(mox.IgnoreArg(),
842
 
        #                                      mox.IgnoreArg())
843
 
        # self.mox.ReplayAll()
844
 
 
845
 
        nat_called = [False]
846
 
 
847
 
        def fake_nat(*args, **kwargs):
848
 
            nat_called[0] = True
849
 
 
850
 
        def fake_bind(*args, **kwargs):
851
 
            self.assertTrue(nat_called[0])
852
 
 
853
 
        self.stubs.Set(self.network.driver,
854
 
                       'ensure_floating_forward',
855
 
                       fake_nat)
856
 
        self.stubs.Set(self.network.driver, 'bind_floating_ip', fake_bind)
857
 
 
858
 
        self.network.l3driver.add_floating_ip('fakefloat',
859
 
                                              'fakefixed',
860
 
                                              'fakeiface',
861
 
                                              'fakenet')
862
 
 
863
780
    def test_floating_ip_init_host(self):
864
781
 
865
782
        def get_all_by_host(_context, _host):
874
791
        self.stubs.Set(self.network.db, 'floating_ip_get_all_by_host',
875
792
                       get_all_by_host)
876
793
 
877
 
        def fixed_ip_get(_context, fixed_ip_id, get_network):
 
794
        def fixed_ip_get(_context, fixed_ip_id):
878
795
            if fixed_ip_id == 1:
879
 
                return {'address': 'fakefixed', 'network': 'fakenet'}
 
796
                return {'address': 'fakefixed'}
880
797
            raise exception.FixedIpNotFound(id=fixed_ip_id)
881
798
        self.stubs.Set(self.network.db, 'fixed_ip_get', fixed_ip_get)
882
799
 
884
801
        self.flags(public_interface=False)
885
802
        self.network.l3driver.add_floating_ip('fakefloat',
886
803
                                              'fakefixed',
887
 
                                              'fakeiface',
888
 
                                              'fakenet')
 
804
                                              'fakeiface')
889
805
        self.mox.ReplayAll()
890
806
        self.network.init_host_floating_ips()
891
807
        self.mox.UnsetStubs()
895
811
        self.flags(public_interface='fooiface')
896
812
        self.network.l3driver.add_floating_ip('fakefloat',
897
813
                                              'fakefixed',
898
 
                                              'fooiface',
899
 
                                              'fakenet')
 
814
                                              'fooiface')
900
815
        self.mox.ReplayAll()
901
816
        self.network.init_host_floating_ips()
902
817
        self.mox.UnsetStubs()
998
913
    def test_add_fixed_ip_instance_without_vpn_requested_networks(self):
999
914
        self.mox.StubOutWithMock(db, 'network_get')
1000
915
        self.mox.StubOutWithMock(db, 'fixed_ip_associate_pool')
 
916
        self.mox.StubOutWithMock(db, 'instance_get')
1001
917
        self.mox.StubOutWithMock(db,
1002
918
                              'virtual_interface_get_by_instance_and_network')
1003
919
        self.mox.StubOutWithMock(db, 'fixed_ip_update')
1004
 
        self.mox.StubOutWithMock(db, 'instance_get_by_uuid')
1005
 
        self.mox.StubOutWithMock(self.network, 'get_instance_nw_info')
1006
920
 
 
921
        db.instance_get(mox.IgnoreArg(),
 
922
                        mox.IgnoreArg()).AndReturn({'uuid': FAKEUUID,
 
923
                                                    'display_name': HOST})
1007
924
        db.fixed_ip_update(mox.IgnoreArg(),
1008
925
                           mox.IgnoreArg(),
1009
926
                           mox.IgnoreArg())
1010
927
        db.virtual_interface_get_by_instance_and_network(mox.IgnoreArg(),
1011
928
                mox.IgnoreArg(), mox.IgnoreArg()).AndReturn({'id': 0})
1012
929
 
1013
 
        db.instance_get_by_uuid(mox.IgnoreArg(),
1014
 
                mox.IgnoreArg()).AndReturn({'security_groups': [{'id': 0}],
1015
 
                                            'availability_zone': '',
1016
 
                                            'uuid': FAKEUUID})
 
930
        db.instance_get(mox.IgnoreArg(),
 
931
                        mox.IgnoreArg()).AndReturn({'security_groups':
 
932
                                                             [{'id': 0}],
 
933
                                                    'availability_zone': '',
 
934
                                                    'uuid': FAKEUUID})
1017
935
        db.fixed_ip_associate_pool(mox.IgnoreArg(),
1018
936
                                   mox.IgnoreArg(),
1019
937
                                   mox.IgnoreArg()).AndReturn('192.168.0.101')
1020
938
        db.network_get(mox.IgnoreArg(),
1021
939
                       mox.IgnoreArg(),
1022
940
                       project_only=mox.IgnoreArg()).AndReturn(networks[0])
1023
 
        db.instance_get_by_uuid(mox.IgnoreArg(),
1024
 
                mox.IgnoreArg()).AndReturn({'display_name': HOST})
1025
 
        self.network.get_instance_nw_info(mox.IgnoreArg(), mox.IgnoreArg(),
1026
 
                                          mox.IgnoreArg(), mox.IgnoreArg())
1027
941
        self.mox.ReplayAll()
1028
 
        self.network.add_fixed_ip_to_instance(self.context, FAKEUUID, HOST,
 
942
        self.network.add_fixed_ip_to_instance(self.context, 1, HOST,
1029
943
                                              networks[0]['id'])
1030
944
 
1031
945
    def test_ip_association_and_allocation_of_other_project(self):
1116
1030
        self.assertFalse(fixed['allocated'])
1117
1031
 
1118
1032
    def test_deallocate_fixed_deleted(self):
1119
 
        # Verify doesn't deallocate deleted fixed_ip from deleted network.
 
1033
        """Verify doesn't deallocate deleted fixed_ip from deleted network"""
1120
1034
 
1121
1035
        def network_get(_context, network_id, project_only="allow_none"):
1122
1036
            return networks[network_id]
1180
1094
        self.network.deallocate_fixed_ip(context1, fix_addr, 'fake')
1181
1095
 
1182
1096
    def test_fixed_ip_cleanup_fail(self):
1183
 
        # Verify IP is not deallocated if the security group refresh fails.
 
1097
        """Verify IP is not deallocated if the security group refresh fails."""
1184
1098
        def network_get(_context, network_id, project_only="allow_none"):
1185
1099
            return networks[network_id]
1186
1100
 
1241
1155
 
1242
1156
    def test_remove_fixed_ip_from_instance(self):
1243
1157
        manager = fake_network.FakeNetworkManager()
1244
 
        manager.remove_fixed_ip_from_instance(self.context, 99,
1245
 
                                              HOST,
 
1158
        manager.remove_fixed_ip_from_instance(self.context, 99, HOST,
1246
1159
                                              '10.0.0.1')
1247
1160
 
1248
1161
        self.assertEquals(manager.deallocate_called, '10.0.0.1')
1618
1531
        self.assertRaises(exception.NetworkNotFound,
1619
1532
                          manager.disassociate_network, fake_context, uuid)
1620
1533
 
1621
 
    def _test_init_host_static_fixed_range(self, net_manager):
1622
 
        self.flags(fake_network=True,
1623
 
                   fixed_range='10.0.0.0/22',
1624
 
                   routing_source_ip='192.168.0.1',
1625
 
                   metadata_host='192.168.0.1',
1626
 
                   public_interface='eth1',
1627
 
                   dmz_cidr=['10.0.3.0/24'])
1628
 
        binary_name = linux_net.get_binary_name()
1629
 
 
1630
 
        # Stub out calls we don't want to really run
1631
 
        self.stubs.Set(linux_net.iptables_manager, '_apply', lambda: None)
1632
 
        self.stubs.Set(floating_ips.FloatingIP, 'init_host_floating_ips',
1633
 
                                                lambda *args: None)
1634
 
 
1635
 
        # Call the network manager init code to configure the fixed_range
1636
 
        net_manager.init_host()
1637
 
 
1638
 
        # Get the iptables rules that got created
1639
 
        current_lines = []
1640
 
        new_lines = linux_net.iptables_manager._modify_rules(current_lines,
1641
 
                                       linux_net.iptables_manager.ipv4['nat'],
1642
 
                                       table_name='nat')
1643
 
 
1644
 
        # The expected rules that should be configured based on the fixed_range
1645
 
        expected_lines = ['[0:0] -A %s-snat -s %s -j SNAT --to-source %s -o %s'
1646
 
                          % (binary_name, CONF.fixed_range,
1647
 
                                          CONF.routing_source_ip,
1648
 
                                          CONF.public_interface),
1649
 
                          '[0:0] -A %s-POSTROUTING -s %s -d %s/32 -j ACCEPT'
1650
 
                          % (binary_name, CONF.fixed_range,
1651
 
                                          CONF.metadata_host),
1652
 
                          '[0:0] -A %s-POSTROUTING -s %s -d %s -j ACCEPT'
1653
 
                          % (binary_name, CONF.fixed_range, CONF.dmz_cidr[0]),
1654
 
                          '[0:0] -A %s-POSTROUTING -s %s -d %s -m conntrack ! '
1655
 
                          '--ctstate DNAT -j ACCEPT' % (binary_name,
1656
 
                                                        CONF.fixed_range,
1657
 
                                                        CONF.fixed_range)]
1658
 
 
1659
 
        # Finally, compare the expected rules against the actual ones
1660
 
        for line in expected_lines:
1661
 
            self.assertTrue(line in new_lines)
1662
 
 
1663
 
    def _test_init_host_dynamic_fixed_range(self, net_manager):
1664
 
        self.flags(fake_network=True,
1665
 
                   fixed_range='',
1666
 
                   routing_source_ip='172.16.0.1',
1667
 
                   metadata_host='172.16.0.1',
1668
 
                   public_interface='eth1',
1669
 
                   dmz_cidr=['10.0.3.0/24'])
1670
 
        binary_name = linux_net.get_binary_name()
1671
 
 
1672
 
        # Stub out calls we don't want to really run, mock the db
1673
 
        self.stubs.Set(linux_net.iptables_manager, '_apply', lambda: None)
1674
 
        self.stubs.Set(floating_ips.FloatingIP, 'init_host_floating_ips',
1675
 
                                                lambda *args: None)
1676
 
        self.stubs.Set(net_manager.l3driver, 'initialize_gateway',
1677
 
                                             lambda *args: None)
1678
 
        self.mox.StubOutWithMock(db, 'network_get_all_by_host')
1679
 
        db.network_get_all_by_host(mox.IgnoreArg(),
1680
 
                        mox.IgnoreArg()).MultipleTimes().AndReturn(networks)
1681
 
        self.mox.ReplayAll()
1682
 
 
1683
 
        # Call the network manager init code to configure the fixed_range
1684
 
        net_manager.init_host()
1685
 
 
1686
 
        # Get the iptables rules that got created
1687
 
        current_lines = []
1688
 
        new_lines = linux_net.iptables_manager._modify_rules(current_lines,
1689
 
                                       linux_net.iptables_manager.ipv4['nat'],
1690
 
                                       table_name='nat')
1691
 
 
1692
 
        # The expected rules that should be configured based on the fixed_range
1693
 
        expected_lines = ['[0:0] -A %s-snat -s %s -j SNAT --to-source %s -o %s'
1694
 
                          % (binary_name, networks[0]['cidr'],
1695
 
                                          CONF.routing_source_ip,
1696
 
                                          CONF.public_interface),
1697
 
                          '[0:0] -A %s-POSTROUTING -s %s -d %s/32 -j ACCEPT'
1698
 
                          % (binary_name, networks[0]['cidr'],
1699
 
                                          CONF.metadata_host),
1700
 
                          '[0:0] -A %s-POSTROUTING -s %s -d %s -j ACCEPT'
1701
 
                          % (binary_name, networks[0]['cidr'],
1702
 
                                          CONF.dmz_cidr[0]),
1703
 
                          '[0:0] -A %s-POSTROUTING -s %s -d %s -m conntrack ! '
1704
 
                          '--ctstate DNAT -j ACCEPT' % (binary_name,
1705
 
                                                        networks[0]['cidr'],
1706
 
                                                        networks[0]['cidr']),
1707
 
                          '[0:0] -A %s-snat -s %s -j SNAT --to-source %s -o %s'
1708
 
                          % (binary_name, networks[1]['cidr'],
1709
 
                                          CONF.routing_source_ip,
1710
 
                                          CONF.public_interface),
1711
 
                          '[0:0] -A %s-POSTROUTING -s %s -d %s/32 -j ACCEPT'
1712
 
                          % (binary_name, networks[1]['cidr'],
1713
 
                                          CONF.metadata_host),
1714
 
                          '[0:0] -A %s-POSTROUTING -s %s -d %s -j ACCEPT'
1715
 
                          % (binary_name, networks[1]['cidr'],
1716
 
                                          CONF.dmz_cidr[0]),
1717
 
                          '[0:0] -A %s-POSTROUTING -s %s -d %s -m conntrack ! '
1718
 
                          '--ctstate DNAT -j ACCEPT' % (binary_name,
1719
 
                                                        networks[1]['cidr'],
1720
 
                                                        networks[1]['cidr'])]
1721
 
 
1722
 
        # Compare the expected rules against the actual ones
1723
 
        for line in expected_lines:
1724
 
            self.assertTrue(line in new_lines)
1725
 
 
1726
 
        # Add an additional network and ensure the rules get configured
1727
 
        new_network = {'id': 2,
1728
 
                       'uuid': 'cccccccc-cccc-cccc-cccc-cccccccc',
1729
 
                       'label': 'test2',
1730
 
                       'injected': False,
1731
 
                       'multi_host': False,
1732
 
                       'cidr': '192.168.2.0/24',
1733
 
                       'cidr_v6': '2001:dba::/64',
1734
 
                       'gateway_v6': '2001:dba::1',
1735
 
                       'netmask_v6': '64',
1736
 
                       'netmask': '255.255.255.0',
1737
 
                       'bridge': 'fa1',
1738
 
                       'bridge_interface': 'fake_fa1',
1739
 
                       'gateway': '192.168.2.1',
1740
 
                       'broadcast': '192.168.2.255',
1741
 
                       'dns1': '192.168.2.1',
1742
 
                       'dns2': '192.168.2.2',
1743
 
                       'vlan': None,
1744
 
                       'host': HOST,
1745
 
                       'project_id': 'fake_project',
1746
 
                       'vpn_public_address': '192.168.2.2',
1747
 
                       'vpn_public_port': '22',
1748
 
                       'vpn_private_address': '10.0.0.2'}
1749
 
 
1750
 
        # Call the network manager init code to configure the fixed_range
1751
 
        ctxt = context.get_admin_context()
1752
 
        net_manager._setup_network_on_host(ctxt, new_network)
1753
 
 
1754
 
        # Get the new iptables rules that got created from adding a new network
1755
 
        current_lines = []
1756
 
        new_lines = linux_net.iptables_manager._modify_rules(current_lines,
1757
 
                                       linux_net.iptables_manager.ipv4['nat'],
1758
 
                                       table_name='nat')
1759
 
 
1760
 
        # Add the new expected rules to the old ones
1761
 
        expected_lines += ['[0:0] -A %s-snat -s %s -j SNAT --to-source %s -o '
1762
 
                           '%s' % (binary_name, new_network['cidr'],
1763
 
                                                CONF.routing_source_ip,
1764
 
                                                CONF.public_interface),
1765
 
                           '[0:0] -A %s-POSTROUTING -s %s -d %s/32 -j ACCEPT'
1766
 
                           % (binary_name, new_network['cidr'],
1767
 
                                           CONF.metadata_host),
1768
 
                           '[0:0] -A %s-POSTROUTING -s %s -d %s -j ACCEPT'
1769
 
                           % (binary_name, new_network['cidr'],
1770
 
                                           CONF.dmz_cidr[0]),
1771
 
                           '[0:0] -A %s-POSTROUTING -s %s -d %s -m conntrack '
1772
 
                           '! --ctstate DNAT -j ACCEPT' % (binary_name,
1773
 
                                                       new_network['cidr'],
1774
 
                                                       new_network['cidr'])]
1775
 
 
1776
 
        # Compare the expected rules (with new network) against the actual ones
1777
 
        for line in expected_lines:
1778
 
            self.assertTrue(line in new_lines)
1779
 
 
1780
 
    def test_flatdhcpmanager_static_fixed_range(self):
1781
 
        """Test FlatDHCPManager NAT rules for fixed_range."""
1782
 
        # Set the network manager
1783
 
        self.network = network_manager.FlatDHCPManager(host=HOST)
1784
 
        self.network.db = db
1785
 
 
1786
 
        # Test existing behavior:
1787
 
        #     CONF.fixed_range is set, NAT based on CONF.fixed_range
1788
 
        self._test_init_host_static_fixed_range(self.network)
1789
 
 
1790
 
    def test_flatdhcpmanager_dynamic_fixed_range(self):
1791
 
        """Test FlatDHCPManager NAT rules for fixed_range."""
1792
 
        # Set the network manager
1793
 
        self.network = network_manager.FlatDHCPManager(host=HOST)
1794
 
        self.network.db = db
1795
 
 
1796
 
        # Test new behavior:
1797
 
        #     CONF.fixed_range is not set, defaults to None
1798
 
        #     Determine networks to NAT based on lookup
1799
 
        self._test_init_host_dynamic_fixed_range(self.network)
1800
 
 
1801
 
    def test_vlanmanager_static_fixed_range(self):
1802
 
        """Test VlanManager NAT rules for fixed_range."""
1803
 
        # Set the network manager
1804
 
        self.network = network_manager.VlanManager(host=HOST)
1805
 
        self.network.db = db
1806
 
 
1807
 
        # Test existing behavior:
1808
 
        #     CONF.fixed_range is set, NAT based on CONF.fixed_range
1809
 
        self._test_init_host_static_fixed_range(self.network)
1810
 
 
1811
 
    def test_vlanmanager_dynamic_fixed_range(self):
1812
 
        """Test VlanManager NAT rules for fixed_range."""
1813
 
        # Set the network manager
1814
 
        self.network = network_manager.VlanManager(host=HOST)
1815
 
        self.network.db = db
1816
 
 
1817
 
        # Test new behavior:
1818
 
        #     CONF.fixed_range is not set, defaults to None
1819
 
        #     Determine networks to NAT based on lookup
1820
 
        self._test_init_host_dynamic_fixed_range(self.network)
1821
 
 
1822
1534
 
1823
1535
class TestRPCFixedManager(network_manager.RPCAllocateFixedIP,
1824
1536
        network_manager.NetworkManager):
1825
 
    """Dummy manager that implements RPCAllocateFixedIP."""
 
1537
    """Dummy manager that implements RPCAllocateFixedIP"""
1826
1538
 
1827
1539
 
1828
1540
class RPCAllocateTestCase(test.TestCase):
1829
 
    """Tests nova.network.manager.RPCAllocateFixedIP."""
 
1541
    """Tests nova.network.manager.RPCAllocateFixedIP"""
1830
1542
    def setUp(self):
1831
1543
        super(RPCAllocateTestCase, self).setUp()
1832
1544
        self.rpc_fixed = TestRPCFixedManager()
1854
1566
 
1855
1567
 
1856
1568
class BackdoorPortTestCase(test.TestCase):
1857
 
    """Tests nova.network.manager.get_backdoor_port."""
 
1569
    """Tests nova.network.manager.get_backdoor_port"""
1858
1570
    def setUp(self):
1859
1571
        super(BackdoorPortTestCase, self).setUp()
1860
1572
        self.manager = network_manager.NetworkManager()
1866
1578
        self.assertEqual(port, self.manager.backdoor_port)
1867
1579
 
1868
1580
 
1869
 
class TestFloatingIPManager(floating_ips.FloatingIP,
 
1581
class TestFloatingIPManager(network_manager.FloatingIP,
1870
1582
        network_manager.NetworkManager):
1871
 
    """Dummy manager that implements FloatingIP."""
 
1583
    """Dummy manager that implements FloatingIP"""
1872
1584
 
1873
1585
 
1874
1586
class AllocateTestCase(test.TestCase):
1875
 
    def setUp(self):
1876
 
        super(AllocateTestCase, self).setUp()
 
1587
    def test_allocate_for_instance(self):
 
1588
        address = "10.10.10.10"
 
1589
        self.flags(auto_assign_floating_ip=True)
1877
1590
        self.conductor = self.start_service(
1878
1591
            'conductor', manager=CONF.conductor.manager)
1879
1592
        self.compute = self.start_service('compute')
1885
1598
                                              self.project_id,
1886
1599
                                              is_admin=True)
1887
1600
 
1888
 
    def test_allocate_for_instance(self):
1889
 
        address = "10.10.10.10"
1890
 
        self.flags(auto_assign_floating_ip=True)
1891
 
 
1892
1601
        db.floating_ip_create(self.context,
1893
1602
                              {'address': address,
1894
1603
                               'pool': 'nova'})
1903
1612
        nw_info = self.network.allocate_for_instance(self.context,
1904
1613
            instance_id=inst['id'], instance_uuid=inst['uuid'],
1905
1614
            host=inst['host'], vpn=None, rxtx_factor=3,
1906
 
            project_id=project_id, macs=None)
 
1615
            project_id=project_id)
1907
1616
        self.assertEquals(1, len(nw_info))
1908
1617
        fixed_ip = nw_info.fixed_ips()[0]['address']
1909
1618
        self.assertTrue(utils.is_valid_ipv4(fixed_ip))
1913
1622
                                             host=self.network.host,
1914
1623
                                             project_id=project_id)
1915
1624
 
1916
 
    def test_allocate_for_instance_with_mac(self):
1917
 
        available_macs = set(['ca:fe:de:ad:be:ef'])
1918
 
        inst = db.instance_create(self.context, {'host': self.compute.host,
1919
 
                                                 'display_name': HOST,
1920
 
                                                 'instance_type_id': 1})
1921
 
        networks = db.network_get_all(self.context)
1922
 
        for network in networks:
1923
 
            db.network_update(self.context, network['id'],
1924
 
                              {'host': self.network.host})
1925
 
        project_id = self.context.project_id
1926
 
        nw_info = self.network.allocate_for_instance(self.context,
1927
 
            instance_id=inst['id'], instance_uuid=inst['uuid'],
1928
 
            host=inst['host'], vpn=None, rxtx_factor=3,
1929
 
            project_id=project_id, macs=available_macs)
1930
 
        assigned_macs = [vif['address'] for vif in nw_info]
1931
 
        self.assertEquals(1, len(assigned_macs))
1932
 
        self.assertEquals(available_macs.pop(), assigned_macs[0])
1933
 
        self.network.deallocate_for_instance(self.context,
1934
 
                                             instance_id=inst['id'],
1935
 
                                             host=self.network.host,
1936
 
                                             project_id=project_id)
1937
 
 
1938
 
    def test_allocate_for_instance_not_enough_macs(self):
1939
 
        available_macs = set()
1940
 
        inst = db.instance_create(self.context, {'host': self.compute.host,
1941
 
                                                 'display_name': HOST,
1942
 
                                                 'instance_type_id': 1})
1943
 
        networks = db.network_get_all(self.context)
1944
 
        for network in networks:
1945
 
            db.network_update(self.context, network['id'],
1946
 
                              {'host': self.network.host})
1947
 
        project_id = self.context.project_id
1948
 
        self.assertRaises(exception.VirtualInterfaceCreateException,
1949
 
                          self.network.allocate_for_instance, self.context,
1950
 
                          instance_id=inst['id'], instance_uuid=inst['uuid'],
1951
 
                          host=inst['host'], vpn=None, rxtx_factor=3,
1952
 
                          project_id=project_id, macs=available_macs)
1953
 
 
1954
1625
 
1955
1626
class FloatingIPTestCase(test.TestCase):
1956
 
    """Tests nova.network.manager.FloatingIP."""
 
1627
    """Tests nova.network.manager.FloatingIP"""
1957
1628
    def setUp(self):
1958
1629
        super(FloatingIPTestCase, self).setUp()
1959
 
        self.tempdir = self.useFixture(fixtures.TempDir()).path
 
1630
        self.tempdir = tempfile.mkdtemp()
1960
1631
        self.flags(log_dir=self.tempdir)
1961
1632
        self.network = TestFloatingIPManager()
1962
1633
        self.network.db = db
1964
1635
        self.context = context.RequestContext('testuser', self.project_id,
1965
1636
            is_admin=False)
1966
1637
 
 
1638
    def tearDown(self):
 
1639
        shutil.rmtree(self.tempdir)
 
1640
        super(FloatingIPTestCase, self).tearDown()
 
1641
 
1967
1642
    def test_disassociate_floating_ip_multi_host_calls(self):
1968
1643
        floating_ip = {
1969
1644
            'fixed_ip_id': 12
1997
1672
                       'fixed_ip_get',
1998
1673
                       lambda _x, _y: fixed_ip)
1999
1674
 
2000
 
        self.stubs.Set(self.network.db,
2001
 
                       'network_get',
 
1675
        self.stubs.Set(self.network,
 
1676
                       '_get_network_by_id',
2002
1677
                       lambda _x, _y: network)
2003
1678
 
2004
1679
        self.stubs.Set(self.network.db,
2055
1730
                       'fixed_ip_get_by_address',
2056
1731
                       lambda _x, _y: fixed_ip)
2057
1732
 
2058
 
        self.stubs.Set(self.network.db,
2059
 
                       'network_get',
 
1733
        self.stubs.Set(self.network,
 
1734
                       '_get_network_by_id',
2060
1735
                       lambda _x, _y: network)
2061
1736
 
2062
1737
        self.stubs.Set(self.network.db,
2101
1776
                'fixed_ip_id': fixed['id'],
2102
1777
                'project_id': self.project_id})
2103
1778
        self.network.deallocate_for_instance(self.context,
2104
 
                instance_id=instance['uuid'])
 
1779
                instance_id=instance['id'])
2105
1780
 
2106
1781
    def test_deallocation_duplicate_floating_ip(self):
2107
1782
        self.stubs.Set(self.network, '_teardown_network_on_host',
2123
1798
                'fixed_ip_id': fixed['id'],
2124
1799
                'project_id': self.project_id})
2125
1800
        self.network.deallocate_for_instance(self.context,
2126
 
                instance_id=instance['uuid'])
 
1801
                instance_id=instance['id'])
2127
1802
 
2128
1803
    def test_migrate_instance_start(self):
2129
1804
        called = {'count': 0}
2135
1810
        def fake_is_stale_floating_ip_address(context, floating_ip):
2136
1811
            return floating_ip['address'] == '172.24.4.23'
2137
1812
 
2138
 
        def fake_fixed_ip_get(context, fixed_ip_id, get_network):
 
1813
        def fake_fixed_ip_get(context, fixed_ip_id):
2139
1814
            return {'instance_uuid': 'fake_uuid',
2140
 
                    'address': '10.0.0.2',
2141
 
                    'network': 'fakenet'}
 
1815
                    'address': '10.0.0.2'}
2142
1816
 
2143
 
        def fake_remove_floating_ip(floating_addr, fixed_addr, interface,
2144
 
                                    network):
 
1817
        def fake_remove_floating_ip(floating_addr, fixed_addr, interface):
2145
1818
            called['count'] += 1
2146
1819
 
2147
1820
        def fake_floating_ip_update(context, address, args):
2178
1851
        def fake_is_stale_floating_ip_address(context, floating_ip):
2179
1852
            return floating_ip['address'] == '172.24.4.23'
2180
1853
 
2181
 
        def fake_fixed_ip_get(context, fixed_ip_id, get_network):
 
1854
        def fake_fixed_ip_get(context, fixed_ip_id):
2182
1855
            return {'instance_uuid': 'fake_uuid',
2183
 
                    'address': '10.0.0.2',
2184
 
                    'network': 'fakenet'}
 
1856
                    'address': '10.0.0.2'}
2185
1857
 
2186
 
        def fake_add_floating_ip(floating_addr, fixed_addr, interface,
2187
 
                                 network):
 
1858
        def fake_add_floating_ip(floating_addr, fixed_addr, interface):
2188
1859
            called['count'] += 1
2189
1860
 
2190
1861
        def fake_floating_ip_update(context, address, args):
2352
2023
        self.network.delete_dns_domain(context_admin, domain2)
2353
2024
 
2354
2025
    def test_mac_conflicts(self):
2355
 
        # Make sure MAC collisions are retried.
 
2026
        """Make sure MAC collisions are retried"""
2356
2027
        self.flags(create_unique_mac_address_attempts=3)
2357
2028
        ctxt = context.RequestContext('testuser', 'testproject', is_admin=True)
2358
2029
        macs = ['bb:bb:bb:bb:bb:bb', 'aa:aa:aa:aa:aa:aa']
2375
2046
        # address column, so fake the collision-avoidance here
2376
2047
        def fake_vif_save(vif):
2377
2048
            if vif.address == crash_test_dummy_vif['address']:
2378
 
                raise db_exc.DBError("If you're smart, you'll retry!")
 
2049
                raise exception.DBError("If you're smart, you'll retry!")
2379
2050
        self.stubs.Set(models.VirtualInterface, 'save', fake_vif_save)
2380
2051
 
2381
2052
        # Attempt to add another and make sure that both MACs are consumed
2382
2053
        # by the retry loop
2383
 
        self.network._add_virtual_interface(ctxt, 'fake_uuid', 'fake_net')
 
2054
        self.network.add_virtual_interface(ctxt, 'fake_uuid', 'fake_net')
2384
2055
        self.assertEqual(macs, [])
2385
2056
 
2386
2057
    def test_deallocate_client_exceptions(self):
2387
 
        # Ensure that FloatingIpNotFoundForAddress is wrapped.
 
2058
        """Ensure that FloatingIpNotFoundForAddress is wrapped"""
2388
2059
        self.mox.StubOutWithMock(self.network.db, 'floating_ip_get_by_address')
2389
2060
        self.network.db.floating_ip_get_by_address(
2390
2061
            self.context, '1.2.3.4').AndRaise(
2395
2066
                          self.context, '1.2.3.4')
2396
2067
 
2397
2068
    def test_associate_client_exceptions(self):
2398
 
        # Ensure that FloatingIpNotFoundForAddress is wrapped.
 
2069
        """Ensure that FloatingIpNotFoundForAddress is wrapped"""
2399
2070
        self.mox.StubOutWithMock(self.network.db, 'floating_ip_get_by_address')
2400
2071
        self.network.db.floating_ip_get_by_address(
2401
2072
            self.context, '1.2.3.4').AndRaise(
2406
2077
                          self.context, '1.2.3.4', '10.0.0.1')
2407
2078
 
2408
2079
    def test_disassociate_client_exceptions(self):
2409
 
        # Ensure that FloatingIpNotFoundForAddress is wrapped.
 
2080
        """Ensure that FloatingIpNotFoundForAddress is wrapped"""
2410
2081
        self.mox.StubOutWithMock(self.network.db, 'floating_ip_get_by_address')
2411
2082
        self.network.db.floating_ip_get_by_address(
2412
2083
            self.context, '1.2.3.4').AndRaise(
2417
2088
                          self.context, '1.2.3.4')
2418
2089
 
2419
2090
    def test_get_floating_ip_client_exceptions(self):
2420
 
        # Ensure that FloatingIpNotFoundForAddress is wrapped.
 
2091
        """Ensure that FloatingIpNotFoundForAddress is wrapped"""
2421
2092
        self.mox.StubOutWithMock(self.network.db, 'floating_ip_get')
2422
2093
        self.network.db.floating_ip_get(self.context, 'fake-id').AndRaise(
2423
2094
            exception.FloatingIpNotFound(id='fake'))
2427
2098
                          self.context, 'fake-id')
2428
2099
 
2429
2100
 
 
2101
class NetworkPolicyTestCase(test.TestCase):
 
2102
    def setUp(self):
 
2103
        super(NetworkPolicyTestCase, self).setUp()
 
2104
 
 
2105
        nova.policy.reset()
 
2106
        nova.policy.init()
 
2107
 
 
2108
        self.context = context.get_admin_context()
 
2109
 
 
2110
    def tearDown(self):
 
2111
        super(NetworkPolicyTestCase, self).tearDown()
 
2112
        nova.policy.reset()
 
2113
 
 
2114
    def test_check_policy(self):
 
2115
        self.mox.StubOutWithMock(nova.policy, 'enforce')
 
2116
        target = {
 
2117
            'project_id': self.context.project_id,
 
2118
            'user_id': self.context.user_id,
 
2119
        }
 
2120
        nova.policy.enforce(self.context, 'network:get_all', target)
 
2121
        self.mox.ReplayAll()
 
2122
        network_manager.check_policy(self.context, 'get_all')
 
2123
 
 
2124
 
2430
2125
class InstanceDNSTestCase(test.TestCase):
2431
 
    """Tests nova.network.manager instance DNS."""
 
2126
    """Tests nova.network.manager instance DNS"""
2432
2127
    def setUp(self):
2433
2128
        super(InstanceDNSTestCase, self).setUp()
2434
 
        self.tempdir = self.useFixture(fixtures.TempDir()).path
 
2129
        self.tempdir = tempfile.mkdtemp()
2435
2130
        self.flags(log_dir=self.tempdir)
2436
2131
        self.network = TestFloatingIPManager()
2437
2132
        self.network.db = db
2439
2134
        self.context = context.RequestContext('testuser', self.project_id,
2440
2135
            is_admin=False)
2441
2136
 
 
2137
    def tearDown(self):
 
2138
        shutil.rmtree(self.tempdir)
 
2139
        super(InstanceDNSTestCase, self).tearDown()
 
2140
 
2442
2141
    def test_dns_domains_private(self):
2443
2142
        zone1 = 'testzone'
2444
2143
        domain1 = 'example.org'
2467
2166
 
2468
2167
 
2469
2168
class LdapDNSTestCase(test.TestCase):
2470
 
    """Tests nova.network.ldapdns.LdapDNS."""
 
2169
    """Tests nova.network.ldapdns.LdapDNS"""
2471
2170
    def setUp(self):
2472
2171
        super(LdapDNSTestCase, self).setUp()
2473
2172