Пример #1
0
def test_event_skipped(mock_libvirt):
    """
    Test that events are skipped if their ID isn't defined in the libvirt
    module (older libvirt)
    """
    mock_libvirt.mock_add_spec(
        [
            "openReadOnly",
            "virEventRegisterDefaultImpl",
            "virEventRunDefaultImpl",
            "VIR_DOMAIN_EVENT_ID_LIFECYCLE",
        ],
        spec_set=True,
    )

    libvirt_events.start("test:///", "test/prefix")

    # Check events registration and deregistration
    mock_cnx = mock_libvirt.openReadOnly.return_value

    mock_cnx.domainEventRegisterAny.assert_any_call(
        None,
        mock_libvirt.VIR_DOMAIN_EVENT_ID_LIFECYCLE,
        libvirt_events._domain_event_lifecycle_cb,
        {
            "prefix": "test/prefix",
            "object": "domain",
            "event": "lifecycle"
        },
    )

    # Network events should have been skipped
    mock_cnx.networkEventRegisterAny.assert_not_called()
Пример #2
0
    def test_event_skipped(self):
        '''
        Test that events are skipped if their ID isn't defined in the libvirt
        module (older libvirt)
        '''
        self.mock_libvirt.mock_add_spec([
            'openReadOnly', 'virEventRegisterDefaultImpl',
            'virEventRunDefaultImpl', 'VIR_DOMAIN_EVENT_ID_LIFECYCLE'
        ],
                                        spec_set=True)

        libvirt_events.start('test:///', 'test/prefix')

        # Check events registration and deregistration
        mock_cnx = self.mock_libvirt.openReadOnly.return_value

        mock_cnx.domainEventRegisterAny.assert_any_call(
            None, self.mock_libvirt.VIR_DOMAIN_EVENT_ID_LIFECYCLE,
            libvirt_events._domain_event_lifecycle_cb, {
                'prefix': 'test/prefix',
                'object': 'domain',
                'event': 'lifecycle'
            })

        # Network events should have been skipped
        mock_cnx.networkEventRegisterAny.assert_not_called()
    def test_event_register(self, mock_libvirt):
        """
        Test that the libvirt_events engine actually registers events catch them and cleans
        before leaving the place.
        """
        mock_cnx = MagicMock()
        mock_libvirt.openReadOnly.return_value = mock_cnx

        # Don't loop for ever
        mock_libvirt.virEventRunDefaultImpl.return_value = -1

        mock_cnx.networkEventRegisterAny.return_value = 10000

        libvirt_events.start("test:///", "test/prefix")

        # Check that the connection has been opened
        mock_libvirt.openReadOnly.assert_called_once_with("test:///")

        # Check that the connection has been closed
        mock_cnx.close.assert_called_once()

        # Check events registration and deregistration
        mock_cnx.domainEventRegisterAny.assert_any_call(
            None,
            mock_libvirt.VIR_DOMAIN_EVENT_ID_LIFECYCLE,
            libvirt_events._domain_event_lifecycle_cb,
            {
                "prefix": "test/prefix",
                "object": "domain",
                "event": "lifecycle"
            },
        )
        mock_cnx.networkEventRegisterAny.assert_any_call(
            None,
            mock_libvirt.VIR_NETWORK_EVENT_ID_LIFECYCLE,
            libvirt_events._network_event_lifecycle_cb,
            {
                "prefix": "test/prefix",
                "object": "network",
                "event": "lifecycle"
            },
        )

        # Check that the deregister events are called with the result of register
        mock_cnx.networkEventDeregisterAny.assert_called_with(
            mock_cnx.networkEventRegisterAny.return_value)

        # Check that the default 'all' filter actually worked
        counts = {
            obj: len(callback_def)
            for obj, callback_def in libvirt_events.CALLBACK_DEFS.items()
        }
        for obj, count in counts.items():
            register = libvirt_events.REGISTER_FUNCTIONS[obj]
            assert getattr(mock_cnx, register).call_count == count
Пример #4
0
    def test_event_filtered(self):
        '''
        Test that events are skipped if their ID isn't defined in the libvirt
        module (older libvirt)
        '''
        libvirt_events.start('test', 'test/prefix', 'domain/lifecycle')

        # Check events registration and deregistration
        mock_cnx = self.mock_libvirt.openReadOnly.return_value

        mock_cnx.domainEventRegisterAny.assert_any_call(
                None, 0, libvirt_events._domain_event_lifecycle_cb,
                {'prefix': 'test/prefix', 'object': 'domain', 'event': 'lifecycle'})

        # Network events should have been filtered out
        mock_cnx.networkEventRegisterAny.assert_not_called()
Пример #5
0
def test_event_filtered(mock_libvirt):
    """
    Test that events are skipped if their ID isn't defined in the libvirt
    module (older libvirt)
    """
    libvirt_events.start("test", "test/prefix", "domain/lifecycle")

    # Check events registration and deregistration
    mock_cnx = mock_libvirt.openReadOnly.return_value

    mock_cnx.domainEventRegisterAny.assert_any_call(
        None,
        0,
        libvirt_events._domain_event_lifecycle_cb,
        {"prefix": "test/prefix", "object": "domain", "event": "lifecycle"},
    )

    # Network events should have been filtered out
    mock_cnx.networkEventRegisterAny.assert_not_called()
Пример #6
0
    def test_event_register(self, mock_libvirt):
        '''
        Test that the libvirt_events engine actually registers events catch them and cleans
        before leaving the place.
        '''
        mock_cnx = MagicMock()
        mock_libvirt.openReadOnly.return_value = mock_cnx

        mock_cnx.networkEventRegisterAny.return_value = 10000

        libvirt_events.start('test:///', 'test/prefix')

        # Check that the connection has been opened
        mock_libvirt.openReadOnly.assert_called_once_with('test:///')

        # Check that the connection has been closed
        mock_cnx.close.assert_called_once()

        # Check events registration and deregistration
        mock_cnx.domainEventRegisterAny.assert_any_call(
                None, mock_libvirt.VIR_DOMAIN_EVENT_ID_LIFECYCLE,
                libvirt_events._domain_event_lifecycle_cb,
                {'prefix': 'test/prefix', 'object': 'domain', 'event': 'lifecycle'})
        mock_cnx.networkEventRegisterAny.assert_any_call(
                None, mock_libvirt.VIR_NETWORK_EVENT_ID_LIFECYCLE,
                libvirt_events._network_event_lifecycle_cb,
                {'prefix': 'test/prefix', 'object': 'network', 'event': 'lifecycle'})

        # Check that the deregister events are called with the result of register
        mock_cnx.networkEventDeregisterAny.assert_called_with(
                mock_cnx.networkEventRegisterAny.return_value)

        # Check that the default 'all' filter actually worked
        counts = {obj: len(callback_def) for obj, callback_def in libvirt_events.CALLBACK_DEFS.items()}
        for obj, count in counts.items():
            register = libvirt_events.REGISTER_FUNCTIONS[obj]
            assert getattr(mock_cnx, register).call_count == count