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 fake_spec_obj(remove_id=False): ctxt = context.RequestContext('fake', 'fake') req_obj = objects.RequestSpec(ctxt) if not remove_id: req_obj.id = 42 req_obj.instance_uuid = uuidutils.generate_uuid() req_obj.image = IMAGE_META req_obj.numa_topology = INSTANCE_NUMA_TOPOLOGY req_obj.pci_requests = PCI_REQUESTS req_obj.flavor = fake_flavor.fake_flavor_obj(ctxt) req_obj.retry = objects.SchedulerRetries() req_obj.limits = objects.SchedulerLimits() req_obj.instance_group = objects.InstanceGroup(uuid=uuids.instgroup) req_obj.project_id = 'fake' req_obj.user_id = 'fake-user' req_obj.num_instances = 1 req_obj.availability_zone = None req_obj.ignore_hosts = ['host2', 'host4'] req_obj.force_hosts = ['host1', 'host3'] req_obj.force_nodes = ['node1', 'node2'] req_obj.scheduler_hints = {'hint': ['over-there']} req_obj.requested_destination = None # This should never be a changed field req_obj.obj_reset_changes(['id']) return req_obj
def test_to_legacy_filter_properties_dict(self): fake_numa_limits = objects.NUMATopologyLimits() fake_computes_obj = objects.ComputeNodeList( objects=[objects.ComputeNode(host='fake1', hypervisor_hostname='node1')]) spec = objects.RequestSpec( ignore_hosts=['ignoredhost'], force_hosts=['fakehost'], force_nodes=['fakenode'], retry=objects.SchedulerRetries(num_attempts=1, hosts=fake_computes_obj), limits=objects.SchedulerLimits(numa_topology=fake_numa_limits, vcpu=1.0, disk_gb=10.0, memory_mb=8192.0), instance_group=objects.InstanceGroup(hosts=['fake1'], policies=['affinity']), scheduler_hints={'foo': ['bar']}) expected = {'ignore_hosts': ['ignoredhost'], 'force_hosts': ['fakehost'], 'force_nodes': ['fakenode'], 'retry': {'num_attempts': 1, 'hosts': [['fake1', 'node1']]}, 'limits': {'numa_topology': fake_numa_limits, 'vcpu': 1.0, 'disk_gb': 10.0, 'memory_mb': 8192.0}, 'group_updated': True, 'group_hosts': set(['fake1']), 'group_policies': set(['affinity']), 'scheduler_hints': {'foo': 'bar'}} self.assertEqual(expected, spec.to_legacy_filter_properties_dict())