def test_save_does_not_persist_requested_fields(self): req_obj = fake_request_spec.fake_spec_obj(remove_id=True) req_obj.create() # change something to make sure _save_in_db is called expected_destination = request_spec.Destination(host='sample-host') req_obj.requested_destination = expected_destination expected_retry = objects.SchedulerRetries( num_attempts=2, hosts=objects.ComputeNodeList(objects=[ objects.ComputeNode(host='host1', hypervisor_hostname='node1'), objects.ComputeNode(host='host2', hypervisor_hostname='node2'), ])) req_obj.retry = expected_retry req_obj.ignore_hosts = [uuids.ignored_host] orig_save_in_db = request_spec.RequestSpec._save_in_db with mock.patch.object(request_spec.RequestSpec, '_save_in_db') \ as mock_save_in_db: mock_save_in_db.side_effect = orig_save_in_db req_obj.save() mock_save_in_db.assert_called_once() updates = mock_save_in_db.mock_calls[0][1][2] # assert that the following fields are not stored in the db # 1. ignore_hosts data = jsonutils.loads(updates['spec'])['nova_object.data'] self.assertIsNone(data['ignore_hosts']) self.assertIsNotNone(data['instance_uuid']) # also we expect that the following fields are not reset after save # 1. ignore_hosts self.assertIsNotNone(req_obj.ignore_hosts) self.assertEqual([uuids.ignored_host], req_obj.ignore_hosts)
def setUp(self): super(ComputeRpcAPITestCase, self).setUp() self.context = context.get_admin_context() self.fake_flavor_obj = fake_flavor.fake_flavor_obj(self.context) self.fake_flavor = jsonutils.to_primitive(self.fake_flavor_obj) instance_attr = {'host': 'fake_host', 'instance_type_id': self.fake_flavor_obj['id'], 'instance_type': self.fake_flavor_obj} self.fake_instance_obj = fake_instance.fake_instance_obj(self.context, **instance_attr) self.fake_instance = jsonutils.to_primitive(self.fake_instance_obj) self.fake_volume_bdm = objects_block_dev.BlockDeviceMapping( **fake_block_device.FakeDbBlockDeviceDict( {'source_type': 'volume', 'destination_type': 'volume', 'instance_uuid': self.fake_instance_obj.uuid, 'volume_id': 'fake-volume-id'})) self.fake_request_spec_obj = fake_request_spec.fake_spec_obj() # FIXME(melwitt): Temporary while things have no mappings self.patcher1 = mock.patch('nova.objects.InstanceMapping.' 'get_by_instance_uuid') self.patcher2 = mock.patch('nova.objects.HostMapping.get_by_host') mock_inst_mapping = self.patcher1.start() mock_host_mapping = self.patcher2.start() mock_inst_mapping.side_effect = exception.InstanceMappingNotFound( uuid=self.fake_instance_obj.uuid) mock_host_mapping.side_effect = exception.HostMappingNotFound( name=self.fake_instance_obj.host)
def test_save(self): req_obj = fake_request_spec.fake_spec_obj() def _test_save_args(self2, context, instance_uuid, changes): self._check_update_primitive(req_obj, changes) # DB creation would have set an id changes["id"] = 42 return changes with mock.patch.object(request_spec.RequestSpec, "_save_in_db", _test_save_args): req_obj.save()
def test_reset_forced_destinations(self): req_obj = fake_request_spec.fake_spec_obj() # Making sure the fake object has forced hosts and nodes self.assertIsNotNone(req_obj.force_hosts) self.assertIsNotNone(req_obj.force_nodes) with mock.patch.object(req_obj, 'obj_reset_changes') as mock_reset: req_obj.reset_forced_destinations() self.assertIsNone(req_obj.force_hosts) self.assertIsNone(req_obj.force_nodes) mock_reset.assert_called_once_with(['force_hosts', 'force_nodes'])
def _create_req(self): req_spec = fake_request_spec.fake_spec_obj(remove_id=True) req_spec.instance_uuid = self.instance_uuid req_spec.create() args = fake_build_request.fake_db_req(request_spec_id=req_spec.id) args.pop('id', None) args.pop('request_spec', None) args['project_id'] = self.project_id return build_request.BuildRequest._from_db_object( self.context, self.build_req_obj, self.build_req_obj._create_in_db(self.context, args))
def test_create(self): req_obj = fake_request_spec.fake_spec_obj(remove_id=True) def _test_create_args(self2, context, changes): self._check_update_primitive(req_obj, changes) # DB creation would have set an id changes['id'] = 42 return changes with mock.patch.object(request_spec.RequestSpec, '_create_in_db', _test_create_args): req_obj.create()
def _create_req(self): req_spec = fake_request_spec.fake_spec_obj(remove_id=True) req_spec.instance_uuid = self.instance_uuid req_spec.create() args = fake_build_request.fake_db_req( request_spec_id=req_spec.id) args.pop('id', None) args.pop('request_spec', None) args['project_id'] = self.project_id return build_request.BuildRequest._from_db_object(self.context, self.build_req_obj, self.build_req_obj._create_in_db(self.context, args))
def fake_db_req(**updates): ctxt = context.RequestContext('fake-user', 'fake-project') instance_uuid = uuidutils.generate_uuid() info_cache = objects.InstanceInfoCache() info_cache.instance_uuid = instance_uuid info_cache.network_info = network_model.NetworkInfo() req_spec = fake_request_spec.fake_spec_obj( context.RequestContext('fake-user', 'fake-project')) req_spec.id = 42 req_spec.obj_reset_changes() instance = fake_instance.fake_instance_obj(ctxt, objects.Instance, uuid=instance_uuid) db_build_request = { 'id': 1, 'project_id': 'fake-project', 'instance_uuid': instance_uuid, 'user_id': 'fake-user', 'display_name': '', 'instance_metadata': jsonutils.dumps({'foo': 'bar'}), 'progress': 0, 'vm_state': vm_states.BUILDING, 'task_state': task_states.SCHEDULING, 'image_ref': None, 'access_ip_v4': '1.2.3.4', 'access_ip_v6': '::1', 'info_cache': jsonutils.dumps(info_cache.obj_to_primitive()), 'security_groups': jsonutils.dumps( objects.SecurityGroupList().obj_to_primitive()), 'config_drive': False, 'key_name': None, 'locked_by': None, 'request_spec': _req_spec_to_db_format(req_spec), 'instance': jsonutils.dumps(instance.obj_to_primitive()), 'created_at': datetime.datetime(2016, 1, 16), 'updated_at': datetime.datetime(2016, 1, 16), } for name, field in objects.BuildRequest.fields.items(): if name in db_build_request: continue if field.nullable: db_build_request[name] = None elif field.default != fields.UnspecifiedDefault: db_build_request[name] = field.default else: raise Exception('fake_db_req needs help with %s' % name) if updates: db_build_request.update(updates) return db_build_request
def test_metrics_filter_missing_metrics(self): _ts_now = datetime.datetime(2015, 11, 11, 11, 0, 0) obj1 = objects.MonitorMetric(name='cpu.frequency', value=1000, timestamp=_ts_now, source='nova.virt.libvirt.driver') metrics_list = objects.MonitorMetricList(objects=[obj1]) self.flags(weight_setting=['foo=1', 'bar=2'], group='metrics') filt_cls = metrics_filter.MetricsFilter() host = fakes.FakeHostState('host1', 'node1', attribute_dict={'metrics': metrics_list}) spec_obj = fake_request_spec.fake_spec_obj() self.assertFalse(filt_cls.host_passes(host, spec_obj))
def test_save(self): req_obj = fake_request_spec.fake_spec_obj() # Make sure the requested_destination is not persisted since it is # only valid per request/operation. req_obj.requested_destination = objects.Destination(host='fake') def _test_save_args(self2, context, instance_uuid, changes): self._check_update_primitive(req_obj, changes) # DB creation would have set an id changes['id'] = 42 return changes with mock.patch.object(request_spec.RequestSpec, '_save_in_db', _test_save_args): req_obj.save()
def test_create_does_not_persist_requested_resources(self): req_obj = fake_request_spec.fake_spec_obj(remove_id=True) rg = request_spec.RequestGroup(resources={'fake-rc': 13}) req_obj.requested_resources = [rg] orig_create_in_db = request_spec.RequestSpec._create_in_db with mock.patch.object(request_spec.RequestSpec, '_create_in_db') \ as mock_create_in_db: mock_create_in_db.side_effect = orig_create_in_db req_obj.create() mock_create_in_db.assert_called_once() updates = mock_create_in_db.mock_calls[0][1][1] # assert that the requested_resources field is not stored in the db data = jsonutils.loads(updates['spec'])['nova_object.data'] self.assertIsNone(data['requested_resources']) self.assertIsNotNone(data['instance_uuid']) # also we expect that requested_resources field does not reset after # create self.assertEqual(13, req_obj.requested_resources[0].resources['fake-rc'])
def test_create_does_not_persist_requested_resources(self): req_obj = fake_request_spec.fake_spec_obj(remove_id=True) rg = request_spec.RequestGroup(resources={'fake-rc': 13}) req_obj.requested_resources = [rg] orig_create_in_db = request_spec.RequestSpec._create_in_db with mock.patch.object(request_spec.RequestSpec, '_create_in_db') \ as mock_create_in_db: mock_create_in_db.side_effect = orig_create_in_db req_obj.create() mock_create_in_db.assert_called_once() updates = mock_create_in_db.mock_calls[0][1][1] # assert that the requested_resources field is not stored in the db data = jsonutils.loads(updates['spec'])['nova_object.data'] self.assertIsNone(data['requested_resources']) self.assertIsNotNone(data['instance_uuid']) # also we expect that requested_resources field does not reset after # create self.assertEqual( 13, req_obj.requested_resources[0].resources['fake-rc'])
def test_metrics_filter_pass(self): _ts_now = datetime.datetime(2015, 11, 11, 11, 0, 0) obj1 = objects.MonitorMetric(name='cpu.frequency', value=1000, timestamp=_ts_now, source='nova.virt.libvirt.driver') obj2 = objects.MonitorMetric(name='numa.membw.current', numa_membw_values={ "0": 10, "1": 43 }, timestamp=_ts_now, source='nova.virt.libvirt.driver') metrics_list = objects.MonitorMetricList(objects=[obj1, obj2]) self.flags(weight_setting=['cpu.frequency=1', 'numa.membw.current=2'], group='metrics') filt_cls = metrics_filter.MetricsFilter() host = fakes.FakeHostState('host1', 'node1', attribute_dict={'metrics': metrics_list}) spec_obj = fake_request_spec.fake_spec_obj() self.assertTrue(filt_cls.host_passes(host, spec_obj))
def test_destroy(self, destroy_in_db): req_obj = fake_request_spec.fake_spec_obj() req_obj.destroy() destroy_in_db.assert_called_once_with(req_obj._context, req_obj.instance_uuid)
def test_save_can_be_called_on_unchanged_object(self): req_obj = fake_request_spec.fake_spec_obj(remove_id=True) req_obj.create() req_obj.save()