Mock the 'libvirt' import on tests

This patch is removing python-libvirt from test-requirements.txt and
mocking that library for the tests.

Closes-Bug: #1616906
Change-Id: Id067476c58bf9ddedd6cfc5fe80303f7d949cb9e
This commit is contained in:
Lucas Alvares Gomes 2016-08-26 15:52:00 +01:00
parent 5ed5ff4d99
commit 3ca96ed906
4 changed files with 25 additions and 4 deletions

View File

@ -41,9 +41,9 @@ Currently supported transports are:
import os
import xml.etree.ElementTree as ET
import libvirt
from oslo_config import cfg
from oslo_log import log as logging
from oslo_utils import importutils
from ironic.common import boot_devices
from ironic.common import exception as ir_exc
@ -54,6 +54,7 @@ from ironic.drivers import base
from ironic.drivers import utils as driver_utils
from ironic_staging_drivers.common import exception as isd_exc
libvirt = importutils.try_import('libvirt')
CONF = cfg.CONF

View File

@ -20,3 +20,12 @@ when needing to mock those third party modules"""
IBOOT_SPEC = (
'iBootInterface',
)
# libvirt
LIBVIRT_SPEC = (
'libvirtError',
'open',
'openAuth',
'VIR_CRED_AUTHNAME',
'VIR_CRED_PASSPHRASE',
)

View File

@ -47,3 +47,17 @@ if not iboot:
# external library has been mocked
if 'ironic.drivers.modules.iboot' in sys.modules:
six.moves.reload_module(sys.modules['ironic.drivers.modules.iboot'])
# attempt to load the external 'libvirt' library, which is required by
# the optional drivers.modules.libvirt module
libvirt = importutils.try_import('libvirt')
if not libvirt:
lv = mock.MagicMock(spec_set=mock_specs.LIBVIRT_SPEC)
lv.libvirtError = Exception
sys.modules['libvirt'] = lv
# if anything has loaded the libvirt driver yet, reload it now that the
# external library has been mocked
if 'ironic_staging_drivers.libvirt.power' in sys.modules:
six.moves.reload_module(
sys.modules['ironic_staging_drivers.libvirt.power'])

View File

@ -16,6 +16,3 @@ testtools>=1.4.0 # MIT
os-testr>=0.4.1 # Apache-2.0
reno>=0.1.1 # Apache2
mock>=1.2 # BSD
# libvirt driver requires libvirt-python
libvirt-python>=1.2.5 # LGPLv2+