def _create_compute_service(self): """Create compute-manager(ComputeNode and Service record).""" ctxt = self.req.environ["nova.context"] dic = { 'host': 'dummy', 'binary': 'nova-compute', 'topic': 'compute', 'report_count': 0 } s_ref = db.service_create(ctxt, dic) dic = { 'service_id': s_ref['id'], 'host': s_ref['host'], 'uuid': uuidsentinel.compute_node, 'vcpus': 16, 'memory_mb': 32, 'local_gb': 100, 'vcpus_used': 16, 'memory_mb_used': 32, 'local_gb_used': 10, 'hypervisor_type': 'qemu', 'hypervisor_version': 12003, 'cpu_info': '', 'stats': '' } db.compute_node_create(ctxt, dic) return db.service_get(ctxt, s_ref['id'])
def _create_service_with_topic(self, topic, host, disabled=False): values = { 'binary': 'nova-bin', 'host': host, 'topic': topic, 'disabled': disabled, } return db.service_create(self.context, values)
def _create_instance_with_availability_zone(self, zone_name): def create(*args, **kwargs): self.assertIn('availability_zone', kwargs) self.assertEqual('nova', kwargs['availability_zone']) return old_create(*args, **kwargs) old_create = compute_api.API.create self.stub_out('nova.compute.api.API.create', create) image_href = '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6' flavor_ref = ('http://localhost' + self.base_url + 'flavors/3') body = { 'server': { 'name': 'server_test', 'imageRef': image_href, 'flavorRef': flavor_ref, 'metadata': { 'hello': 'world', 'open': 'stack', }, 'availability_zone': zone_name, }, } req = fakes.HTTPRequest.blank('') req.method = 'POST' req.body = jsonutils.dump_as_bytes(body) req.headers['content-type'] = 'application/json' admin_context = context.get_admin_context() db.service_create( admin_context, { 'host': 'host1_zones', 'binary': "nova-compute", 'topic': 'compute', 'report_count': 0 }) agg = objects.Aggregate(admin_context, name='agg1', uuid=uuidsentinel.agg_uuid, metadata={'availability_zone': 'nova'}) agg.create() agg.add_host('host1_zones') return req, body
def test_populate_missing_availability_zones(self): # create two instances once with avz set and other not set. inst1 = self._create_instance(host="fake-host1") uuid1 = inst1.uuid inst2 = self._create_instance(availability_zone="fake", host="fake-host2") # ... and one without a host (simulating failed spawn) self._create_instance(host=None) self.assertIsNone(inst1.availability_zone) self.assertEqual("fake", inst2.availability_zone) count_all, count_hit = ( objects.instance.populate_missing_availability_zones( self.context, 10)) # we get only the instance whose avz was None and where host is set self.assertEqual(1, count_all) self.assertEqual(1, count_hit) # since instance has no avz, avz is set by get_host_availability_zone # to CONF.default_availability_zone i.e 'nova' which is the default # zone for compute services. inst1 = objects.Instance.get_by_uuid(self.context, uuid1) self.assertEqual('nova', inst1.availability_zone) # create an instance with avz as None on a host that has avz. host = 'fake-host' agg_meta = { 'name': 'az_agg', 'uuid': uuidutils.generate_uuid(), 'metadata': { 'availability_zone': 'nova-test' } } agg = objects.Aggregate(self.context, **agg_meta) agg.create() agg = objects.Aggregate.get_by_id(self.context, agg.id) values = { 'binary': 'nova-compute', 'host': host, 'topic': 'compute', 'disabled': False, } service = db.service_create(self.context, values) agg.add_host(service['host']) inst3 = self._create_instance(host=host) uuid3 = inst3.uuid self.assertIsNone(inst3.availability_zone) count_all, count_hit = ( objects.instance.populate_missing_availability_zones( self.context, 10)) # we get only the instance whose avz was None i.e inst3. self.assertEqual(1, count_all) self.assertEqual(1, count_hit) inst3 = objects.Instance.get_by_uuid(self.context, uuid3) self.assertEqual('nova-test', inst3.availability_zone)
def create(self): if self.obj_attr_is_set('id'): raise exception.ObjectActionError(action='create', reason='already created') self._check_minimum_version() updates = self.obj_get_changes() if 'uuid' not in updates: updates['uuid'] = uuidutils.generate_uuid() self.uuid = updates['uuid'] db_service = db.service_create(self._context, updates) self._from_db_object(self._context, self, db_service) self._send_notification(fields.NotificationAction.CREATE)
def test_instance_list_old_deleted_service_with_no_uuid(self): # Create a nova-compute service record with a host that will match the # instance's host, with no uuid. We can't do this through the # Service object because it will automatically generate a uuid. # Use service version 9, which is too old compared to the minimum # version in the rest of the deployment. service = db.service_create(self.context, { 'host': 'fake-host', 'binary': 'nova-compute', 'version': 9 }) self.assertIsNone(service['uuid']) # Now delete it. db.service_destroy(self.context, service['id']) # Create a new service with the same host name that has a UUID and a # current version. new_service = objects.Service(context=self.context, host='fake-host', binary='nova-compute') new_service.create() # Create an instance whose host will match both services, including the # deleted one. inst = objects.Instance(context=self.context, project_id=self.context.project_id, host='fake-host') inst.create() insts = objects.InstanceList.get_by_filters( self.context, {}, expected_attrs=['services']) self.assertEqual(1, len(insts)) self.assertEqual(2, len(insts[0].services)) # Deleted service should not have a UUID for service in insts[0].services: if service.deleted: self.assertNotIn('uuid', service) else: self.assertIsNotNone(service.uuid)