예제 #1
0
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)
예제 #2
0
 def test_support_spice(self):
     """Checks that VirtController.supports_spice() returns True if the
        version of Libvirt is >= 8.0.6
     """
     MockLibvirt.version = 8005
     self.assertIs(VirtController.supports_spice(), False)
     MockLibvirt.version = 8006
     self.assertIs(VirtController.supports_spice(), True)
예제 #3
0
    def test_event_register(self):
        """Checks that VirtController.event_register() properly calls
           virEventRunDefaultImpl() function of Libvirt
        """

        with mock.patch('libvirt.virEventRunDefaultImpl',) as m:
            VirtController.event_run()
            m.assert_called_once_with()
예제 #4
0
    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()
예제 #5
0
    def __parse_graphics(self, conf):
        """
            Parses the graphics parameter over the conf dictionary given in
            parameter and raises appropriate exception if a problem is found.
        """

        if conf.has_key('graphics'):

            graphics = conf['graphics']

            if type(graphics) is not str:
                raise CloubedConfigurationException(
                          "format of graphics parameter of domain {domain} " \
                          "is not valid" \
                              .format(domain=self.name))

            valid_graphics = ["sdl", "vnc", "rdp", "spice"]

            if graphics not in valid_graphics:
                raise CloubedConfigurationException(
                          "value {graphics} of graphics parameter of domain " \
                          "{domain} is not valid" \
                              .format(graphics=graphics,
                                      domain=self.name))

            self.graphics = graphics

        else:
            # default is spice if controller has support
            if VirtController.supports_spice():
                self.graphics = "spice"
            else:
                self.graphics = "vnc"
예제 #6
0
    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")
예제 #7
0
    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)
예제 #8
0
    def test_parse_graphics_ok(self):
        """
            ConfigurationDomain.__parse_graphics() should properly set graphics
            attribute
        """

        conf = { 'graphics': 'vnc' }
        self.domain_conf._ConfigurationDomain__parse_graphics(conf)
        self.assertEqual(self.domain_conf.graphics, 'vnc')

        conf = { } # default is spice if controller supports else it is vnc
        self.domain_conf._ConfigurationDomain__parse_graphics(conf)
        if VirtController.supports_spice():
            self.assertEqual(self.domain_conf.graphics, 'spice')
        else:
            self.assertEqual(self.domain_conf.graphics, 'vnc')
예제 #9
0
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)