Convert BaseDriver.*_interfaces to tuples

Use tuples instead of lists as it will be more difficult for developers
to accidentally overwrite/modify the variables.

This was inspired by commit 338651eae5b7c416f04970b9d60f09dc2dab8adb
which was fixing an issue where the base class variables were being
modified accidentally.

Change-Id: Ib7d67580ca377fabeb958c68a4d0d9bd2e2fd33a
This commit is contained in:
John L. Villalovos 2017-03-28 08:31:58 -07:00
parent 338651eae5
commit 200ed9244d
2 changed files with 41 additions and 13 deletions
ironic
drivers
tests/unit/drivers

@ -57,25 +57,24 @@ class BaseDriver(object):
third-party CI, or in the process of being deprecated.
"""
core_interfaces = []
standard_interfaces = []
# NOTE(jlvillal): These should be tuples to help prevent child classes from
# accidentally modifying the base class values.
core_interfaces = ('deploy', 'power')
standard_interfaces = ('boot', 'console', 'inspect', 'management', 'raid')
power = None
core_interfaces.append('power')
"""`Core` attribute for managing power state.
A reference to an instance of :class:PowerInterface.
"""
deploy = None
core_interfaces.append('deploy')
"""`Core` attribute for managing deployments.
A reference to an instance of :class:DeployInterface.
"""
console = None
standard_interfaces.append('console')
"""`Standard` attribute for managing console access.
A reference to an instance of :class:ConsoleInterface.
@ -98,7 +97,6 @@ class BaseDriver(object):
A reference to an instance of :class:ManagementInterface.
May be None, if unsupported by a driver.
"""
standard_interfaces.append('management')
boot = None
"""`Standard` attribute for boot related features.
@ -106,7 +104,6 @@ class BaseDriver(object):
A reference to an instance of :class:BootInterface.
May be None, if unsupported by a driver.
"""
standard_interfaces.append('boot')
vendor = None
"""Attribute for accessing any vendor-specific extensions.
@ -121,7 +118,6 @@ class BaseDriver(object):
A reference to an instance of :class:InspectInterface.
May be None, if unsupported by a driver.
"""
standard_interfaces.append('inspect')
raid = None
"""`Standard` attribute for RAID related features.
@ -129,18 +125,18 @@ class BaseDriver(object):
A reference to an instance of :class:RaidInterface.
May be None, if unsupported by a driver.
"""
standard_interfaces.append('raid')
def __init__(self):
pass
@property
def all_interfaces(self):
return self.core_interfaces + self.standard_interfaces + ['vendor']
return (list(self.core_interfaces + self.standard_interfaces) +
['vendor'])
@property
def non_vendor_interfaces(self):
return self.core_interfaces + self.standard_interfaces
return list(self.core_interfaces + self.standard_interfaces)
def get_properties(self):
"""Get the properties of the driver.
@ -168,14 +164,14 @@ class BareDriver(BaseDriver):
A reference to an instance of :class:NetworkInterface.
"""
core_interfaces = BaseDriver.core_interfaces + ['network']
core_interfaces = BaseDriver.core_interfaces + ('network',)
storage = None
"""`Standard` attribute for (remote) storage interface.
A reference to an instance of :class:StorageInterface.
"""
standard_interfaces = BaseDriver.standard_interfaces + ['storage']
standard_interfaces = BaseDriver.standard_interfaces + ('storage',)
ALL_INTERFACES = set(BareDriver().all_interfaces)

@ -508,3 +508,35 @@ class TestManagementInterface(base.TestCase):
self.assertRaises(exception.UnsupportedDriverExtension,
management.inject_nmi, task_mock)
class TestBaseDriver(base.TestCase):
def test_class_variables_immutable(self):
# Test to make sure that our *_interfaces variables in the class don't
# get modified by a child class
self.assertEqual(('deploy', 'power'),
driver_base.BaseDriver.core_interfaces)
self.assertEqual(('boot', 'console', 'inspect', 'management', 'raid'),
driver_base.BaseDriver.standard_interfaces)
# Ensure that instantiating an instance of a derived class does not
# change our variables.
driver_base.BareDriver()
self.assertEqual(('deploy', 'power'),
driver_base.BaseDriver.core_interfaces)
self.assertEqual(('boot', 'console', 'inspect', 'management', 'raid'),
driver_base.BaseDriver.standard_interfaces)
class TestBareDriver(base.TestCase):
def test_class_variables_immutable(self):
# Test to make sure that our *_interfaces variables in the class don't
# get modified by a child class
self.assertEqual(('deploy', 'power', 'network'),
driver_base.BareDriver.core_interfaces)
self.assertEqual(
('boot', 'console', 'inspect', 'management', 'raid', 'storage'),
driver_base.BareDriver.standard_interfaces
)