def test_list_vm_json_header(self): expected_out_json = \ '{"virtualmachines": [{"id": "vm-01", "links": [{"href": "http://localhost:8774/v2.0/virtualmachines/vm-01", "rel": "self"}, \ {"href": "http://localhost:8774/virtualmachines/vm-01", "rel": "bookmark"}], "name": "vm-01"}, \ {"id": "vm-02", "links": [{"href": "http://localhost:8774/v2.0/virtualmachines/vm-02", "rel": "self"}, \ {"href": "http://localhost:8774/virtualmachines/vm-02", "rel": "bookmark"}], "name": "vm-02"}]}' virtualmachines = self.get_vm_list() self.mock.StubOutWithMock(api, 'vm_get_all_by_filters') api.vm_get_all_by_filters(mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(virtualmachines) self.mock.ReplayAll() request = webob.Request.blank('/v2.0/vm', base_url='http://localhost:8774/v2.0/') request.headers['Accept'] = 'application/json' request.environ['nova.context'] = self.admin_context resp = VMController().index(request) self.assertNotEqual(resp, None, 'Return json string') self.compare_json(expected_out_json, resp.body) # self.assertEqual(self.expected_index_json, resp.body) self.mock.stubs.UnsetAll()
def test_list_vm_xml_header(self): expected_out_xml = \ '<virtualmachines xmlns:atom="http://www.w3.org/2005/Atom" xmlns="http://docs.openstack.org/ext/healthnmon/api/v2.0">\ <vm id="vm-01" name="vm-01"><atom:link href="http://localhost:8774/v2.0/virtualmachines/vm-01" rel="self"/>\ <atom:link href="http://localhost:8774/virtualmachines/vm-01" rel="bookmark"/></vm>\ <vm id="vm-02" name="vm-02"><atom:link href="http://localhost:8774/v2.0/virtualmachines/vm-02" rel="self"/>\ <atom:link href="http://localhost:8774/virtualmachines/vm-02" rel="bookmark"/></vm>\ </virtualmachines>' virtualmachines = self.get_vm_list() self.mock.StubOutWithMock(api, 'vm_get_all_by_filters') api.vm_get_all_by_filters(mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(virtualmachines) self.mock.ReplayAll() request = webob.Request.blank('/v2.0/vm', base_url='http://localhost:8774/v2.0/') request.headers['Accept'] = 'application/xml' request.environ['nova.context'] = self.admin_context resp = VMController().index(request) self.assertNotEqual(resp, None, 'Return xml string') self.compare_xml(expected_out_xml, resp.body) # self.assertEqual(resp.body, self.expected_index_xml) self.mock.stubs.UnsetAll()
def test_list_virtual_machine_none_check(self): self.mock.StubOutWithMock(api, 'vm_get_all_by_filters') api.vm_get_all_by_filters(mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(None) self.mock.ReplayAll() request = webob.Request.blank('/v2.0/virtualmachines', base_url='http://localhost:8774/v2.0/') request.environ['nova.context'] = self.admin_context resp = VMController().index(request) self.assertEqual(resp.body, '{"virtualmachines": []}', 'Return json string')
def test_vm_none_detail_xml(self): vms = None self.mock.StubOutWithMock(api, 'vm_get_all_by_filters') api.vm_get_all_by_filters(mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(vms) self.mock.ReplayAll() request = webob.Request.blank('/v2.0/virtualmachines/detail.xml', base_url='http://localhost:8774/v2.0/') request.environ['nova.context'] = self.admin_context resp = VMController().detail(request) self.assertNotEqual(resp, None, 'Return xml string') self.mock.stubs.UnsetAll()
def test_vm_get_all_by_filters_changessince(self): # Create Vm vm_ids = ('VM1', 'VM2', 'VM3') vm_names = ('name1', 'name2', 'name3') for i in range(len(vm_ids)): self.__create_vm(id=vm_ids[i], name=vm_names[i]) created_time = long(time.time() * 1000L) # Wait for 1 sec and update second vm and delete third vm time.sleep(1) second_vm = healthnmon_db_api.vm_get_by_ids( self.admin_context, [vm_ids[1]])[0] second_vm.name = 'New name' healthnmon_db_api.vm_save(self.admin_context, second_vm) healthnmon_db_api.vm_delete_by_ids(self.admin_context, [vm_ids[2]]) # Query with filter expected_updated_ids = [vm_ids[1], vm_ids[2]] filters = {'changes-since': created_time} vms = healthnmon_db_api.vm_get_all_by_filters( self.admin_context, filters, None, None) self.assert_(vms is not None) self.assert_(len(vms) == 2) for vm in vms: self.assert_(vm is not None) self.assert_(vm.id in expected_updated_ids)
def test_deleted_vm_host(self): """ Test if vmhost get all by filters lists deleted virtual machines if the vmhost was deleted. """ vmhost, vm_01, vm_02 = self._setup_host() self.__save(vmhost, vm_01, vm_02) healthnmon_db_api.vm_host_delete_by_ids(self.admin_context, vmhost.id) self.assertEqual( healthnmon_db_api.vm_get_all_by_filters(self.admin_context, {'id': vm_01.id}, None, None)[0].deleted, True, 'Delete vm host and assert if VM is deleted')
def test_inconsistent_vmhost(self): """ Test if vmhost get all by filters lists deleted virtual machines if an inconsistent vmhost was deleted. """ vmhost, vm_01, vm_02 = self._setup_host() vmhost.virtualMachineIds = ['a', 'b'] self.__save(vmhost, vm_01, vm_02) healthnmon_db_api.vm_host_delete_by_ids(self.admin_context, vmhost.id) self.assertEqual( healthnmon_db_api.vm_get_all_by_filters(self.admin_context, {'deleted': 'true'}, None, None)[0].deleted, True, 'Delete inconsistent vm host and assert if VM is deleted')
def test_vm_get_all_by_filters(self): # Create Vm vm_ids = ('VM1', 'VM2') vm_names = ('name1', 'name2') for i in range(len(vm_ids)): self.__create_vm(id=vm_ids[i], name=vm_names[i]) # Query with filter filters = {'name': vm_names[1]} vms = healthnmon_db_api.vm_get_all_by_filters( self.admin_context, filters, 'id', DbConstants.ORDER_ASC) self.assert_(vms is not None) self.assert_(len(vms) == 1) self.assert_(vms[0] is not None) self.assert_(vms[0].id == vm_ids[1])
def test_vm_get_all_by_filters(self): # Create Vm vm_ids = ('VM1', 'VM2') vm_names = ('name1', 'name2') for i in range(len(vm_ids)): self.__create_vm(id=vm_ids[i], name=vm_names[i]) # Query with filter filters = {'name': vm_names[1]} vms = healthnmon_db_api.vm_get_all_by_filters(self.admin_context, filters, 'id', DbConstants.ORDER_ASC) self.assert_(vms is not None) self.assert_(len(vms) == 1) self.assert_(vms[0] is not None) self.assert_(vms[0].id == vm_ids[1])
def vm_get_all_by_filters(context, filters, sort_key, sort_dir): """ This API will make a call to db layer to fetch the list of all the VM objects. Parameters: context - nova.context.RequestContext object filters - dictionary of filters to be applied keys should be fields of Vm model if value is simple value = filter is applied and if value is list or tuple 'IN' filter is applied eg : {'powerState':'ACTIVE', 'name':['n1', 'n2']} will filter as powerState = 'ACTIVE' AND name in ('n1', 'n2') sort_key - Field on which sorting is to be applied sort_dir - asc for Ascending sort direction, desc for descending sort direction Returns: list of vms that match all filters and sorted with sort_key """ return api.vm_get_all_by_filters(context, filters, sort_key, sort_dir)
def test_vm_get_all_by_filters_changessince(self): # Create Vm vm_ids = ('VM1', 'VM2', 'VM3') vm_names = ('name1', 'name2', 'name3') for i in range(len(vm_ids)): self.__create_vm(id=vm_ids[i], name=vm_names[i]) created_time = long(time.time() * 1000L) # Wait for 1 sec and update second vm and delete third vm time.sleep(1) second_vm = healthnmon_db_api.vm_get_by_ids(self.admin_context, [vm_ids[1]])[0] second_vm.name = 'New name' healthnmon_db_api.vm_save(self.admin_context, second_vm) healthnmon_db_api.vm_delete_by_ids(self.admin_context, [vm_ids[2]]) # Query with filter expected_updated_ids = [vm_ids[1], vm_ids[2]] filters = {'changes-since': created_time} vms = healthnmon_db_api.vm_get_all_by_filters(self.admin_context, filters, None, None) self.assert_(vms is not None) self.assert_(len(vms) == 2) for vm in vms: self.assert_(vm is not None) self.assert_(vm.id in expected_updated_ids)