def test_list_vmhost_none_check(self): self.mock.StubOutWithMock(api, 'vm_host_get_all_by_filters') api.vm_host_get_all_by_filters(mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(None) self.mock.ReplayAll() request = webob.Request.blank('/v2.0/vmhosts', base_url='http://localhost:8774/v2.0/') request.environ['nova.context'] = self.admin_context resp = VmHostsController().index(request) self.assertEqual(resp.body, '{"vmhosts": []}', 'Return json string')
def test_list_hosts_none_detail_xml(self): hosts = None self.mock.StubOutWithMock(api, 'vm_host_get_all_by_filters') api.vm_host_get_all_by_filters(mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(hosts) self.mock.ReplayAll() request = webob.Request.blank('/v2.0/vmhosts/detail.xml', base_url='http://localhost:8774/v2.0/') request.environ['nova.context'] = self.admin_context resp = VmHostsController().detail(request) self.assertNotEqual(resp, None, 'Return xml string')
def test_list_hosts_limited_xml(self): hosts = self.get_limited_list(3) self.mock.StubOutWithMock(api, 'vm_host_get_all_by_filters') api.vm_host_get_all_by_filters(mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(hosts) self.mock.ReplayAll() request = webob.Request.blank( '/v2.0/vmhosts.xml?limit=1&marker=host-1', base_url='http://localhost:8774/v2.0/') request.environ['nova.context'] = self.admin_context resp = VmHostsController().index(request) self.assertEqual(resp.body, self.expected_index_limited_xml)
def test_list_hosts_json(self): hosts = self.get_host_list() self.mock.StubOutWithMock(api, 'vm_host_get_all_by_filters') api.vm_host_get_all_by_filters(mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(hosts) self.mock.ReplayAll() request = webob.Request.blank('/v2.0/vmhosts.json', base_url='http://localhost:8774/v2.0/') request.environ['nova.context'] = self.admin_context resp = VmHostsController().index(request) self.assertNotEqual(resp, None, 'Return json string') self.assertEqual(self.expected_index_json, resp.body)
def test_list_host_details_limited_json(self): hosts = self.get_host_list() self.mock.StubOutWithMock(api, 'vm_host_get_all_by_filters') api.vm_host_get_all_by_filters(mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(hosts) self.mock.ReplayAll() request = webob.Request.blank( '/v2.0/vmhosts/detail?limit=1&marker=host-01', base_url='http://localhost:8774/v2.0/') request.environ['nova.context'] = self.admin_context resp = VmHostsController().detail(request) self.assertNotEqual(resp, None) self.assertEqual(self.expected_detail_limit_marker_json, resp.body)
def test_vm_host_get_all_by_filters_changessince(self): # Create VmHosts host_ids = ('VH1', 'VH2', 'VH3') host_names = ('name1', 'name2', 'name3') for i in range(len(host_ids)): self.__create_vm_host(id=host_ids[i], name=host_names[i]) created_time = long(time.time() * 1000L) # Wait for 1 sec and update second host and delete third host time.sleep(1) second_host = healthnmon_db_api.vm_host_get_by_ids( self.admin_context, [host_ids[1]])[0] second_host.name = 'New name' healthnmon_db_api.vm_host_save(self.admin_context, second_host) healthnmon_db_api.vm_host_delete_by_ids(self.admin_context, [host_ids[2]]) # Query with filter expected_updated_ids = [host_ids[1], host_ids[2]] filters = {'changes-since': created_time} vmhosts = healthnmon_db_api.vm_host_get_all_by_filters( self.admin_context, filters, None, None) self.assert_(vmhosts is not None) self.assert_(len(vmhosts) == 2) for host in vmhosts: self.assert_(host is not None) self.assert_(host.id in expected_updated_ids)
def test_deleted_vm(self): """ Test if vmhost get all by filters lists deleted virtual machines """ vmhost, vm_01, vm_02 = self._setup_host() vm_01.deleted = True self.__save(vmhost, vm_01, vm_02) hosts = healthnmon_db_api.vm_host_get_all_by_filters( self.admin_context, None, None, None) self.assertEquals(hosts[0].get_virtualMachineIds(), ['u23lksd-32342-324l-demo']) # Delete second VM vm_02.deleted = True self.__save(vmhost, vm_01, vm_02) hosts = healthnmon_db_api.vm_host_get_all_by_filters( self.admin_context, None, None, None) self.assertEquals(hosts[0].get_virtualMachineIds(), [])
def test_vm_host_get_all_by_filters_changessince(self): # Create VmHosts host_ids = ('VH1', 'VH2', 'VH3') host_names = ('name1', 'name2', 'name3') for i in range(len(host_ids)): self.__create_vm_host(id=host_ids[i], name=host_names[i]) created_time = long(time.time() * 1000L) # Wait for 1 sec and update second host and delete third host time.sleep(1) second_host = healthnmon_db_api.vm_host_get_by_ids( self.admin_context, [host_ids[1]])[0] second_host.name = 'New name' healthnmon_db_api.vm_host_save(self.admin_context, second_host) healthnmon_db_api.vm_host_delete_by_ids( self.admin_context, [host_ids[2]]) # Query with filter expected_updated_ids = [host_ids[1], host_ids[2]] filters = {'changes-since': created_time} vmhosts = healthnmon_db_api.vm_host_get_all_by_filters( self.admin_context, filters, None, None) self.assert_(vmhosts is not None) self.assert_(len(vmhosts) == 2) for host in vmhosts: self.assert_(host is not None) self.assert_(host.id in expected_updated_ids)
def test_filtered_ordered_query_changessince_invalid_value(self): # Create VmHost vmhost = VmHost() vmhost.id = 'VH1' healthnmon_db_api.vm_host_save(self.admin_context, vmhost) # Query with invalid changes-since filters = {'changes-since': 'invalid-value'} vmhosts = healthnmon_db_api.vm_host_get_all_by_filters( self.admin_context, filters, None, None) self.assert_(vmhosts is not None) self.assert_(len(vmhosts) == 1) self.assert_(vmhosts[0] is not None) self.assert_(vmhosts[0].id == vmhost.id)
def test_vm_not_deleted(self): """ Test if vmhost get all by filters lists virtual machines which have deleted flag set to false or None. """ vmhost, vm_01, vm_02 = self._setup_host() vm_01.deleted = False vm_02.deleted = None self.__save(vmhost, vm_01, vm_02) hosts = healthnmon_db_api.vm_host_get_all_by_filters( self.admin_context, None, None, None) self.assertEquals( hosts[0].get_virtualMachineIds(), ['u23lksd-32342-324l-23423', 'u23lksd-32342-324l-demo'])
def test_filtered_ordered_query_sort_no_field(self): # Create VmHost vmhost = VmHost() vmhost.id = 'VH1' healthnmon_db_api.vm_host_save(self.admin_context, vmhost) # Query with invalid sort key vmhosts = healthnmon_db_api.vm_host_get_all_by_filters( self.admin_context, None, 'invalidSortField', DbConstants.ORDER_ASC) self.assert_(vmhosts is not None) self.assert_(len(vmhosts) == 1) self.assert_(vmhosts[0] is not None) self.assert_(vmhosts[0].id == vmhost.id)
def test_vm_host_get_all_by_filters(self): # Create VmHosts host_ids = ('VH1', 'VH2') host_names = ('name1', 'name2') for i in range(len(host_ids)): self.__create_vm_host(id=host_ids[i], name=host_names[i]) # Query with filter filters = {'name': host_names[1]} vmhosts = healthnmon_db_api.vm_host_get_all_by_filters( self.admin_context, filters, 'id', DbConstants.ORDER_ASC) self.assert_(vmhosts is not None) self.assert_(len(vmhosts) == 1) self.assert_(vmhosts[0] is not None) self.assert_(vmhosts[0].id == host_ids[1])
def test_vm_not_deleted(self): """ Test if vmhost get all by filters lists virtual machines which have deleted flag set to false or None. """ vmhost, vm_01, vm_02 = self._setup_host() vm_01.deleted = False vm_02.deleted = None self.__save(vmhost, vm_01, vm_02) hosts = healthnmon_db_api.vm_host_get_all_by_filters( self.admin_context, None, None, None) self.assertEquals(hosts[0].get_virtualMachineIds(), ['u23lksd-32342-324l-23423', 'u23lksd-32342-324l-demo'])
def vm_host_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 VmHost objects. Parameters: context - nova.context.RequestContext object filters - dictionary of filters to be applied keys should be fields of VmHost model if value is simple value = filter is applied and if value is list or tuple 'IN' filter is applied eg : {'connectionState':'Connected', 'name':['n1', 'n2']} will filter as connectionState = 'Connected' 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 vm_hosts that match all filters and sorted with sort_key """ return api.vm_host_get_all_by_filters(context, filters, sort_key, sort_dir)
def test_vmhost_save_modify_delete_with_vSwitch_pGroup(self): """Test case for filter deleted virtual switch and port group 1. Create host with 2 virtual switch and port groups. 2. Assert for the above point. 3. Save VmHost by removing one virtualwitch and one port group. 4. Assert for deleted virtual switch and port group. 5. Delete the host. 6. Use filter_by api to assert for deleted host, virtual switch and port group. """ "Test for points 1 and 2" host_id = 'VH1' vmhost = VmHost() vmhost.id = host_id cost = self._create_cost() vSwitch1 = self._create_switch(host_id + '_vSwitch-01') vSwitch1.set_cost(cost) portGroup1 = self._create_port_group(host_id + '_pGroup-01') portGroup1.set_cost(cost) vSwitch1.add_portGroups(portGroup1) vmhost.add_virtualSwitches(vSwitch1) vmhost.add_portGroups(portGroup1) "Add the second vswitch and portgroup" vSwitch2 = self._create_switch(host_id + '_vSwitch-02') vSwitch2.set_cost(cost) portGroup2 = self._create_port_group(host_id + '_pGroup-02') portGroup2.set_cost(cost) vSwitch2.add_portGroups(portGroup2) vmhost.add_virtualSwitches(vSwitch2) vmhost.add_portGroups(portGroup2) healthnmon_db_api.vm_host_save(get_admin_context(), vmhost) vmhosts = healthnmon_db_api.vm_host_get_by_ids(get_admin_context(), [host_id]) self.assertFalse(vmhosts is None, 'Host get by id returned a none list') self.assertTrue( len(vmhosts[0].get_virtualSwitches()) > 0, 'Host get by virtual switch returned invalid number of list') vss = vmhosts[0].get_virtualSwitches() vs_ids = [] for vs in vss: vs_ids.append(vs.get_id()) self.assertTrue( vSwitch1.get_id() in vs_ids, "Added virtual switch1 does not appears in the host api") self.assertTrue( vSwitch2.get_id() in vs_ids, "Added virtual switch2 does not appears in the host api") pgs = vmhosts[0].get_portGroups() pg_ids = [] for pg in pgs: pg_ids.append(pg.get_id()) self.assertTrue(portGroup1.get_id() in pg_ids, "Added port group1 does not appears in the host api") self.assertTrue(portGroup2.get_id() in pg_ids, "Added port group2 does not appears in the host api") "Points 3 and 4 - Remove the second vswitch and portgroup from the vmhost and the save the vmhost" vmhost = VmHost() vmhost.id = host_id cost = self._create_cost() vSwitch = self._create_switch(host_id + '_vSwitch-01') vSwitch.set_cost(cost) portGroup = self._create_port_group(host_id + '_pGroup-01') portGroup.set_cost(cost) vSwitch.add_portGroups(portGroup) vmhost.add_virtualSwitches(vSwitch) vmhost.add_portGroups(portGroup) healthnmon_db_api.vm_host_save(get_admin_context(), vmhost) vmhosts = healthnmon_db_api.vm_host_get_by_ids(get_admin_context(), [host_id]) self.assertFalse(vmhosts is None, 'Host get by id returned a none list') vss = vmhosts[0].get_virtualSwitches() vs_ids = [] for vs in vss: vs_ids.append(vs.get_id()) self.assertTrue( vSwitch.get_id() in vs_ids, "Modified virtual switch1 not appearing in the host api") self.assertTrue(vSwitch2.get_id() not in vs_ids, "Deleted virtual switch2 appears in the host api") pgs = vmhosts[0].get_portGroups() pg_ids = [] for pg in pgs: pg_ids.append(pg.get_id()) self.assertTrue(portGroup.get_id() in pg_ids, "Modified port group1 not appearing in the host api") self.assertTrue(portGroup2.get_id() not in pg_ids, "Deleted port group2 appears in the host api") "Points 5 and 6 - Delete the host and assert for deletion using filter-by api" filters = {'id': host_id, 'deleted': 'true'} healthnmon_db_api.vm_host_delete_by_ids(get_admin_context(), [host_id]) del_vmhosts = healthnmon_db_api.vm_host_get_all_by_filters( get_admin_context(), filters, 'id', 'asc') self.assertFalse(del_vmhosts is None, 'Host get by filters returned a none list') vss = del_vmhosts[0].get_virtualSwitches() vs_ids = [] for vs in vss: vs_ids.append(vs.get_id()) self.assertTrue( vSwitch1.get_id() in vs_ids, "Deleted virtual switch1 not appearing in the host filter api") self.assertTrue( vSwitch2.get_id() in vs_ids, "Deleted virtual switch2 not appears in the host filter api") pgs = del_vmhosts[0].get_portGroups() pg_ids = [] for pg in pgs: pg_ids.append(pg.get_id()) self.assertTrue( portGroup1.get_id() in pg_ids, "Deleted port group1 not appearing in the host filter api") self.assertTrue( portGroup2.get_id() in pg_ids, "Deleted port group2 not appears in the host filter api")
def test_vmhost_save_modify_delete_with_vSwitch_pGroup(self): """Test case for filter deleted virtual switch and port group 1. Create host with 2 virtual switch and port groups. 2. Assert for the above point. 3. Save VmHost by removing one virtualwitch and one port group. 4. Assert for deleted virtual switch and port group. 5. Delete the host. 6. Use filter_by api to assert for deleted host, virtual switch and port group. """ "Test for points 1 and 2" host_id = 'VH1' vmhost = VmHost() vmhost.id = host_id cost = self._create_cost() vSwitch1 = self._create_switch(host_id + '_vSwitch-01') vSwitch1.set_cost(cost) portGroup1 = self._create_port_group(host_id + '_pGroup-01') portGroup1.set_cost(cost) vSwitch1.add_portGroups(portGroup1) vmhost.add_virtualSwitches(vSwitch1) vmhost.add_portGroups(portGroup1) "Add the second vswitch and portgroup" vSwitch2 = self._create_switch(host_id + '_vSwitch-02') vSwitch2.set_cost(cost) portGroup2 = self._create_port_group(host_id + '_pGroup-02') portGroup2.set_cost(cost) vSwitch2.add_portGroups(portGroup2) vmhost.add_virtualSwitches(vSwitch2) vmhost.add_portGroups(portGroup2) healthnmon_db_api.vm_host_save(get_admin_context(), vmhost) vmhosts = healthnmon_db_api.vm_host_get_by_ids( get_admin_context(), [host_id]) self.assertFalse( vmhosts is None, 'Host get by id returned a none list') self.assertTrue( len(vmhosts[0].get_virtualSwitches()) > 0, 'Host get by virtual switch returned invalid number of list') vss = vmhosts[0].get_virtualSwitches() vs_ids = [] for vs in vss: vs_ids.append(vs.get_id()) self.assertTrue( vSwitch1.get_id() in vs_ids, "Added virtual switch1 does not appears in the host api") self.assertTrue( vSwitch2.get_id() in vs_ids, "Added virtual switch2 does not appears in the host api") pgs = vmhosts[0].get_portGroups() pg_ids = [] for pg in pgs: pg_ids.append(pg.get_id()) self.assertTrue(portGroup1.get_id( ) in pg_ids, "Added port group1 does not appears in the host api") self.assertTrue(portGroup2.get_id( ) in pg_ids, "Added port group2 does not appears in the host api") # Points 3 and 4 - Remove the second vswitch and # portgroup from the vmhost and the save the vmhost" vmhost = VmHost() vmhost.id = host_id cost = self._create_cost() vSwitch = self._create_switch(host_id + '_vSwitch-01') vSwitch.set_cost(cost) portGroup = self._create_port_group(host_id + '_pGroup-01') portGroup.set_cost(cost) vSwitch.add_portGroups(portGroup) vmhost.add_virtualSwitches(vSwitch) vmhost.add_portGroups(portGroup) healthnmon_db_api.vm_host_save(get_admin_context(), vmhost) vmhosts = healthnmon_db_api.vm_host_get_by_ids( get_admin_context(), [host_id]) self.assertFalse( vmhosts is None, 'Host get by id returned a none list') vss = vmhosts[0].get_virtualSwitches() vs_ids = [] for vs in vss: vs_ids.append(vs.get_id()) self.assertTrue( vSwitch.get_id() in vs_ids, "Modified virtual switch1 not appearing in the host api") self.assertTrue(vSwitch2.get_id() not in vs_ids, "Deleted virtual switch2 appears in the host api") pgs = vmhosts[0].get_portGroups() pg_ids = [] for pg in pgs: pg_ids.append(pg.get_id()) self.assertTrue(portGroup.get_id( ) in pg_ids, "Modified port group1 not appearing in the host api") self.assertTrue(portGroup2.get_id( ) not in pg_ids, "Deleted port group2 appears in the host api") # Points 5 and 6 - Delete the host and # assert for deletion using filter-by api" filters = {'id': host_id, 'deleted': 'true'} healthnmon_db_api.vm_host_delete_by_ids(get_admin_context(), [host_id]) del_vmhosts = healthnmon_db_api.vm_host_get_all_by_filters( get_admin_context(), filters, 'id', 'asc') self.assertFalse( del_vmhosts is None, 'Host get by filters returned a none list') vss = del_vmhosts[0].get_virtualSwitches() vs_ids = [] for vs in vss: vs_ids.append(vs.get_id()) self.assertTrue( vSwitch1.get_id() in vs_ids, "Deleted virtual switch1 not appearing in the host filter api") self.assertTrue( vSwitch2.get_id() in vs_ids, "Deleted virtual switch2 not appears in the host filter api") pgs = del_vmhosts[0].get_portGroups() pg_ids = [] for pg in pgs: pg_ids.append(pg.get_id()) self.assertTrue( portGroup1.get_id() in pg_ids, "Deleted port group1 not appearing in the host filter api") self.assertTrue( portGroup2.get_id() in pg_ids, "Deleted port group2 not appears in the host filter api")
def test_timestamp_columns(self): """ Test the time stamp columns createEpoch, modifiedEpoch and deletedEpoch """ vmhost = VmHost() vmhost.set_id('VH1') virSw1 = VirtualSwitch() virSw1.set_id('VS1_VH1') portGrp1 = PortGroup() portGrp1.set_id('PG1_VH1') virSw1.add_portGroups(portGrp1) vmhost.add_virtualSwitches(virSw1) vmhost.add_portGroups(portGrp1) # Check for createEpoch epoch_before = utils.get_current_epoch_ms() healthnmon_db_api.vm_host_save(self.admin_context, vmhost) epoch_after = utils.get_current_epoch_ms() vmhost_queried = healthnmon_db_api.vm_host_get_by_ids( self.admin_context, [vmhost.get_id()])[0] self.assert_( test_utils.is_timestamp_between(epoch_before, epoch_after, vmhost_queried.get_createEpoch())) for virSw in vmhost_queried.get_virtualSwitches(): self.assert_( test_utils.is_timestamp_between(epoch_before, epoch_after, virSw.get_createEpoch())) for pg in virSw.get_portGroups(): self.assert_( test_utils.is_timestamp_between(epoch_before, epoch_after, pg.get_createEpoch())) # Check for lastModifiedEpoch after modifying host vmhost_modified = vmhost_queried test_utils.unset_timestamp_fields(vmhost_modified) vmhost_modified.set_name('changed_name') epoch_before = utils.get_current_epoch_ms() healthnmon_db_api.vm_host_save(self.admin_context, vmhost_modified) epoch_after = utils.get_current_epoch_ms() vmhost_queried = healthnmon_db_api.vm_host_get_by_ids( self.admin_context, [vmhost.get_id()])[0] self.assert_(vmhost_modified.get_createEpoch() == vmhost_queried.get_createEpoch()) self.assert_( test_utils.is_timestamp_between( epoch_before, epoch_after, vmhost_queried.get_lastModifiedEpoch())) for virSw in vmhost_queried.get_virtualSwitches(): self.assert_( test_utils.is_timestamp_between(epoch_before, epoch_after, virSw.get_lastModifiedEpoch())) for pg in virSw.get_portGroups(): self.assert_( test_utils.is_timestamp_between( epoch_before, epoch_after, pg.get_lastModifiedEpoch())) # Check for createdEpoch after adding switch and portgroup to host vmhost_modified = vmhost_queried test_utils.unset_timestamp_fields(vmhost_modified) virSw2 = VirtualSwitch() virSw2.set_id('VS2_VH1') portGrp2 = PortGroup() portGrp2.set_id('PG2_VH1') virSw2.add_portGroups(portGrp2) vmhost_modified.add_virtualSwitches(virSw2) vmhost_modified.add_portGroups(portGrp2) epoch_before = utils.get_current_epoch_ms() healthnmon_db_api.vm_host_save(self.admin_context, vmhost_modified) epoch_after = utils.get_current_epoch_ms() vmhost_queried = healthnmon_db_api.vm_host_get_by_ids( self.admin_context, [vmhost.get_id()])[0] self.assert_(vmhost_modified.get_createEpoch() == vmhost_queried.get_createEpoch()) self.assert_( test_utils.is_timestamp_between( epoch_before, epoch_after, vmhost_queried.get_lastModifiedEpoch())) for virSw in vmhost_queried.get_virtualSwitches(): if virSw.get_id() == virSw2.get_id(): self.assert_( test_utils.is_timestamp_between(epoch_before, epoch_after, virSw.get_createEpoch())) else: self.assert_( test_utils.is_timestamp_between( epoch_before, epoch_after, virSw.get_lastModifiedEpoch())) for pg in virSw.get_portGroups(): if pg.get_id() == portGrp2.get_id(): self.assert_( test_utils.is_timestamp_between( epoch_before, epoch_after, pg.get_createEpoch())) else: self.assert_( test_utils.is_timestamp_between( epoch_before, epoch_after, pg.get_lastModifiedEpoch())) # Check for deletedEpoch epoch_before = utils.get_current_epoch_ms() healthnmon_db_api.vm_host_delete_by_ids(self.admin_context, [vmhost_queried.get_id()]) epoch_after = utils.get_current_epoch_ms() deleted_host = healthnmon_db_api.vm_host_get_all_by_filters( self.admin_context, {"id": vmhost_queried.get_id()}, None, None)[0] self.assertTrue(deleted_host.get_deleted()) self.assert_( test_utils.is_timestamp_between(epoch_before, epoch_after, deleted_host.get_deletedEpoch())) deleted_switches = healthnmon_db_api.virtual_switch_get_all_by_filters( self.admin_context, {"id": (virSw1.get_id(), virSw2.get_id())}, None, None) for deleted_switch in deleted_switches: self.assertTrue(deleted_switch.get_deleted()) self.assert_( test_utils.is_timestamp_between( epoch_before, epoch_after, deleted_switch.get_deletedEpoch())) for deleted_portgrp in deleted_switch.get_portGroups(): self.assertTrue(deleted_portgrp.get_deleted()) self.assert_( test_utils.is_timestamp_between( epoch_before, epoch_after, deleted_portgrp.get_deletedEpoch()))
def test_timestamp_columns(self): """ Test the time stamp columns createEpoch, modifiedEpoch and deletedEpoch """ vmhost = VmHost() vmhost.set_id('VH1') virSw1 = VirtualSwitch() virSw1.set_id('VS1_VH1') portGrp1 = PortGroup() portGrp1.set_id('PG1_VH1') virSw1.add_portGroups(portGrp1) vmhost.add_virtualSwitches(virSw1) vmhost.add_portGroups(portGrp1) # Check for createEpoch epoch_before = utils.get_current_epoch_ms() healthnmon_db_api.vm_host_save(self.admin_context, vmhost) epoch_after = utils.get_current_epoch_ms() vmhost_queried = healthnmon_db_api.vm_host_get_by_ids( self.admin_context, [vmhost.get_id()])[0] self.assert_(test_utils.is_timestamp_between( epoch_before, epoch_after, vmhost_queried.get_createEpoch())) for virSw in vmhost_queried.get_virtualSwitches(): self.assert_(test_utils.is_timestamp_between( epoch_before, epoch_after, virSw.get_createEpoch())) for pg in virSw.get_portGroups(): self.assert_(test_utils.is_timestamp_between( epoch_before, epoch_after, pg.get_createEpoch())) # Check for lastModifiedEpoch after modifying host vmhost_modified = vmhost_queried test_utils.unset_timestamp_fields(vmhost_modified) vmhost_modified.set_name('changed_name') epoch_before = utils.get_current_epoch_ms() healthnmon_db_api.vm_host_save(self.admin_context, vmhost_modified) epoch_after = utils.get_current_epoch_ms() vmhost_queried = healthnmon_db_api.vm_host_get_by_ids( self.admin_context, [vmhost.get_id()])[0] self.assert_(vmhost_modified.get_createEpoch( ) == vmhost_queried.get_createEpoch()) self.assert_(test_utils.is_timestamp_between( epoch_before, epoch_after, vmhost_queried.get_lastModifiedEpoch())) for virSw in vmhost_queried.get_virtualSwitches(): self.assert_(test_utils.is_timestamp_between( epoch_before, epoch_after, virSw.get_lastModifiedEpoch())) for pg in virSw.get_portGroups(): self.assert_(test_utils.is_timestamp_between( epoch_before, epoch_after, pg.get_lastModifiedEpoch())) # Check for createdEpoch after adding switch and portgroup to host vmhost_modified = vmhost_queried test_utils.unset_timestamp_fields(vmhost_modified) virSw2 = VirtualSwitch() virSw2.set_id('VS2_VH1') portGrp2 = PortGroup() portGrp2.set_id('PG2_VH1') virSw2.add_portGroups(portGrp2) vmhost_modified.add_virtualSwitches(virSw2) vmhost_modified.add_portGroups(portGrp2) epoch_before = utils.get_current_epoch_ms() healthnmon_db_api.vm_host_save(self.admin_context, vmhost_modified) epoch_after = utils.get_current_epoch_ms() vmhost_queried = healthnmon_db_api.vm_host_get_by_ids( self.admin_context, [vmhost.get_id()])[0] self.assert_(vmhost_modified.get_createEpoch( ) == vmhost_queried.get_createEpoch()) self.assert_(test_utils.is_timestamp_between( epoch_before, epoch_after, vmhost_queried.get_lastModifiedEpoch())) for virSw in vmhost_queried.get_virtualSwitches(): if virSw.get_id() == virSw2.get_id(): self.assert_(test_utils.is_timestamp_between( epoch_before, epoch_after, virSw.get_createEpoch())) else: self.assert_(test_utils.is_timestamp_between( epoch_before, epoch_after, virSw.get_lastModifiedEpoch())) for pg in virSw.get_portGroups(): if pg.get_id() == portGrp2.get_id(): self.assert_(test_utils.is_timestamp_between( epoch_before, epoch_after, pg.get_createEpoch())) else: self.assert_(test_utils.is_timestamp_between( epoch_before, epoch_after, pg.get_lastModifiedEpoch())) # Check for deletedEpoch epoch_before = utils.get_current_epoch_ms() healthnmon_db_api.vm_host_delete_by_ids( self.admin_context, [vmhost_queried.get_id()]) epoch_after = utils.get_current_epoch_ms() deleted_host = healthnmon_db_api.vm_host_get_all_by_filters( self.admin_context, {"id": vmhost_queried.get_id()}, None, None)[0] self.assertTrue(deleted_host.get_deleted()) self.assert_(test_utils.is_timestamp_between( epoch_before, epoch_after, deleted_host.get_deletedEpoch())) deleted_switches = healthnmon_db_api.\ virtual_switch_get_all_by_filters(self.admin_context, {"id": (virSw1.get_id(), virSw2.get_id())}, None, None) for deleted_switch in deleted_switches: self.assertTrue(deleted_switch.get_deleted()) self.assert_(test_utils.is_timestamp_between( epoch_before, epoch_after, deleted_switch.get_deletedEpoch())) for deleted_portgrp in deleted_switch.get_portGroups(): self.assertTrue(deleted_portgrp.get_deleted()) self.assert_(test_utils.is_timestamp_between( epoch_before, epoch_after, deleted_portgrp.get_deletedEpoch()))