Add custom namespace

Adds the CUSTOM_ trait namespace prefix, matching the CUSTOM_ prefix
from the resource classes in the placement API. Also adds a simple
is_custom() method to the library for testing whether a trait is
custom or not.

Change-Id: I37cd61516eb68c9bf07778fe6ad42bb34c19d4da
This commit is contained in:
Jay Pipes 2017-03-02 11:00:44 -05:00 committed by Sylvain Bauza
parent 9452af54b1
commit 2855c25eb3
3 changed files with 18 additions and 1 deletions

View File

@ -74,3 +74,12 @@ def check_traits(traits):
valid_traits = trait_set & valid_trait_set
return (valid_traits, trait_set - valid_traits)
def is_custom(trait):
"""Returns True if the trait string represents a custom trait, or False
otherwise.
:param trait: String name of the trait
"""
return trait.startswith(NAMESPACES['CUSTOM'])

View File

@ -12,6 +12,9 @@
# License for the specific language governing permissions and limitations
# under the License.
# Any user-specified feature/trait is prefixed with the custom namespace
_CUSTOM_NS = 'CUSTOM_'
# All hardware-specific features are prefixed with this namespace
_HW_NS = 'HW_'
@ -68,6 +71,7 @@ HW_CPU_X86_VMX = _CPU_X86_NS + 'VMX'
HW_CPU_X86_SVM = _CPU_X86_NS + 'SVM'
NAMESPACES = {
'CUSTOM': _CUSTOM_NS,
'HARDWARE': _HW_NS,
'HW': _HW_NS,
'CPU': _CPU_NS,

View File

@ -37,7 +37,7 @@ class TestOs_traits(base.TestCase):
def test_namespaces(self):
namespaces = ot.NAMESPACES
self.assertIn(("HARDWARE", "HW_"), namespaces.items())
self.assertEqual(4, len(namespaces))
self.assertEqual(5, len(namespaces))
def test_get_traits(self):
traits = ot.get_traits(ot.NAMESPACES['X86'])
@ -53,3 +53,7 @@ class TestOs_traits(base.TestCase):
check_traits.extend(not_traits)
self.assertEqual((traits, not_traits),
ot.check_traits(check_traits))
def test_is_custom(self):
self.assertTrue(ot.is_custom('CUSTOM_FOO'))
self.assertFalse(ot.is_custom('HW_CPU_X86_SSE42'))