class EventManager: """ EventManager class """ tbd = None def __init__(self, tbd): VirtController.event_register() self._stop = threading.Event() self._thread = threading.Thread(target=self.run_event_loop, name="libvirtEventLoop") self._thread.setDaemon(True) self._thread.start() self._ctl = VirtController(read_only=True) self._ctl.domain_event_register(EventManager.manage_event) self._ctl.setKeepAlive(5, 3) EventManager.tbd = tbd logging.debug("initialized event manager") def terminate(self): logging.debug("terminating event manager thread") self._stop.set() def run_event_loop(self): """ run_event_loop: Starts libvirt event loop """ # In race conditions on python interpreter exit, libvirt sometimes # becomes None. So loop conditioner tests if it is still defined in # order to avoid errors. while not self._stop.is_set(): VirtController.event_run() @staticmethod def manage_event(conn, dom, event_type, event_detail, opaque): """ manage_event: handler launched by libvirt in case of event """ event = DomainEvent(event_type, event_detail) logging.debug("event on domain {domain_name}({domain_id}) " \ "{event_type} {event_detail}" \ .format(domain_name=dom.name(), domain_id=dom.ID(), event_type=event.type, event_detail=event.detail)) domain = EventManager.tbd.get_domain_by_libvirt_name(dom.name()) # test if notified event comes from a domain in current testbed if domain is None: logging.debug("event received for domain {domain} but not found " \ "in testbed".format(domain=dom.name())) else: domain.notify_event(event)
class TestVirtControllerMethods(CloubedTestCase): def setUp(self): patcher_open = mock.patch('libvirt.open', libvirt_mod_m.open) patcher_conn = mock.patch('libvirt.virConnect', libvirt_conn_m) patcher_open.start() patcher_conn.start() self.addCleanup(patcher_open.stop) self.addCleanup(patcher_conn.stop) self.ctl = VirtController() self.tbd = FakeCloubed(self.ctl) self._loader = MockConfigurationLoader(conf_minimal) self.conf = Configuration(self._loader) def test_find_storage_pool(self): """Checks that VirtController.find_storage_pool() finds the storage pool if existing else None """ # self.ctl.conn is a MockLibvirtConnect self.ctl.conn.pools = [] self.ctl.conn.defined_pools = [] self.assertIs(self.ctl.find_storage_pool('fail'), None) self.ctl.conn.pools = [ MockLibvirtStoragePool('test1'), ] self.ctl.conn.defined_pools = [ MockLibvirtStoragePool('test2'), ] self.assertIsNot(self.ctl.find_storage_pool('test2'), None) def test_create_storage_pool(self): """Checks that VirtController.create_storage_pool() does not raise any issue """ xml = "<pool type='dir'><name>pool_name</name>" \ "<target><path>/test_pool_path</path></target></pool>" self.ctl.create_storage_pool(xml) def test_find_storage_volume(self): """Checks that VirtController.find_storage_volume() finds the storage volume in the storage pool if existing else None """ pool_item = { 'name': 'test_name', 'path': '/test_path' } pool_conf = ConfigurationStoragePool(self.conf, pool_item) storage_pool = StoragePool(self.tbd, pool_conf) self.assertIs(self.ctl.find_storage_volume(storage_pool,'fail'), None) pool = MockLibvirtStoragePool('/test_path') pool.volumes.append('volume1') self.ctl.conn.pools = [ pool, ] self.assertEquals(self.ctl.find_storage_volume(storage_pool,'volume1'), 'volume1') def test_create_storage_volume(self): """Checks that VirtController.create_storage_volume() does not raise any issue unless the storage pool does not exist and CloubedControllerException is raised """ pool_item = { 'name': 'test_name', 'path': '/test_path' } pool_conf = ConfigurationStoragePool(self.conf, pool_item) pool1 = StoragePool(self.tbd, pool_conf) xml = object() pool = MockLibvirtStoragePool('/test_path') self.ctl.conn.pools = [ pool, ] self.ctl.create_storage_volume(pool1, xml) pool_item = { 'name': 'test_name', 'path': '/test_path2' } pool_conf = ConfigurationStoragePool(self.conf, pool_item) pool2 = StoragePool(self.tbd, pool_conf) self.assertRaisesRegexp(CloubedControllerException, "pool /test_path2 not found by virtualization controller", self.ctl.create_storage_volume, pool2, 'volume1') def test_find_network(self): """Checks that VirtController.find_network() finds the network if existing else None """ self.ctl.conn.networks = [] self.ctl.conn.defined_networks = [] self.assertIs(self.ctl.find_network('fail'), None) self.ctl.conn.networks = [ MockLibvirtNetwork('net1'), ] self.ctl.conn.defined_networks = [ MockLibvirtNetwork('net2'), ] self.assertIsNot(self.ctl.find_network('net1'), None) self.assertIsNot(self.ctl.find_network('net2'), None) def test_create_network(self): """Checks that VirtController.create_network() does not raise any issue """ xml = "<network><name>network_name</name></network>" self.ctl.create_network(xml) def test_find_domain(self): """Checks that VirtController.find_domain() finds the domain if existing else None """ self.ctl.conn.domains = [] self.ctl.conn.defined_domains = [] self.assertIs(self.ctl.find_domain('fail'), None) self.ctl.conn.domains = [ MockLibvirtDomain(0, 'domain1'), ] self.ctl.conn.defined_domains = [ MockLibvirtDomain(1, 'domain2'), ] self.assertIsNot(self.ctl.find_domain('domain1'), None) self.assertIsNot(self.ctl.find_domain('domain2'), None) self.assertIs(self.ctl.find_domain('domain3'), None) def test_create_domain(self): """Checks that VirtController.create_domain() does not raise any issue """ xml = "<domain><name>domain_name</name></domain>" self.ctl.create_domain(xml) def test_setKeepAlive(self): """Checks that VirtController.setKeepAlive() does not raise any issue""" self.ctl.setKeepAlive(0, 1) def test_domain_event_register(self): """Checks that VirtController.domain_event_register() does not raise any issue """ def handler(): pass self.ctl.domain_event_register(handler)