diff options
author | Alexey Stupnikov <aleksey.stupnikov@gmail.com> | 2021-07-26 11:37:19 +0000 |
---|---|---|
committer | Alexey Stupnikov <aleksey.stupnikov@gmail.com> | 2021-08-03 13:13:58 +0200 |
commit | 2c74bb92e5e9561e2ad1386c2e14fb042d6dc062 (patch) | |
tree | a9a90b8b4a0c2fbaf220f59367176327d1bc3ae4 | |
parent | 75c4abd909aa86f01e138329e89ffc31d53ef2d3 (diff) | |
download | oslo-utils-2c74bb92e5e9561e2ad1386c2e14fb042d6dc062.tar.gz |
Modify UUID sentinel to support keystone-like UUIDs4.10.0
Keystone User IDs and Project IDs are used in unit tests, but
_UUIDSentinels() class doesn't generate UUIDs without hyphens.
This patch makes backward compatible modifications to
_UUIDSentinels() class and introduces keystoneidsentinel global
that could be used in the same way as existing uuidsentinel.
Original "UUID sentinel" change: I214ff21b461fa1ca4b83476e1d0a763efe986217
Related-Bug: #1746747
Change-Id: Idb3e893cc03d64ad0522b5e4cedfa30c4f4a2a2f
-rw-r--r-- | oslo_utils/fixture.py | 14 | ||||
-rw-r--r-- | oslo_utils/tests/test_fixture.py | 8 | ||||
-rw-r--r-- | releasenotes/notes/introduce-keystoneidsentinel-16bf3e7f2ae7e9f3.yaml | 6 |
3 files changed, 25 insertions, 3 deletions
diff --git a/oslo_utils/fixture.py b/oslo_utils/fixture.py index 6a874de..9aec6d3 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 b106f15..c6b2e6d 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 0000000..02e6134 --- /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. |