def test_create_instances_here_pops_problematic_properties(self,
                                                               mock_update):
        values = {
            'uuid': uuidsentinel.instance,
            'metadata': [],
            'id': 1,
            'name': 'foo',
            'info_cache': 'bar',
            'security_groups': 'not secure',
            'flavor': 'chocolate',
            'pci_requests': 'no thanks',
            'ec2_ids': 'prime',
        }

        @mock.patch.object(self.scheduler.compute_api,
                           'create_db_entry_for_new_instance')
        def test(mock_create_db):
            self.scheduler._create_instances_here(
                self.ctxt, [uuidsentinel.instance], values,
                objects.Flavor(), 'foo', [], [])

        test()

        # NOTE(danms): Make sure that only the expected properties
        # are applied to the instance object. The complex ones that
        # would have been mangled over RPC should be removed.
        mock_update.assert_called_once_with(
            {'uuid': uuidsentinel.instance,
             'metadata': {}})
示例#2
0
文件: test_utils.py 项目: mahak/nova
    def test_claim_resources_on_destination_no_source_allocations(self):
        """Tests the negative scenario where the instance does not have
        allocations in Placement on the source compute node so no claim is
        attempted on the destination compute node.
        """
        reportclient = report.SchedulerReportClient()
        instance = fake_instance.fake_instance_obj(self.context)
        source_node = objects.ComputeNode(
            uuid=uuids.source_node, host=instance.host)
        dest_node = objects.ComputeNode(uuid=uuids.dest_node, host='dest-host')

        @mock.patch.object(reportclient,
                           'get_allocs_for_consumer',
                           return_value={})
        @mock.patch.object(reportclient,
                           'claim_resources',
                           new_callable=mock.NonCallableMock)
        def test(mock_claim, mock_get_allocs):
            ex = self.assertRaises(
                exception.ConsumerAllocationRetrievalFailed,
                utils.claim_resources_on_destination,
                self.context, reportclient, instance, source_node, dest_node)
            mock_get_allocs.assert_called_once_with(
                self.context, instance.uuid)
            self.assertIn(
                'Expected to find allocations for source node resource '
                'provider %s' % source_node.uuid, six.text_type(ex))

        test()
示例#3
0
    def test_create_instances_here_pops_problematic_properties(self, mock_update):
        values = {
            "uuid": uuidsentinel.instance,
            "metadata": [],
            "id": 1,
            "name": "foo",
            "info_cache": "bar",
            "security_groups": "not secure",
            "flavor": "chocolate",
            "pci_requests": "no thanks",
            "ec2_ids": "prime",
        }
        block_device_mapping = [
            objects.BlockDeviceMapping(
                context=self.ctxt,
                **fake_block_device.FakeDbBlockDeviceDict(
                    block_device.create_image_bdm(uuidsentinel.fake_image_ref), anon=True
                )
            )
        ]

        @mock.patch.object(self.scheduler.compute_api, "create_db_entry_for_new_instance")
        @mock.patch.object(self.scheduler.compute_api, "_bdm_validate_set_size_and_instance")
        def test(mock_bdm_validate, mock_create_db):
            self.scheduler._create_instances_here(
                self.ctxt, [uuidsentinel.instance], values, objects.Flavor(), "foo", [], block_device_mapping
            )

        test()

        # NOTE(danms): Make sure that only the expected properties
        # are applied to the instance object. The complex ones that
        # would have been mangled over RPC should be removed.
        mock_update.assert_called_once_with({"uuid": uuidsentinel.instance, "metadata": {}})
示例#4
0
文件: test_utils.py 项目: mahak/nova
    def test_claim_resources_on_destination_claim_fails(self):
        """Tests the negative scenario where the resource allocation claim
        on the destination compute node fails, resulting in an error.
        """
        reportclient = report.SchedulerReportClient()
        instance = fake_instance.fake_instance_obj(self.context)
        source_node = objects.ComputeNode(
            uuid=uuids.source_node, host=instance.host)
        dest_node = objects.ComputeNode(uuid=uuids.dest_node, host='dest-host')
        source_res_allocs = {
            'allocations': {
                uuids.source_node: {
                    'resources': {
                        'VCPU': instance.vcpus,
                        'MEMORY_MB': instance.memory_mb,
                        # This would really include ephemeral and swap too but
                        # we're lazy.
                        'DISK_GB': instance.root_gb
                    }
                }
            },
            'consumer_generation': 1,
            'project_id': uuids.project_id,
            'user_id': uuids.user_id
        }
        dest_alloc_request = {
            'allocations': {
                uuids.dest_node: {
                    'resources': {
                        'VCPU': instance.vcpus,
                        'MEMORY_MB': instance.memory_mb,
                        'DISK_GB': instance.root_gb
                    }
                }
            },
        }

        @mock.patch.object(reportclient,
                           'get_allocs_for_consumer',
                           return_value=source_res_allocs)
        @mock.patch.object(reportclient,
                           'claim_resources', return_value=False)
        def test(mock_claim, mock_get_allocs):
            # NOTE(danms): Don't pass source_node_allocations here to test
            # that they are fetched if needed.
            self.assertRaises(exception.NoValidHost,
                              utils.claim_resources_on_destination,
                              self.context, reportclient, instance,
                              source_node, dest_node)
            mock_get_allocs.assert_called_once_with(
                self.context, instance.uuid)
            mock_claim.assert_called_once_with(
                self.context, instance.uuid, dest_alloc_request,
                instance.project_id, instance.user_id,
                allocation_request_version='1.28', consumer_generation=1)

        test()
示例#5
0
    def test_delete_instance_no_cell_instance_disappear(self, mock_lookup,
                                                        mock_local_delete):
        inst = self._create_fake_instance_obj()

        @mock.patch.object(self.compute_api.cells_rpcapi,
                           'instance_delete_everywhere')
        def test(mock_inst_del):
            self.compute_api.delete(self.context, inst)
            mock_lookup.assert_called_once_with(self.context, inst.uuid)
            mock_inst_del.assert_called_once_with(self.context, inst, 'hard')
            self.assertFalse(mock_local_delete.called)

        test()
示例#6
0
    def test_get_migrations(self):
        filters = {'cell_name': 'ChildCell', 'status': 'confirmed'}
        migrations = {'migrations': [{'id': 1234}]}

        @mock.patch.object(self.compute_api.cells_rpcapi, 'get_migrations',
                           return_value=migrations)
        def test(mock_cell_get_migrations):
            response = self.compute_api.get_migrations(self.context,
                                                       filters)
            mock_cell_get_migrations.assert_called_once_with(self.context,
                                                             filters)
            self.assertEqual(migrations, response)

        test()
示例#7
0
文件: test_utils.py 项目: mahak/nova
    def test_claim_resources_on_destination(self):
        """Happy path test where everything is successful."""
        reportclient = report.SchedulerReportClient()
        instance = fake_instance.fake_instance_obj(self.context)
        source_node = objects.ComputeNode(
            uuid=uuids.source_node, host=instance.host)
        dest_node = objects.ComputeNode(uuid=uuids.dest_node, host='dest-host')
        source_res_allocs = {
            uuids.source_node: {
                'resources': {
                    'VCPU': instance.vcpus,
                    'MEMORY_MB': instance.memory_mb,
                    # This would really include ephemeral and swap too but
                    # we're lazy.
                    'DISK_GB': instance.root_gb
                }
            }
        }
        dest_alloc_request = {
            'allocations': {
                uuids.dest_node: {
                    'resources': {
                        'VCPU': instance.vcpus,
                        'MEMORY_MB': instance.memory_mb,
                        'DISK_GB': instance.root_gb
                    }
                }
            },
        }

        @mock.patch.object(reportclient,
                           'get_allocs_for_consumer')
        @mock.patch.object(reportclient,
                           'claim_resources', return_value=True)
        def test(mock_claim, mock_get_allocs):
            utils.claim_resources_on_destination(
                self.context, reportclient, instance, source_node, dest_node,
                source_res_allocs, consumer_generation=None)
            self.assertFalse(mock_get_allocs.called)
            mock_claim.assert_called_once_with(
                self.context, instance.uuid, dest_alloc_request,
                instance.project_id, instance.user_id,
                allocation_request_version='1.28', consumer_generation=None)

        test()
示例#8
0
    def test_delete_instance_no_cell_then_no_instance(self,
            mock_delete, mock_lookup_instance, mock_local_delete,
            mock_delete_while_booting):
        # This checks the case where initially an instance has no cell_name,
        # and therefore no host, set but instance.destroy fails because
        # there is now a host. And then the instance can't be looked up.
        instance = self._create_fake_instance_obj()
        mock_lookup_instance.return_value = None, None

        cells_rpcapi = self.compute_api.cells_rpcapi

        @mock.patch.object(cells_rpcapi, 'instance_delete_everywhere')
        def test(mock_inst_delete_everywhere):
            self.compute_api.delete(self.context, instance)
            mock_local_delete.assert_not_called()
            mock_delete.assert_not_called()

        test()
示例#9
0
文件: test_utils.py 项目: yzr213/nova
    def test_claim_resources_on_destination_claim_fails(self):
        """Tests the negative scenario where the resource allocation claim
        on the destination compute node fails, resulting in an error.
        """
        reportclient = report.SchedulerReportClient()
        instance = fake_instance.fake_instance_obj(self.context)
        source_node = objects.ComputeNode(
            uuid=uuids.source_node, host=instance.host)
        dest_node = objects.ComputeNode(uuid=uuids.dest_node, host='dest-host')
        source_res_allocs = {
            'VCPU': instance.vcpus,
            'MEMORY_MB': instance.memory_mb,
            # This would really include ephemeral and swap too but we're lazy.
            'DISK_GB': instance.root_gb
        }
        dest_alloc_request = {
            'allocations': {
                uuids.dest_node: {
                    'resources': source_res_allocs
                }
            }
        }

        @mock.patch.object(reportclient,
                           'get_allocations_for_consumer_by_provider',
                           return_value=source_res_allocs)
        @mock.patch.object(reportclient,
                           'claim_resources', return_value=False)
        def test(mock_claim, mock_get_allocs):
            # NOTE(danms): Don't pass source_node_allocations here to test
            # that they are fetched if needed.
            self.assertRaises(exception.NoValidHost,
                              utils.claim_resources_on_destination,
                              self.context, reportclient, instance,
                              source_node, dest_node)
            mock_get_allocs.assert_called_once_with(
                self.context, uuids.source_node, instance.uuid)
            mock_claim.assert_called_once_with(
                self.context, instance.uuid, dest_alloc_request,
                instance.project_id, instance.user_id,
                allocation_request_version='1.12')

        test()
    def test_claim_resources_on_destination_no_source_allocations(self):
        """Tests the negative scenario where the instance does not have
        allocations in Placement on the source compute node so no claim is
        attempted on the destination compute node.
        """
        source_node = objects.ComputeNode(uuid=uuids.source_node)
        dest_node = objects.ComputeNode(uuid=uuids.dest_node)

        @mock.patch.object(self.task.scheduler_client.reportclient,
                           'get_allocations_for_instance', return_value={})
        @mock.patch.object(self.task.scheduler_client.reportclient,
                           'claim_resources',
                           new_callable=mock.NonCallableMock)
        def test(mock_claim, mock_get_allocs):
            self.task._claim_resources_on_destination(source_node, dest_node)
            mock_get_allocs.assert_called_once_with(
                uuids.source_node, self.instance)

        test()
示例#11
0
    def test_create_instances_here_pops_problematic_properties(
            self, mock_update):
        values = {
            'uuid': uuidsentinel.instance,
            'metadata': [],
            'id': 1,
            'name': 'foo',
            'info_cache': 'bar',
            'security_groups': 'not secure',
            'flavor': 'chocolate',
            'pci_requests': 'no thanks',
            'ec2_ids': 'prime',
        }
        block_device_mapping = [
            objects.BlockDeviceMapping(
                context=self.ctxt,
                **fake_block_device.FakeDbBlockDeviceDict(
                    block_device.create_image_bdm(uuidsentinel.fake_image_ref),
                    anon=True))
        ]

        @mock.patch.object(self.scheduler.compute_api,
                           'create_db_entry_for_new_instance')
        @mock.patch.object(self.scheduler.compute_api,
                           '_bdm_validate_set_size_and_instance')
        def test(mock_bdm_validate, mock_create_db):
            self.scheduler._create_instances_here(self.ctxt,
                                                  [uuidsentinel.instance],
                                                  values, objects.Flavor(),
                                                  'foo', [],
                                                  block_device_mapping)

        test()

        # NOTE(danms): Make sure that only the expected properties
        # are applied to the instance object. The complex ones that
        # would have been mangled over RPC should be removed.
        mock_update.assert_called_once_with({
            'uuid': uuidsentinel.instance,
            'metadata': {}
        })
示例#12
0
    def test_claim_resources_on_destination_claim_fails(self):
        """Tests the negative scenario where the resource allocation claim
        on the destination compute node fails, resulting in an error.
        """
        reportclient = report.SchedulerReportClient()
        instance = fake_instance.fake_instance_obj(
            nova_context.get_admin_context())
        source_node = objects.ComputeNode(uuid=uuids.source_node,
                                          host=instance.host)
        dest_node = objects.ComputeNode(uuid=uuids.dest_node, host='dest-host')
        source_res_allocs = {
            'VCPU': instance.vcpus,
            'MEMORY_MB': instance.memory_mb,
            # This would really include ephemeral and swap too but we're lazy.
            'DISK_GB': instance.root_gb
        }
        dest_alloc_request = {
            'allocations': [{
                'resource_provider': {
                    'uuid': uuids.dest_node
                },
                'resources': source_res_allocs
            }]
        }

        @mock.patch.object(reportclient,
                           'get_allocations_for_instance',
                           return_value=source_res_allocs)
        @mock.patch.object(reportclient, 'claim_resources', return_value=False)
        def test(mock_claim, mock_get_allocs):
            self.assertRaises(exception.NoValidHost,
                              utils.claim_resources_on_destination,
                              reportclient, instance, source_node, dest_node)
            mock_get_allocs.assert_called_once_with(uuids.source_node,
                                                    instance)
            mock_claim.assert_called_once_with(instance.uuid,
                                               dest_alloc_request,
                                               instance.project_id,
                                               instance.user_id)

        test()
示例#13
0
    def _test_delete_instance_no_cell(self, method_name, mock_local_delete):
        cells_rpcapi = self.compute_api.cells_rpcapi
        inst = self._create_fake_instance_obj()
        delete_type = method_name == 'soft_delete' and 'soft' or 'hard'

        @mock.patch.object(cells_rpcapi, 'instance_delete_everywhere')
        @mock.patch.object(compute_api.API,
                           '_lookup_instance',
                           return_value=(None, inst))
        def test(mock_lookup, mock_inst_del):
            self.stub_out('nova.network.api.deallocate_for_instance',
                          lambda *a, **kw: None)
            getattr(self.compute_api, method_name)(self.context, inst)
            mock_lookup.assert_called_once_with(self.context, inst.uuid)
            mock_local_delete.assert_called_once_with(self.context, inst,
                                                      mock.ANY, method_name,
                                                      mock.ANY)
            mock_inst_del.assert_called_once_with(self.context, inst,
                                                  delete_type)

        test()
示例#14
0
    def _test_delete_instance_no_cell(self, method_name, mock_local_delete):
        cells_rpcapi = self.compute_api.cells_rpcapi
        inst = self._create_fake_instance_obj()
        delete_type = method_name == 'soft_delete' and 'soft' or 'hard'

        @mock.patch.object(cells_rpcapi,
                           'instance_delete_everywhere')
        @mock.patch.object(compute_api.API, '_lookup_instance',
                           return_value=(None, inst))
        def test(mock_lookup, mock_inst_del):
            self.stub_out('nova.network.api.deallocate_for_instance',
                          lambda *a, **kw: None)
            getattr(self.compute_api, method_name)(self.context, inst)
            mock_lookup.assert_called_once_with(self.context, inst.uuid)
            mock_local_delete.assert_called_once_with(self.context, inst,
                                                      mock.ANY, method_name,
                                                      mock.ANY)
            mock_inst_del.assert_called_once_with(self.context,
                                                  inst, delete_type)

        test()
示例#15
0
    def test_delete_instance_no_cell_then_cell(self, mock_delete,
                                               mock_lookup_instance,
                                               mock_local_delete,
                                               mock_delete_while_booting):
        # This checks the case where initially an instance has no cell_name,
        # and therefore no host, set but instance.destroy fails because
        # there is now a host.
        instance = self._create_fake_instance_obj()
        instance_with_cell = copy.deepcopy(instance)
        instance_with_cell.cell_name = 'foo'
        mock_lookup_instance.return_value = instance_with_cell

        cells_rpcapi = self.compute_api.cells_rpcapi

        @mock.patch.object(cells_rpcapi, 'instance_delete_everywhere')
        def test(mock_inst_delete_everywhere):
            self.compute_api.delete(self.context, instance)
            mock_local_delete.assert_not_called()
            mock_delete.assert_called_once_with(self.context,
                                                instance_with_cell)

        test()
示例#16
0
    def test_claim_resources_on_destination_claim_fails(self):
        """Tests the negative scenario where the resource allocation claim
        on the destination compute node fails, resulting in an error.
        """
        source_node = objects.ComputeNode(uuid=uuids.source_node)
        dest_node = objects.ComputeNode(uuid=uuids.dest_node)
        source_res_allocs = {
            'VCPU': self.instance.vcpus,
            'MEMORY_MB': self.instance.memory_mb,
            # This would really include ephemeral and swap too but we're lazy.
            'DISK_GB': self.instance.root_gb
        }
        dest_alloc_request = {
            'allocations': [{
                'resource_provider': {
                    'uuid': uuids.dest_node
                },
                'resources': source_res_allocs
            }]
        }

        @mock.patch.object(self.task.scheduler_client.reportclient,
                           'get_allocations_for_instance',
                           return_value=source_res_allocs)
        @mock.patch.object(self.task.scheduler_client.reportclient,
                           'claim_resources',
                           return_value=False)
        def test(mock_claim, mock_get_allocs):
            self.assertRaises(exception.MigrationPreCheckError,
                              self.task._claim_resources_on_destination,
                              source_node, dest_node)
            mock_get_allocs.assert_called_once_with(uuids.source_node,
                                                    self.instance)
            mock_claim.assert_called_once_with(self.instance.uuid,
                                               dest_alloc_request,
                                               self.instance.project_id,
                                               self.instance.user_id)

        test()
示例#17
0
    def test_claim_resources_on_destination(self):
        """Happy path test where everything is successful."""
        reportclient = report.SchedulerReportClient()
        instance = fake_instance.fake_instance_obj(
            nova_context.get_admin_context())
        source_node = objects.ComputeNode(
            uuid=uuids.source_node, host=instance.host)
        dest_node = objects.ComputeNode(uuid=uuids.dest_node, host='dest-host')
        source_res_allocs = {
            'VCPU': instance.vcpus,
            'MEMORY_MB': instance.memory_mb,
            # This would really include ephemeral and swap too but we're lazy.
            'DISK_GB': instance.root_gb
        }
        dest_alloc_request = {
            'allocations': [
                {
                    'resource_provider': {
                        'uuid': uuids.dest_node
                    },
                    'resources': source_res_allocs
                }
            ]
        }

        @mock.patch.object(reportclient,
                           'get_allocations_for_consumer_by_provider')
        @mock.patch.object(reportclient,
                           'claim_resources', return_value=True)
        def test(mock_claim, mock_get_allocs):
            utils.claim_resources_on_destination(
                reportclient, instance, source_node, dest_node,
                source_res_allocs)
            self.assertFalse(mock_get_allocs.called)
            mock_claim.assert_called_once_with(
                instance.uuid, dest_alloc_request,
                instance.project_id, instance.user_id)

        test()
示例#18
0
    def test_delete_instance_no_cell_then_cell(self, mock_delete,
                                               mock_lookup_instance,
                                               mock_local_delete,
                                               mock_delete_while_booting):
        # This checks the case where initially an instance has no cell_name,
        # and therefore no host, set but instance.destroy fails because
        # there is now a host.
        instance = self._create_fake_instance_obj()
        instance_with_cell = copy.deepcopy(instance)
        instance_with_cell.cell_name = 'foo'
        mock_lookup_instance.return_value = instance_with_cell

        cells_rpcapi = self.compute_api.cells_rpcapi

        @mock.patch.object(cells_rpcapi, 'instance_delete_everywhere')
        def test(mock_inst_delete_everywhere):
            self.compute_api.delete(self.context, instance)
            mock_local_delete.assert_not_called()
            mock_delete.assert_called_once_with(self.context,
                                                instance_with_cell)

        test()
示例#19
0
    def test_create_instances_here_pops_problematic_properties(self,
                                                               mock_update):
        values = {
            'uuid': uuidsentinel.instance,
            'metadata': [],
            'id': 1,
            'name': 'foo',
            'info_cache': 'bar',
            'security_groups': 'not secure',
            'flavor': 'chocolate',
            'pci_requests': 'no thanks',
            'ec2_ids': 'prime',
        }
        block_device_mapping = [
                objects.BlockDeviceMapping(context=self.ctxt,
                    **fake_block_device.FakeDbBlockDeviceDict(
                            block_device.create_image_bdm(
                                uuidsentinel.fake_image_ref),
                            anon=True))
               ]

        @mock.patch.object(self.scheduler.compute_api,
                           'create_db_entry_for_new_instance')
        @mock.patch.object(self.scheduler.compute_api,
                           '_bdm_validate_set_size_and_instance')
        def test(mock_bdm_validate, mock_create_db):
            self.scheduler._create_instances_here(
                self.ctxt, [uuidsentinel.instance], values,
                objects.Flavor(), 'foo', [], block_device_mapping)

        test()

        # NOTE(danms): Make sure that only the expected properties
        # are applied to the instance object. The complex ones that
        # would have been mangled over RPC should be removed.
        mock_update.assert_called_once_with(
            {'uuid': uuidsentinel.instance,
             'metadata': {}})
示例#20
0
    def test_claim_resources_on_destination_no_source_allocations(self):
        """Tests the negative scenario where the instance does not have
        allocations in Placement on the source compute node so no claim is
        attempted on the destination compute node.
        """
        reportclient = report.SchedulerReportClient()
        instance = fake_instance.fake_instance_obj(self.context)
        source_node = objects.ComputeNode(
            uuid=uuids.source_node, host=instance.host)
        dest_node = objects.ComputeNode(uuid=uuids.dest_node, host='dest-host')

        @mock.patch.object(reportclient,
                           'get_allocations_for_consumer_by_provider',
                           return_value={})
        @mock.patch.object(reportclient,
                           'claim_resources',
                           new_callable=mock.NonCallableMock)
        def test(mock_claim, mock_get_allocs):
            utils.claim_resources_on_destination(
                self.context, reportclient, instance, source_node, dest_node)
            mock_get_allocs.assert_called_once_with(
                self.context, uuids.source_node, instance.uuid)

        test()
示例#21
0
    def test_claim_resources_on_destination_no_source_allocations(self):
        """Tests the negative scenario where the instance does not have
        allocations in Placement on the source compute node so no claim is
        attempted on the destination compute node.
        """
        reportclient = report.SchedulerReportClient()
        instance = fake_instance.fake_instance_obj(self.context)
        source_node = objects.ComputeNode(
            uuid=uuids.source_node, host=instance.host)
        dest_node = objects.ComputeNode(uuid=uuids.dest_node, host='dest-host')

        @mock.patch.object(reportclient,
                           'get_allocs_for_consumer',
                           return_value={})
        @mock.patch.object(reportclient,
                           'claim_resources',
                           new_callable=mock.NonCallableMock)
        def test(mock_claim, mock_get_allocs):
            utils.claim_resources_on_destination(
                self.context, reportclient, instance, source_node, dest_node)
            mock_get_allocs.assert_called_once_with(
                self.context, instance.uuid)

        test()