diff --git a/oslo_utils/fixture.py b/oslo_utils/fixture.py index 6a874de3..9aec6d38 100644 --- a/oslo_utils/fixture.py +++ b/oslo_utils/fixture.py @@ -55,7 +55,8 @@ class TimeFixture(fixtures.Fixture): class _UUIDSentinels(object): - """Registry of dynamically created, named, random UUID strings. + """Registry of dynamically created, named, random UUID strings in regular + (with hyphens) and similar to some keystone IDs (without hyphens) formats. An instance of this class will dynamically generate attributes as they are referenced, associating a random UUID string with each. Thereafter, @@ -65,6 +66,7 @@ class _UUIDSentinels(object): Usage:: from oslo_utils.fixture import uuidsentinel as uuids + from oslo_utils.fixture import keystoneidsentinel as keystids ... foo = uuids.foo do_a_thing(foo) @@ -72,17 +74,22 @@ class _UUIDSentinels(object): assert foo == uuids.foo # But a different one will be different assert foo != uuids.bar + # Same approach is valid for keystoneidsentinel: + data = create_some_data_structure(keystids.bar, var1, var2, var3) + assert extract_bar(data) == keystids.bar """ - def __init__(self): + def __init__(self, is_dashed=True): self._sentinels = {} self._lock = threading.Lock() + self.is_dashed = is_dashed def __getattr__(self, name): if name.startswith('_'): raise AttributeError('Sentinels must not start with _') with self._lock: if name not in self._sentinels: - self._sentinels[name] = uuidutils.generate_uuid() + self._sentinels[name] = uuidutils.generate_uuid( + dashed=self.is_dashed) return self._sentinels[name] @@ -90,3 +97,4 @@ class _UUIDSentinels(object): # same process (including across multiple modules) will result in the same # values uuidsentinel = _UUIDSentinels() +keystoneidsentinel = _UUIDSentinels(is_dashed=False) diff --git a/oslo_utils/tests/test_fixture.py b/oslo_utils/tests/test_fixture.py index b106f15e..c6b2e6dd 100644 --- a/oslo_utils/tests/test_fixture.py +++ b/oslo_utils/tests/test_fixture.py @@ -19,6 +19,7 @@ import datetime from oslotest import base as test_base from oslo_utils import fixture +from oslo_utils.fixture import keystoneidsentinel as keystids from oslo_utils.fixture import uuidsentinel as uuids from oslo_utils import timeutils from oslo_utils import uuidutils @@ -71,13 +72,20 @@ class UUIDSentinelsTest(test_base.BaseTestCase): uuid1 = uuids.foobar uuid2 = uuids.barfoo self.assertNotEqual(uuid1, uuid2) + keystid1 = keystids.foobar + keystid2 = keystids.barfoo + self.assertNotEqual(keystid1, keystid2) def test_returns_uuid(self): self.assertTrue(uuidutils.is_uuid_like(uuids.foo)) + self.assertTrue(uuidutils.is_uuid_like(keystids.foo)) def test_returns_string(self): self.assertIsInstance(uuids.foo, str) + self.assertIsInstance(keystids.foo, str) def test_with_underline_prefix(self): ex = self.assertRaises(AttributeError, getattr, uuids, '_foo') self.assertIn("Sentinels must not start with _", str(ex)) + ex = self.assertRaises(AttributeError, getattr, keystids, '_foo') + self.assertIn("Sentinels must not start with _", str(ex)) diff --git a/releasenotes/notes/introduce-keystoneidsentinel-16bf3e7f2ae7e9f3.yaml b/releasenotes/notes/introduce-keystoneidsentinel-16bf3e7f2ae7e9f3.yaml new file mode 100644 index 00000000..02e6134a --- /dev/null +++ b/releasenotes/notes/introduce-keystoneidsentinel-16bf3e7f2ae7e9f3.yaml @@ -0,0 +1,6 @@ +--- +features: + - | + keystoneidsentinel singleton was introduced to generate + random keystone-like UUIDs. New sentinel could be used in the same + way as existing uuidsentinel.