diff --git a/nova/tests/test_db_api.py b/nova/tests/test_db_api.py index 19337855..79005d10 100644 --- a/nova/tests/test_db_api.py +++ b/nova/tests/test_db_api.py @@ -45,9 +45,45 @@ class DbApiTestCase(test.TestCase): def create_instances_with_args(self, **kwargs): args = {'reservation_id': 'a', 'image_ref': 1, 'host': 'host1', - 'project_id': self.project_id} + 'project_id': self.project_id, 'vm_state': 'fake'} + if 'context' in kwargs: + ctxt = kwargs.pop('context') + args['project_id'] = ctxt.project_id + else: + ctxt = self.context args.update(kwargs) - return db.instance_create(self.context, args) + return db.instance_create(ctxt, args) + + def test_create_instance_unique_hostname(self): + otherprojectcontext = context.RequestContext(self.user_id, + "%s2" % self.project_id) + + self.create_instances_with_args(hostname='fake_name') + + # With scope 'global' any duplicate should fail, be it this project: + self.flags(osapi_compute_unique_server_name_scope='global') + self.assertRaises(exception.InstanceExists, + self.create_instances_with_args, + hostname='fake_name') + + # or another: + self.assertRaises(exception.InstanceExists, + self.create_instances_with_args, + context=otherprojectcontext, + hostname='fake_name') + + # With scope 'project' a duplicate in the project should fail: + self.flags(osapi_compute_unique_server_name_scope='project') + self.assertRaises(exception.InstanceExists, + self.create_instances_with_args, + hostname='fake_name') + + # With scope 'project' a duplicate in a different project should work: + self.flags(osapi_compute_unique_server_name_scope='project') + self.create_instances_with_args(context=otherprojectcontext, + hostname='fake_name') + + self.flags(osapi_compute_unique_server_name_scope=None) def test_ec2_ids_not_found_are_printable(self): def check_exc_format(method): @@ -291,6 +327,56 @@ class DbApiTestCase(test.TestCase): self.assertEqual(instance['instance_type']['name'], inst_type2['name']) + def test_instance_update_unique_name(self): + otherprojectcontext = context.RequestContext(self.user_id, + "%s2" % self.project_id) + + inst = self.create_instances_with_args(hostname='fake_name') + uuid1p1 = inst['uuid'] + inst = self.create_instances_with_args(hostname='fake_name2') + uuid2p1 = inst['uuid'] + + inst = self.create_instances_with_args(context=otherprojectcontext, + hostname='fake_name3') + uuid1p2 = inst['uuid'] + + # osapi_compute_unique_server_name_scope is unset so this should work: + values = {'hostname': 'fake_name2'} + db.instance_update(self.context, uuid1p1, values) + values = {'hostname': 'fake_name'} + db.instance_update(self.context, uuid1p1, values) + + # With scope 'global' any duplicate should fail. + self.flags(osapi_compute_unique_server_name_scope='global') + self.assertRaises(exception.InstanceExists, + db.instance_update, + self.context, + uuid2p1, + values) + + self.assertRaises(exception.InstanceExists, + db.instance_update, + otherprojectcontext, + uuid1p2, + values) + + # But we should definitely be able to update our name if we aren't + # really changing it. + case_only_values = {'hostname': 'fake_NAME'} + db.instance_update(self.context, uuid1p1, case_only_values) + + # With scope 'project' a duplicate in the project should fail: + self.flags(osapi_compute_unique_server_name_scope='project') + self.assertRaises(exception.InstanceExists, + db.instance_update, + self.context, + uuid2p1, + values) + + # With scope 'project' a duplicate in a different project should work: + self.flags(osapi_compute_unique_server_name_scope='project') + db.instance_update(otherprojectcontext, uuid1p2, values) + def test_instance_update_with_and_get_original(self): ctxt = context.get_admin_context()