63d7649ed5
Add a private _UUIDSentinels() class to oslo_utils.fixture that behaves like a mock.sentinel, but returns specifically UUIDs. Expose a (singleton) instance of it called uuidsentinel. This is mostly copied from [1], with the following differences: - We don't do the enforced-singleton thing [2]. But importing the uuidsentinel global yields the same behavior. - We don't do the local import thing [3][4], because we're already in the right lib, so no issues with circular imports. - Locking is added to make this threadsafe. (See discussion at [5]) Note that there is some question as to whether it is more appropriate for this to live here or in oslotest [6]. This has been discussed on the dev ML [7] and it was concluded [8] that it should live here. [1]722d5b4772/nova/tests/uuidsentinel.py
[2]722d5b4772/nova/tests/uuidsentinel.py (L30)
[3]722d5b4772/nova/tests/uuidsentinel.py (L18-L19)
[4]722d5b4772/nova/tests/uuidsentinel.py (L26)
[5] http://eavesdrop.openstack.org/irclogs/%23openstack-oslo/%23openstack-oslo.2018-08-20.log.html#t2018-08-20T20:10:33 [6] https://review.openstack.org/#/c/594068/ [7] http://lists.openstack.org/pipermail/openstack-dev/2018-August/133670.html [8] http://lists.openstack.org/pipermail/openstack-dev/2018-August/133861.html Change-Id: I214ff21b461fa1ca4b83476e1d0a763efe986217
85 lines
3.2 KiB
Python
85 lines
3.2 KiB
Python
|
|
# Copyright 2015 OpenStack Foundation
|
|
# All Rights Reserved.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
# not use this file except in compliance with the License. You may obtain
|
|
# a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
# License for the specific language governing permissions and limitations
|
|
# under the License.
|
|
|
|
import datetime
|
|
|
|
from oslotest import base as test_base
|
|
import six
|
|
|
|
from oslo_utils import fixture
|
|
from oslo_utils.fixture import uuidsentinel as uuids
|
|
from oslo_utils import timeutils
|
|
from oslo_utils import uuidutils
|
|
|
|
|
|
class TimeFixtureTest(test_base.BaseTestCase):
|
|
|
|
def test_set_time_override_using_default(self):
|
|
# When the fixture is used with its default constructor, the
|
|
# override_time is set to the current timestamp.
|
|
# Also, when the fixture is cleaned up, the override_time is reset.
|
|
|
|
self.assertIsNone(timeutils.utcnow.override_time)
|
|
with fixture.TimeFixture():
|
|
self.assertIsNotNone(timeutils.utcnow.override_time)
|
|
self.assertIsNone(timeutils.utcnow.override_time)
|
|
|
|
def test_set_time_override(self):
|
|
# When the fixture is used to set a time, utcnow returns that time.
|
|
|
|
new_time = datetime.datetime(2015, 1, 2, 3, 4, 6, 7)
|
|
self.useFixture(fixture.TimeFixture(new_time))
|
|
self.assertEqual(new_time, timeutils.utcnow())
|
|
# Call again to make sure it keeps returning the same time.
|
|
self.assertEqual(new_time, timeutils.utcnow())
|
|
|
|
def test_advance_time_delta(self):
|
|
# advance_time_delta() advances the overridden time by some timedelta.
|
|
|
|
new_time = datetime.datetime(2015, 1, 2, 3, 4, 6, 7)
|
|
time_fixture = self.useFixture(fixture.TimeFixture(new_time))
|
|
time_fixture.advance_time_delta(datetime.timedelta(seconds=1))
|
|
expected_time = datetime.datetime(2015, 1, 2, 3, 4, 7, 7)
|
|
self.assertEqual(expected_time, timeutils.utcnow())
|
|
|
|
def test_advance_time_seconds(self):
|
|
# advance_time_seconds() advances the overridden time by some number of
|
|
# seconds.
|
|
|
|
new_time = datetime.datetime(2015, 1, 2, 3, 4, 6, 7)
|
|
time_fixture = self.useFixture(fixture.TimeFixture(new_time))
|
|
time_fixture.advance_time_seconds(2)
|
|
expected_time = datetime.datetime(2015, 1, 2, 3, 4, 8, 7)
|
|
self.assertEqual(expected_time, timeutils.utcnow())
|
|
|
|
|
|
class UUIDSentinelsTest(test_base.BaseTestCase):
|
|
|
|
def test_different_sentinel(self):
|
|
uuid1 = uuids.foobar
|
|
uuid2 = uuids.barfoo
|
|
self.assertNotEqual(uuid1, uuid2)
|
|
|
|
def test_returns_uuid(self):
|
|
self.assertTrue(uuidutils.is_uuid_like(uuids.foo))
|
|
|
|
def test_returns_string(self):
|
|
self.assertIsInstance(uuids.foo, str)
|
|
|
|
def test_with_underline_prefix(self):
|
|
ex = self.assertRaises(ValueError, getattr, uuids, '_foo')
|
|
self.assertIn("Sentinels must not start with _", six.text_type(ex))
|