summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn L. Villalovos <john.l.villalovos@intel.com>2017-03-28 08:31:58 -0700
committerJohn L. Villalovos <john.l.villalovos@intel.com>2017-03-28 21:21:19 -0700
commit200ed9244d3c8153389fefa242c0e82f12536b5e (patch)
treeed0c38ff92616835f614c33b3f9933fa3328a07c
parent338651eae5b7c416f04970b9d60f09dc2dab8adb (diff)
downloadironic-200ed9244d3c8153389fefa242c0e82f12536b5e.tar.gz
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
-rw-r--r--ironic/drivers/base.py22
-rw-r--r--ironic/tests/unit/drivers/test_base.py32
2 files changed, 41 insertions, 13 deletions
diff --git a/ironic/drivers/base.py b/ironic/drivers/base.py
index 6c7ca4dd8..5508084cb 100644
--- a/ironic/drivers/base.py
+++ b/ironic/drivers/base.py
@@ -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)
diff --git a/ironic/tests/unit/drivers/test_base.py b/ironic/tests/unit/drivers/test_base.py
index afbf64537..23ecd2e7d 100644
--- a/ironic/tests/unit/drivers/test_base.py
+++ b/ironic/tests/unit/drivers/test_base.py
@@ -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
+ )