Пример #1
0
    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)
Пример #2
0
    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)
Пример #3
0
class TestLibvirtVmHostPerfData(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.connection = LibvirtConnection(False)
        cls.mox = mox.Mox()
        cls.connection._wrapped_conn = libvirt.open('qemu:///system')
        rm_context = ComputeRMContext(rmType='fake',
                                      rmIpAddress='host',
                                      rmUserName='******',
                                      rmPassword='******')
        cls.connection.init_rmcontext(rm_context)
        cls.connection.libvirt = libvirt
        cls.vm_id = '25f04dd3-e924-02b2-9eac-876e3c943262'
        cls.vmhost_id = '1'

    def setUp(self):
        self.libvirtPerf_monitor = LibvirtPerfMonitor()
        self.libvirtVmHost = LibvirtVmHostPerfData(
            self.connection._wrapped_conn, self.vmhost_id)
        self._createPerfCache()

    def _createPerfCache(self):

        self.libvirtPerf_monitor.perfDataCache[self.vmhost_id] = {
            Constants.OLD_STATS: None,
            Constants.NEW_STATS: None
        }

    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 test_refresh_perfdata(self):
        for _ in xrange(2):
            self._create_compute_inventory()
            self.libvirtVmHost.refresh_perfdata()
            self.mox.UnsetStubs()

        self.assertNotEquals(
            self.libvirtPerf_monitor.get_perfdata_fromCache(
                self.vmhost_id, Constants.NEW_STATS), None)
        self.assertNotEquals(
            self.libvirtPerf_monitor.get_perfdata_fromCache(
                self.vmhost_id, Constants.OLD_STATS), None)

    def tearDown(self):
        self.libvirtPerf_monitor.perfDataCache = {}
        self.mox.UnsetStubs()
Пример #4
0
    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)
Пример #5
0
class TestLibvirtVmHostPerfData(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.connection = LibvirtConnection(False)
        cls.mox = mox.Mox()
        cls.connection._wrapped_conn = libvirt.open('qemu:///system')
        rm_context = ComputeRMContext(
            rmType='fake', rmIpAddress='host', rmUserName='******',
            rmPassword='******')
        cls.connection.init_rmcontext(rm_context)
        cls.connection.libvirt = libvirt
        cls.vm_id = '25f04dd3-e924-02b2-9eac-876e3c943262'
        cls.vmhost_id = '1'

    def setUp(self):
        self.libvirtPerf_monitor = LibvirtPerfMonitor()
        self.libvirtVmHost = LibvirtVmHostPerfData(
            self.connection._wrapped_conn, self.vmhost_id)
        self._createPerfCache()

    def _createPerfCache(self):

        self.libvirtPerf_monitor.perfDataCache[
            self.vmhost_id
        ] = {Constants.OLD_STATS: None,
             Constants.NEW_STATS: None}

    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 test_refresh_perfdata(self):
        for _ in xrange(2):
            self._create_compute_inventory()
            self.libvirtVmHost.refresh_perfdata()
            self.mox.UnsetStubs()

        self.assertNotEquals(self.libvirtPerf_monitor.get_perfdata_fromCache(
            self.vmhost_id, Constants.NEW_STATS), None)
        self.assertNotEquals(self.libvirtPerf_monitor.get_perfdata_fromCache(
            self.vmhost_id, Constants.OLD_STATS), None)

    def tearDown(self):
        self.libvirtPerf_monitor.perfDataCache = {}
        self.mox.UnsetStubs()
Пример #6
0
    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)
Пример #7
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_hostcpu_perfdata_notsampled(self):
        self.createInvCache(True)
        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)
Пример #9
0
    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)
Пример #10
0
    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)
Пример #11
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)
Пример #12
0
    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)
Пример #13
0
    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(self):
        self.createInvCache(True)
        old_stats = self.createfake_oldStats()
        new_stats = self.createfake_newStats()
        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)
Пример #15
0
    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)
Пример #16
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)
Пример #17
0
 def setUp(self):
     self.libvirtPerf_monitor = LibvirtPerfMonitor()
     self.libvirtVM = LibvirtVmPerfData(self.connection._wrapped_conn,
                                        self.vm_id)
     self._createPerfCache()
Пример #18
0
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()
Пример #19
0
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
Пример #20
0
class TestLibvirtVmPerfData(unittest.TestCase):

    libvirt_domain_cls = libvirt.virDomain

    @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.libvirtVM = LibvirtVmPerfData(self.connection._wrapped_conn,
                                           self.vm_id)
        self._createPerfCache()

    def _createPerfCache(self):

        self.libvirtPerf_monitor.perfDataCache[self.vm_id] = {
            Constants.OLD_STATS: None,
            Constants.NEW_STATS: None
        }

    def test_refresh_perfdata(self):
        for _ in xrange(2):
            self.libvirtVM.refresh_perfdata()

        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)

    def test_refresh_perfdata_notvalid(self):
        for _ in xrange(2):
            self.libvirtVM.refresh_perfdata()

        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)

    def test_refresh_perfdata_nonactive(self):
        self.libvirtVM = LibvirtVmPerfData(self.connection._wrapped_conn,
                                           'dummy')
        self.libvirtVM.refresh_perfdata()

        self.assertEquals(
            self.libvirtPerf_monitor.get_perfdata_fromCache(
                'dummy', Constants.NEW_STATS), None)

    def test_refresh_perfdata_nodomainInfo(self):
        self.libvirtVM.domainObj = \
            self.connection._wrapped_conn.lookupByUUIDString(
                self.vm_id
            )
        self.mox.StubOutWithMock(self.libvirt_domain_cls, 'info')
        self.libvirtVM.domainObj.info().AndRaise(libvirt.libvirtError)

        self.mox.ReplayAll()
        self.libvirtVM.refresh_perfdata()

        self.assertEquals(
            self.libvirtPerf_monitor.get_perfdata_fromCache(
                self.vm_id, Constants.NEW_STATS).status, -1)
        self.mox.UnsetStubs()

    def test_refresh_perfdata_netException(self):
        self.libvirtVM.domainObj = \
            self.connection._wrapped_conn.lookupByUUIDString(
                self.vm_id
            )
        self.mox.StubOutWithMock(self.libvirt_domain_cls, 'interfaceStats')
        self.libvirtVM.domainObj.interfaceStats(mox.IgnoreArg()). \
            AndRaise(libvirt.libvirtError)

        self.mox.ReplayAll()
        self.libvirtVM.refresh_perfdata()

        self.assertEquals(
            self.libvirtPerf_monitor.get_perfdata_fromCache(
                self.vm_id, Constants.NEW_STATS).status, -1)
        self.mox.UnsetStubs()

    def test_refresh_perfdata_diskException(self):
        self.libvirtVM.domainObj = \
            self.connection._wrapped_conn.lookupByUUIDString(
                self.vm_id
            )
        self.mox.StubOutWithMock(self.libvirt_domain_cls, 'blockStats')
        self.libvirtVM.domainObj.blockStats(mox.IgnoreArg()). \
            AndRaise(libvirt.libvirtError)

        self.mox.ReplayAll()
        self.libvirtVM.refresh_perfdata()

        self.assertEquals(
            self.libvirtPerf_monitor.get_perfdata_fromCache(
                self.vm_id, Constants.NEW_STATS).status, -1)
        self.mox.UnsetStubs()

    def tearDown(self):
        self.libvirtPerf_monitor.perfDataCache = {}
Пример #21
0
 def setUp(self):
     self.libvirtPerf_monitor = LibvirtPerfMonitor()
     self._createPerfCache()
Пример #22
0
 def setUp(self):
     self.libvirtPerf_monitor = LibvirtPerfMonitor()
     self._createPerfCache()
     self.sample_perfdata = SamplePerfData()
Пример #23
0
 def setUp(self):
     self.libvirtPerf_monitor = LibvirtPerfMonitor()
     self._createPerfCache()
     self.sample_perfdata = SamplePerfData()
Пример #24
0
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()
Пример #25
0
 def setUp(self):
     self.libvirtPerf_monitor = LibvirtPerfMonitor()
     self.libvirtVM = LibvirtVmPerfData(self.connection._wrapped_conn,
                                        self.vm_id)
     self._createPerfCache()
Пример #26
0
class TestLibvirtVmPerfData(unittest.TestCase):

    libvirt_domain_cls = libvirt.virDomain

    @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.libvirtVM = LibvirtVmPerfData(self.connection._wrapped_conn,
                                           self.vm_id)
        self._createPerfCache()

    def _createPerfCache(self):

        self.libvirtPerf_monitor.perfDataCache[
            self.vm_id
        ] = {Constants.OLD_STATS: None,
             Constants.NEW_STATS: None}

    def test_refresh_perfdata(self):
        for _ in xrange(2):
            self.libvirtVM.refresh_perfdata()

        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)

    def test_refresh_perfdata_notvalid(self):
        for _ in xrange(2):
            self.libvirtVM.refresh_perfdata()

        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)

    def test_refresh_perfdata_nonactive(self):
        self.libvirtVM = LibvirtVmPerfData(
            self.connection._wrapped_conn, 'dummy')
        self.libvirtVM.refresh_perfdata()

        self.assertEquals(
            self.libvirtPerf_monitor.
            get_perfdata_fromCache('dummy', Constants.NEW_STATS), None)

    def test_refresh_perfdata_nodomainInfo(self):
        self.libvirtVM.domainObj = \
            self.connection._wrapped_conn.lookupByUUIDString(
                self.vm_id
            )
        self.mox.StubOutWithMock(self.libvirt_domain_cls, 'info')
        self.libvirtVM.domainObj.info().AndRaise(libvirt.libvirtError)

        self.mox.ReplayAll()
        self.libvirtVM.refresh_perfdata()

        self.assertEquals(self.libvirtPerf_monitor.get_perfdata_fromCache(
            self.vm_id, Constants.NEW_STATS).status, -1)
        self.mox.UnsetStubs()

    def test_refresh_perfdata_netException(self):
        self.libvirtVM.domainObj = \
            self.connection._wrapped_conn.lookupByUUIDString(
                self.vm_id
            )
        self.mox.StubOutWithMock(self.libvirt_domain_cls,
                                 'interfaceStats')
        self.libvirtVM.domainObj.interfaceStats(mox.IgnoreArg()). \
            AndRaise(libvirt.libvirtError)

        self.mox.ReplayAll()
        self.libvirtVM.refresh_perfdata()

        self.assertEquals(self.libvirtPerf_monitor.get_perfdata_fromCache(
            self.vm_id, Constants.NEW_STATS).status, -1)
        self.mox.UnsetStubs()

    def test_refresh_perfdata_diskException(self):
        self.libvirtVM.domainObj = \
            self.connection._wrapped_conn.lookupByUUIDString(
                self.vm_id
            )
        self.mox.StubOutWithMock(self.libvirt_domain_cls, 'blockStats')
        self.libvirtVM.domainObj.blockStats(mox.IgnoreArg()). \
            AndRaise(libvirt.libvirtError)

        self.mox.ReplayAll()
        self.libvirtVM.refresh_perfdata()

        self.assertEquals(self.libvirtPerf_monitor.get_perfdata_fromCache(
            self.vm_id, Constants.NEW_STATS).status, -1)
        self.mox.UnsetStubs()

    def tearDown(self):
        self.libvirtPerf_monitor.perfDataCache = {}
Пример #27
0
 def setUp(self):
     self.libvirtPerf_monitor = LibvirtPerfMonitor()
     self._createPerfCache()
Пример #28
0
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