def test_get_vm_resource_utilization(self): self.sample_perfdata = SamplePerfData() self.mox.StubOutWithMock(SamplePerfData, 'sample_vm_perfdata') self.sample_perfdata.sample_vm_perfdata(self.vm_id, 5).AndReturn(None) self.mox.ReplayAll() result = \ self.libvirtPerf_monitor.get_resource_utilization( self.vm_id, Constants.Vm, 5) self.assert_(True)
class TestLibvirtPerfData(unittest.TestCase): inv_manager_cls = inventory_manager.InventoryManager paramiko.SSHClient = FakeSSH.SSHClient connection.libvirt = libvirt @classmethod def setUpClass(cls): cls.connection = LibvirtConnection(False) cls.mox = mox.Mox() cls.connection._wrapped_conn = libvirt.open('qemu:///system') cls.connection.libvirt = libvirt cls.vm_id = '25f04dd3-e924-02b2-9eac-876e3c943262' cls.vmhost_id = '1' def setUp(self): self.libvirtPerf_monitor = LibvirtPerfMonitor() self._createPerfCache() def _createPerfCache(self): self.libvirtPerf_monitor.perfDataCache[ self.vm_id ] = {Constants.OLD_STATS: None, Constants.NEW_STATS: None} self.libvirtPerf_monitor.perfDataCache[ self.vmhost_id ] = {Constants.OLD_STATS: None, Constants.NEW_STATS: None} def testDelete_perfdata_fromCache(self): self.assertEquals(len(self.libvirtPerf_monitor.perfDataCache), 2) self.libvirtPerf_monitor.delete_perfdata_fromCache( self.vm_id ) self.assertEquals(len(self.libvirtPerf_monitor.perfDataCache), 1) def test_refresh_perfdata_forVm(self): self.libvirtVM = LibvirtVmPerfData(self.connection._wrapped_conn, self.vm_id) self.mox.StubOutWithMock(LibvirtVmPerfData, 'refresh_perfdata') self.libvirtVM.refresh_perfdata().AndReturn(None) self.mox.ReplayAll() self.libvirtPerf_monitor.refresh_perfdata( self.connection._wrapped_conn, self.vm_id, Constants.Vm) self.assert_(True) def test_refresh_perfdata_forHost(self): self.mox.StubOutWithMock(event_api, 'notify_host_update') event_api.notify_host_update(mox.IgnoreArg(), mox.IgnoreArg()) self.libvirtVmHost = LibvirtVmHostPerfData( self.connection._wrapped_conn, self.vmhost_id) self.mox.StubOutWithMock(LibvirtVmHostPerfData, 'refresh_perfdata') self.libvirtVmHost.refresh_perfdata().AndReturn(None) self.mox.ReplayAll() self.libvirtPerf_monitor.refresh_perfdata( self.connection._wrapped_conn, self.vmhost_id, Constants.VmHost) self.assert_(True) def test_refresh_perfdata_NoCache(self): self.libvirtPerf_monitor.delete_perfdata_fromCache( self.vm_id ) self.libvirtVmHost = LibvirtVmHostPerfData( self.connection._wrapped_conn, self.vmhost_id) self.mox.StubOutWithMock(LibvirtVmHostPerfData, 'refresh_perfdata') self.libvirtVmHost.refresh_perfdata().AndReturn(None) self.mox.ReplayAll() self.libvirtPerf_monitor.refresh_perfdata( self.connection._wrapped_conn, self.vm_id, Constants.VmHost) def test_get_host_resource_utilization(self): self.sample_perfdata = SamplePerfData() self.mox.StubOutWithMock(SamplePerfData, 'sample_host_perfdata') self.sample_perfdata.sample_host_perfdata( self.vmhost_id, 5).AndReturn(None) self.mox.ReplayAll() result = \ self.libvirtPerf_monitor.get_resource_utilization( self.vmhost_id, Constants.VmHost, 5) self.assert_(True) def test_get_vm_resource_utilization(self): self.sample_perfdata = SamplePerfData() self.mox.StubOutWithMock(SamplePerfData, 'sample_vm_perfdata') self.sample_perfdata.sample_vm_perfdata(self.vm_id, 5).AndReturn(None) self.mox.ReplayAll() result = \ self.libvirtPerf_monitor.get_resource_utilization( self.vm_id, Constants.Vm, 5) self.assert_(True) def createInvCache(self, vmrunning, hostconnection='Connected'): vmhost = VmHost() vmhost.set_id(self.vmhost_id) vmhost.set_connectionState(hostconnection) vm = Vm() vm.set_id(self.vm_id) if vmrunning: vm.set_powerState(Constants.VM_POWER_STATES[1]) else: vm.set_powerState(Constants.VM_POWER_STATES[0]) vm.set_vmHostId(self.vmhost_id) vmhost.set_virtualMachineIds([self.vm_id ]) vmhost.set_processorSpeedMhz(2100) vmhost.set_processorCoresCount(4) vmhost.set_processorCount('2') vmhost.set_memorySize(2097152) vmhost.set_memoryConsumed(2097152) InventoryCacheManager.update_object_in_cache(self.vmhost_id, vmhost) InventoryCacheManager.update_object_in_cache( self.vm_id, vm) def tearDown(self): self.libvirtPerf_monitor.perfDataCache = {} self.mox.UnsetStubs()
def setUp(self): self.libvirtPerf_monitor = LibvirtPerfMonitor() self._createPerfCache() self.sample_perfdata = SamplePerfData()
class TestSamplePerfData(unittest.TestCase): @classmethod def setUpClass(cls): cls.connection = LibvirtConnection(False) cls.mox = mox.Mox() cls.connection._wrapped_conn = libvirt.open('qemu:///system') cls.connection.libvirt = libvirt cls.vm_id = '25f04dd3-e924-02b2-9eac-876e3c943262' cls.vmhost_id = '1' def setUp(self): self.libvirtPerf_monitor = LibvirtPerfMonitor() self._createPerfCache() self.sample_perfdata = SamplePerfData() def _createPerfCache(self): self.libvirtPerf_monitor.perfDataCache[ self.vm_id ] = {Constants.OLD_STATS: None, Constants.NEW_STATS: None} self.libvirtPerf_monitor.perfDataCache[ self.vmhost_id ] = {Constants.OLD_STATS: None, Constants.NEW_STATS: None} def test_sample_vm_perfdata(self): self.createInvCache(True) old_stats = self.createfake_oldStats() new_stats = self.createfake_newStats(False, True) LibvirtPerfMonitor.update_perfdata_InCache( self.vm_id, old_stats, new_stats) resource_util = \ self.sample_perfdata.sample_vm_perfdata( self.vm_id, 5) self.assertNotEquals(self.sample_perfdata.resource_utilization, None) self.assertEquals(resource_util.get_status(), 0) self._assert(resource_util) def test_sample_vm_perfdata_notsampled(self): self.createInvCache(True) new_stats = self.createfake_newStats(False, True) LibvirtPerfMonitor.update_perfdata_InCache( self.vm_id, None, new_stats) resource_util = \ self.sample_perfdata.sample_vm_perfdata( self.vm_id, 5) self.assertEquals(resource_util.get_status(), -1) def test_sample_vm_perfdata_notvalid(self): self.createInvCache(True) old_stats = self.createfake_oldStats() new_stats = self.createfake_newStats(False, False) LibvirtPerfMonitor.update_perfdata_InCache( self.vm_id, old_stats, new_stats) resource_util = \ self.sample_perfdata.sample_vm_perfdata( self.vm_id, 5) self.assertEquals(resource_util.get_status(), -1) def test_sample_vm_perfdata_notRunning(self): self.createInvCache(False) old_stats = self.createfake_oldStats() new_stats = self.createfake_newStats(False, False) LibvirtPerfMonitor.update_perfdata_InCache( self.vm_id, old_stats, new_stats) resource_util = \ self.sample_perfdata.sample_vm_perfdata( self.vm_id, 5) self.assertEquals(resource_util.get_status(), -1) self.assertEquals(len(self.libvirtPerf_monitor.perfDataCache), 1) def test_sample_vm_perfdata_nochange_inperf(self): self.createInvCache(True) old_stats = self.createfake_oldStats() new_stats = self.createfake_newStats_nochange(False, False) LibvirtPerfMonitor.update_perfdata_InCache( self.vm_id, old_stats, new_stats) resource_util = \ self.sample_perfdata.sample_vm_perfdata( self.vm_id, 5) self.assertEquals(resource_util.get_status(), 0) def test_sample_vm_perfdata_poweroff(self): self.createInvCache(True) old_stats = self.createfake_oldStats() new_stats = self.createfake_newStats_nochange(True, False) LibvirtPerfMonitor.update_perfdata_InCache( self.vm_id, old_stats, new_stats) resource_util = \ self.sample_perfdata.sample_vm_perfdata( self.vm_id, 5) self.assertEquals(resource_util.get_status(), 0) def test_sample_vm_perfdata_negativeStats(self): self.createInvCache(True) old_stats = self.createfake_oldStats() new_stats = self.createfake_newStats_nochange(False, True) LibvirtPerfMonitor.update_perfdata_InCache( self.vm_id, old_stats, new_stats) resource_util = \ self.sample_perfdata.sample_vm_perfdata( self.vm_id, 5) self.assertEquals(resource_util.get_status(), 0) def test_sample_host_perfdata(self): self.createInvCache(True) old_stats = self.createfake_oldStats() new_stats = self.createfake_newStats(True, True) new_stats_forhost = self.createfake_newStats_forhost() LibvirtPerfMonitor.update_perfdata_InCache( self.vmhost_id, None, new_stats_forhost) LibvirtPerfMonitor.update_perfdata_InCache( self.vm_id, old_stats, new_stats) resource_util = self.sample_perfdata.sample_host_perfdata( self.vmhost_id, 5) self.assertNotEquals(self.libvirtPerf_monitor.get_perfdata_fromCache( self.vm_id, Constants.NEW_STATS), None) self.assertNotEquals(self.libvirtPerf_monitor.get_perfdata_fromCache( self.vm_id, Constants.OLD_STATS), None) self.assertNotEquals(self.sample_perfdata.resource_utilization, None) self.assertEquals(resource_util.get_status(), 0) self._assert(resource_util) def test_sample_host_perfdata_invlidStatus(self): self.createInvCache(True) old_stats = self.createfake_oldStats() new_stats = self.createfake_newStats(True, True) new_stats_forhost = self.createfake_newStats_forhost(False) LibvirtPerfMonitor.update_perfdata_InCache( self.vmhost_id, None, new_stats_forhost) LibvirtPerfMonitor.update_perfdata_InCache( self.vm_id, old_stats, new_stats) resource_util = self.sample_perfdata.sample_host_perfdata( self.vmhost_id, 5) self.assertNotEquals(self.libvirtPerf_monitor.get_perfdata_fromCache( self.vm_id, Constants.NEW_STATS), None) self.assertNotEquals(self.libvirtPerf_monitor.get_perfdata_fromCache( self.vm_id, Constants.OLD_STATS), None) self.assertNotEquals(self.sample_perfdata.resource_utilization, None) self.assertEquals(resource_util.get_status(), 0) self.assertEquals(resource_util.get_totalMemory(), 0) self.assertEquals(resource_util.get_freeMemory(), 0) def test_sample_host_perfdata_notValid(self): self.createInvCache(True) old_stats = self.createfake_oldStats() new_stats = self.createfake_newStats(False, True) new_stats_forhost = self.createfake_newStats_forhost() LibvirtPerfMonitor.update_perfdata_InCache( self.vmhost_id, None, new_stats_forhost) LibvirtPerfMonitor.update_perfdata_InCache( self.vm_id, old_stats, new_stats) resource_util = self.sample_perfdata.sample_host_perfdata( self.vmhost_id, 5) self.assertEquals(resource_util.get_status(), -1) def test_sample_host_perfdata_vmnotRunning(self): self.createInvCache(False) new_stats = self.createfake_newStats(False, True) new_stats_forhost = self.createfake_newStats_forhost() LibvirtPerfMonitor.update_perfdata_InCache( self.vmhost_id, None, new_stats_forhost) LibvirtPerfMonitor.update_perfdata_InCache( self.vm_id, None, new_stats) resource_util = self.sample_perfdata.sample_host_perfdata( self.vmhost_id, 5) self.assertEquals(resource_util.get_status(), 0) def test_sample_host_perfdata_notsampled(self): self.createInvCache(True) new_stats = self.createfake_newStats(False, True) LibvirtPerfMonitor.update_perfdata_InCache( self.vm_id, None, new_stats) new_stats_forhost = self.createfake_newStats_forhost() LibvirtPerfMonitor.update_perfdata_InCache( self.vmhost_id, None, new_stats_forhost) resource_util = self.sample_perfdata.sample_host_perfdata( self.vmhost_id, 5) self.assertEquals(resource_util.get_status(), -1) def test_sample_host_perfdata_Disconnected(self): self.createInvCache(True, hostconnection='Disconnected') new_stats = self.createfake_newStats(False, True) LibvirtPerfMonitor.update_perfdata_InCache( self.vm_id, None, new_stats) resource_util = self.sample_perfdata.sample_host_perfdata( self.vmhost_id, 5) self.assertEquals(resource_util.get_status(), -1) def tearDown(self): self.libvirtPerf_monitor.perfDataCache = {} self.mox.UnsetStubs() def createInvCache(self, vmrunning, hostconnection='Connected'): vmhost = VmHost() vmhost.set_id(self.vmhost_id) vmhost.set_connectionState(hostconnection) vm = Vm() vm.set_id(self.vm_id) if vmrunning: vm.set_powerState(Constants.VM_POWER_STATES[1]) else: vm.set_powerState(Constants.VM_POWER_STATES[0]) vm.set_vmHostId(self.vmhost_id) vmhost.set_virtualMachineIds([self.vm_id ]) vmhost.set_processorSpeedMhz(2100) vmhost.set_processorCoresCount(4) vmhost.set_processorCount('2') vmhost.set_memorySize(2097152) vmhost.set_memoryConsumed(2097152) InventoryCacheManager.update_object_in_cache(self.vmhost_id, vmhost) InventoryCacheManager.update_object_in_cache( self.vm_id, vm) def create_compute_inventory(self): compute_inventory = ComputeInventory(self.connection.compute_rmcontext) InventoryCacheManager.get_all_compute_inventory( )[self.vmhost_id] = compute_inventory InventoryCacheManager.get_all_compute_inventory( )[self.vm_id] = compute_inventory self.mox.StubOutWithMock(ComputeInventory, 'get_compute_conn_driver') compute_inventory.get_compute_conn_driver().AndReturn(self.connection) self.mox.ReplayAll() def _assert(self, result): self.assertFalse((result.get_resourceId() is None or result.get_resourceId() == ''), 'Resource id is null') self.assertFalse((result.get_timestamp() is None), 'Timestamp is null') self.assertFalse( (result.get_granularity() is None), 'Granularity is null') self.assertFalse( (result.get_cpuUserLoad() is None), 'Cpu userLoad is null') self.assert_((result.get_cpuSystemLoad( ) is None), 'Cpu system load is not null') self.assertFalse( (result.get_cpuUserLoad() is None), 'Cpu userLoad is null') self.assertFalse( (result.get_hostCpuSpeed() is None), 'hostCpuSpeed is null') self.assertFalse( (result.get_hostMaxCpuSpeed() is None), 'hostMaxCpuSpeed is null') self.assertFalse((result.get_ncpus() is None), 'NCpus is null') self.assertFalse( (result.get_diskRead() is None), 'DiskRead userLoad is null') self.assertFalse( (result.get_diskWrite() is None), 'Disk Write userLoad is null') self.assertFalse((result.get_netRead() is None), 'NetRead is null') self.assertFalse((result.get_netWrite() is None), 'NetWrite is null') self.assertFalse( (result.get_totalMemory() is None), 'Total Memory is null') self.assertFalse( (result.get_freeMemory() is None), 'Free memory is null') self.assertFalse((result.get_configuredMemory( ) is None), 'Configured Memory is null') self.assert_((result.get_uptimeMinute() is None), 'uptime is not null') self.assert_((result.get_reservedSystemCapacity( ) is None), 'Reserved system capacity is not null') self.assert_((result.get_maximumSystemCapacity( ) is None), 'Maximum system capacity is not null') self.assert_((result.get_reservedSystemCapacity( ) is None), 'Reserved system capacity is not null') self.assert_((result.get_relativeWeight( ) is None), 'Relative weight is not null') self.assert_((result.get_reservedSystemMemory( ) is None), 'reserved System Memory is not null') self.assert_((result.get_maximumSystemMemory( ) is None), 'maximum System Memory is not null') self.assert_((result.get_memoryRelativeWeight( ) is None), 'Memory Relative weight is not null') self.assertFalse((result.get_status() is None), 'Status is null') self.assertNotEqual(result, None) def createfake_oldStats(self): stats = Stats() stats.freeMemory = 0L stats.totalMemory = 2097152L stats.timestamp = 1331372444.5931201 stats.ncpus = 1 stats.diskReadBytes = 226644480L stats.diskWriteBytes = 33723392L stats.netReceivedBytes = 357325L stats.netTransmittedBytes = 8302L stats.cpuStats = CPUStats() stats.cpuStats.cycles['user'] = 696880000000 stats.cpuPerfTime = 1331372444.3490119 stats.diskPerfTime = 1331372444.472137 stats.netPerfTime = 1331372444.5922019 return stats def createfake_newStats_nochange(self, poweroff, stats_decrement): stats = Stats() stats.freeMemory = 0L stats.totalMemory = 2097152L stats.timestamp = 1331372444.5931201 stats.ncpus = 1 if poweroff: stats.diskReadBytes = 0 elif stats_decrement: stats.diskReadBytes = 226643380L else: stats.diskReadBytes = 226644480L stats.diskWriteBytes = 33723392L stats.netReceivedBytes = 357325L stats.netTransmittedBytes = 8302L stats.cpuStats = CPUStats() stats.cpuStats.cycles['user'] = 696880000000 stats.cpuPerfTime = 1331372444.3490119 stats.diskPerfTime = 1331372444.472137 stats.netPerfTime = 1331372444.5922019 return stats def createfake_newStats(self, now, status): stats = Stats() stats.freeMemory = 0L stats.totalMemory = 2097152L if now: stats.timestamp = time.time() else: stats.timestamp = 1331372625.171705 stats.ncpus = 1 stats.diskReadBytes = 226644480L stats.diskWriteBytes = 34006016L stats.netReceivedBytes = 362137L stats.netTransmittedBytes = 8302L stats.cpuStats = CPUStats() stats.cpuStats.cycles['user'] = 697480000000 stats.cpuPerfTime = 1331372624.9259009 stats.diskPerfTime = 1331372625.050205 stats.netPerfTime = 1331372625.1707871 if status: stats.status = 0 else: stats.status = -1 return stats def createfake_newStats_forhost(self, status=True): stats = Stats() stats.freeMemory = 99982L stats.totalMemory = 2097152L stats.timestamp = time.time() stats.ncpus = 1 if status: stats.status = 0 else: stats.status = -1 return stats
class TestLibvirtPerfData(unittest.TestCase): inv_manager_cls = inventory_manager.InventoryManager paramiko.SSHClient = FakeSSH.SSHClient connection.libvirt = libvirt @classmethod def setUpClass(cls): cls.connection = LibvirtConnection(False) cls.mox = mox.Mox() cls.connection._wrapped_conn = libvirt.open('qemu:///system') cls.connection.libvirt = libvirt cls.vm_id = '25f04dd3-e924-02b2-9eac-876e3c943262' cls.vmhost_id = '1' def setUp(self): self.libvirtPerf_monitor = LibvirtPerfMonitor() self._createPerfCache() def _createPerfCache(self): self.libvirtPerf_monitor.perfDataCache[self.vm_id] = { Constants.OLD_STATS: None, Constants.NEW_STATS: None } self.libvirtPerf_monitor.perfDataCache[self.vmhost_id] = { Constants.OLD_STATS: None, Constants.NEW_STATS: None } def testDelete_perfdata_fromCache(self): self.assertEquals(len(self.libvirtPerf_monitor.perfDataCache), 2) self.libvirtPerf_monitor.delete_perfdata_fromCache(self.vm_id) self.assertEquals(len(self.libvirtPerf_monitor.perfDataCache), 1) def test_refresh_perfdata_forVm(self): self.libvirtVM = LibvirtVmPerfData(self.connection._wrapped_conn, self.vm_id) self.mox.StubOutWithMock(LibvirtVmPerfData, 'refresh_perfdata') self.libvirtVM.refresh_perfdata().AndReturn(None) self.mox.ReplayAll() self.libvirtPerf_monitor.refresh_perfdata( self.connection._wrapped_conn, self.vm_id, Constants.Vm) self.assert_(True) def test_refresh_perfdata_forHost(self): self.mox.StubOutWithMock(event_api, 'notify_host_update') event_api.notify_host_update(mox.IgnoreArg(), mox.IgnoreArg()) self.libvirtVmHost = LibvirtVmHostPerfData( self.connection._wrapped_conn, self.vmhost_id) self.mox.StubOutWithMock(LibvirtVmHostPerfData, 'refresh_perfdata') self.libvirtVmHost.refresh_perfdata().AndReturn(None) self.mox.ReplayAll() self.libvirtPerf_monitor.refresh_perfdata( self.connection._wrapped_conn, self.vmhost_id, Constants.VmHost) self.assert_(True) def test_refresh_perfdata_NoCache(self): self.libvirtPerf_monitor.delete_perfdata_fromCache(self.vm_id) self.libvirtVmHost = LibvirtVmHostPerfData( self.connection._wrapped_conn, self.vmhost_id) self.mox.StubOutWithMock(LibvirtVmHostPerfData, 'refresh_perfdata') self.libvirtVmHost.refresh_perfdata().AndReturn(None) self.mox.ReplayAll() self.libvirtPerf_monitor.refresh_perfdata( self.connection._wrapped_conn, self.vm_id, Constants.VmHost) def test_get_host_resource_utilization(self): self.sample_perfdata = SamplePerfData() self.mox.StubOutWithMock(SamplePerfData, 'sample_host_perfdata') self.sample_perfdata.sample_host_perfdata(self.vmhost_id, 5).AndReturn(None) self.mox.ReplayAll() result = \ self.libvirtPerf_monitor.get_resource_utilization( self.vmhost_id, Constants.VmHost, 5) self.assert_(True) def test_get_vm_resource_utilization(self): self.sample_perfdata = SamplePerfData() self.mox.StubOutWithMock(SamplePerfData, 'sample_vm_perfdata') self.sample_perfdata.sample_vm_perfdata(self.vm_id, 5).AndReturn(None) self.mox.ReplayAll() result = \ self.libvirtPerf_monitor.get_resource_utilization( self.vm_id, Constants.Vm, 5) self.assert_(True) def createInvCache(self, vmrunning, hostconnection='Connected'): vmhost = VmHost() vmhost.set_id(self.vmhost_id) vmhost.set_connectionState(hostconnection) vm = Vm() vm.set_id(self.vm_id) if vmrunning: vm.set_powerState(Constants.VM_POWER_STATES[1]) else: vm.set_powerState(Constants.VM_POWER_STATES[0]) vm.set_vmHostId(self.vmhost_id) vmhost.set_virtualMachineIds([self.vm_id]) vmhost.set_processorSpeedMhz(2100) vmhost.set_processorCoresCount(4) vmhost.set_processorCount('2') vmhost.set_memorySize(2097152) vmhost.set_memoryConsumed(2097152) InventoryCacheManager.update_object_in_cache(self.vmhost_id, vmhost) InventoryCacheManager.update_object_in_cache(self.vm_id, vm) def tearDown(self): self.libvirtPerf_monitor.perfDataCache = {} self.mox.UnsetStubs()
class TestSamplePerfData(unittest.TestCase): @classmethod def setUpClass(cls): cls.connection = LibvirtConnection(False) cls.mox = mox.Mox() cls.connection._wrapped_conn = libvirt.open('qemu:///system') cls.connection.libvirt = libvirt cls.vm_id = '25f04dd3-e924-02b2-9eac-876e3c943262' cls.vmhost_id = '1' def setUp(self): self.libvirtPerf_monitor = LibvirtPerfMonitor() self._createPerfCache() self.sample_perfdata = SamplePerfData() def _createPerfCache(self): self.libvirtPerf_monitor.perfDataCache[self.vm_id] = { Constants.OLD_STATS: None, Constants.NEW_STATS: None } self.libvirtPerf_monitor.perfDataCache[self.vmhost_id] = { Constants.OLD_STATS: None, Constants.NEW_STATS: None } def test_sample_vm_perfdata(self): self.createInvCache(True) old_stats = self.createfake_oldStats() new_stats = self.createfake_newStats(False, True) LibvirtPerfMonitor.update_perfdata_InCache(self.vm_id, old_stats, new_stats) resource_util = \ self.sample_perfdata.sample_vm_perfdata( self.vm_id, 5) self.assertNotEquals(self.sample_perfdata.resource_utilization, None) self.assertEquals(resource_util.get_status(), 0) self._assert(resource_util) def test_sample_vm_perfdata_notsampled(self): self.createInvCache(True) new_stats = self.createfake_newStats(False, True) LibvirtPerfMonitor.update_perfdata_InCache(self.vm_id, None, new_stats) resource_util = \ self.sample_perfdata.sample_vm_perfdata( self.vm_id, 5) self.assertEquals(resource_util.get_status(), -1) def test_sample_vm_perfdata_notvalid(self): self.createInvCache(True) old_stats = self.createfake_oldStats() new_stats = self.createfake_newStats(False, False) LibvirtPerfMonitor.update_perfdata_InCache(self.vm_id, old_stats, new_stats) resource_util = \ self.sample_perfdata.sample_vm_perfdata( self.vm_id, 5) self.assertEquals(resource_util.get_status(), -1) def test_sample_vm_perfdata_notRunning(self): self.createInvCache(False) old_stats = self.createfake_oldStats() new_stats = self.createfake_newStats(False, False) LibvirtPerfMonitor.update_perfdata_InCache(self.vm_id, old_stats, new_stats) resource_util = \ self.sample_perfdata.sample_vm_perfdata( self.vm_id, 5) self.assertEquals(resource_util.get_status(), -1) self.assertEquals(len(self.libvirtPerf_monitor.perfDataCache), 1) def test_sample_vm_perfdata_nochange_inperf(self): self.createInvCache(True) old_stats = self.createfake_oldStats() new_stats = self.createfake_newStats_nochange(False, False) LibvirtPerfMonitor.update_perfdata_InCache(self.vm_id, old_stats, new_stats) resource_util = \ self.sample_perfdata.sample_vm_perfdata( self.vm_id, 5) self.assertEquals(resource_util.get_status(), 0) def test_sample_vm_perfdata_poweroff(self): self.createInvCache(True) old_stats = self.createfake_oldStats() new_stats = self.createfake_newStats_nochange(True, False) LibvirtPerfMonitor.update_perfdata_InCache(self.vm_id, old_stats, new_stats) resource_util = \ self.sample_perfdata.sample_vm_perfdata( self.vm_id, 5) self.assertEquals(resource_util.get_status(), 0) def test_sample_vm_perfdata_negativeStats(self): self.createInvCache(True) old_stats = self.createfake_oldStats() new_stats = self.createfake_newStats_nochange(False, True) LibvirtPerfMonitor.update_perfdata_InCache(self.vm_id, old_stats, new_stats) resource_util = \ self.sample_perfdata.sample_vm_perfdata( self.vm_id, 5) self.assertEquals(resource_util.get_status(), 0) def test_sample_host_perfdata(self): self.createInvCache(True) old_stats = self.createfake_oldStats() new_stats = self.createfake_newStats(True, True) new_stats_forhost = self.createfake_newStats_forhost() LibvirtPerfMonitor.update_perfdata_InCache(self.vmhost_id, None, new_stats_forhost) LibvirtPerfMonitor.update_perfdata_InCache(self.vm_id, old_stats, new_stats) resource_util = self.sample_perfdata.sample_host_perfdata( self.vmhost_id, 5) self.assertNotEquals( self.libvirtPerf_monitor.get_perfdata_fromCache( self.vm_id, Constants.NEW_STATS), None) self.assertNotEquals( self.libvirtPerf_monitor.get_perfdata_fromCache( self.vm_id, Constants.OLD_STATS), None) self.assertNotEquals(self.sample_perfdata.resource_utilization, None) self.assertEquals(resource_util.get_status(), 0) self._assert(resource_util) def test_sample_host_perfdata_invlidStatus(self): self.createInvCache(True) old_stats = self.createfake_oldStats() new_stats = self.createfake_newStats(True, True) new_stats_forhost = self.createfake_newStats_forhost(False) LibvirtPerfMonitor.update_perfdata_InCache(self.vmhost_id, None, new_stats_forhost) LibvirtPerfMonitor.update_perfdata_InCache(self.vm_id, old_stats, new_stats) resource_util = self.sample_perfdata.sample_host_perfdata( self.vmhost_id, 5) self.assertNotEquals( self.libvirtPerf_monitor.get_perfdata_fromCache( self.vm_id, Constants.NEW_STATS), None) self.assertNotEquals( self.libvirtPerf_monitor.get_perfdata_fromCache( self.vm_id, Constants.OLD_STATS), None) self.assertNotEquals(self.sample_perfdata.resource_utilization, None) self.assertEquals(resource_util.get_status(), 0) self.assertEquals(resource_util.get_totalMemory(), 0) self.assertEquals(resource_util.get_freeMemory(), 0) def test_sample_host_perfdata_notValid(self): self.createInvCache(True) old_stats = self.createfake_oldStats() new_stats = self.createfake_newStats(False, True) new_stats_forhost = self.createfake_newStats_forhost() LibvirtPerfMonitor.update_perfdata_InCache(self.vmhost_id, None, new_stats_forhost) LibvirtPerfMonitor.update_perfdata_InCache(self.vm_id, old_stats, new_stats) resource_util = self.sample_perfdata.sample_host_perfdata( self.vmhost_id, 5) self.assertEquals(resource_util.get_status(), -1) def test_sample_host_perfdata_vmnotRunning(self): self.createInvCache(False) new_stats = self.createfake_newStats(False, True) new_stats_forhost = self.createfake_newStats_forhost() LibvirtPerfMonitor.update_perfdata_InCache(self.vmhost_id, None, new_stats_forhost) LibvirtPerfMonitor.update_perfdata_InCache(self.vm_id, None, new_stats) resource_util = self.sample_perfdata.sample_host_perfdata( self.vmhost_id, 5) self.assertEquals(resource_util.get_status(), 0) def test_sample_host_perfdata_notsampled(self): self.createInvCache(True) new_stats = self.createfake_newStats(False, True) LibvirtPerfMonitor.update_perfdata_InCache(self.vm_id, None, new_stats) new_stats_forhost = self.createfake_newStats_forhost() LibvirtPerfMonitor.update_perfdata_InCache(self.vmhost_id, None, new_stats_forhost) resource_util = self.sample_perfdata.sample_host_perfdata( self.vmhost_id, 5) self.assertEquals(resource_util.get_status(), -1) def test_sample_host_perfdata_Disconnected(self): self.createInvCache(True, hostconnection='Disconnected') new_stats = self.createfake_newStats(False, True) LibvirtPerfMonitor.update_perfdata_InCache(self.vm_id, None, new_stats) resource_util = self.sample_perfdata.sample_host_perfdata( self.vmhost_id, 5) self.assertEquals(resource_util.get_status(), -1) def tearDown(self): self.libvirtPerf_monitor.perfDataCache = {} self.mox.UnsetStubs() def createInvCache(self, vmrunning, hostconnection='Connected'): vmhost = VmHost() vmhost.set_id(self.vmhost_id) vmhost.set_connectionState(hostconnection) vm = Vm() vm.set_id(self.vm_id) if vmrunning: vm.set_powerState(Constants.VM_POWER_STATES[1]) else: vm.set_powerState(Constants.VM_POWER_STATES[0]) vm.set_vmHostId(self.vmhost_id) vmhost.set_virtualMachineIds([self.vm_id]) vmhost.set_processorSpeedMhz(2100) vmhost.set_processorCoresCount(4) vmhost.set_processorCount('2') vmhost.set_memorySize(2097152) vmhost.set_memoryConsumed(2097152) InventoryCacheManager.update_object_in_cache(self.vmhost_id, vmhost) InventoryCacheManager.update_object_in_cache(self.vm_id, vm) def create_compute_inventory(self): compute_inventory = ComputeInventory(self.connection.compute_rmcontext) InventoryCacheManager.get_all_compute_inventory()[ self.vmhost_id] = compute_inventory InventoryCacheManager.get_all_compute_inventory()[ self.vm_id] = compute_inventory self.mox.StubOutWithMock(ComputeInventory, 'get_compute_conn_driver') compute_inventory.get_compute_conn_driver().AndReturn(self.connection) self.mox.ReplayAll() def _assert(self, result): self.assertFalse( (result.get_resourceId() is None or result.get_resourceId() == ''), 'Resource id is null') self.assertFalse((result.get_timestamp() is None), 'Timestamp is null') self.assertFalse((result.get_granularity() is None), 'Granularity is null') self.assertFalse((result.get_cpuUserLoad() is None), 'Cpu userLoad is null') self.assert_((result.get_cpuSystemLoad() is None), 'Cpu system load is not null') self.assertFalse((result.get_cpuUserLoad() is None), 'Cpu userLoad is null') self.assertFalse((result.get_hostCpuSpeed() is None), 'hostCpuSpeed is null') self.assertFalse((result.get_hostMaxCpuSpeed() is None), 'hostMaxCpuSpeed is null') self.assertFalse((result.get_ncpus() is None), 'NCpus is null') self.assertFalse((result.get_diskRead() is None), 'DiskRead userLoad is null') self.assertFalse((result.get_diskWrite() is None), 'Disk Write userLoad is null') self.assertFalse((result.get_netRead() is None), 'NetRead is null') self.assertFalse((result.get_netWrite() is None), 'NetWrite is null') self.assertFalse((result.get_totalMemory() is None), 'Total Memory is null') self.assertFalse((result.get_freeMemory() is None), 'Free memory is null') self.assertFalse((result.get_configuredMemory() is None), 'Configured Memory is null') self.assert_((result.get_uptimeMinute() is None), 'uptime is not null') self.assert_((result.get_reservedSystemCapacity() is None), 'Reserved system capacity is not null') self.assert_((result.get_maximumSystemCapacity() is None), 'Maximum system capacity is not null') self.assert_((result.get_reservedSystemCapacity() is None), 'Reserved system capacity is not null') self.assert_((result.get_relativeWeight() is None), 'Relative weight is not null') self.assert_((result.get_reservedSystemMemory() is None), 'reserved System Memory is not null') self.assert_((result.get_maximumSystemMemory() is None), 'maximum System Memory is not null') self.assert_((result.get_memoryRelativeWeight() is None), 'Memory Relative weight is not null') self.assertFalse((result.get_status() is None), 'Status is null') self.assertNotEqual(result, None) def createfake_oldStats(self): stats = Stats() stats.freeMemory = 0L stats.totalMemory = 2097152L stats.timestamp = 1331372444.5931201 stats.ncpus = 1 stats.diskReadBytes = 226644480L stats.diskWriteBytes = 33723392L stats.netReceivedBytes = 357325L stats.netTransmittedBytes = 8302L stats.cpuStats = CPUStats() stats.cpuStats.cycles['user'] = 696880000000 stats.cpuPerfTime = 1331372444.3490119 stats.diskPerfTime = 1331372444.472137 stats.netPerfTime = 1331372444.5922019 return stats def createfake_newStats_nochange(self, poweroff, stats_decrement): stats = Stats() stats.freeMemory = 0L stats.totalMemory = 2097152L stats.timestamp = 1331372444.5931201 stats.ncpus = 1 if poweroff: stats.diskReadBytes = 0 elif stats_decrement: stats.diskReadBytes = 226643380L else: stats.diskReadBytes = 226644480L stats.diskWriteBytes = 33723392L stats.netReceivedBytes = 357325L stats.netTransmittedBytes = 8302L stats.cpuStats = CPUStats() stats.cpuStats.cycles['user'] = 696880000000 stats.cpuPerfTime = 1331372444.3490119 stats.diskPerfTime = 1331372444.472137 stats.netPerfTime = 1331372444.5922019 return stats def createfake_newStats(self, now, status): stats = Stats() stats.freeMemory = 0L stats.totalMemory = 2097152L if now: stats.timestamp = time.time() else: stats.timestamp = 1331372625.171705 stats.ncpus = 1 stats.diskReadBytes = 226644480L stats.diskWriteBytes = 34006016L stats.netReceivedBytes = 362137L stats.netTransmittedBytes = 8302L stats.cpuStats = CPUStats() stats.cpuStats.cycles['user'] = 697480000000 stats.cpuPerfTime = 1331372624.9259009 stats.diskPerfTime = 1331372625.050205 stats.netPerfTime = 1331372625.1707871 if status: stats.status = 0 else: stats.status = -1 return stats def createfake_newStats_forhost(self, status=True): stats = Stats() stats.freeMemory = 99982L stats.totalMemory = 2097152L stats.timestamp = time.time() stats.ncpus = 1 if status: stats.status = 0 else: stats.status = -1 return stats