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

« back to all changes in this revision

Viewing changes to nova/tests/test_xenapi.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short, Chuck Short, James Page
  • Date: 2013-03-20 12:59:22 UTC
  • mfrom: (1.1.69)
  • Revision ID: package-import@ubuntu.com-20130320125922-ohvfav96lemn9wlz
Tags: 1:2013.1~rc1-0ubuntu1
[ Chuck Short ]
* New upstream release.
* debian/patches/avoid_setuptools_git_dependency.patch: Refreshed.
* debian/control: Clean up dependencies:
  - Dropped python-gflags no longer needed.
  - Dropped python-daemon no longer needed.
  - Dropped python-glance no longer needed.
  - Dropped python-lockfile no longer needed.
  - Dropped python-simplejson no longer needed.
  - Dropped python-tempita no longer needed.
  - Dropped python-xattr no longer needed.
  - Add sqlite3 required for the testsuite.

[ James Page ]
* d/watch: Update uversionmangle to deal with upstream versioning
  changes, remove tarballs.openstack.org. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
176
176
    return decorated_function
177
177
 
178
178
 
 
179
def create_instance_with_system_metadata(context, instance_values):
 
180
    instance_type = db.instance_type_get(context,
 
181
                                         instance_values['instance_type_id'])
 
182
    sys_meta = instance_types.save_instance_type_info({},
 
183
                                                      instance_type)
 
184
    instance_values['system_metadata'] = sys_meta
 
185
    return db.instance_create(context, instance_values)
 
186
 
 
187
 
179
188
class XenAPIVolumeTestCase(stubs.XenAPITestBase):
180
189
    """Unit tests for Volume operations."""
181
190
    def setUp(self):
365
374
        instances = self.conn.list_instances()
366
375
        self.assertEquals(instances, [])
367
376
 
 
377
    def test_list_instance_uuids_0(self):
 
378
        instance_uuids = self.conn.list_instance_uuids()
 
379
        self.assertEquals(instance_uuids, [])
 
380
 
 
381
    def test_list_instance_uuids(self):
 
382
        uuids = []
 
383
        for x in xrange(1, 4):
 
384
            instance = self._create_instance(x)
 
385
            uuids.append(instance['uuid'])
 
386
        instance_uuids = self.conn.list_instance_uuids()
 
387
        self.assertEqual(len(uuids), len(instance_uuids))
 
388
        self.assertEqual(set(uuids), set(instance_uuids))
 
389
 
368
390
    def test_get_rrd_server(self):
369
391
        self.flags(xenapi_connection_url='myscheme://myaddress/')
370
392
        server_info = vm_utils._get_rrd_server()
613
635
                    create_record=True, empty_dns=False,
614
636
                    image_meta={'id': IMAGE_VHD,
615
637
                                'disk_format': 'vhd'},
616
 
                    block_device_info=None):
 
638
                    block_device_info=None,
 
639
                    key_data=None):
617
640
        if injected_files is None:
618
641
            injected_files = []
619
642
 
625
648
 
626
649
        if create_record:
627
650
            instance_values = {'id': instance_id,
628
 
                      'project_id': self.project_id,
629
 
                      'user_id': self.user_id,
630
 
                      'image_ref': image_ref,
631
 
                      'kernel_id': kernel_id,
632
 
                      'ramdisk_id': ramdisk_id,
633
 
                      'root_gb': 20,
634
 
                      'instance_type_id': instance_type_id,
635
 
                      'os_type': os_type,
636
 
                      'hostname': hostname,
637
 
                      'architecture': architecture}
638
 
            instance = db.instance_create(self.context, instance_values)
 
651
                               'project_id': self.project_id,
 
652
                               'user_id': self.user_id,
 
653
                               'image_ref': image_ref,
 
654
                               'kernel_id': kernel_id,
 
655
                               'ramdisk_id': ramdisk_id,
 
656
                               'root_gb': 20,
 
657
                               'instance_type_id': instance_type_id,
 
658
                               'os_type': os_type,
 
659
                               'hostname': hostname,
 
660
                               'key_data': key_data,
 
661
                               'architecture': architecture}
 
662
            instance = create_instance_with_system_metadata(self.context,
 
663
                                                            instance_values)
639
664
        else:
640
665
            instance = db.instance_get(self.context, instance_id)
641
666
 
875
900
            self.assertEquals(vif_rec['qos_algorithm_params']['kbps'],
876
901
                              str(3 * 10 * 1024))
877
902
 
 
903
    def test_spawn_ssh_key_injection(self):
 
904
        # Test spawning with key_data on an instance.  Should use
 
905
        # agent file injection.
 
906
        actual_injected_files = []
 
907
 
 
908
        def fake_inject_file(self, method, args):
 
909
            path = base64.b64decode(args['b64_path'])
 
910
            contents = base64.b64decode(args['b64_contents'])
 
911
            actual_injected_files.append((path, contents))
 
912
            return jsonutils.dumps({'returncode': '0', 'message': 'success'})
 
913
 
 
914
        def noop(*args, **kwargs):
 
915
            pass
 
916
 
 
917
        self.stubs.Set(stubs.FakeSessionForVMTests,
 
918
                       '_plugin_agent_inject_file', fake_inject_file)
 
919
        self.stubs.Set(agent.XenAPIBasedAgent,
 
920
                       'set_admin_password', noop)
 
921
 
 
922
        expected_data = ('\n# The following ssh key was injected by '
 
923
                         'Nova\nfake_keydata\n')
 
924
 
 
925
        injected_files = [('/root/.ssh/authorized_keys', expected_data)]
 
926
        self._test_spawn(IMAGE_VHD, None, None,
 
927
                         os_type="linux", architecture="x86-64",
 
928
                         key_data='fake_keydata')
 
929
        self.assertEquals(actual_injected_files, injected_files)
 
930
 
878
931
    def test_spawn_injected_files(self):
879
932
        # Test spawning with injected_files.
880
933
        actual_injected_files = []
1093
1146
            'os_type': 'linux',
1094
1147
            'vm_mode': 'hvm',
1095
1148
            'architecture': 'x86-64'}
1096
 
        instance = db.instance_create(self.context, instance_values)
 
1149
 
 
1150
        instance = create_instance_with_system_metadata(self.context,
 
1151
                                                        instance_values)
1097
1152
        network_info = fake_network.fake_get_instance_nw_info(self.stubs,
1098
1153
                                                              spectacular=True)
1099
1154
        image_meta = {'id': IMAGE_VHD,
1252
1307
                          '127.0.0.1', instance_type, None)
1253
1308
 
1254
1309
    def test_revert_migrate(self):
1255
 
        instance = db.instance_create(self.context, self.instance_values)
 
1310
        instance = create_instance_with_system_metadata(self.context,
 
1311
                                                        self.instance_values)
1256
1312
        self.called = False
1257
1313
        self.fake_vm_start_called = False
1258
1314
        self.fake_finish_revert_migration_called = False
1293
1349
        self.assertEqual(self.fake_finish_revert_migration_called, True)
1294
1350
 
1295
1351
    def test_finish_migrate(self):
1296
 
        instance = db.instance_create(self.context, self.instance_values)
 
1352
        instance = create_instance_with_system_metadata(self.context,
 
1353
                                                        self.instance_values)
1297
1354
        self.called = False
1298
1355
        self.fake_vm_start_called = False
1299
1356
 
1325
1382
        tiny_type_id = tiny_type['id']
1326
1383
        self.instance_values.update({'instance_type_id': tiny_type_id,
1327
1384
                                     'root_gb': 0})
1328
 
        instance = db.instance_create(self.context, self.instance_values)
 
1385
        instance = create_instance_with_system_metadata(self.context,
 
1386
                                                        self.instance_values)
1329
1387
 
1330
1388
        def fake_vdi_resize(*args, **kwargs):
1331
1389
            raise Exception("This shouldn't be called")
1341
1399
                              network_info, image_meta, resize_instance=True)
1342
1400
 
1343
1401
    def test_finish_migrate_no_resize_vdi(self):
1344
 
        instance = db.instance_create(self.context, self.instance_values)
 
1402
        instance = create_instance_with_system_metadata(self.context,
 
1403
                                                        self.instance_values)
1345
1404
 
1346
1405
        def fake_vdi_resize(*args, **kwargs):
1347
1406
            raise Exception("This shouldn't be called")
1616
1675
                                            fake.FakeVirtAPI())
1617
1676
 
1618
1677
        disk_image_type = vm_utils.ImageType.DISK_VHD
1619
 
        instance = db.instance_create(self.context, self.instance_values)
 
1678
        instance = create_instance_with_system_metadata(self.context,
 
1679
                                                        self.instance_values)
1620
1680
        vm_ref = xenapi_fake.create_vm(instance['name'], 'Halted')
1621
1681
        vdi_ref = xenapi_fake.create_vdi(instance['name'], 'fake')
1622
1682
 
1716
1776
 
1717
1777
    def test_generate_swap(self):
1718
1778
        # Test swap disk generation.
1719
 
        instance = db.instance_create(self.context, self.instance_values)
1720
 
        instance = db.instance_update(self.context, instance['uuid'],
1721
 
                                      {'instance_type_id': 5})
1722
 
 
1723
 
        # NOTE(danms): because we're stubbing out the instance_types from
1724
 
        # the database, our instance['instance_type'] doesn't get properly
1725
 
        # filled out here, so put what we need into it
1726
 
        instance['instance_type']['swap'] = 1024
 
1779
        instance_values = dict(self.instance_values, instance_type_id=5)
 
1780
        instance = create_instance_with_system_metadata(self.context,
 
1781
                                                        instance_values)
1727
1782
 
1728
1783
        def fake_generate_swap(*args, **kwargs):
1729
1784
            self.called = True
1733
1788
 
1734
1789
    def test_generate_ephemeral(self):
1735
1790
        # Test ephemeral disk generation.
1736
 
        instance = db.instance_create(self.context, self.instance_values)
1737
 
        instance = db.instance_update(self.context, instance['uuid'],
1738
 
                                      {'instance_type_id': 4})
1739
 
 
1740
 
        # NOTE(danms): because we're stubbing out the instance_types from
1741
 
        # the database, our instance['instance_type'] doesn't get properly
1742
 
        # filled out here, so put what we need into it
1743
 
        instance['instance_type']['ephemeral_gb'] = 160
 
1791
        instance_values = dict(self.instance_values, instance_type_id=4)
 
1792
        instance = create_instance_with_system_metadata(self.context,
 
1793
                                                        instance_values)
1744
1794
 
1745
1795
        def fake_generate_ephemeral(*args):
1746
1796
            self.called = True
2068
2118
        ipv6 = self.fw.iptables.ipv6['filter'].rules
2069
2119
        ipv4_network_rules = len(ipv4) - len(inst_ipv4) - ipv4_len
2070
2120
        ipv6_network_rules = len(ipv6) - len(inst_ipv6) - ipv6_len
2071
 
        self.assertEquals(ipv4_network_rules,
2072
 
                  ipv4_rules_per_addr * ipv4_addr_per_network * networks_count)
 
2121
        # Extra rules are for the DHCP request
 
2122
        rules = (ipv4_rules_per_addr * ipv4_addr_per_network *
 
2123
                 networks_count) + 2
 
2124
        self.assertEquals(ipv4_network_rules, rules)
2073
2125
        self.assertEquals(ipv6_network_rules,
2074
2126
                  ipv6_rules_per_addr * ipv6_addr_per_network * networks_count)
2075
2127
 
3112
3164
            'VDI.resize',
3113
3165
            ops.check_resize_func_name())
3114
3166
 
 
3167
    def _test_finish_revert_migration_after_crash(self, backup_made, new_made):
 
3168
        instance = {'name': 'foo',
 
3169
                    'task_state': task_states.RESIZE_MIGRATING}
 
3170
        session = self._get_mock_session(None, None)
 
3171
        ops = vmops.VMOps(session, fake.FakeVirtAPI())
 
3172
 
 
3173
        self.mox.StubOutWithMock(vm_utils, 'lookup')
 
3174
        self.mox.StubOutWithMock(ops, '_destroy')
 
3175
        self.mox.StubOutWithMock(vm_utils, 'set_vm_name_label')
 
3176
        self.mox.StubOutWithMock(ops, '_attach_mapped_block_devices')
 
3177
        self.mox.StubOutWithMock(ops, '_start')
 
3178
 
 
3179
        vm_utils.lookup(session, 'foo-orig').AndReturn(
 
3180
            backup_made and 'foo' or None)
 
3181
        vm_utils.lookup(session, 'foo').AndReturn(
 
3182
            (not backup_made or new_made) and 'foo' or None)
 
3183
        if backup_made:
 
3184
            if new_made:
 
3185
                ops._destroy(instance, 'foo')
 
3186
            vm_utils.set_vm_name_label(session, 'foo', 'foo')
 
3187
            ops._attach_mapped_block_devices(instance, [])
 
3188
        ops._start(instance, 'foo')
 
3189
 
 
3190
        self.mox.ReplayAll()
 
3191
 
 
3192
        ops.finish_revert_migration(instance, [])
 
3193
 
 
3194
    def test_finish_revert_migration_after_crash(self):
 
3195
        self._test_finish_revert_migration_after_crash(True, True)
 
3196
 
 
3197
    def test_finish_revert_migration_after_crash_before_new(self):
 
3198
        self._test_finish_revert_migration_after_crash(True, False)
 
3199
 
 
3200
    def test_finish_revert_migration_after_crash_before_backup(self):
 
3201
        self._test_finish_revert_migration_after_crash(False, False)
 
3202
 
3115
3203
 
3116
3204
class XenAPISessionTestCase(test.TestCase):
3117
3205
    def _get_mock_xapisession(self, software_version):