def _save_helper(self, cell_type, update_cells, mock_update,
                     mock_rpc, mock_get_type):
        obj = instance_info_cache.InstanceInfoCache()
        cells_api = cells_rpcapi.CellsAPI()

        nwinfo = network_model.NetworkInfo.hydrate([{'address': 'foo'}])
        new_info_cache = fake_info_cache.copy()
        new_info_cache['network_info'] = nwinfo.json()
        mock_update.return_value = new_info_cache
        with mock.patch.object(cells_api,
                               'instance_info_cache_update_at_top'):
            if update_cells:
                mock_get_type.return_vaule = cell_type
                if cell_type == 'compute':
                    mock_rpc.return_value = cells_api
                    cells_api.instance_info_cache_update_at_top(
                        self.context, 'foo')
                mock_rpc.assert_called_once_with()
        obj._context = self.context
        obj.instance_uuid = uuids.info_instance
        obj.network_info = nwinfo
        obj.save(update_cells=update_cells)
        mock_update.asssert_called_once_with(self.context, obj.instance_uuid,
                                             {'network_info': nwinfo.json()})
        if update_cells:
            mock_get_type.assert_called_once()
    def _save_helper(self, cell_type, update_cells):
        obj = instance_info_cache.InstanceInfoCache()
        cells_api = cells_rpcapi.CellsAPI()

        self.mox.StubOutWithMock(db, 'instance_info_cache_update')
        self.mox.StubOutWithMock(cells_opts, 'get_cell_type')
        self.mox.StubOutWithMock(cells_rpcapi,
                                 'CellsAPI',
                                 use_mock_anything=True)
        self.mox.StubOutWithMock(cells_api,
                                 'instance_info_cache_update_at_top')
        nwinfo = network_model.NetworkInfo.hydrate([{'address': 'foo'}])
        db.instance_info_cache_update(self.context, 'fake-uuid', {
            'network_info': nwinfo.json()
        }).AndReturn('foo')
        if update_cells:
            cells_opts.get_cell_type().AndReturn(cell_type)
            if cell_type == 'compute':
                cells_rpcapi.CellsAPI().AndReturn(cells_api)
                cells_api.instance_info_cache_update_at_top(
                    self.context, 'foo')
        self.mox.ReplayAll()
        obj._context = self.context
        obj.instance_uuid = 'fake-uuid'
        obj.network_info = nwinfo
        obj.save(update_cells=update_cells)
 def test_compat_info_cache(self):
     inst = instance.Instance()
     inst.info_cache = instance_info_cache.InstanceInfoCache()
     primitive = inst.obj_to_primitive(target_version='1.9')
     self.assertEqual(
         '1.4',
         primitive['nova_object.data']['info_cache']['nova_object.version'])
Exemple #4
0
 def test_create_with_special_things(self):
     ctxt = context.get_admin_context()
     self.mox.StubOutWithMock(db, 'instance_create')
     fake_inst = fake_instance.fake_db_instance()
     db.instance_create(
         ctxt, {
             'host': 'foo-host',
             'security_groups': ['foo', 'bar'],
             'info_cache': {
                 'network_info': '[]'
             },
         }).AndReturn(fake_inst)
     self.mox.ReplayAll()
     inst = instance.Instance()
     inst.host = 'foo-host'
     secgroups = security_group.SecurityGroupList()
     secgroups.objects = []
     for name in ('foo', 'bar'):
         secgroup = security_group.SecurityGroup()
         secgroup.name = name
         secgroups.objects.append(secgroup)
     inst.security_groups = secgroups
     inst.info_cache = instance_info_cache.InstanceInfoCache()
     inst.info_cache.network_info = []
     inst.create(ctxt)
Exemple #5
0
 def _info_cache_for(instance):
     info_cache = dict(test_instance_info_cache.fake_info_cache,
                       network_info=_get_fake_cache(),
                       instance_uuid=instance['uuid'])
     if isinstance(instance, obj_base.NovaObject):
         _info_cache = instance_info_cache.InstanceInfoCache()
         instance_info_cache.InstanceInfoCache._from_db_object(
             context, _info_cache, info_cache)
         info_cache = _info_cache
     instance['info_cache'] = info_cache
 def test_from_db_object_not_overwrite_info_cache(self):
     info_cache = instance_info_cache.InstanceInfoCache()
     inst = instance.Instance(context=self.context, info_cache=info_cache)
     db_inst = fake_instance.fake_db_instance()
     db_inst['info_cache'] = dict(test_instance_info_cache.fake_info_cache)
     inst._from_db_object(self.context,
                          inst,
                          db_inst,
                          expected_attrs=['info_cache'])
     self.assertIs(info_cache, inst.info_cache)
Exemple #7
0
 def _info_cache_for(instance):
     info_cache = {'network_info': _get_fake_cache(),
                   'instance_uuid': instance['uuid']}
     if isinstance(instance, obj_base.NovaObject):
         _info_cache = instance_info_cache.InstanceInfoCache()
         instance_info_cache.InstanceInfoCache._from_db_object(context,
                                                               _info_cache,
                                                               info_cache)
         info_cache = _info_cache
     instance['info_cache'] = info_cache
 def test_save(self):
     ctxt = context.get_admin_context()
     self.mox.StubOutWithMock(db, 'instance_info_cache_update')
     db.instance_info_cache_update(ctxt, 'fake-uuid',
                                   {'network_info': 'foo'})
     self.mox.ReplayAll()
     obj = instance_info_cache.InstanceInfoCache()
     obj._context = ctxt
     obj.instance_uuid = 'fake-uuid'
     obj.network_info = 'foo'
     obj.save()
    def _from_db_object(context, instance, db_inst, expected_attrs=None):
        """Method to help with migration to objects.

        Converts a database entity to a formal object.
        """
        if expected_attrs is None:
            expected_attrs = []
        # Most of the field names match right now, so be quick
        for field in instance.fields:
            if field in INSTANCE_OPTIONAL_ATTRS:
                continue
            elif field == 'deleted':
                instance.deleted = db_inst['deleted'] == db_inst['id']
            elif field == 'cleaned':
                instance.cleaned = db_inst['cleaned'] == 1
            else:
                instance[field] = db_inst[field]

        if 'metadata' in expected_attrs:
            instance['metadata'] = utils.instance_meta(db_inst)
        if 'system_metadata' in expected_attrs:
            instance['system_metadata'] = utils.instance_sys_meta(db_inst)
        if 'fault' in expected_attrs:
            instance['fault'] = (
                instance_fault.InstanceFault.get_latest_for_instance(
                    context, instance.uuid))

        if 'pci_devices' in expected_attrs:
            pci_devices = base.obj_make_list(context,
                                             pci_device.PciDeviceList(),
                                             pci_device.PciDevice,
                                             db_inst['pci_devices'])
            instance['pci_devices'] = pci_devices
        if 'info_cache' in expected_attrs:
            if db_inst['info_cache'] is None:
                instance.info_cache = None
            elif not instance.obj_attr_is_set('info_cache'):
                # TODO(danms): If this ever happens on a backlevel instance
                # passed to us by a backlevel service, things will break
                instance.info_cache = instance_info_cache.InstanceInfoCache()
            if instance.info_cache is not None:
                instance_info_cache.InstanceInfoCache._from_db_object(
                    context, instance.info_cache, db_inst['info_cache'])
        if 'security_groups' in expected_attrs:
            sec_groups = base.obj_make_list(context,
                                            security_group.SecurityGroupList(),
                                            security_group.SecurityGroup,
                                            db_inst['security_groups'])
            instance['security_groups'] = sec_groups

        instance._context = context
        instance.obj_reset_changes()
        return instance
Exemple #10
0
    def _from_db_object(context, instance, db_inst, expected_attrs=None):
        """Method to help with migration to objects.

        Converts a database entity to a formal object.
        """
        if expected_attrs is None:
            expected_attrs = []
        # Most of the field names match right now, so be quick
        for field in instance.fields:
            if field in INSTANCE_OPTIONAL_FIELDS + INSTANCE_IMPLIED_FIELDS:
                continue
            elif field == 'deleted':
                instance.deleted = db_inst['deleted'] == db_inst['id']
            elif field == 'cleaned':
                instance.cleaned = db_inst['cleaned'] == 1
            else:
                instance[field] = db_inst[field]

        if 'metadata' in expected_attrs:
            instance['metadata'] = utils.instance_meta(db_inst)
        if 'system_metadata' in expected_attrs:
            instance['system_metadata'] = utils.instance_sys_meta(db_inst)
        if 'fault' in expected_attrs:
            instance['fault'] = (
                instance_fault.InstanceFault.get_latest_for_instance(
                    context, instance.uuid))

        if 'pci_devices' in expected_attrs:
            if db_inst['pci_devices'] is None:
                pci_devices = None
            else:
                pci_devices = pci_device._make_pci_list(
                        context, pci_device.PciDeviceList(),
                        db_inst['pci_devices'])
            instance['pci_devices'] = pci_devices

        # NOTE(danms): info_cache and security_groups are almost
        # always joined in the DB layer right now, so check to see if
        # they are asked for and are present in the resulting object
        if 'info_cache' in expected_attrs and db_inst.get('info_cache'):
            instance['info_cache'] = instance_info_cache.InstanceInfoCache()
            instance_info_cache.InstanceInfoCache._from_db_object(
                    context, instance['info_cache'], db_inst['info_cache'])
        if ('security_groups' in expected_attrs and
                db_inst.get('security_groups') is not None):
            instance['security_groups'] = security_group.SecurityGroupList()
            security_group._make_secgroup_list(context,
                                               instance['security_groups'],
                                               db_inst['security_groups'])

        instance._context = context
        instance.obj_reset_changes()
        return instance
Exemple #11
0
    def _create_instance_obj(self, params=None, flavor=None):
        """Create a test instance."""
        if not params:
            params = {}

        if flavor is None:
            flavor = self._create_flavor()

        def make_fake_sys_meta():
            sys_meta = {}
            for key in flavors.system_metadata_flavor_props:
                sys_meta['instance_type_%s' % key] = flavor[key]
            return sys_meta

        now = timeutils.utcnow()

        instance = instance_obj.Instance()
        instance._context = self.context
        instance.id = 1
        instance.uuid = uuidutils.generate_uuid()
        instance.cell_name = 'api!child'
        instance.vm_state = vm_states.ACTIVE
        instance.task_state = None
        instance.image_ref = FAKE_IMAGE_REF
        instance.reservation_id = 'r-fakeres'
        instance.user_id = self.user_id
        instance.project_id = self.project_id
        instance.host = 'fake_host'
        instance.node = NODENAME
        instance.instance_type_id = flavor['id']
        instance.ami_launch_index = 0
        instance.memory_mb = 0
        instance.vcpus = 0
        instance.root_gb = 0
        instance.ephemeral_gb = 0
        instance.architecture = 'x86_64'
        instance.os_type = 'Linux'
        instance.system_metadata = make_fake_sys_meta()
        instance.locked = False
        instance.created_at = now
        instance.updated_at = now
        instance.launched_at = now
        instance.disable_terminate = False
        instance.info_cache = instance_info_cache.InstanceInfoCache()

        if params:
            instance.update(params)
        instance.obj_reset_changes()
        return instance
 def test_save_updates_self(self, mock_update):
     fake_updated_at = datetime.datetime(2015, 1, 1)
     nwinfo = network_model.NetworkInfo.hydrate([{'address': 'foo'}])
     nwinfo_json = nwinfo.json()
     new_info_cache = fake_info_cache.copy()
     new_info_cache['id'] = 1
     new_info_cache['updated_at'] = fake_updated_at
     new_info_cache['network_info'] = nwinfo_json
     mock_update.return_value = new_info_cache
     obj = instance_info_cache.InstanceInfoCache(context=self.context)
     obj.instance_uuid = uuids.info_instance
     obj.network_info = nwinfo_json
     obj.save()
     mock_update.assert_called_once_with(self.context, uuids.info_instance,
                                         {'network_info': nwinfo_json})
     self.assertEqual(timeutils.normalize_time(fake_updated_at),
                      timeutils.normalize_time(obj.updated_at))
    def _from_db_object(context, instance, db_inst, expected_attrs=None):
        """Method to help with migration to objects.

        Converts a database entity to a formal object.
        """
        if expected_attrs is None:
            expected_attrs = []
        # Most of the field names match right now, so be quick
        for field in instance.fields:
            if field in INSTANCE_OPTIONAL_FIELDS + INSTANCE_IMPLIED_FIELDS:
                continue
            elif field == 'deleted':
                instance.deleted = db_inst['deleted'] == db_inst['id']
            else:
                instance[field] = db_inst[field]

        if 'metadata' in expected_attrs:
            instance['metadata'] = utils.metadata_to_dict(db_inst['metadata'])
        if 'system_metadata' in expected_attrs:
            instance['system_metadata'] = utils.metadata_to_dict(
                db_inst['system_metadata'])
        if 'fault' in expected_attrs:
            instance['fault'] = (
                instance_fault.InstanceFault.get_latest_for_instance(
                    context, instance.uuid))
        # NOTE(danms): info_cache and security_groups are almost always joined
        # in the DB layer right now, so check to see if they're filled instead
        # of looking at expected_attrs
        if db_inst['info_cache']:
            instance['info_cache'] = instance_info_cache.InstanceInfoCache()
            instance_info_cache.InstanceInfoCache._from_db_object(
                context, instance['info_cache'], db_inst['info_cache'])
        if db_inst['security_groups']:
            instance['security_groups'] = security_group.SecurityGroupList()
            security_group._make_secgroup_list(context,
                                               instance['security_groups'],
                                               db_inst['security_groups'])

        instance._context = context
        instance.obj_reset_changes()
        return instance
    def test_save_fkey_constraint_fail(self, mock_update):
        fake_updated_at = datetime.datetime(2015, 1, 1)
        nwinfo = network_model.NetworkInfo.hydrate([{'address': 'foo'}])
        nwinfo_json = nwinfo.json()
        new_info_cache = fake_info_cache.copy()
        new_info_cache['id'] = 1
        new_info_cache['updated_at'] = fake_updated_at
        new_info_cache['network_info'] = nwinfo_json

        # We should see InstanceNotFound raised for fkey=instance_uuid
        mock_update.side_effect = db_exc.DBReferenceError(
            'table', 'constraint', 'instance_uuid', 'key_table')

        obj = instance_info_cache.InstanceInfoCache(context=self.context)
        obj.instance_uuid = uuids.info_instance
        obj.network_info = nwinfo_json
        self.assertRaises(exception.InstanceNotFound, obj.save)

        # We should see the original exception raised for any other fkey
        mock_update.side_effect = db_exc.DBReferenceError(
            'table', 'constraint', 'otherkey', 'key_table')
        self.assertRaises(db_exc.DBReferenceError, obj.save)