Ejemplo n.º 1
0
 def test_get_filter_uuid_unicode_exception_logging(self, debug):
     with mock.patch.object(self.fw._conn, 'nwfilterLookupByName') as look:
         look.side_effect = fakelibvirt.libvirtError(u"\U0001F4A9")
         self.fw._get_filter_uuid('test')
     self.assertEqual(2, debug.call_count)
     self.assertEqual(u"Cannot find UUID for filter '%(name)s': '%(e)s'",
                      debug.call_args_list[0][0][0])
Ejemplo n.º 2
0
 def test_time_sync_no_errors(self, time_mock):
     self.domain.setTime.side_effect = fakelibvirt.libvirtError('error')
     self.guest.sync_guest_time()
     self.domain.setTime.assert_called_once_with(time={
         'nseconds': 125000000,
         'seconds': 1234567890
     })
Ejemplo n.º 3
0
    def filterDefineXMLMock(self, xml):
        class FakeNWFilterInternal(object):
            def __init__(self, parent, name, u, xml):
                self.name = name
                self.uuid = u
                self.parent = parent
                self.xml = xml

            def XMLDesc(self, flags):
                return self.xml

            def undefine(self):
                del self.parent.filters[self.name]

        tree = etree.fromstring(xml)
        name = tree.get('name')
        u = tree.find('uuid')
        if u is None:
            u = uuid.uuid4().hex
        else:
            u = u.text
        if name not in self.filters:
            self.filters[name] = FakeNWFilterInternal(self, name, u, xml)
        else:
            if self.filters[name].uuid != u:
                raise fakelibvirt.libvirtError(
                    "Mismatching name '%s' with uuid '%s' vs '%s'" %
                    (name, self.filters[name].uuid, u))
            self.filters[name].xml = xml
        return True
Ejemplo n.º 4
0
    def filterDefineXMLMock(self, xml):
        class FakeNWFilterInternal(object):
            def __init__(self, parent, name, u, xml):
                self.name = name
                self.uuid = u
                self.parent = parent
                self.xml = xml

            def XMLDesc(self, flags):
                return self.xml

            def undefine(self):
                del self.parent.filters[self.name]

        tree = etree.fromstring(xml)
        name = tree.get('name')
        u = tree.find('uuid')
        if u is None:
            u = uuid.uuid4().hex
        else:
            u = u.text
        if name not in self.filters:
            self.filters[name] = FakeNWFilterInternal(self, name, u, xml)
        else:
            if self.filters[name].uuid != u:
                raise fakelibvirt.libvirtError(
                    "Mismatching name '%s' with uuid '%s' vs '%s'"
                    % (name, self.filters[name].uuid, u))
            self.filters[name].xml = xml
        return True
Ejemplo n.º 5
0
 def test_get_filter_uuid_unicode_exception_logging(self, debug):
     with mock.patch.object(self.fw._conn, 'nwfilterLookupByName') as look:
         look.side_effect = fakelibvirt.libvirtError(u"\U0001F4A9")
         self.fw._get_filter_uuid('test')
     self.assertEqual(2, debug.call_count)
     self.assertEqual(u"Cannot find UUID for filter '%(name)s': '%(e)s'",
                      debug.call_args_list[0][0][0])
Ejemplo n.º 6
0
    def test_delete_configuration_exception(self):
        domain = mock.MagicMock()
        domain.undefineFlags.side_effect = fakelibvirt.libvirtError('oops')

        guest = libvirt_guest.Guest(domain)
        guest.delete_configuration()

        domain.undefine.assert_called_once_with()
Ejemplo n.º 7
0
    def test_delete_configuration_exception(self):
        domain = mock.MagicMock()
        domain.undefineFlags.side_effect = fakelibvirt.libvirtError('oops')

        guest = libvirt_guest.Guest(domain)
        guest.delete_configuration()

        domain.undefine.assert_called_once_with()
Ejemplo n.º 8
0
    def test_unfilter_instance_retry_not_found(self, mock_sleep, mock_lookup):
        # Tests that we exit if the nw filter is not found.
        in_use = fakelibvirt.libvirtError('nwfilter is in use')
        in_use.err = (fakelibvirt.VIR_ERR_OPERATION_INVALID, )
        not_found = fakelibvirt.libvirtError('no nwfilter with matching name')
        not_found.err = (fakelibvirt.VIR_ERR_NO_NWFILTER, )
        mock_undefine = mock.Mock(side_effect=(in_use, not_found))
        fakefilter = mock.MagicMock(undefine=mock_undefine)
        mock_lookup.return_value = fakefilter

        instance_ref = self._create_instance()
        network_info = _fake_network_info(self, 1)

        self.fw.unfilter_instance(instance_ref, network_info)
        self.assertEqual(2, mock_lookup.call_count)
        self.assertEqual(2, mock_undefine.call_count)
        mock_sleep.assert_called_once_with(1)
Ejemplo n.º 9
0
    def test_unfilter_instance_retry_not_found(self, mock_sleep, mock_lookup):
        # Tests that we exit if the nw filter is not found.
        in_use = fakelibvirt.libvirtError('nwfilter is in use')
        in_use.err = (fakelibvirt.VIR_ERR_OPERATION_INVALID,)
        not_found = fakelibvirt.libvirtError('no nwfilter with matching name')
        not_found.err = (fakelibvirt.VIR_ERR_NO_NWFILTER,)
        mock_undefine = mock.Mock(side_effect=(in_use, not_found))
        fakefilter = mock.MagicMock(undefine=mock_undefine)
        mock_lookup.return_value = fakefilter

        instance_ref = self._create_instance()
        network_info = _fake_network_info(self, 1)

        self.fw.unfilter_instance(instance_ref, network_info)
        self.assertEqual(2, mock_lookup.call_count)
        self.assertEqual(2, mock_undefine.call_count)
        mock_sleep.assert_called_once_with(1)
Ejemplo n.º 10
0
    def test_delete_configuration_exception(self):
        domain = mock.Mock(spec=fakelibvirt.virDomain)
        domain.undefineFlags.side_effect = fakelibvirt.libvirtError('oops')
        domain.ID.return_value = 1

        guest = libvirt_guest.Guest(domain)
        guest.delete_configuration()

        domain.undefine.assert_called_once_with()
Ejemplo n.º 11
0
    def test_delete_configuration_exception(self):
        domain = mock.Mock(spec=fakelibvirt.virDomain)
        domain.undefineFlags.side_effect = fakelibvirt.libvirtError('oops')
        domain.ID.return_value = 1

        guest = libvirt_guest.Guest(domain)
        guest.delete_configuration()

        domain.undefine.assert_called_once_with()
 def test_define_filter_fails_wrong_code(self):
     """Tests that we reraise the libvirt error for an operational failure
     if the error code is something unexpected.
     """
     error = fakelibvirt.libvirtError('already exists')
     error.err = (fakelibvirt.VIR_ERR_OPERATION_TIMEOUT, None, 'timeout',)
     with mock.patch.object(self.fw._conn, 'nwfilterDefineXML',
                            side_effect=error) as define:
         self.assertRaises(fakelibvirt.libvirtError,
                           self.fw._define_filter, mock.sentinel.xml)
     define.assert_called_once_with(mock.sentinel.xml)
Ejemplo n.º 13
0
 def test_define_filter_fails_wrong_code(self):
     """Tests that we reraise the libvirt error for an operational failure
     if the error code is something unexpected.
     """
     error = fakelibvirt.libvirtError('already exists')
     error.err = (fakelibvirt.VIR_ERR_OPERATION_TIMEOUT, None, 'timeout',)
     with mock.patch.object(self.fw._conn, 'nwfilterDefineXML',
                            side_effect=error) as define:
         self.assertRaises(fakelibvirt.libvirtError,
                           self.fw._define_filter, mock.sentinel.xml)
     define.assert_called_once_with(mock.sentinel.xml)
 def test_define_filter_already_exists(self):
     """Tests that we ignore a libvirt error when the nw filter already
     exists for a given name.
     """
     error = fakelibvirt.libvirtError('already exists')
     error.err = (fakelibvirt.VIR_ERR_OPERATION_FAILED, None,
                  "filter 'nova-no-nd-reflection' already exists with uuid "
                  "e740c5ec-c715-4f73-9874-630cc73d4ac2",)
     with mock.patch.object(self.fw._conn, 'nwfilterDefineXML',
                            side_effect=error) as define:
         self.fw._define_filter(mock.sentinel.xml)
     define.assert_called_once_with(mock.sentinel.xml)
Ejemplo n.º 15
0
 def test_define_filter_already_exists(self):
     """Tests that we ignore a libvirt error when the nw filter already
     exists for a given name.
     """
     error = fakelibvirt.libvirtError('already exists')
     error.err = (fakelibvirt.VIR_ERR_OPERATION_FAILED, None,
                  "filter 'nova-no-nd-reflection' already exists with uuid "
                  "e740c5ec-c715-4f73-9874-630cc73d4ac2",)
     with mock.patch.object(self.fw._conn, 'nwfilterDefineXML',
                            side_effect=error) as define:
         self.fw._define_filter(mock.sentinel.xml)
     define.assert_called_once_with(mock.sentinel.xml)
Ejemplo n.º 16
0
    def test_conn_event_up_down(self, mock_conn, mock_test_conn):
        handler = mock.MagicMock()
        h = host.Host("qemu:///system", conn_event_handler=handler)
        mock_conn.side_effect = (mock.MagicMock(),
                                 fakelibvirt.libvirtError('test'))
        mock_test_conn.return_value = False

        h.get_connection()
        self.assertRaises(exception.HypervisorUnavailable, h.get_connection)
        h._dispatch_conn_event()
        h._dispatch_conn_event()

        handler.assert_has_calls(
            [mock.call(True, None),
             mock.call(False, StringMatcher())])
Ejemplo n.º 17
0
    def test_unfilter_instance_retry_and_pass(self, mock_sleep, mock_lookup):
        # Tests that we retry on in-use error but pass if undefine() works
        # while looping.
        in_use = fakelibvirt.libvirtError('nwfilter is in use')
        in_use.err = (fakelibvirt.VIR_ERR_OPERATION_INVALID,)
        mock_undefine = mock.Mock(side_effect=(in_use, None))
        fakefilter = mock.MagicMock(undefine=mock_undefine)
        mock_lookup.return_value = fakefilter

        instance_ref = self._create_instance()
        network_info = _fake_network_info(self, 1)

        self.fw.unfilter_instance(instance_ref, network_info)
        self.assertEqual(2, mock_lookup.call_count)
        self.assertEqual(2, mock_undefine.call_count)
        mock_sleep.assert_called_once_with(1)
Ejemplo n.º 18
0
    def test_unfilter_instance_retry_and_pass(self, mock_sleep, mock_lookup):
        # Tests that we retry on in-use error but pass if undefine() works
        # while looping.
        in_use = fakelibvirt.libvirtError('nwfilter is in use')
        in_use.err = (fakelibvirt.VIR_ERR_OPERATION_INVALID, )
        mock_undefine = mock.Mock(side_effect=(in_use, None))
        fakefilter = mock.MagicMock(undefine=mock_undefine)
        mock_lookup.return_value = fakefilter

        instance_ref = self._create_instance()
        network_info = _fake_network_info(self, 1)

        self.fw.unfilter_instance(instance_ref, network_info)
        self.assertEqual(2, mock_lookup.call_count)
        self.assertEqual(2, mock_undefine.call_count)
        mock_sleep.assert_called_once_with(1)
Ejemplo n.º 19
0
    def test_conn_event_up_down(self, mock_conn, mock_test_conn):
        handler = mock.MagicMock()
        h = host.Host("qemu:///system", conn_event_handler=handler)
        mock_conn.side_effect = (mock.MagicMock(),
                                 fakelibvirt.libvirtError('test'))
        mock_test_conn.return_value = False

        h.get_connection()
        self.assertRaises(exception.HypervisorUnavailable, h.get_connection)
        h._dispatch_conn_event()
        h._dispatch_conn_event()

        handler.assert_has_calls([
            mock.call(True, None),
            mock.call(False, StringMatcher())
        ])
Ejemplo n.º 20
0
    def test_unfilter_instance_retry_and_error(self, mock_sleep, mock_lookup):
        # Tests that we try to undefine the network filter when it's in use
        # until we hit a timeout. We try two times and sleep once in between.
        self.flags(live_migration_retry_count=2)
        in_use = fakelibvirt.libvirtError("nwfilter is in use")
        in_use.err = (fakelibvirt.VIR_ERR_OPERATION_INVALID,)
        mock_undefine = mock.Mock(side_effect=in_use)
        fakefilter = mock.MagicMock(undefine=mock_undefine)
        mock_lookup.return_value = fakefilter

        instance_ref = self._create_instance()
        network_info = _fake_network_info(self, 1)

        self.assertRaises(fakelibvirt.libvirtError, self.fw.unfilter_instance, instance_ref, network_info)
        self.assertEqual(2, mock_lookup.call_count)
        self.assertEqual(2, mock_undefine.call_count)
        mock_sleep.assert_called_once_with(1)
Ejemplo n.º 21
0
    def test_libvirt_connect_error(self, mock_get_conn):
        mock_get_conn.side_effect = fakelibvirt.libvirtError(
            'Sample exception for versioned notification test.')
        # restart the compute service
        self.assertRaises(exception.HypervisorUnavailable,
                          self.restart_compute_service, self.compute)

        self.assertEqual(1, len(fake_notifier.VERSIONED_NOTIFICATIONS))
        self._verify_notification(
            'libvirt-connect-error',
            replacements={
                'ip': CONF.my_ip,
                'reason.function_name': self.ANY,
                'reason.module_name': self.ANY,
                'reason.traceback': self.ANY
            },
            actual=fake_notifier.VERSIONED_NOTIFICATIONS[0])
Ejemplo n.º 22
0
    def test_unfilter_instance_retry_and_error(self, mock_sleep, mock_lookup):
        # Tests that we try to undefine the network filter when it's in use
        # until we hit a timeout. We try two times and sleep once in between.
        self.flags(live_migration_retry_count=2)
        in_use = fakelibvirt.libvirtError('nwfilter is in use')
        in_use.err = (fakelibvirt.VIR_ERR_OPERATION_INVALID, )
        mock_undefine = mock.Mock(side_effect=in_use)
        fakefilter = mock.MagicMock(undefine=mock_undefine)
        mock_lookup.return_value = fakefilter

        instance_ref = self._create_instance()
        network_info = _fake_network_info(self, 1)

        self.assertRaises(fakelibvirt.libvirtError, self.fw.unfilter_instance,
                          instance_ref, network_info)
        self.assertEqual(2, mock_lookup.call_count)
        self.assertEqual(2, mock_undefine.call_count)
        mock_sleep.assert_called_once_with(1)
Ejemplo n.º 23
0
    def test_find_secret(self, mock_sec):
        """finding secrets with various usage_type."""
        expected = [
            mock.call(fakelibvirt.VIR_SECRET_USAGE_TYPE_CEPH, 'rbdvol'),
            mock.call(fakelibvirt.VIR_SECRET_USAGE_TYPE_CEPH, 'cephvol'),
            mock.call(fakelibvirt.VIR_SECRET_USAGE_TYPE_ISCSI, 'iscsivol'),
            mock.call(fakelibvirt.VIR_SECRET_USAGE_TYPE_VOLUME, 'vol')]

        self.host.find_secret('rbd', 'rbdvol')
        self.host.find_secret('ceph', 'cephvol')
        self.host.find_secret('iscsi', 'iscsivol')
        self.host.find_secret('volume', 'vol')
        self.assertEqual(expected, mock_sec.mock_calls)
        self.assertRaises(exception.NovaException,
                          self.host.find_secret, "foo", "foovol")

        mock_sec.side_effect = fakelibvirt.libvirtError("")
        mock_sec.side_effect.err = (66, )
        self.assertIsNone(self.host.find_secret('rbd', 'rbdvol'))
Ejemplo n.º 24
0
    def test_find_secret(self, mock_sec):
        """finding secrets with various usage_type."""
        expected = [
            mock.call(fakelibvirt.VIR_SECRET_USAGE_TYPE_CEPH, 'rbdvol'),
            mock.call(fakelibvirt.VIR_SECRET_USAGE_TYPE_CEPH, 'cephvol'),
            mock.call(fakelibvirt.VIR_SECRET_USAGE_TYPE_ISCSI, 'iscsivol'),
            mock.call(fakelibvirt.VIR_SECRET_USAGE_TYPE_VOLUME, 'vol')]

        self.host.find_secret('rbd', 'rbdvol')
        self.host.find_secret('ceph', 'cephvol')
        self.host.find_secret('iscsi', 'iscsivol')
        self.host.find_secret('volume', 'vol')
        self.assertEqual(expected, mock_sec.mock_calls)
        self.assertRaises(exception.NovaException,
                          self.host.find_secret, "foo", "foovol")

        mock_sec.side_effect = fakelibvirt.libvirtError("")
        mock_sec.side_effect.err = (66, )
        self.assertIsNone(self.host.find_secret('rbd', 'rbdvol'))
Ejemplo n.º 25
0
    def test_conn_event_fail(self, mock_conn):
        handler = mock.MagicMock()
        h = host.Host("qemu:///system", conn_event_handler=handler)
        mock_conn.side_effect = fakelibvirt.libvirtError('test')

        self.assertRaises(exception.HypervisorUnavailable, h.get_connection)
        h._dispatch_conn_event()

        handler.assert_called_once_with(False, StringMatcher())

        # Attempt to get a second connection, and assert that we don't add
        # queue a second callback. Note that we can't call
        # _dispatch_conn_event() and assert no additional call to the handler
        # here as above. This is because we haven't added an event, so it would
        # block. We mock the helper method which queues an event for callback
        # instead.
        with mock.patch.object(h, '_queue_conn_event_handler') as mock_queue:
            self.assertRaises(exception.HypervisorUnavailable,
                              h.get_connection)
            mock_queue.assert_not_called()
Ejemplo n.º 26
0
    def test_conn_event_fail(self, mock_conn):
        handler = mock.MagicMock()
        h = host.Host("qemu:///system", conn_event_handler=handler)
        mock_conn.side_effect = fakelibvirt.libvirtError('test')

        self.assertRaises(exception.HypervisorUnavailable, h.get_connection)
        h._dispatch_conn_event()

        handler.assert_called_once_with(False, StringMatcher())

        # Attempt to get a second connection, and assert that we don't add
        # queue a second callback. Note that we can't call
        # _dispatch_conn_event() and assert no additional call to the handler
        # here as above. This is because we haven't added an event, so it would
        # block. We mock the helper method which queues an event for callback
        # instead.
        with mock.patch.object(h, '_queue_conn_event_handler') as mock_queue:
            self.assertRaises(exception.HypervisorUnavailable,
                              h.get_connection)
            mock_queue.assert_not_called()
Ejemplo n.º 27
0
 def test_list_devices_other_exc(self, mock_listDevices):
     mock_listDevices.side_effect = fakelibvirt.libvirtError('test')
     self.assertRaises(fakelibvirt.libvirtError,
                       self.host._list_devices, 'mdev', 8)
Ejemplo n.º 28
0
 def nwfilterLookupByName(self, name):
     if name in self.filters:
         return self.filters[name]
     raise fakelibvirt.libvirtError('Filter Not Found')
Ejemplo n.º 29
0
 def test_time_sync_no_errors(self, time_mock):
     self.domain.setTime.side_effect = fakelibvirt.libvirtError('error')
     self.guest.resume()
     self.domain.setTime.assert_called_once_with(time={
                                                 'nseconds': 125000000,
                                                 'seconds': 1234567890})
Ejemplo n.º 30
0
 def test_is_job_complete_exception(self):
     self.domain.blockJobInfo.side_effect = fakelibvirt.libvirtError('fake')
     self.assertRaises(fakelibvirt.libvirtError,
                       self.gblock.is_job_complete)
Ejemplo n.º 31
0
 def test_list_devices_other_exc(self, mock_listDevices):
     mock_listDevices.side_effect = fakelibvirt.libvirtError('test')
     self.assertRaises(fakelibvirt.libvirtError, self.host._list_devices,
                       'mdev', 8)
Ejemplo n.º 32
0
 def nwfilterLookupByName(self, name):
     if name in self.filters:
         return self.filters[name]
     raise fakelibvirt.libvirtError('Filter Not Found')
Ejemplo n.º 33
0
 def test_is_job_complete_exception(self):
     self.domain.blockJobInfo.side_effect = fakelibvirt.libvirtError('fake')
     self.assertRaises(fakelibvirt.libvirtError,
                       self.gblock.is_job_complete)
Ejemplo n.º 34
0
 def test_delete_configuration_exception(self):
     self.domain.undefineFlags.side_effect = fakelibvirt.libvirtError(
         'oops')
     self.domain.ID.return_value = 1
     self.guest.delete_configuration()
     self.domain.undefine.assert_called_once_with()
Ejemplo n.º 35
0
 def test_delete_configuration_exception(self):
     self.domain.undefineFlags.side_effect = fakelibvirt.libvirtError(
         'oops')
     self.domain.ID.return_value = 1
     self.guest.delete_configuration()
     self.domain.undefine.assert_called_once_with()