Esempio n. 1
0
    def testProcessUpdates_compute_stopped_exception(self):
        vmHost = VmHost()
        vmHost.set_id('1')
        vmHost.set_connectionState(Constants.VMHOST_CONNECTED)
        InventoryCacheManager.update_object_in_cache('1', vmHost)
        self.mock.StubOutWithMock(api, 'vm_host_save')
        api.vm_host_save(
            mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None)

        self.mock.StubOutWithMock(
            InventoryCacheManager, 'get_compute_conn_driver')
        InventoryCacheManager.get_compute_conn_driver(
            self.libvirtVmHost.compute_id,
            Constants.VmHost).AndReturn(fake.get_connection())

        fake_computes = [{'id': '1', 'service': {'created_at':
                                                 'created',
                                                 'updated_at':'updated'}}]
        self.mock.StubOutWithMock(novadb, 'compute_node_get_all')
        novadb.compute_node_get_all(mox.IgnoreArg()).AndReturn(fake_computes)

        self.mock.StubOutWithMock(hnm_utils, 'is_service_alive')
        hnm_utils.is_service_alive(
            mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(False)

        self.mock.StubOutWithMock(event_api, 'notify_host_update')
        event_api.notify_host_update(
            mox.IgnoreArg(), mox.IgnoreArg()).AndRaise(Exception())
        self.mock.ReplayAll()

        self.assertEquals(self.libvirtVmHost.processUpdates(), None)
        self.mock.stubs.UnsetAll()
Esempio n. 2
0
    def skiptestProcessUpdates_network_stopped(self):
        vmHost = VmHost()
        vmHost.set_id('1')
        vmHost.set_connectionState(Constants.VMHOST_CONNECTED)
        InventoryCacheManager.update_object_in_cache('1', vmHost)
        self.mock.StubOutWithMock(
            self.libvirtVmHost, '_get_compute_running_status')
        self.libvirtVmHost._get_compute_running_status().AndReturn(
            (True, 'host'))

        self.mock.StubOutWithMock(api, 'vm_host_save')
        api.vm_host_save(
            mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None)

        fake_networks = {
            'id': '1', 'created_at': 'created', 'updated_at': 'updated'}
        self.mock.StubOutWithMock(novadb, 'service_get_by_host_and_topic')
        novadb.service_get_by_host_and_topic(
            mox.IgnoreArg(), mox.IgnoreArg(),
            mox.IgnoreArg()).AndReturn(fake_networks)

        self.mock.StubOutWithMock(hnm_utils, 'is_service_alive')
        hnm_utils.is_service_alive(
            mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(False)

        self.mock.StubOutWithMock(event_api, 'notify_host_update')
        event_api.notify_host_update(
            mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(None)
        self.mock.ReplayAll()

        self.assertEquals(self.libvirtVmHost.processUpdates(), None)
        self.assertEquals(
            self.libvirtVmHost.cachedvmHost.get_connectionState(),
            'Disconnected')
        self.mock.stubs.UnsetAll()
Esempio n. 3
0
    def testProcessUpdates_compute_stopped_exception(self):
        vmHost = VmHost()
        vmHost.set_id('1')
        vmHost.set_connectionState(Constants.VMHOST_CONNECTED)
        InventoryCacheManager.update_object_in_cache('1', vmHost)
        self.mock.StubOutWithMock(api, 'vm_host_save')
        api.vm_host_save(
            mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None)

        self.mock.StubOutWithMock(
            InventoryCacheManager, 'get_compute_conn_driver')
        InventoryCacheManager.get_compute_conn_driver(
            self.libvirtVmHost.compute_id,
            Constants.VmHost).AndReturn(fake.get_connection())

        fake_computes = [{'id': '1', 'service': {'created_at':
                                                 'created', 'updated_at':'updated'}}]
        self.mock.StubOutWithMock(novadb, 'compute_node_get_all')
        novadb.compute_node_get_all(mox.IgnoreArg()).AndReturn(fake_computes)

        self.mock.StubOutWithMock(hnm_utils, 'is_service_alive')
        hnm_utils.is_service_alive(
            mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(False)

        self.mock.StubOutWithMock(event_api, 'notify_host_update')
        event_api.notify_host_update(
            mox.IgnoreArg(), mox.IgnoreArg()).AndRaise(Exception())
        self.mock.ReplayAll()

        self.assertEquals(self.libvirtVmHost.processUpdates(), None)
        self.mock.stubs.UnsetAll()
Esempio n. 4
0
    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)
Esempio n. 5
0
    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_from_db_delete_host(self):
        self._createInvCache()
        InventoryCacheManager.get_all_compute_inventory().clear()
        compute = []
        self.mox.StubOutWithMock(db, 'compute_node_get_all')
        db.compute_node_get_all(mox.IgnoreArg()).AndReturn(compute)

        im = self.inv_manager
        self.assertEquals(
            len(InventoryCacheManager.get_all_compute_inventory()), 0)

        compute = _create_Compute(compute_id='vmhost1')
        service = compute['service']
        rm_context = \
            rmcontext.ComputeRMContext(rmType=compute['hypervisor_type'],
                                       rmIpAddress=service['host'],
                                       rmUserName='******',
                                       rmPassword='******')
        InventoryCacheManager.get_all_compute_inventory()['vmhost1'] = \
            ComputeInventory(rm_context)

        vmhost = VmHost()
        vmhost.set_id('vmhost1')
        vmhost.set_name('vmhost1')
        InventoryCacheManager.get_all_compute_inventory(
        )['vmhost1'].update_compute_info(rm_context, vmhost)

        self.mox.StubOutWithMock(api, 'vm_host_delete_by_ids')

        api.vm_host_delete_by_ids(
            mox.IgnoreArg(),
            mox.IgnoreArg()).MultipleTimes().AndReturn(None)

        self.mox.StubOutWithMock(event_api, 'notify_host_update')
        event_api.notify_host_update(mox.IgnoreArg(), mox.IgnoreArg())

        self.mox.ReplayAll()

        im._refresh_from_db(None)
        self.mox.VerifyAll()
        self.mox.stubs.UnsetAll()
        self.assertEquals(
            len(InventoryCacheManager.get_all_compute_inventory()), 0)
        self.assertTrue(InventoryCacheManager.get_all_compute_inventory(
        ).get('compute1') is None)

        self.mox.UnsetStubs()
Esempio n. 7
0
    def test_refresh_from_db_delete_host(self):
        self._createInvCache()
        InventoryCacheManager.get_all_compute_inventory().clear()
        compute = []
        self.mox.StubOutWithMock(db, 'compute_node_get_all')
        db.compute_node_get_all(mox.IgnoreArg()).AndReturn(compute)

        im = self.inv_manager
        self.assertEquals(
            len(InventoryCacheManager.get_all_compute_inventory()), 0)

        compute = _create_Compute(compute_id='vmhost1')
        service = compute['service']
        rm_context = \
            rmcontext.ComputeRMContext(rmType=compute['hypervisor_type'
                                                      ], rmIpAddress=service['host'], rmUserName='******',
                                       rmPassword='******')
        InventoryCacheManager.get_all_compute_inventory()['vmhost1'] = \
            ComputeInventory(rm_context)

        vmhost = VmHost()
        vmhost.set_id('vmhost1')
        vmhost.set_name('vmhost1')
        InventoryCacheManager.get_all_compute_inventory(
        )['vmhost1'].update_compute_info(rm_context, vmhost)

        self.mox.StubOutWithMock(api, 'vm_host_delete_by_ids')

        api.vm_host_delete_by_ids(
            mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None)

        self.mox.StubOutWithMock(event_api, 'notify_host_update')
        event_api.notify_host_update(mox.IgnoreArg(), mox.IgnoreArg())

        self.mox.ReplayAll()

        im._refresh_from_db(None)
        self.mox.VerifyAll()
        self.mox.stubs.UnsetAll()
        self.assertEquals(
            len(InventoryCacheManager.get_all_compute_inventory()), 0)
        self.assertTrue(InventoryCacheManager.get_all_compute_inventory().get(
            'compute1') is None)

        self.mox.UnsetStubs()
    def _clean_deleted_computes(self, db_keys):
        keys = InventoryCacheManager.get_all_compute_inventory(
        ).keys()  # since we're deleting
        deletion_list = []
        for compute_id in keys:
            if compute_id not in db_keys:
                vmHostObj = InventoryCacheManager.get_all_compute_inventory()[
                    compute_id].get_compute_info()
                if vmHostObj is not None:
                    deletion_list.append(vmHostObj.get_id())

        host_deleted_list = []
        if len(deletion_list) != 0:
            # Delete object from cache
            for _id in deletion_list:
                host_deleted = InventoryCacheManager.get_object_from_cache(
                    _id, Constants.VmHost)
                if host_deleted is not None:
                    host_deleted_list.append(
                        InventoryCacheManager.
                        get_object_from_cache(_id, Constants.VmHost))
                else:
                    LOG.warn(_(
                        "VmHost object for id %s not found in cache") % _id)

            # Delete the VmHost from DB
            api.vm_host_delete_by_ids(get_admin_context(), deletion_list)
            # Generate the VmHost Removed Event
            for host_deleted in host_deleted_list:
                LOG.debug(_('Generating Host Removed event for the \
                host id : %s') % str(
                    host_deleted.get_id()))
                event_api.notify_host_update(
                    event_metadata.EVENT_TYPE_HOST_REMOVED, host_deleted)
                # VmHost is deleted from compute inventory and inventory cache
                # after notifying the event
                del InventoryCacheManager.get_all_compute_inventory()[
                    host_deleted.get_id()]
                InventoryCacheManager.delete_object_in_cache(
                    host_deleted.get_id(), Constants.VmHost)
                LOG.audit(_('Host with (UUID, host name) - (%s, %s) \
                got removed') % (
                    host_deleted.get_id(), host_deleted.get_name()))
    def test_refresh_from_db_delete_host(self):
        self._createInvCache()
        InventoryCacheManager.get_all_compute_inventory().clear()
        compute = []
        self.mox.StubOutWithMock(db, "compute_node_get_all")
        db.compute_node_get_all(mox.IgnoreArg()).AndReturn(compute)

        im = self.inv_manager
        self.assertEquals(len(InventoryCacheManager.get_all_compute_inventory()), 0)

        compute = _create_Compute(compute_id="vmhost1")
        service = compute["service"]
        rm_context = rmcontext.ComputeRMContext(
            rmType=compute["hypervisor_type"],
            rmIpAddress=service["host"],
            rmUserName="******",
            rmPassword="******",
        )
        InventoryCacheManager.get_all_compute_inventory()["vmhost1"] = ComputeInventory(rm_context)

        vmhost = VmHost()
        vmhost.set_id("vmhost1")
        vmhost.set_name("vmhost1")
        InventoryCacheManager.get_all_compute_inventory()["vmhost1"].update_compute_info(rm_context, vmhost)

        self.mox.StubOutWithMock(api, "vm_host_delete_by_ids")

        api.vm_host_delete_by_ids(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None)

        self.mox.StubOutWithMock(event_api, "notify_host_update")
        event_api.notify_host_update(mox.IgnoreArg(), mox.IgnoreArg())

        self.mox.ReplayAll()

        im._refresh_from_db(None)
        self.mox.VerifyAll()
        self.mox.stubs.UnsetAll()
        self.assertEquals(len(InventoryCacheManager.get_all_compute_inventory()), 0)
        self.assertTrue(InventoryCacheManager.get_all_compute_inventory().get("compute1") is None)

        self.mox.UnsetStubs()
Esempio n. 10
0
    def refresh_perfdata(
        self,
        conn,
        uuid,
        perfmon_type,
    ):
        '''Refreshes the performance data  '''

        if uuid not in LibvirtPerfMonitor.perfDataCache:
            LibvirtPerfMonitor.perfDataCache[uuid] = \
                {Constants.OLD_STATS: None, Constants.NEW_STATS: None}

        if perfmon_type == Constants.VmHost:
            LibvirtVmHostPerfData(conn, uuid).refresh_perfdata()
            host_obj = InventoryCacheManager.get_object_from_cache(
                uuid,
                Constants.VmHost)
            if host_obj is not None:
                event_api.notify_host_update(
                    event_metadata.EVENT_TYPE_HOST_UPDATED, host_obj)
        elif perfmon_type == Constants.Vm:
            LibvirtVmPerfData(conn, uuid).refresh_perfdata()
Esempio n. 11
0
    def refresh_perfdata(
        self,
        conn,
        uuid,
        perfmon_type,
    ):
        '''Refreshes the performance data  '''

        LOG.info(_('Entering refresh_perfdata for uuid :' + uuid))
        if uuid not in LibvirtPerfMonitor.perfDataCache:
            LibvirtPerfMonitor.perfDataCache[uuid] = \
                {Constants.OLD_STATS: None, Constants.NEW_STATS: None}

        if perfmon_type == Constants.VmHost:
            LibvirtVmHostPerfData(conn, uuid).refresh_perfdata()
            host_obj = InventoryCacheManager.get_object_from_cache(
                uuid, Constants.VmHost)
            if host_obj is not None:
                event_api.notify_host_update(
                    event_metadata.EVENT_TYPE_HOST_UPDATED, host_obj)
        elif perfmon_type == Constants.Vm:
            LibvirtVmPerfData(conn, uuid).refresh_perfdata()
        LOG.info(_('Exiting refresh_perfdata for uuid :' + uuid))
Esempio n. 12
0
    def _refresh_from_db(self, context):
        """Make our compute_node inventory map match the db."""

        # Add/update existing compute_nodes ...

        computes = db.compute_node_get_all(context)
        existing = InventoryCacheManager.get_all_compute_inventory().keys()
        db_keys = []
        for compute in computes:
            compute_id = str(compute["id"])
            service = compute["service"]
            compute_hypervisor_type = compute["hypervisor_type"]
            LOG.debug(_(" Compute-id -> %s: Hypervisor Type -> %s") % (compute_id, compute_hypervisor_type))

            if service is not None and compute_hypervisor_type == "QEMU":
                compute_alive = hnm_utils.is_service_alive(service["updated_at"], service["created_at"])
                db_keys.append(compute_id)
                if not compute_alive:
                    LOG.warn(_("Service %s for host %s is not active") % (service["binary"], service["host"]))
                    continue
                if compute_id not in existing:
                    rm_context = rmcontext.ComputeRMContext(
                        rmType=compute["hypervisor_type"],
                        rmIpAddress=service["host"],
                        rmUserName="******",
                        rmPassword="******",
                    )
                    InventoryCacheManager.get_all_compute_inventory()[compute_id] = ComputeInventory(rm_context)
                    LOG.audit(_("New Host with compute_id  %s is obtained") % (compute_id))
                InventoryCacheManager.get_all_compute_inventory()[compute_id].update_compute_Id(compute_id)
            else:
                LOG.warn(_(" No services entry found for compute id  %s") % compute_id)

        # Cleanup compute_nodes removed from db ...

        keys = InventoryCacheManager.get_all_compute_inventory().keys()  # since we're deleting
        deletion_list = []
        for compute_id in keys:
            if compute_id not in db_keys:
                vmHostObj = InventoryCacheManager.get_all_compute_inventory()[compute_id].get_compute_info()
                if vmHostObj is not None:
                    deletion_list.append(vmHostObj.get_id())

        host_deleted_list = []
        if len(deletion_list) != 0:
            # Delete object from cache
            for _id in deletion_list:
                host_deleted = InventoryCacheManager.get_object_from_cache(_id, Constants.VmHost)
                if host_deleted is not None:
                    host_deleted_list.append(InventoryCacheManager.get_object_from_cache(_id, Constants.VmHost))
                else:
                    LOG.warn(_("VmHost object for id %s not found in cache") % _id)

            # Delete the VmHost from DB
            api.vm_host_delete_by_ids(get_admin_context(), deletion_list)
            # Generate the VmHost Removed Event
            for host_deleted in host_deleted_list:
                LOG.debug(
                    _(
                        "Generating Host Removed event \
                for the host id : %s"
                    )
                    % str(host_deleted.get_id())
                )
                event_api.notify_host_update(event_metadata.EVENT_TYPE_HOST_REMOVED, host_deleted)
                # VmHost is deleted from compute inventory and inventory
                # cache after notifying the event
                del InventoryCacheManager.get_all_compute_inventory()[host_deleted.get_id()]
                InventoryCacheManager.delete_object_in_cache(host_deleted.get_id(), Constants.VmHost)
                LOG.audit(
                    _(
                        "Host with (UUID, host name) \
                - (%s, %s) got removed"
                    )
                    % (host_deleted.get_id(), host_deleted.get_name())
                )
Esempio n. 13
0
    def _refresh_from_db(self, context):
        """Make our compute_node inventory map match the db."""

        # Add/update existing compute_nodes ...

        computes = db.compute_node_get_all(context)
        existing = InventoryCacheManager.get_all_compute_inventory().keys()
        db_keys = []
        for compute in computes:
            compute_id = str(compute['id'])
            service = compute['service']
            compute_hypervisor_type = compute['hypervisor_type']
            LOG.debug(_(' Compute-id -> %s: Hypervisor Type -> %s') %
                      (compute_id, compute_hypervisor_type))

            if service is not None and compute_hypervisor_type == 'QEMU':
                compute_alive = hnm_utils.is_service_alive(
                    service['updated_at'], service['created_at'])
                db_keys.append(compute_id)
                if not compute_alive:
                    LOG.warn(_('Service %s for host %s is not active')
                             % (service['binary'], service['host']))
                    continue
                if compute_id not in existing:
                    rm_context = \
                        rmcontext.ComputeRMContext(
                            rmType=compute['hypervisor_type'],
                            rmIpAddress=service['host'],
                            rmUserName='******', rmPassword='******')
                    InventoryCacheManager.\
                        get_all_compute_inventory()[compute_id] =\
                        ComputeInventory(rm_context)
                    LOG.audit(_('New Host with compute_id  %s is obtained')
                              % (compute_id))
                InventoryCacheManager.get_all_compute_inventory(
                )[compute_id].update_compute_Id(compute_id)
            else:
                LOG.warn(_(' No services entry found for compute id  %s')
                         % compute_id)

        # Cleanup compute_nodes removed from db ...

        keys = InventoryCacheManager.get_all_compute_inventory(
        ).keys()  # since we're deleting
        deletion_list = []
        for compute_id in keys:
            if compute_id not in db_keys:
                vmHostObj = InventoryCacheManager.get_all_compute_inventory(
                )[compute_id].get_compute_info()
                if vmHostObj is not None:
                    deletion_list.append(vmHostObj.get_id())

        host_deleted_list = []
        if len(deletion_list) != 0:
            # Delete object from cache
            for _id in deletion_list:
                host_deleted = InventoryCacheManager.get_object_from_cache(
                    _id, Constants.VmHost)
                if host_deleted is not None:
                    host_deleted_list.append(
                        InventoryCacheManager.get_object_from_cache(
                            _id, Constants.VmHost))
                else:
                    LOG.warn(
                        _("VmHost object for id %s not found in cache") % _id)

            # Delete the VmHost from DB
            api.vm_host_delete_by_ids(get_admin_context(), deletion_list)
            # Generate the VmHost Removed Event
            for host_deleted in host_deleted_list:
                LOG.debug(_('Generating Host Removed event \
                for the host id : %s') % str(host_deleted.get_id()))
                event_api.notify_host_update(
                    event_metadata.EVENT_TYPE_HOST_REMOVED, host_deleted)
                # VmHost is deleted from compute inventory and inventory
                # cache after notifying the event
                del InventoryCacheManager.get_all_compute_inventory(
                )[host_deleted.get_id()]
                InventoryCacheManager.delete_object_in_cache(
                    host_deleted.get_id(), Constants.VmHost)
                LOG.audit(_('Host with (UUID, host name) \
                - (%s, %s) got removed') % (host_deleted.get_id(),
                                            host_deleted.get_name()))