~hudson-openstack/nova/trunk

« back to all changes in this revision

Viewing changes to nova/tests/test_network.py

  • Committer: Tarmac
  • Author(s): Rick Harris
  • Date: 2011-09-21 22:14:15 UTC
  • mfrom: (1561.2.14 server_progress)
  • Revision ID: tarmac-20110921221415-xv5njvvv03wlsksh
This patch adds instance progress which is used by the OpenStack API to indicate how far along the current executing action is (BUILD/REBUILD, MIGRATION/RESIZE).

For the first cut, we decided to keep it simple and compute progress by counting discrete steps. This is not ideal since some steps, in particular, steps which involve transferring large amounts of data over the network, take *much* longer than others. A better approximation would account for the data-transferred to the destination host, since in most cases, this dominates the time spent.

In addition to adding progress, this patch:

- Allows resizes to use same host for source and destination which is useful for dev environments without a second host. This is enabled by the --allow_resize_to_same_host flag.

- Fixes a bug in the glance and migration XenAPI plugins where the VHDs were being copied into the SR in the wrong order. Before the base-copy was copied first meaning it was possible for snapwatchd to see the base-copy before the dependent cow was present. It was treat the base_copy as an unreferenced parent, and GC it.

- Additional refactoring and cleanups.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
15
# License for the specific language governing permissions and limitations
16
16
# under the License.
 
17
import mox
17
18
 
18
19
from nova import context
19
20
from nova import db
21
22
from nova import log as logging
22
23
from nova import test
23
24
from nova.network import manager as network_manager
24
 
 
25
 
 
26
 
import mox
 
25
from nova.tests import fake_network
27
26
 
28
27
 
29
28
LOG = logging.getLogger('nova.tests.network')
58
57
             'dns1': '192.168.0.1',
59
58
             'dns2': '192.168.0.2',
60
59
             'vlan': None,
61
 
             'host': None,
 
60
             'host': HOST,
62
61
             'project_id': 'fake_project',
63
62
             'vpn_public_address': '192.168.0.2'},
64
63
            {'id': 1,
78
77
             'dns1': '192.168.0.1',
79
78
             'dns2': '192.168.0.2',
80
79
             'vlan': None,
81
 
             'host': None,
 
80
             'host': HOST,
82
81
             'project_id': 'fake_project',
83
82
             'vpn_public_address': '192.168.1.2'}]
84
83
 
118
117
        {'id': 1,
119
118
         'address': 'DE:AD:BE:EF:00:01',
120
119
         'uuid': '00000000-0000-0000-0000-0000000000000001',
121
 
         'network_id': 0,
122
120
         'network_id': 1,
123
121
         'network': FakeModel(**networks[1]),
 
122
         'instance_id': 0},
 
123
        {'id': 2,
 
124
         'address': 'DE:AD:BE:EF:00:02',
 
125
         'uuid': '00000000-0000-0000-0000-0000000000000002',
 
126
         'network_id': 2,
 
127
         'network': None,
124
128
         'instance_id': 0}]
125
129
 
126
130
 
133
137
                                              is_admin=False)
134
138
 
135
139
    def test_get_instance_nw_info(self):
136
 
        self.mox.StubOutWithMock(db, 'fixed_ip_get_by_instance')
137
 
        self.mox.StubOutWithMock(db, 'virtual_interface_get_by_instance')
138
 
        self.mox.StubOutWithMock(db, 'instance_type_get')
139
 
 
140
 
        db.fixed_ip_get_by_instance(mox.IgnoreArg(),
141
 
                                    mox.IgnoreArg()).AndReturn(fixed_ips)
142
 
        db.virtual_interface_get_by_instance(mox.IgnoreArg(),
143
 
                                             mox.IgnoreArg()).AndReturn(vifs)
144
 
        db.instance_type_get(mox.IgnoreArg(),
145
 
                                   mox.IgnoreArg()).AndReturn(flavor)
146
 
        self.mox.ReplayAll()
147
 
 
148
 
        nw_info = self.network.get_instance_nw_info(None, 0, 0, None)
149
 
 
150
 
        self.assertTrue(nw_info)
151
 
 
152
 
        for i, nw in enumerate(nw_info):
153
 
            i8 = i + 8
154
 
            check = {'bridge': 'fa%s' % i,
 
140
        fake_get_instance_nw_info = fake_network.fake_get_instance_nw_info
 
141
 
 
142
        nw_info = fake_get_instance_nw_info(self.stubs, 0, 2)
 
143
        self.assertFalse(nw_info)
 
144
 
 
145
        for i, (nw, info) in enumerate(nw_info):
 
146
            check = {'bridge': 'fake_br%d' % i,
155
147
                     'cidr': '192.168.%s.0/24' % i,
156
 
                     'cidr_v6': '2001:db%s::/64' % i8,
 
148
                     'cidr_v6': '2001:db8:0:%x::/64' % i,
157
149
                     'id': i,
158
150
                     'multi_host': False,
159
 
                     'injected': 'DONTCARE',
160
 
                     'bridge_interface': 'fake_fa%s' % i,
 
151
                     'injected': False,
 
152
                     'bridge_interface': 'fake_eth%d' % i,
161
153
                     'vlan': None}
162
154
 
163
 
            self.assertDictMatch(nw[0], check)
 
155
            self.assertDictMatch(nw, check)
164
156
 
165
 
            check = {'broadcast': '192.168.%s.255' % i,
166
 
                     'dhcp_server': '192.168.%s.1' % i,
167
 
                     'dns': 'DONTCARE',
168
 
                     'gateway': '192.168.%s.1' % i,
169
 
                     'gateway6': '2001:db%s::1' % i8,
 
157
            check = {'broadcast': '192.168.%d.255' % i,
 
158
                     'dhcp_server': '192.168.%d.1' % i,
 
159
                     'dns': ['192.168.%d.3' % n, '192.168.%d.4' % n],
 
160
                     'gateway': '192.168.%d.1' % i,
 
161
                     'gateway6': '2001:db8:0:%x::1' % i,
170
162
                     'ip6s': 'DONTCARE',
171
163
                     'ips': 'DONTCARE',
172
 
                     'label': 'test%s' % i,
173
 
                     'mac': 'DE:AD:BE:EF:00:0%s' % i,
174
 
                     'vif_uuid': ('00000000-0000-0000-0000-000000000000000%s' %
175
 
                                  i),
176
 
                     'rxtx_cap': 'DONTCARE',
 
164
                     'label': 'test%d' % i,
 
165
                     'mac': 'DE:AD:BE:EF:00:%02x' % i,
 
166
                     'vif_uuid':
 
167
                        '00000000-0000-0000-0000-00000000000000%02d' % i,
 
168
                     'rxtx_cap': 3,
177
169
                     'should_create_vlan': False,
178
170
                     'should_create_bridge': False}
179
 
            self.assertDictMatch(nw[1], check)
 
171
            self.assertDictMatch(info, check)
180
172
 
181
173
            check = [{'enabled': 'DONTCARE',
182
 
                      'ip': '2001:db%s::dcad:beff:feef:%s' % (i8, i),
 
174
                      'ip': '2001:db8::dcad:beff:feef:%s' % i,
183
175
                      'netmask': '64'}]
184
 
            self.assertDictListMatch(nw[1]['ip6s'], check)
 
176
            self.assertDictListMatch(info['ip6s'], check)
185
177
 
186
 
            check = [{'enabled': '1',
187
 
                      'ip': '192.168.%s.100' % i,
188
 
                      'netmask': '255.255.255.0'}]
189
 
            self.assertDictListMatch(nw[1]['ips'], check)
 
178
            num_fixed_ips = len(info['ips'])
 
179
            check = [{'enabled': 'DONTCARE',
 
180
                      'ip': '192.168.%d.1%02d' % (i, ip_num),
 
181
                      'netmask': '255.255.255.0'}
 
182
                      for ip_num in xrange(num_fixed_ips)]
 
183
            self.assertDictListMatch(info['ips'], check)
190
184
 
191
185
    def test_validate_networks(self):
192
186
        self.mox.StubOutWithMock(db, 'network_get_all_by_uuids')
247
241
 
248
242
        self.network.validate_networks(None, requested_networks)
249
243
 
 
244
    def test_add_fixed_ip_instance_without_vpn_requested_networks(self):
 
245
        self.mox.StubOutWithMock(db, 'network_get')
 
246
        self.mox.StubOutWithMock(db, 'network_update')
 
247
        self.mox.StubOutWithMock(db, 'fixed_ip_associate_pool')
 
248
        self.mox.StubOutWithMock(db, 'instance_get')
 
249
        self.mox.StubOutWithMock(db,
 
250
                              'virtual_interface_get_by_instance_and_network')
 
251
        self.mox.StubOutWithMock(db, 'fixed_ip_update')
 
252
 
 
253
        db.fixed_ip_update(mox.IgnoreArg(),
 
254
                           mox.IgnoreArg(),
 
255
                           mox.IgnoreArg())
 
256
        db.virtual_interface_get_by_instance_and_network(mox.IgnoreArg(),
 
257
                mox.IgnoreArg(), mox.IgnoreArg()).AndReturn({'id': 0})
 
258
 
 
259
        db.instance_get(mox.IgnoreArg(),
 
260
                        mox.IgnoreArg()).AndReturn({'security_groups':
 
261
                                                             [{'id': 0}]})
 
262
        db.fixed_ip_associate_pool(mox.IgnoreArg(),
 
263
                                   mox.IgnoreArg(),
 
264
                                   mox.IgnoreArg()).AndReturn('192.168.0.101')
 
265
        db.network_get(mox.IgnoreArg(),
 
266
                       mox.IgnoreArg()).AndReturn(networks[0])
 
267
        db.network_update(mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg())
 
268
        self.mox.ReplayAll()
 
269
        self.network.add_fixed_ip_to_instance(self.context, 1, HOST,
 
270
                                              networks[0]['id'])
 
271
 
250
272
 
251
273
class VlanNetworkTestCase(test.TestCase):
252
274
    def setUp(self):
264
286
 
265
287
        db.fixed_ip_associate(mox.IgnoreArg(),
266
288
                              mox.IgnoreArg(),
267
 
                              mox.IgnoreArg()).AndReturn('192.168.0.1')
 
289
                              mox.IgnoreArg(),
 
290
                              reserved=True).AndReturn('192.168.0.1')
268
291
        db.fixed_ip_update(mox.IgnoreArg(),
269
292
                           mox.IgnoreArg(),
270
293
                           mox.IgnoreArg())
371
394
        self.mox.ReplayAll()
372
395
        self.network.validate_networks(self.context, requested_networks)
373
396
 
 
397
    def test_cant_associate_associated_floating_ip(self):
 
398
        ctxt = context.RequestContext('testuser', 'testproject',
 
399
                                      is_admin=False)
 
400
 
 
401
        def fake_floating_ip_get_by_address(context, address):
 
402
            return {'address': '10.10.10.10',
 
403
                    'fixed_ip': {'address': '10.0.0.1'}}
 
404
        self.stubs.Set(self.network.db, 'floating_ip_get_by_address',
 
405
                                fake_floating_ip_get_by_address)
 
406
 
 
407
        self.assertRaises(exception.FloatingIpAlreadyInUse,
 
408
                          self.network.associate_floating_ip,
 
409
                          ctxt,
 
410
                          mox.IgnoreArg(),
 
411
                          mox.IgnoreArg())
 
412
 
 
413
    def test_add_fixed_ip_instance_without_vpn_requested_networks(self):
 
414
        self.mox.StubOutWithMock(db, 'network_get')
 
415
        self.mox.StubOutWithMock(db, 'fixed_ip_associate_pool')
 
416
        self.mox.StubOutWithMock(db, 'instance_get')
 
417
        self.mox.StubOutWithMock(db,
 
418
                              'virtual_interface_get_by_instance_and_network')
 
419
        self.mox.StubOutWithMock(db, 'fixed_ip_update')
 
420
 
 
421
        db.fixed_ip_update(mox.IgnoreArg(),
 
422
                           mox.IgnoreArg(),
 
423
                           mox.IgnoreArg())
 
424
        db.virtual_interface_get_by_instance_and_network(mox.IgnoreArg(),
 
425
                mox.IgnoreArg(), mox.IgnoreArg()).AndReturn({'id': 0})
 
426
 
 
427
        db.instance_get(mox.IgnoreArg(),
 
428
                        mox.IgnoreArg()).AndReturn({'security_groups':
 
429
                                                             [{'id': 0}]})
 
430
        db.fixed_ip_associate_pool(mox.IgnoreArg(),
 
431
                                   mox.IgnoreArg(),
 
432
                                   mox.IgnoreArg()).AndReturn('192.168.0.101')
 
433
        db.network_get(mox.IgnoreArg(),
 
434
                       mox.IgnoreArg()).AndReturn(networks[0])
 
435
        self.mox.ReplayAll()
 
436
        self.network.add_fixed_ip_to_instance(self.context, 1, HOST,
 
437
                                              networks[0]['id'])
 
438
 
 
439
    def test_ip_association_and_allocation_of_other_project(self):
 
440
        """Makes sure that we cannot deallocaate or disassociate
 
441
        a public ip of other project"""
 
442
 
 
443
        context1 = context.RequestContext('user', 'project1')
 
444
        context2 = context.RequestContext('user', 'project2')
 
445
 
 
446
        address = '1.2.3.4'
 
447
        float_addr = db.floating_ip_create(context1.elevated(),
 
448
                {'address': address,
 
449
                 'project_id': context1.project_id})
 
450
 
 
451
        instance = db.instance_create(context1,
 
452
                {'project_id': 'project1'})
 
453
 
 
454
        fix_addr = db.fixed_ip_associate_pool(context1.elevated(),
 
455
                1, instance['id'])
 
456
 
 
457
        # Associate the IP with non-admin user context
 
458
        self.assertRaises(exception.NotAuthorized,
 
459
                          self.network.associate_floating_ip,
 
460
                          context2,
 
461
                          float_addr,
 
462
                          fix_addr)
 
463
 
 
464
        # Deallocate address from other project
 
465
        self.assertRaises(exception.NotAuthorized,
 
466
                          self.network.deallocate_floating_ip,
 
467
                          context2,
 
468
                          float_addr)
 
469
 
 
470
        # Now Associates the address to the actual project
 
471
        self.network.associate_floating_ip(context1, float_addr, fix_addr)
 
472
 
 
473
        # Now try dis-associating from other project
 
474
        self.assertRaises(exception.NotAuthorized,
 
475
                          self.network.disassociate_floating_ip,
 
476
                          context2,
 
477
                          float_addr)
 
478
 
 
479
        # Clean up the ip addresses
 
480
        self.network.deallocate_floating_ip(context1, float_addr)
 
481
        self.network.deallocate_fixed_ip(context1, fix_addr)
 
482
        db.floating_ip_destroy(context1.elevated(), float_addr)
 
483
        db.fixed_ip_disassociate(context1.elevated(), fix_addr)
 
484
 
374
485
 
375
486
class CommonNetworkTestCase(test.TestCase):
376
 
 
377
 
    class FakeNetworkManager(network_manager.NetworkManager):
378
 
        """This NetworkManager doesn't call the base class so we can bypass all
379
 
        inherited service cruft and just perform unit tests.
380
 
        """
381
 
 
382
 
        class FakeDB:
383
 
            def fixed_ip_get_by_instance(self, context, instance_id):
384
 
                return [dict(address='10.0.0.0'),  dict(address='10.0.0.1'),
385
 
                        dict(address='10.0.0.2')]
386
 
 
387
 
            def network_get_by_cidr(self, context, cidr):
388
 
                raise exception.NetworkNotFoundForCidr()
389
 
 
390
 
            def network_create_safe(self, context, net):
391
 
                fakenet = dict(net)
392
 
                fakenet['id'] = 999
393
 
                return fakenet
394
 
 
395
 
            def network_get_all(self, context):
396
 
                raise exception.NoNetworksFound()
397
 
 
398
 
        def __init__(self):
399
 
            self.db = self.FakeDB()
400
 
            self.deallocate_called = None
401
 
 
402
 
        def deallocate_fixed_ip(self, context, address):
403
 
            self.deallocate_called = address
404
 
 
405
 
        def _create_fixed_ips(self, context, network_id):
406
 
            pass
407
 
 
408
487
    def fake_create_fixed_ips(self, context, network_id):
409
488
        return None
410
489
 
411
490
    def test_remove_fixed_ip_from_instance(self):
412
 
        manager = self.FakeNetworkManager()
 
491
        manager = fake_network.FakeNetworkManager()
413
492
        manager.remove_fixed_ip_from_instance(None, 99, '10.0.0.1')
414
493
 
415
494
        self.assertEquals(manager.deallocate_called, '10.0.0.1')
416
495
 
417
496
    def test_remove_fixed_ip_from_instance_bad_input(self):
418
 
        manager = self.FakeNetworkManager()
 
497
        manager = fake_network.FakeNetworkManager()
419
498
        self.assertRaises(exception.FixedIpNotFoundForSpecificInstance,
420
499
                          manager.remove_fixed_ip_from_instance,
421
500
                          None, 99, 'bad input')
422
501
 
423
502
    def test_validate_cidrs(self):
424
 
        manager = self.FakeNetworkManager()
 
503
        manager = fake_network.FakeNetworkManager()
425
504
        nets = manager.create_networks(None, 'fake', '192.168.0.0/24',
426
505
                                       False, 1, 256, None, None, None,
427
506
                                       None)
430
509
        self.assertTrue('192.168.0.0/24' in cidrs)
431
510
 
432
511
    def test_validate_cidrs_split_exact_in_half(self):
433
 
        manager = self.FakeNetworkManager()
 
512
        manager = fake_network.FakeNetworkManager()
434
513
        nets = manager.create_networks(None, 'fake', '192.168.0.0/24',
435
514
                                       False, 2, 128, None, None, None,
436
515
                                       None)
440
519
        self.assertTrue('192.168.0.128/25' in cidrs)
441
520
 
442
521
    def test_validate_cidrs_split_cidr_in_use_middle_of_range(self):
443
 
        manager = self.FakeNetworkManager()
 
522
        manager = fake_network.FakeNetworkManager()
444
523
        self.mox.StubOutWithMock(manager.db, 'network_get_all')
445
524
        ctxt = mox.IgnoreArg()
446
525
        manager.db.network_get_all(ctxt).AndReturn([{'id': 1,
458
537
        self.assertFalse('192.168.2.0/24' in cidrs)
459
538
 
460
539
    def test_validate_cidrs_smaller_subnet_in_use(self):
461
 
        manager = self.FakeNetworkManager()
 
540
        manager = fake_network.FakeNetworkManager()
462
541
        self.mox.StubOutWithMock(manager.db, 'network_get_all')
463
542
        ctxt = mox.IgnoreArg()
464
543
        manager.db.network_get_all(ctxt).AndReturn([{'id': 1,
471
550
        self.assertRaises(ValueError, manager.create_networks, *args)
472
551
 
473
552
    def test_validate_cidrs_split_smaller_cidr_in_use(self):
474
 
        manager = self.FakeNetworkManager()
 
553
        manager = fake_network.FakeNetworkManager()
475
554
        self.mox.StubOutWithMock(manager.db, 'network_get_all')
476
555
        ctxt = mox.IgnoreArg()
477
556
        manager.db.network_get_all(ctxt).AndReturn([{'id': 1,
488
567
        self.assertFalse('192.168.2.0/24' in cidrs)
489
568
 
490
569
    def test_validate_cidrs_split_smaller_cidr_in_use2(self):
491
 
        manager = self.FakeNetworkManager()
 
570
        manager = fake_network.FakeNetworkManager()
492
571
        self.mox.StubOutWithMock(manager.db, 'network_get_all')
493
572
        ctxt = mox.IgnoreArg()
494
573
        manager.db.network_get_all(ctxt).AndReturn([{'id': 1,
504
583
        self.assertFalse('192.168.2.0/27' in cidrs)
505
584
 
506
585
    def test_validate_cidrs_split_all_in_use(self):
507
 
        manager = self.FakeNetworkManager()
 
586
        manager = fake_network.FakeNetworkManager()
508
587
        self.mox.StubOutWithMock(manager.db, 'network_get_all')
509
588
        ctxt = mox.IgnoreArg()
510
589
        in_use = [{'id': 1, 'cidr': '192.168.2.9/29'},
520
599
        self.assertRaises(ValueError, manager.create_networks, *args)
521
600
 
522
601
    def test_validate_cidrs_one_in_use(self):
523
 
        manager = self.FakeNetworkManager()
 
602
        manager = fake_network.FakeNetworkManager()
524
603
        args = (None, 'fake', '192.168.0.0/24', False, 2, 256, None, None,
525
604
                None, None)
526
605
        # ValueError: network_size * num_networks exceeds cidr size
527
606
        self.assertRaises(ValueError, manager.create_networks, *args)
528
607
 
529
608
    def test_validate_cidrs_already_used(self):
530
 
        manager = self.FakeNetworkManager()
 
609
        manager = fake_network.FakeNetworkManager()
531
610
        self.mox.StubOutWithMock(manager.db, 'network_get_all')
532
611
        ctxt = mox.IgnoreArg()
533
612
        manager.db.network_get_all(ctxt).AndReturn([{'id': 1,
539
618
        self.assertRaises(ValueError, manager.create_networks, *args)
540
619
 
541
620
    def test_validate_cidrs_too_many(self):
542
 
        manager = self.FakeNetworkManager()
 
621
        manager = fake_network.FakeNetworkManager()
543
622
        args = (None, 'fake', '192.168.0.0/24', False, 200, 256, None, None,
544
623
                None, None)
545
624
        # ValueError: Not enough subnets avail to satisfy requested
547
626
        self.assertRaises(ValueError, manager.create_networks, *args)
548
627
 
549
628
    def test_validate_cidrs_split_partial(self):
550
 
        manager = self.FakeNetworkManager()
 
629
        manager = fake_network.FakeNetworkManager()
551
630
        nets = manager.create_networks(None, 'fake', '192.168.0.0/16',
552
631
                                       False, 2, 256, None, None, None, None)
553
632
        returned_cidrs = [str(net['cidr']) for net in nets]
555
634
        self.assertTrue('192.168.1.0/24' in returned_cidrs)
556
635
 
557
636
    def test_validate_cidrs_conflict_existing_supernet(self):
558
 
        manager = self.FakeNetworkManager()
 
637
        manager = fake_network.FakeNetworkManager()
559
638
        self.mox.StubOutWithMock(manager.db, 'network_get_all')
560
639
        ctxt = mox.IgnoreArg()
561
640
        fakecidr = [{'id': 1, 'cidr': '192.168.0.0/8'}]
569
648
 
570
649
    def test_create_networks(self):
571
650
        cidr = '192.168.0.0/24'
572
 
        manager = self.FakeNetworkManager()
 
651
        manager = fake_network.FakeNetworkManager()
573
652
        self.stubs.Set(manager, '_create_fixed_ips',
574
653
                                self.fake_create_fixed_ips)
575
654
        args = [None, 'foo', cidr, None, 1, 256, 'fd00::/48', None, None,
576
655
                None]
577
 
        result = manager.create_networks(*args)
578
656
        self.assertTrue(manager.create_networks(*args))
579
657
 
580
658
    def test_create_networks_cidr_already_used(self):
581
 
        manager = self.FakeNetworkManager()
 
659
        manager = fake_network.FakeNetworkManager()
582
660
        self.mox.StubOutWithMock(manager.db, 'network_get_all')
583
661
        ctxt = mox.IgnoreArg()
584
662
        fakecidr = [{'id': 1, 'cidr': '192.168.0.0/24'}]
590
668
 
591
669
    def test_create_networks_many(self):
592
670
        cidr = '192.168.0.0/16'
593
 
        manager = self.FakeNetworkManager()
 
671
        manager = fake_network.FakeNetworkManager()
594
672
        self.stubs.Set(manager, '_create_fixed_ips',
595
673
                                self.fake_create_fixed_ips)
596
674
        args = [None, 'foo', cidr, None, 10, 256, 'fd00::/48', None, None,
597
675
                None]
598
676
        self.assertTrue(manager.create_networks(*args))
 
677
 
 
678
    def test_get_instance_uuids_by_ip_regex(self):
 
679
        manager = fake_network.FakeNetworkManager()
 
680
        _vifs = manager.db.virtual_interface_get_all(None)
 
681
 
 
682
        # Greedy get eveything
 
683
        res = manager.get_instance_uuids_by_ip_filter(None, {'ip': '.*'})
 
684
        self.assertEqual(len(res), len(_vifs))
 
685
 
 
686
        # Doesn't exist
 
687
        res = manager.get_instance_uuids_by_ip_filter(None, {'ip': '10.0.0.1'})
 
688
        self.assertFalse(res)
 
689
 
 
690
        # Get instance 1
 
691
        res = manager.get_instance_uuids_by_ip_filter(None,
 
692
                                                    {'ip': '172.16.0.2'})
 
693
        self.assertTrue(res)
 
694
        self.assertEqual(len(res), 1)
 
695
        self.assertEqual(res[0]['instance_id'], _vifs[1]['instance_id'])
 
696
 
 
697
        # Get instance 2
 
698
        res = manager.get_instance_uuids_by_ip_filter(None,
 
699
                                                    {'ip': '173.16.0.2'})
 
700
        self.assertTrue(res)
 
701
        self.assertEqual(len(res), 1)
 
702
        self.assertEqual(res[0]['instance_id'], _vifs[2]['instance_id'])
 
703
 
 
704
        # Get instance 0 and 1
 
705
        res = manager.get_instance_uuids_by_ip_filter(None,
 
706
                                                    {'ip': '172.16.0.*'})
 
707
        self.assertTrue(res)
 
708
        self.assertEqual(len(res), 2)
 
709
        self.assertEqual(res[0]['instance_id'], _vifs[0]['instance_id'])
 
710
        self.assertEqual(res[1]['instance_id'], _vifs[1]['instance_id'])
 
711
 
 
712
        # Get instance 1 and 2
 
713
        res = manager.get_instance_uuids_by_ip_filter(None,
 
714
                                                    {'ip': '17..16.0.2'})
 
715
        self.assertTrue(res)
 
716
        self.assertEqual(len(res), 2)
 
717
        self.assertEqual(res[0]['instance_id'], _vifs[1]['instance_id'])
 
718
        self.assertEqual(res[1]['instance_id'], _vifs[2]['instance_id'])
 
719
 
 
720
    def test_get_instance_uuids_by_ipv6_regex(self):
 
721
        manager = fake_network.FakeNetworkManager()
 
722
        _vifs = manager.db.virtual_interface_get_all(None)
 
723
 
 
724
        # Greedy get eveything
 
725
        res = manager.get_instance_uuids_by_ip_filter(None, {'ip6': '.*'})
 
726
        self.assertEqual(len(res), len(_vifs))
 
727
 
 
728
        # Doesn't exist
 
729
        res = manager.get_instance_uuids_by_ip_filter(None,
 
730
                                                      {'ip6': '.*1034.*'})
 
731
        self.assertFalse(res)
 
732
 
 
733
        # Get instance 1
 
734
        res = manager.get_instance_uuids_by_ip_filter(None,
 
735
                                                    {'ip6': '2001:.*:2'})
 
736
        self.assertTrue(res)
 
737
        self.assertEqual(len(res), 1)
 
738
        self.assertEqual(res[0]['instance_id'], _vifs[1]['instance_id'])
 
739
 
 
740
        # Get instance 2
 
741
        ip6 = '2002:db8::dcad:beff:feef:2'
 
742
        res = manager.get_instance_uuids_by_ip_filter(None, {'ip6': ip6})
 
743
        self.assertTrue(res)
 
744
        self.assertEqual(len(res), 1)
 
745
        self.assertEqual(res[0]['instance_id'], _vifs[2]['instance_id'])
 
746
 
 
747
        # Get instance 0 and 1
 
748
        res = manager.get_instance_uuids_by_ip_filter(None, {'ip6': '2001:.*'})
 
749
        self.assertTrue(res)
 
750
        self.assertEqual(len(res), 2)
 
751
        self.assertEqual(res[0]['instance_id'], _vifs[0]['instance_id'])
 
752
        self.assertEqual(res[1]['instance_id'], _vifs[1]['instance_id'])
 
753
 
 
754
        # Get instance 1 and 2
 
755
        ip6 = '200.:db8::dcad:beff:feef:2'
 
756
        res = manager.get_instance_uuids_by_ip_filter(None, {'ip6': ip6})
 
757
        self.assertTrue(res)
 
758
        self.assertEqual(len(res), 2)
 
759
        self.assertEqual(res[0]['instance_id'], _vifs[1]['instance_id'])
 
760
        self.assertEqual(res[1]['instance_id'], _vifs[2]['instance_id'])
 
761
 
 
762
    def test_get_instance_uuids_by_ip(self):
 
763
        manager = fake_network.FakeNetworkManager()
 
764
        _vifs = manager.db.virtual_interface_get_all(None)
 
765
 
 
766
        # No regex for you!
 
767
        res = manager.get_instance_uuids_by_ip_filter(None,
 
768
                                                      {'fixed_ip': '.*'})
 
769
        self.assertFalse(res)
 
770
 
 
771
        # Doesn't exist
 
772
        ip = '10.0.0.1'
 
773
        res = manager.get_instance_uuids_by_ip_filter(None,
 
774
                                                      {'fixed_ip': ip})
 
775
        self.assertFalse(res)
 
776
 
 
777
        # Get instance 1
 
778
        ip = '172.16.0.2'
 
779
        res = manager.get_instance_uuids_by_ip_filter(None,
 
780
                                                      {'fixed_ip': ip})
 
781
        self.assertTrue(res)
 
782
        self.assertEqual(len(res), 1)
 
783
        self.assertEqual(res[0]['instance_id'], _vifs[1]['instance_id'])
 
784
 
 
785
        # Get instance 2
 
786
        ip = '173.16.0.2'
 
787
        res = manager.get_instance_uuids_by_ip_filter(None,
 
788
                                                      {'fixed_ip': ip})
 
789
        self.assertTrue(res)
 
790
        self.assertEqual(len(res), 1)
 
791
        self.assertEqual(res[0]['instance_id'], _vifs[2]['instance_id'])