Mock pyghmi lib in unit tests if not present

Mock the pyghmi library from within ironic/tests/drivers so that
test_ipminative.py can import ironic/drivers/ipminative and run the unit
tests on it, even when the external library is not present. Also
corrects pep8 issue in ipminative by grouping the imports.

Change-Id: I870724bf48346857b1170463fcc7d22c08aca3bd
This commit is contained in:
Devananda van der Veen 2014-05-07 18:37:04 -07:00 committed by Michael Davies
parent 581ffa98d2
commit 2ba774deb6
5 changed files with 34 additions and 5 deletions

View File

@ -25,9 +25,13 @@ from ironic.common import exception
from ironic.common import states
from ironic.conductor import task_manager
from ironic.drivers import base
from ironic.openstack.common import importutils
from ironic.openstack.common import log as logging
from pyghmi import exceptions as pyghmi_exception
from pyghmi.ipmi import command as ipmi_command
pyghmi = importutils.try_import('pyghmi')
if pyghmi:
from pyghmi import exceptions as pyghmi_exception
from pyghmi.ipmi import command as ipmi_command
opts = [
cfg.IntOpt('retry_timeout',

View File

@ -78,6 +78,10 @@ class PXEAndIPMINativeDriver(base.BaseDriver):
"""
def __init__(self):
if not importutils.try_import('pyghmi'):
raise exception.DriverLoadError(
driver=self.__class__.__name__,
reason=_("Unable to import pyghmi library"))
self.power = ipminative.NativeIPMIPower()
self.deploy = pxe.PXEDeploy()
self.pxe_vendor = pxe.VendorPassthru()

View File

@ -18,8 +18,11 @@
"""
Test class for Native IPMI power driver module.
"""
import mock
from oslo.config import cfg
from ironic.common import driver_factory
from ironic.common import exception
from ironic.common import states
@ -32,7 +35,6 @@ from ironic.tests.conductor import utils as mgr_utils
from ironic.tests.db import base as db_base
from ironic.tests.db import utils as db_utils
from ironic.tests.objects import utils as obj_utils
from oslo.config import cfg
CONF = cfg.CONF

View File

@ -22,6 +22,7 @@ respective external libraries' actually being present.
Any external library required by a third-party driver should be mocked here.
Current list of mocked libraries:
seamicroclient
ipminative
"""
import sys
@ -49,8 +50,27 @@ if not seamicroclient:
if 'ironic.drivers.modules.seamicro' in sys.modules:
reload(sys.modules['ironic.drivers.modules.seamicro'])
# IPMITool driver checks the system for presense of 'ipmitool' binary during
# __init__. We bypass that check in order to run the unit tests, which do not
# depend on 'ipmitool' being on the system.
ipmitool.TIMING_SUPPORT = False
pyghmi = importutils.try_import("pyghmi")
if not pyghmi:
p = mock.Mock()
p.exceptions = mock.Mock()
p.exceptions.IpmiException = Exception
p.ipmi = mock.Mock()
p.ipmi.command = mock.Mock()
p.ipmi.command.Command = mock.Mock()
sys.modules['pyghmi'] = p
sys.modules['pyghmi.exceptions'] = p.exceptions
sys.modules['pyghmi.ipmi'] = p.ipmi
sys.modules['pyghmi.ipmi.command'] = p.ipmi.command
# FIXME(deva): the next line is a hack, because several unit tests
# actually depend on this particular string being present
# in pyghmi.ipmi.command.boot_devices
p.ipmi.command.boot_devices = {'pxe': 4}
if 'ironic.drivers.modules.ipminative' in sys.modules:
reload(sys.modules['ironic.drivers.modules.ipminative'])

View File

@ -27,5 +27,4 @@ six>=1.7.0
jsonpatch>=1.1
WSME>=0.6
Jinja2
pyghmi>=0.6.11
oslo.messaging>=1.3.0