def test_load(self): self.mox.StubOutWithMock(db, 'instance_get_by_uuid') fake_uuid = self.fake_instance['uuid'] db.instance_get_by_uuid( self.context, fake_uuid, columns_to_join=['info_cache', 'security_groups'], use_slave=False).AndReturn(self.fake_instance) fake_inst2 = dict(self.fake_instance, system_metadata=[{ 'key': 'foo', 'value': 'bar' }]) db.instance_get_by_uuid(self.context, fake_uuid, columns_to_join=['system_metadata'], use_slave=False).AndReturn(fake_inst2) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(self.context, fake_uuid) self.assertFalse(hasattr(inst, '_system_metadata')) sys_meta = inst.system_metadata self.assertEqual(sys_meta, {'foo': 'bar'}) self.assertTrue(hasattr(inst, '_system_metadata')) # Make sure we don't run load again sys_meta2 = inst.system_metadata self.assertEqual(sys_meta2, {'foo': 'bar'}) self.assertRemotes()
def test_save_rename_sends_notification(self): # Tests that simply changing the 'display_name' on the instance # will send a notification. self.flags(enable=False, group='cells') old_ref = dict(self.fake_instance, display_name='hello') fake_uuid = old_ref['uuid'] expected_updates = dict(display_name='goodbye') new_ref = dict(old_ref, **expected_updates) self.mox.StubOutWithMock(db, 'instance_get_by_uuid') self.mox.StubOutWithMock(db, 'instance_update_and_get_original') self.mox.StubOutWithMock(notifications, 'send_update') db.instance_get_by_uuid(self.context, fake_uuid, columns_to_join=['info_cache', 'security_groups'] ).AndReturn(old_ref) db.instance_update_and_get_original( self.context, fake_uuid, expected_updates, update_cells=False, columns_to_join=['info_cache', 'security_groups'] ).AndReturn((old_ref, new_ref)) notifications.send_update(self.context, mox.IgnoreArg(), mox.IgnoreArg()) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(self.context, old_ref['uuid']) self.assertEqual('hello', inst.display_name) inst.display_name = 'goodbye' inst.save() self.assertEqual('goodbye', inst.display_name) self.assertEqual(set([]), inst.obj_what_changed())
def test_with_info_cache(self): fake_inst = dict(self.fake_instance) fake_uuid = fake_inst['uuid'] nwinfo1 = network_model.NetworkInfo.hydrate([{'address': 'foo'}]) nwinfo2 = network_model.NetworkInfo.hydrate([{'address': 'bar'}]) nwinfo1_json = nwinfo1.json() nwinfo2_json = nwinfo2.json() fake_inst['info_cache'] = dict( test_instance_info_cache.fake_info_cache, network_info=nwinfo1_json, instance_uuid=fake_uuid) self.mox.StubOutWithMock(db, 'instance_get_by_uuid') self.mox.StubOutWithMock(db, 'instance_update_and_get_original') self.mox.StubOutWithMock(db, 'instance_info_cache_update') db.instance_get_by_uuid(self.context, fake_uuid, columns_to_join=['info_cache', 'security_groups'], use_slave=False ).AndReturn(fake_inst) db.instance_info_cache_update(self.context, fake_uuid, {'network_info': nwinfo2_json}) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(self.context, fake_uuid) self.assertEqual(inst.info_cache.network_info, nwinfo1) self.assertEqual(inst.info_cache.instance_uuid, fake_uuid) inst.info_cache.network_info = nwinfo2 inst.save()
def test_bless_instance_exception(self): self.vmsconn.set_return_val("bless", utils.TestInducedException()) self.vmsconn.set_return_val("get_instance_info", {'state': power_state.RUNNING}) self.vmsconn.set_return_val("unpause_instance", None) blessed_uuid = utils.create_pre_blessed_instance(self.context) blessed_instance = db.instance_get_by_uuid(self.context, blessed_uuid) self.assertTrue(blessed_instance['disable_terminate']) try: self.cobalt.bless_instance(self.context, instance_uuid=blessed_uuid, migration_url=None) self.fail("The bless error should have been re-raised up.") except utils.TestInducedException: pass blessed_instance = db.instance_get_by_uuid(self.context, blessed_uuid) self.assertEquals(vm_states.ERROR, blessed_instance['vm_state']) system_metadata = db.instance_system_metadata_get(self.context, blessed_uuid) self.assertEquals(None, system_metadata.get('images', None)) self.assertEquals(None, system_metadata.get('blessed', None)) self.assertEquals(None, blessed_instance['launched_at']) self.assertTrue(blessed_instance['disable_terminate'])
def test_bless_instance_migrate(self): self.vmsconn.set_return_val( "bless", ("newname", "migration_url", ["file1", "file2", "file3"])) self.vmsconn.set_return_val("post_bless", ["file1_ref", "file2_ref", "file3_ref"]) self.vmsconn.set_return_val("bless_cleanup", None) blessed_uuid = utils.create_instance(self.context) pre_bless_instance = db.instance_get_by_uuid(self.context, blessed_uuid) migration_url = self.gridcentric.bless_instance( self.context, instance_uuid=blessed_uuid, migration_url="mcdist://migrate_addr") post_bless_instance = db.instance_get_by_uuid(self.context, blessed_uuid) self.assertEquals(pre_bless_instance['vm_state'], post_bless_instance['vm_state']) self.assertEquals("migration_url", migration_url) metadata = db.instance_metadata_get(self.context, blessed_uuid) self.assertEquals("file1_ref,file2_ref,file3_ref", metadata['images']) self.assertEquals(pre_bless_instance['launched_at'], post_bless_instance['launched_at']) self.assertFalse(pre_bless_instance.get('disable_terminate', None), post_bless_instance.get('disable_terminate', None))
def test_processVm_disk_exception_next_retry(self): self.mock.StubOutWithMock(api, 'vm_save') api.vm_save(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mock.StubOutWithMock(nova_db, 'service_get_all_by_topic') nova_db.service_get_all_by_topic(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().\ AndReturn(None) self.mock.StubOutWithMock(novadb, 'instance_get_by_uuid') novadb.instance_get_by_uuid(mox.IgnoreArg(), mox.IgnoreArg())\ .AndReturn({'display_name': 'test_vm'}) self.mock.StubOutWithMock( self.libvirtVM.libvirtconn, 'storageVolLookupByPath') self.libvirtVM.libvirtconn.storageVolLookupByPath( mox.IgnoreArg()).AndRaise(Exception) self.mock.ReplayAll() InventoryCacheManager.delete_object_in_cache( '25f04dd3-e924-02b2-9eac-876e3c943262', Constants.Vm) libvirt_inventorymonitor.incomplete_vms = { self.libvirtVM.compute_id: {}} self.assertEquals(self.libvirtVM._processVm(libvirt.virDomain()), None) vm = InventoryCacheManager.get_object_from_cache( "25f04dd3-e924-02b2-9eac-876e3c943262", Constants.Vm) self.assertEqual(len( vm.get_vmDisks()), 0, "Instance disk should not exist when there \ is an exception") self.mock.VerifyAll() self.mock.stubs.UnsetAll()
def test_save_rename_sends_notification(self): # Tests that simply changing the 'display_name' on the instance # will send a notification. self.flags(enable=False, group='cells') old_ref = dict(self.fake_instance, display_name='hello') fake_uuid = old_ref['uuid'] expected_updates = dict(display_name='goodbye') new_ref = dict(old_ref, **expected_updates) self.mox.StubOutWithMock(db, 'instance_get_by_uuid') self.mox.StubOutWithMock(db, 'instance_update_and_get_original') self.mox.StubOutWithMock(notifications, 'send_update') db.instance_get_by_uuid(self.context, fake_uuid, columns_to_join=['info_cache', 'security_groups'], use_slave=False ).AndReturn(old_ref) db.instance_update_and_get_original( self.context, fake_uuid, expected_updates, update_cells=False, columns_to_join=['info_cache', 'security_groups', 'system_metadata'] ).AndReturn((old_ref, new_ref)) notifications.send_update(self.context, mox.IgnoreArg(), mox.IgnoreArg()) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(self.context, old_ref['uuid'], use_slave=False) self.assertEqual('hello', inst.display_name) inst.display_name = 'goodbye' inst.save() self.assertEqual('goodbye', inst.display_name) self.assertEqual(set([]), inst.obj_what_changed())
def test_refresh(self): self.mox.StubOutWithMock(db, 'instance_get_by_uuid') fake_uuid = self.fake_instance['uuid'] db.instance_get_by_uuid(self.context, fake_uuid, columns_to_join=['info_cache', 'security_groups'], use_slave=False ).AndReturn(dict(self.fake_instance, host='orig-host')) db.instance_get_by_uuid(self.context, fake_uuid, columns_to_join=['info_cache', 'security_groups'], use_slave=False ).AndReturn(dict(self.fake_instance, host='new-host')) self.mox.StubOutWithMock(instance_info_cache.InstanceInfoCache, 'refresh') instance_info_cache.InstanceInfoCache.refresh() self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(self.context, fake_uuid) self.assertEqual(inst.host, 'orig-host') inst.refresh() self.assertEqual(inst.host, 'new-host') self.assertRemotes() self.assertEqual(set([]), inst.obj_what_changed())
def test_refresh(self): self.mox.StubOutWithMock(db, 'instance_get_by_uuid') fake_uuid = self.fake_instance['uuid'] db.instance_get_by_uuid( self.context, fake_uuid, columns_to_join=['info_cache', 'security_groups'], use_slave=False).AndReturn( dict(self.fake_instance, host='orig-host')) db.instance_get_by_uuid( self.context, fake_uuid, columns_to_join=['info_cache', 'security_groups'], use_slave=False).AndReturn( dict(self.fake_instance, host='new-host')) self.mox.StubOutWithMock(instance_info_cache.InstanceInfoCache, 'refresh') instance_info_cache.InstanceInfoCache.refresh() self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(self.context, fake_uuid) self.assertEqual(inst.host, 'orig-host') inst.refresh() self.assertEqual(inst.host, 'new-host') self.assertRemotes() self.assertEqual(set([]), inst.obj_what_changed())
def test_with_security_groups(self): fake_inst = dict(self.fake_instance) fake_uuid = fake_inst['uuid'] fake_inst['security_groups'] = [ {'id': 1, 'name': 'secgroup1', 'description': 'fake-desc', 'user_id': 'fake-user', 'project_id': 'fake_project', 'created_at': None, 'updated_at': None, 'deleted_at': None, 'deleted': False}, {'id': 2, 'name': 'secgroup2', 'description': 'fake-desc', 'user_id': 'fake-user', 'project_id': 'fake_project', 'created_at': None, 'updated_at': None, 'deleted_at': None, 'deleted': False}, ] self.mox.StubOutWithMock(db, 'instance_get_by_uuid') self.mox.StubOutWithMock(db, 'instance_update_and_get_original') self.mox.StubOutWithMock(db, 'security_group_update') db.instance_get_by_uuid(self.context, fake_uuid, columns_to_join=['info_cache', 'security_groups'] ).AndReturn(fake_inst) db.security_group_update(self.context, 1, {'description': 'changed'} ).AndReturn(fake_inst['security_groups'][0]) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(self.context, fake_uuid) self.assertEqual(len(inst.security_groups), 2) for index, group in enumerate(fake_inst['security_groups']): for key in group: self.assertEqual(group[key], inst.security_groups[index][key]) self.assertIsInstance(inst.security_groups[index], security_group.SecurityGroup) self.assertEqual(inst.security_groups.obj_what_changed(), set()) inst.security_groups[0].description = 'changed' inst.save() self.assertEqual(inst.security_groups.obj_what_changed(), set())
def test_with_info_cache(self): fake_inst = dict(self.fake_instance) fake_uuid = fake_inst['uuid'] nwinfo1 = network_model.NetworkInfo.hydrate([{'address': 'foo'}]) nwinfo2 = network_model.NetworkInfo.hydrate([{'address': 'bar'}]) nwinfo1_json = nwinfo1.json() nwinfo2_json = nwinfo2.json() fake_inst['info_cache'] = dict( test_instance_info_cache.fake_info_cache, network_info=nwinfo1_json, instance_uuid=fake_uuid) self.mox.StubOutWithMock(db, 'instance_get_by_uuid') self.mox.StubOutWithMock(db, 'instance_update_and_get_original') self.mox.StubOutWithMock(db, 'instance_info_cache_update') db.instance_get_by_uuid( self.context, fake_uuid, columns_to_join=['info_cache', 'security_groups'], use_slave=False).AndReturn(fake_inst) db.instance_info_cache_update(self.context, fake_uuid, {'network_info': nwinfo2_json}) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(self.context, fake_uuid) self.assertEqual(inst.info_cache.network_info, nwinfo1) self.assertEqual(inst.info_cache.instance_uuid, fake_uuid) inst.info_cache.network_info = nwinfo2 inst.save()
def test_with_info_cache(self): ctxt = context.get_admin_context() fake_inst = dict(self.fake_instance) fake_uuid = fake_inst['uuid'] nwinfo1 = network_model.NetworkInfo.hydrate([{'address': 'foo'}]) nwinfo2 = network_model.NetworkInfo.hydrate([{'address': 'bar'}]) nwinfo1_json = nwinfo1.json() nwinfo2_json = nwinfo2.json() fake_inst['info_cache'] = { 'network_info': nwinfo1_json, 'instance_uuid': fake_uuid } self.mox.StubOutWithMock(db, 'instance_get_by_uuid') self.mox.StubOutWithMock(db, 'instance_update_and_get_original') self.mox.StubOutWithMock(db, 'instance_info_cache_update') db.instance_get_by_uuid(ctxt, fake_uuid, columns_to_join=[]).AndReturn(fake_inst) db.instance_info_cache_update(ctxt, fake_uuid, {'network_info': nwinfo2_json}) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(ctxt, fake_uuid) self.assertEqual(inst.info_cache.network_info, nwinfo1) self.assertEqual(inst.info_cache.instance_uuid, fake_uuid) inst.info_cache.network_info = nwinfo2 inst.save()
def test_with_security_groups(self): fake_inst = dict(self.fake_instance) fake_uuid = fake_inst['uuid'] fake_inst['security_groups'] = [ {'id': 1, 'name': 'secgroup1', 'description': 'fake-desc', 'user_id': 'fake-user', 'project_id': 'fake_project', 'created_at': None, 'updated_at': None, 'deleted_at': None, 'deleted': False}, {'id': 2, 'name': 'secgroup2', 'description': 'fake-desc', 'user_id': 'fake-user', 'project_id': 'fake_project', 'created_at': None, 'updated_at': None, 'deleted_at': None, 'deleted': False}, ] self.mox.StubOutWithMock(db, 'instance_get_by_uuid') self.mox.StubOutWithMock(db, 'instance_update_and_get_original') self.mox.StubOutWithMock(db, 'security_group_update') db.instance_get_by_uuid(self.context, fake_uuid, columns_to_join=[] ).AndReturn(fake_inst) db.security_group_update(self.context, 1, {'description': 'changed'} ).AndReturn(fake_inst['security_groups'][0]) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(self.context, fake_uuid) self.assertEqual(len(inst.security_groups), 2) for index, group in enumerate(fake_inst['security_groups']): for key in group: self.assertEqual(group[key], inst.security_groups[index][key]) self.assertTrue(isinstance(inst.security_groups[index], security_group.SecurityGroup)) self.assertEqual(inst.security_groups.obj_what_changed(), set()) inst.security_groups[0].description = 'changed' inst.save() self.assertEqual(inst.security_groups.obj_what_changed(), set())
def test_bless_instance_exception(self): self.vmsconn.set_return_val("bless", utils.TestInducedException()) self.vmsconn.set_return_val("get_instance_info", {'state': power_state.RUNNING}) self.vmsconn.set_return_val("unpause_instance", None) blessed_uuid = utils.create_pre_blessed_instance(self.context) blessed_instance = db.instance_get_by_uuid(self.context, blessed_uuid) self.assertTrue(blessed_instance['disable_terminate']) try: self.cobalt.bless_instance(self.context, instance_uuid=blessed_uuid, migration_url=None) self.fail("The bless error should have been re-raised up.") except utils.TestInducedException: pass blessed_instance = db.instance_get_by_uuid(self.context, blessed_uuid) self.assertEquals(vm_states.ERROR, blessed_instance['vm_state']) system_metadata = db.instance_system_metadata_get( self.context, blessed_uuid) self.assertEquals(None, system_metadata.get('images', None)) self.assertEquals(None, system_metadata.get('blessed', None)) self.assertEquals(None, blessed_instance['launched_at']) self.assertTrue(blessed_instance['disable_terminate'])
def test_save_rename_sends_notification(self): # Tests that simply changing the 'display_name' on the instance # will send a notification. self.flags(enable=False, group="cells") old_ref = dict(self.fake_instance, display_name="hello") fake_uuid = old_ref["uuid"] expected_updates = dict(display_name="goodbye") new_ref = dict(old_ref, **expected_updates) self.mox.StubOutWithMock(db, "instance_get_by_uuid") self.mox.StubOutWithMock(db, "instance_update_and_get_original") self.mox.StubOutWithMock(notifications, "send_update") db.instance_get_by_uuid( self.context, fake_uuid, columns_to_join=["info_cache", "security_groups"], use_slave=False ).AndReturn(old_ref) db.instance_update_and_get_original( self.context, fake_uuid, expected_updates, update_cells=False, columns_to_join=["info_cache", "security_groups"], ).AndReturn((old_ref, new_ref)) notifications.send_update(self.context, mox.IgnoreArg(), mox.IgnoreArg()) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(self.context, old_ref["uuid"], use_slave=False) self.assertEqual("hello", inst.display_name) inst.display_name = "goodbye" inst.save() self.assertEqual("goodbye", inst.display_name) self.assertEqual(set([]), inst.obj_what_changed())
def test_with_empty_pci_devices(self): fake_inst = dict(self.fake_instance, pci_devices=[]) fake_uuid = fake_inst["uuid"] self.mox.StubOutWithMock(db, "instance_get_by_uuid") db.instance_get_by_uuid(self.context, fake_uuid, columns_to_join=["pci_devices"]).AndReturn(fake_inst) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(self.context, fake_uuid, ["pci_devices"]) self.assertEqual(len(inst.pci_devices), 0)
def test_get_without_expected(self): self.mox.StubOutWithMock(db, "instance_get_by_uuid") db.instance_get_by_uuid(self.context, "uuid", columns_to_join=[], use_slave=False).AndReturn(self.fake_instance) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(self.context, "uuid", expected_attrs=[]) for attr in instance.INSTANCE_OPTIONAL_ATTRS: self.assertFalse(inst.obj_attr_is_set(attr)) self.assertRemotes()
def test_with_info_cache_none(self): fake_inst = dict(self.fake_instance, info_cache=None) fake_uuid = fake_inst["uuid"] self.mox.StubOutWithMock(db, "instance_get_by_uuid") db.instance_get_by_uuid(self.context, fake_uuid, columns_to_join=["info_cache"]).AndReturn(fake_inst) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(self.context, fake_uuid, ["info_cache"]) self.assertEqual(None, inst.info_cache)
def test_with_empty_security_groups(self): fake_inst = dict(self.fake_instance, security_groups=[]) fake_uuid = fake_inst['uuid'] self.mox.StubOutWithMock(db, 'instance_get_by_uuid') db.instance_get_by_uuid(self.context, fake_uuid, columns_to_join=[] ).AndReturn(fake_inst) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(self.context, fake_uuid) self.assertEqual(0, len(inst.security_groups))
def test_with_empty_security_groups(self): fake_inst = dict(self.fake_instance, security_groups=[]) fake_uuid = fake_inst['uuid'] self.mox.StubOutWithMock(db, 'instance_get_by_uuid') db.instance_get_by_uuid(self.context, fake_uuid, columns_to_join=[]).AndReturn(fake_inst) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(self.context, fake_uuid) self.assertEqual(0, len(inst.security_groups))
def test_get_with_expected(self): ctxt = context.get_admin_context() self.mox.StubOutWithMock(db, "instance_get_by_uuid") db.instance_get_by_uuid(ctxt, "uuid", ["metadata", "system_metadata"]).AndReturn(self.fake_instance) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(ctxt, "uuid", expected_attrs=["metadata", "system_metadata"]) self.assertTrue(hasattr(inst, "_metadata")) self.assertTrue(hasattr(inst, "_system_metadata")) self.assertRemotes()
def _save_test_helper(self, cell_type, save_kwargs): """Common code for testing save() for cells/non-cells.""" if cell_type: self.flags(enable=True, cell_type=cell_type, group="cells") else: self.flags(enable=False, group="cells") ctxt = context.get_admin_context() old_ref = dict(self.fake_instance, host="oldhost", user_data="old", vm_state="old", task_state="old") fake_uuid = old_ref["uuid"] expected_updates = dict(vm_state="meow", task_state="wuff", user_data="new") new_ref = dict(old_ref, host="newhost", **expected_updates) exp_vm_state = save_kwargs.get("expected_vm_state") exp_task_state = save_kwargs.get("expected_task_state") admin_reset = save_kwargs.get("admin_state_reset", False) if exp_vm_state: expected_updates["expected_vm_state"] = exp_vm_state if exp_task_state: expected_updates["expected_task_state"] = exp_task_state self.mox.StubOutWithMock(db, "instance_get_by_uuid") self.mox.StubOutWithMock(db, "instance_update_and_get_original") self.mox.StubOutWithMock(db, "instance_info_cache_update") cells_api_mock = self.mox.CreateMock(cells_rpcapi.CellsAPI) self.mox.StubOutWithMock(cells_api_mock, "instance_update_at_top") self.mox.StubOutWithMock(cells_api_mock, "instance_update_from_api") self.mox.StubOutWithMock(cells_rpcapi, "CellsAPI", use_mock_anything=True) db.instance_get_by_uuid(ctxt, fake_uuid, columns_to_join=[]).AndReturn(old_ref) db.instance_update_and_get_original(ctxt, fake_uuid, expected_updates, update_cells=False).AndReturn( (old_ref, new_ref) ) if cell_type == "api": cells_rpcapi.CellsAPI().AndReturn(cells_api_mock) cells_api_mock.instance_update_from_api( ctxt, mox.IsA(instance.Instance), exp_vm_state, exp_task_state, admin_reset ) elif cell_type == "compute": cells_rpcapi.CellsAPI().AndReturn(cells_api_mock) cells_api_mock.instance_update_at_top(ctxt, new_ref) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(ctxt, old_ref["uuid"]) self.assertEqual("old", inst.task_state) self.assertEqual("old", inst.vm_state) self.assertEqual("old", inst.user_data) inst.vm_state = "meow" inst.task_state = "wuff" inst.user_data = "new" inst.save(**save_kwargs) self.assertEqual("newhost", inst.host) self.assertEqual("meow", inst.vm_state) self.assertEqual("wuff", inst.task_state) self.assertEqual("new", inst.user_data) self.assertEqual(set([]), inst.obj_what_changed())
def test_get_deleted(self): ctxt = context.get_admin_context() fake_inst = dict(self.fake_instance, id=123, deleted=123) fake_uuid = fake_inst['uuid'] self.mox.StubOutWithMock(db, 'instance_get_by_uuid') db.instance_get_by_uuid(ctxt, fake_uuid, []).AndReturn(fake_inst) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(ctxt, fake_uuid) # NOTE(danms): Make sure it's actually a bool self.assertEqual(inst.deleted, True)
def test_get_cleaned(self): fake_inst = dict(self.fake_instance, id=123, cleaned=1) fake_uuid = fake_inst['uuid'] self.mox.StubOutWithMock(db, 'instance_get_by_uuid') db.instance_get_by_uuid(self.context, fake_uuid, columns_to_join=[]).AndReturn(fake_inst) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(self.context, fake_uuid) # NOTE(mikal): Make sure it's actually a bool self.assertEqual(inst.cleaned, True)
def test_get_without_expected(self): ctxt = context.get_admin_context() self.mox.StubOutWithMock(db, 'instance_get_by_uuid') db.instance_get_by_uuid(ctxt, 'uuid', []).AndReturn(self.fake_instance) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(ctxt, uuid='uuid') # Make sure these weren't loaded self.assertFalse(hasattr(inst, '_metadata')) self.assertFalse(hasattr(inst, '_system_metadata')) self.assertRemotes()
def test_with_empty_security_groups(self): fake_inst = dict(self.fake_instance, security_groups=[]) fake_uuid = fake_inst["uuid"] self.mox.StubOutWithMock(db, "instance_get_by_uuid") db.instance_get_by_uuid( self.context, fake_uuid, columns_to_join=["info_cache", "security_groups"], use_slave=False ).AndReturn(fake_inst) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(self.context, fake_uuid) self.assertEqual(0, len(inst.security_groups))
def test_get_deleted(self): ctxt = context.get_admin_context() fake_inst = dict(self.fake_instance, id=123, deleted=123) fake_uuid = fake_inst["uuid"] self.mox.StubOutWithMock(db, "instance_get_by_uuid") db.instance_get_by_uuid(ctxt, fake_uuid, []).AndReturn(fake_inst) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(ctxt, fake_uuid) # NOTE(danms): Make sure it's actually a bool self.assertEqual(inst.deleted, True)
def test_get_with_expected(self): ctxt = context.get_admin_context() self.mox.StubOutWithMock(db, "instance_get_by_uuid") db.instance_get_by_uuid(ctxt, "uuid", ["metadata", "system_metadata"]).AndReturn(self.fake_instance) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(ctxt, "uuid", expected_attrs=instance.INSTANCE_OPTIONAL_FIELDS) for attr in instance.INSTANCE_OPTIONAL_FIELDS: attrname = base.get_attrname(attr) self.assertTrue(hasattr(inst, attrname)) self.assertRemotes()
def test_get_without_expected(self): self.mox.StubOutWithMock(db, 'instance_get_by_uuid') db.instance_get_by_uuid(self.context, 'uuid', columns_to_join=[] ).AndReturn(self.fake_instance) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(self.context, 'uuid') # Make sure these weren't loaded for attr in instance.INSTANCE_OPTIONAL_FIELDS: self.assertFalse(inst.obj_attr_is_set(attr)) self.assertRemotes()
def test_get_cleaned(self): fake_inst = dict(self.fake_instance, id=123, cleaned=1) fake_uuid = fake_inst['uuid'] self.mox.StubOutWithMock(db, 'instance_get_by_uuid') db.instance_get_by_uuid(self.context, fake_uuid, columns_to_join=[] ).AndReturn(fake_inst) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(self.context, fake_uuid) # NOTE(mikal): Make sure it's actually a bool self.assertEqual(inst.cleaned, True)
def test_with_info_cache_none(self): fake_inst = dict(self.fake_instance, info_cache=None) fake_uuid = fake_inst['uuid'] self.mox.StubOutWithMock(db, 'instance_get_by_uuid') db.instance_get_by_uuid(self.context, fake_uuid, columns_to_join=['info_cache'] ).AndReturn(fake_inst) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(self.context, fake_uuid, ['info_cache']) self.assertEqual(None, inst.info_cache)
def test_get_without_expected(self): ctxt = context.get_admin_context() self.mox.StubOutWithMock(db, 'instance_get_by_uuid') db.instance_get_by_uuid(ctxt, 'uuid', []).AndReturn(self.fake_instance) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(ctxt, 'uuid') # Make sure these weren't loaded for attr in instance.INSTANCE_OPTIONAL_FIELDS: attrname = base.get_attrname(attr) self.assertFalse(hasattr(inst, attrname)) self.assertRemotes()
def test_with_none_pci_devices(self): fake_inst = dict(self.fake_instance, pci_devices=None) fake_uuid = fake_inst['uuid'] self.mox.StubOutWithMock(db, 'instance_get_by_uuid') db.instance_get_by_uuid(self.context, fake_uuid, columns_to_join=['pci_devices'] ).AndReturn(fake_inst) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(self.context, fake_uuid, ['pci_devices']) self.assertEqual(None, inst.pci_devices)
def test_get_without_expected(self): ctxt = context.get_admin_context() self.mox.StubOutWithMock(db, "instance_get_by_uuid") db.instance_get_by_uuid(ctxt, "uuid", []).AndReturn(self.fake_instance) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(ctxt, "uuid") # Make sure these weren't loaded for attr in instance.INSTANCE_OPTIONAL_FIELDS: attrname = base.get_attrname(attr) self.assertFalse(hasattr(inst, attrname)) self.assertRemotes()
def test_get_cleaned(self): fake_inst = dict(self.fake_instance, id=123, cleaned=1) fake_uuid = fake_inst["uuid"] self.mox.StubOutWithMock(db, "instance_get_by_uuid") db.instance_get_by_uuid( self.context, fake_uuid, columns_to_join=["info_cache", "security_groups"], use_slave=False ).AndReturn(fake_inst) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(self.context, fake_uuid) # NOTE(mikal): Make sure it's actually a bool self.assertEqual(inst.cleaned, True)
def test_get_without_expected(self): ctxt = context.get_admin_context() self.mox.StubOutWithMock(db, 'instance_get_by_uuid') db.instance_get_by_uuid(ctxt, 'uuid', columns_to_join=[]).AndReturn( self.fake_instance) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(ctxt, 'uuid') # Make sure these weren't loaded for attr in instance.INSTANCE_OPTIONAL_FIELDS: self.assertFalse(inst.obj_attr_is_set(attr)) self.assertRemotes()
def test_get_without_expected(self): self.mox.StubOutWithMock(db, 'instance_get_by_uuid') db.instance_get_by_uuid(self.context, 'uuid', columns_to_join=[] ).AndReturn(self.fake_instance) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(self.context, 'uuid', expected_attrs=[]) for attr in instance.INSTANCE_OPTIONAL_ATTRS: self.assertFalse(inst.obj_attr_is_set(attr)) self.assertRemotes()
def test_with_empty_pci_devices(self): fake_inst = dict(self.fake_instance, pci_devices=[]) fake_uuid = fake_inst['uuid'] self.mox.StubOutWithMock(db, 'instance_get_by_uuid') db.instance_get_by_uuid(self.context, fake_uuid, columns_to_join=['pci_devices'] ).AndReturn(fake_inst) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(self.context, fake_uuid, ['pci_devices']) self.assertEqual(len(inst.pci_devices), 0)
def test_instance(self): ctxt = context.get_admin_context() fake_inst = fake_instance.fake_db_instance() self.mox.StubOutWithMock(db, 'instance_get_by_uuid') db.instance_get_by_uuid(ctxt, fake_migration['instance_uuid'], columns_to_join=[]).AndReturn(fake_inst) mig = migration.Migration._from_db_object(ctxt, migration.Migration(), fake_migration) mig._context = ctxt self.mox.ReplayAll() self.assertEqual(mig.instance.host, fake_inst['host'])
def test_with_empty_pci_devices(self): fake_inst = dict(self.fake_instance, pci_devices=[]) fake_uuid = fake_inst['uuid'] self.mox.StubOutWithMock(db, 'instance_get_by_uuid') db.instance_get_by_uuid(self.context, fake_uuid, columns_to_join=['pci_devices'], use_slave=False ).AndReturn(fake_inst) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(self.context, fake_uuid, ['pci_devices']) self.assertEqual(len(inst.pci_devices), 0)
def test_with_info_cache_none(self): fake_inst = dict(self.fake_instance, info_cache=None) fake_uuid = fake_inst['uuid'] self.mox.StubOutWithMock(db, 'instance_get_by_uuid') db.instance_get_by_uuid(self.context, fake_uuid, columns_to_join=['info_cache'], use_slave=False ).AndReturn(fake_inst) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(self.context, fake_uuid, ['info_cache']) self.assertIsNone(inst.info_cache)
def test_get_with_expected(self): ctxt = context.get_admin_context() self.mox.StubOutWithMock(db, 'instance_get_by_uuid') db.instance_get_by_uuid( ctxt, 'uuid', ['metadata', 'system_metadata']).AndReturn(self.fake_instance) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid( ctxt, 'uuid', expected_attrs=['metadata', 'system_metadata']) self.assertTrue(hasattr(inst, '_metadata')) self.assertTrue(hasattr(inst, '_system_metadata')) self.assertRemotes()
def test_with_fault(self): fake_inst = dict(self.fake_instance) fake_uuid = fake_inst["uuid"] fake_faults = [dict(x, instance_uuid=fake_uuid) for x in test_instance_fault.fake_faults["fake-uuid"]] self.mox.StubOutWithMock(db, "instance_get_by_uuid") self.mox.StubOutWithMock(db, "instance_fault_get_by_instance_uuids") db.instance_get_by_uuid(self.context, fake_uuid, columns_to_join=[]).AndReturn(self.fake_instance) db.instance_fault_get_by_instance_uuids(self.context, [fake_uuid]).AndReturn({fake_uuid: fake_faults}) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(self.context, fake_uuid, expected_attrs=["fault"]) self.assertEqual(fake_faults[0], dict(inst.fault.items())) self.assertRemotes()
def test_refresh(self): ctxt = context.get_admin_context() self.mox.StubOutWithMock(db, "instance_get_by_uuid") fake_uuid = self.fake_instance["uuid"] db.instance_get_by_uuid(ctxt, fake_uuid, []).AndReturn(dict(self.fake_instance, host="orig-host")) db.instance_get_by_uuid(ctxt, fake_uuid, []).AndReturn(dict(self.fake_instance, host="new-host")) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(ctxt, fake_uuid) self.assertEqual(inst.host, "orig-host") inst.refresh() self.assertEqual(inst.host, "new-host") self.assertRemotes()
def test_with_none_pci_devices(self): ctxt = context.get_admin_context() fake_inst = dict(self.fake_instance, pci_devices=None) fake_uuid = fake_inst['uuid'] self.mox.StubOutWithMock(db, 'instance_get_by_uuid') db.instance_get_by_uuid(ctxt, fake_uuid, columns_to_join=['pci_devices' ]).AndReturn(fake_inst) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(ctxt, fake_uuid, ['pci_devices']) self.assertEqual(None, inst.pci_devices)
def test_get_with_expected(self): ctxt = context.get_admin_context() self.mox.StubOutWithMock(db, 'instance_get_by_uuid') db.instance_get_by_uuid(ctxt, 'uuid', ['metadata', 'system_metadata']).AndReturn( self.fake_instance) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid( ctxt, uuid='uuid', expected_attrs=['metadata', 'system_metadata']) self.assertTrue(hasattr(inst, '_metadata')) self.assertTrue(hasattr(inst, '_system_metadata')) self.assertRemotes()
def test_get_deleted(self): fake_inst = dict(self.fake_instance, id=123, deleted=123) fake_uuid = fake_inst['uuid'] self.mox.StubOutWithMock(db, 'instance_get_by_uuid') db.instance_get_by_uuid(self.context, fake_uuid, columns_to_join=['info_cache', 'security_groups'] ).AndReturn(fake_inst) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(self.context, fake_uuid) # NOTE(danms): Make sure it's actually a bool self.assertEqual(inst.deleted, True)
def test_get_remote(self): # isotime doesn't have microseconds and is always UTC ctxt = context.get_admin_context() self.mox.StubOutWithMock(db, 'instance_get_by_uuid') fake_instance = self.fake_instance db.instance_get_by_uuid(ctxt, 'fake-uuid', []).AndReturn(fake_instance) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(ctxt, uuid='fake-uuid') self.assertEqual(inst.id, fake_instance['id']) self.assertEqual(inst.launched_at, fake_instance['launched_at']) self.assertEqual(str(inst.access_ip_v4), fake_instance['access_ip_v4']) self.assertEqual(str(inst.access_ip_v6), fake_instance['access_ip_v6']) self.assertRemotes()
def test_get_remote(self): # isotime doesn't have microseconds and is always UTC ctxt = context.get_admin_context() self.mox.StubOutWithMock(db, "instance_get_by_uuid") fake_instance = self.fake_instance db.instance_get_by_uuid(ctxt, "fake-uuid", []).AndReturn(fake_instance) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(ctxt, "fake-uuid") self.assertEqual(inst.id, fake_instance["id"]) self.assertEqual(inst.launched_at, fake_instance["launched_at"]) self.assertEqual(str(inst.access_ip_v4), fake_instance["access_ip_v4"]) self.assertEqual(str(inst.access_ip_v6), fake_instance["access_ip_v6"]) self.assertRemotes()
def test_get_with_expected(self): self.mox.StubOutWithMock(db, 'instance_get_by_uuid') db.instance_get_by_uuid( self.context, 'uuid', columns_to_join=['metadata', 'system_metadata', 'pci_devices'] ).AndReturn(self.fake_instance) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid( self.context, 'uuid', expected_attrs=instance.INSTANCE_OPTIONAL_FIELDS) for attr in instance.INSTANCE_OPTIONAL_FIELDS: self.assertTrue(inst.obj_attr_is_set(attr)) self.assertRemotes()
def test_get_with_expected(self): ctxt = context.get_admin_context() self.mox.StubOutWithMock(db, 'instance_get_by_uuid') db.instance_get_by_uuid(ctxt, 'uuid', ['metadata', 'system_metadata']).AndReturn( self.fake_instance) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid( ctxt, 'uuid', expected_attrs=instance.INSTANCE_OPTIONAL_FIELDS) for attr in instance.INSTANCE_OPTIONAL_FIELDS: attrname = base.get_attrname(attr) self.assertTrue(hasattr(inst, attrname)) self.assertRemotes()
def test_get_not_cleaned(self): fake_inst = dict(self.fake_instance, id=123, cleaned=None) fake_uuid = fake_inst['uuid'] self.mox.StubOutWithMock(db, 'instance_get_by_uuid') db.instance_get_by_uuid( self.context, fake_uuid, columns_to_join=['info_cache', 'security_groups'], use_slave=False).AndReturn(fake_inst) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(self.context, fake_uuid) # NOTE(mikal): Make sure it's actually a bool self.assertEqual(inst.cleaned, False)
def test_with_pci_devices(self): fake_inst = dict(self.fake_instance) fake_uuid = fake_inst['uuid'] fake_inst['pci_devices'] = [ { 'created_at': None, 'updated_at': None, 'deleted_at': None, 'deleted': None, 'id': 2, 'compute_node_id': 1, 'address': 'a1', 'vendor_id': 'v1', 'product_id': 'p1', 'dev_type': 't', 'status': 'allocated', 'dev_id': 'i', 'label': 'l', 'instance_uuid': fake_uuid, 'extra_info': '{}' }, { 'created_at': None, 'updated_at': None, 'deleted_at': None, 'deleted': None, 'id': 1, 'compute_node_id': 1, 'address': 'a', 'vendor_id': 'v', 'product_id': 'p', 'dev_type': 't', 'status': 'allocated', 'dev_id': 'i', 'label': 'l', 'instance_uuid': fake_uuid, 'extra_info': '{}' }, ] self.mox.StubOutWithMock(db, 'instance_get_by_uuid') db.instance_get_by_uuid(self.context, fake_uuid, columns_to_join=['pci_devices'], use_slave=False).AndReturn(fake_inst) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(self.context, fake_uuid, ['pci_devices']) self.assertEqual(len(inst.pci_devices), 2) self.assertEqual(inst.pci_devices[0].instance_uuid, fake_uuid) self.assertEqual(inst.pci_devices[1].instance_uuid, fake_uuid)
def test_refresh(self): ctxt = context.get_admin_context() self.mox.StubOutWithMock(db, 'instance_get_by_uuid') fake_uuid = self.fake_instance['uuid'] db.instance_get_by_uuid(ctxt, fake_uuid, []).AndReturn( dict(self.fake_instance, host='orig-host')) db.instance_get_by_uuid(ctxt, fake_uuid, []).AndReturn( dict(self.fake_instance, host='new-host')) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(ctxt, uuid=fake_uuid) self.assertEqual(inst.host, 'orig-host') inst.refresh() self.assertEqual(inst.host, 'new-host') self.assertRemotes()
def test_get_with_expected(self): ctxt = context.get_admin_context() self.mox.StubOutWithMock(db, 'instance_get_by_uuid') db.instance_get_by_uuid( ctxt, 'uuid', columns_to_join=['metadata', 'system_metadata', 'pci_devices']).AndReturn(self.fake_instance) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid( ctxt, 'uuid', expected_attrs=instance.INSTANCE_OPTIONAL_FIELDS) for attr in instance.INSTANCE_OPTIONAL_FIELDS: self.assertTrue(inst.obj_attr_is_set(attr)) self.assertRemotes()
def test_save(self): ctxt = context.get_admin_context() fake_inst = dict(self.fake_instance, host='oldhost') fake_uuid = fake_inst['uuid'] self.mox.StubOutWithMock(db, 'instance_get_by_uuid') self.mox.StubOutWithMock(db, 'instance_update_and_get_original') db.instance_get_by_uuid(ctxt, fake_uuid, []).AndReturn(fake_inst) db.instance_update_and_get_original(ctxt, fake_uuid, { 'user_data': 'foo' }).AndReturn((fake_inst, dict(fake_inst, host='newhost'))) self.mox.ReplayAll() inst = instance.Instance.get_by_uuid(ctxt, uuid=fake_uuid) inst.user_data = 'foo' inst.save() self.assertEqual(inst.host, 'newhost')
def test_bless_instance(self): instance_uuid = utils.create_instance(self.context) num_instance_before = len(db.instance_get_all(self.context)) blessed_instance = self.cobalt_api.bless_instance(self.context, instance_uuid) self.assertEquals(vm_states.BUILDING, blessed_instance['vm_state']) # Ensure that we have a 2nd instance in the database that is a "clone" # of our original instance. instances = db.instance_get_all(self.context) self.assertTrue(len(instances) == (num_instance_before + 1), "There should be one new instance after blessing.") # The virtual machine should be marked that it is now blessed. metadata = db.instance_metadata_get(self.context, blessed_instance['uuid']) self.assertTrue(metadata.has_key('blessed_from'), "The instance should have a bless metadata after being blessed.") self.assertTrue(metadata['blessed_from'] == '%s' % instance_uuid, "The instance should have the blessed_from metadata set to true after being blessed. " \ + "(value=%s)" % (metadata['blessed_from'])) system_metadata = db.instance_system_metadata_get(self.context, blessed_instance['uuid']) self.assertTrue(system_metadata.has_key('blessed_from'), "The instance should have a bless system_metadata after being blessed.") self.assertTrue(system_metadata['blessed_from'] == '%s' % instance_uuid, "The instance should have the blessed_from system_metadata set to true after being blessed. "\ + "(value=%s)" % (system_metadata['blessed_from'])) db_blessed_instance = db.instance_get_by_uuid(self.context, blessed_instance['uuid']) self.assertTrue(db_blessed_instance['info_cache']) self.assertIsNotNone(db_blessed_instance['info_cache']['network_info'])
def _test_create_image(self, cache_type): sys_meta = {'image_cache_in_nova': True} instance = db.instance_create(self.context, {'system_metadata': sys_meta}) instance = db.instance_get_by_uuid(self.context, instance['uuid']) self.flags(cache_images=cache_type) was = {'called': None} def fake_create_cached_image(*args): was['called'] = 'some' return {} self.stubs.Set(vm_utils, '_create_cached_image', fake_create_cached_image) def fake_fetch_image(*args): was['called'] = 'none' return {} self.stubs.Set(vm_utils, '_fetch_image', fake_fetch_image) vm_utils._create_image(self.context, None, instance, 'foo', 'bar', 'baz') self.assertEqual(was['called'], cache_type)