summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/unit/rpc/common.py36
-rw-r--r--tests/unit/test_lockutils.py80
-rw-r--r--tests/unit/test_notifier.py19
-rw-r--r--tests/unit/test_service.py54
-rw-r--r--tests/unit/test_timeutils.py55
5 files changed, 163 insertions, 81 deletions
diff --git a/tests/unit/rpc/common.py b/tests/unit/rpc/common.py
index 70a1ad0..9838c6f 100644
--- a/tests/unit/rpc/common.py
+++ b/tests/unit/rpc/common.py
@@ -29,6 +29,7 @@ import nose
from openstack.common import cfg
from openstack.common import exception
from openstack.common.gettextutils import _
+from openstack.common import jsonutils
from openstack.common.rpc import amqp as rpc_amqp
from openstack.common.rpc import common as rpc_common
from openstack.common.rpc import dispatcher as rpc_dispatcher
@@ -276,6 +277,41 @@ class BaseRpcAMQPTestCase(BaseRpcTestCase):
"args": {"value": value}})
self.assertEqual(value, result)
+ def test_notification_envelope(self):
+ raw_msg = {'a': 'b'}
+ self.test_msg = None
+
+ def fake_notify_send(_conn, topic, msg):
+ self.test_msg = msg
+
+ self.stubs.Set(self.rpc.Connection, 'notify_send', fake_notify_send)
+
+ self.rpc.notify(FLAGS, self.context, 'notifications.info', raw_msg,
+ envelope=False)
+ self.assertEqual(self.test_msg, raw_msg)
+
+ # Envelopes enabled, but not enabled for notifications
+ self.stubs.Set(rpc_common, '_SEND_RPC_ENVELOPE', True)
+ self.rpc.notify(FLAGS, self.context, 'notifications.info', raw_msg,
+ envelope=False)
+ self.assertEqual(self.test_msg, raw_msg)
+
+ # Now turn it on for notifications
+ msg = {
+ 'oslo.version': rpc_common._RPC_ENVELOPE_VERSION,
+ 'oslo.message': jsonutils.dumps(raw_msg),
+ }
+ self.rpc.notify(FLAGS, self.context, 'notifications.info', raw_msg,
+ envelope=True)
+ self.assertEqual(self.test_msg, msg)
+
+ # Make sure envelopes are still on notifications, even if turned off
+ # for general messages.
+ self.stubs.Set(rpc_common, '_SEND_RPC_ENVELOPE', False)
+ self.rpc.notify(FLAGS, self.context, 'notifications.info', raw_msg,
+ envelope=True)
+ self.assertEqual(self.test_msg, msg)
+
class TestReceiver(object):
"""Simple Proxy class so the consumer has methods to call.
diff --git a/tests/unit/test_lockutils.py b/tests/unit/test_lockutils.py
index 5f50217..57271a3 100644
--- a/tests/unit/test_lockutils.py
+++ b/tests/unit/test_lockutils.py
@@ -15,6 +15,7 @@
# under the License.
import errno
+import fcntl
import os
import select
import shutil
@@ -27,6 +28,7 @@ from eventlet import greenthread
from eventlet import greenpool
from openstack.common import lockutils
+from openstack.common import testutils
from tests import utils as test_utils
@@ -132,34 +134,50 @@ class LockTestCase(test_utils.BaseTestCase):
"""We can lock across multiple processes"""
tempdir = tempfile.mkdtemp()
self.config(lock_path=tempdir)
- rpipe1, wpipe1 = os.pipe()
- rpipe2, wpipe2 = os.pipe()
-
- @lockutils.synchronized('testlock1', 'test-', external=True)
- def f(rpipe, wpipe):
- try:
- os.write(wpipe, "foo")
- except OSError, e:
- self.assertEquals(e.errno, errno.EPIPE)
- return
-
- rfds, _wfds, _efds = select.select([rpipe], [], [], 1)
- self.assertEquals(len(rfds), 0, "The other process, which was "
- "supposed to be locked, "
- "wrote on its end of the "
- "pipe")
- os.close(rpipe)
-
- pid = os.fork()
- if pid > 0:
- os.close(wpipe1)
- os.close(rpipe2)
-
- f(rpipe1, wpipe2)
- else:
- os.close(rpipe1)
- os.close(wpipe2)
-
- time.sleep(0.1)
- f(rpipe2, wpipe1)
- os._exit(0)
+
+ @lockutils.synchronized('external', 'test-', external=True)
+ def lock_files(tempdir):
+ if not os.path.exists(tempdir):
+ os.makedirs(tempdir)
+
+ # Open some files we can use for locking
+ handles = []
+ for n in range(50):
+ path = os.path.join(tempdir, ('file-%s' % n))
+ handles.append(open(path, 'w'))
+
+ # Loop over all the handles and try locking the file
+ # without blocking, keep a count of how many files we
+ # were able to lock and then unlock. If the lock fails
+ # we get an IOError and bail out with bad exit code
+ count = 0
+ for handle in handles:
+ try:
+ fcntl.flock(handle, fcntl.LOCK_EX | fcntl.LOCK_NB)
+ count += 1
+ fcntl.flock(handle, fcntl.LOCK_UN)
+ except IOError:
+ os._exit(2)
+ finally:
+ handle.close()
+
+ # Check if we were able to open all files
+ self.assertEqual(50, count)
+
+ try:
+ children = []
+ for n in range(50):
+ pid = os.fork()
+ if pid:
+ children.append(pid)
+ else:
+ lock_files(tempdir)
+ os._exit(0)
+
+ for i, child in enumerate(children):
+ (pid, status) = os.waitpid(child, 0)
+ if pid:
+ self.assertEqual(0, status)
+ finally:
+ if os.path.exists(tempdir):
+ shutil.rmtree(tempdir, ignore_errors=True)
diff --git a/tests/unit/test_notifier.py b/tests/unit/test_notifier.py
index 1cabae0..be58da2 100644
--- a/tests/unit/test_notifier.py
+++ b/tests/unit/test_notifier.py
@@ -75,19 +75,30 @@ class NotifierTestCase(test_utils.BaseTestCase):
notifier_api.notify(ctxt, 'publisher_id', 'event_type',
notifier_api.WARN, dict(a=3))
- def test_send_rabbit_notification(self):
- self.stubs.Set(cfg.CONF, 'notification_driver',
- ['openstack.common.notifier.rabbit_notifier'])
+ def _test_rpc_notify(self, driver, envelope=False):
+ self.stubs.Set(cfg.CONF, 'notification_driver', [driver])
self.mock_notify = False
+ self.envelope = False
- def mock_notify(cls, *args):
+ def mock_notify(cls, *args, **kwargs):
self.mock_notify = True
+ self.envelope = kwargs.get('envelope', False)
self.stubs.Set(rpc, 'notify', mock_notify)
notifier_api.notify(ctxt, 'publisher_id', 'event_type',
notifier_api.WARN, dict(a=3))
self.assertEqual(self.mock_notify, True)
+ self.assertEqual(self.envelope, envelope)
+
+ def test_rabbit_notifier(self):
+ self._test_rpc_notify('openstack.common.notifier.rabbit_notifier')
+
+ def test_rpc_notifier(self):
+ self._test_rpc_notify('openstack.common.notifier.rpc_notifier')
+
+ def test_rpc_notifier2(self):
+ self._test_rpc_notify('openstack.common.notifier.rpc_notifier2', True)
def test_invalid_priority(self):
self.assertRaises(notifier_api.BadPriorityException,
diff --git a/tests/unit/test_service.py b/tests/unit/test_service.py
index 59f3145..c7455e0 100644
--- a/tests/unit/test_service.py
+++ b/tests/unit/test_service.py
@@ -97,18 +97,24 @@ class ServiceLauncherTest(utils.BaseTestCase):
self.pid = pid
- # Wait for up to a second for workers to get started
- start = time.time()
- while time.time() - start < 1:
- workers = self._get_workers()
- if len(workers) == self.workers:
- break
-
- time.sleep(.1)
+ # Wait at most 10 seconds to spawn workers
+ cond = lambda: self.workers == len(self._get_workers())
+ timeout = 10
+ self._wait(cond, timeout)
+ workers = self._get_workers()
self.assertEqual(len(workers), self.workers)
return workers
+ def _wait(self, cond, timeout):
+ start = time.time()
+ while True:
+ if cond():
+ break
+ if time.time() - start > timeout:
+ break
+ time.sleep(.1)
+
def setUp(self):
super(ServiceLauncherTest, self).setUp()
# FIXME(markmc): Ugly hack to workaround bug #1073732
@@ -149,18 +155,14 @@ class ServiceLauncherTest(utils.BaseTestCase):
LOG.info('pid of first child is %s' % start_workers[0])
os.kill(start_workers[0], signal.SIGTERM)
- # loop and check if new worker is spawned (for 1 second max)
- start = time.time()
- while time.time() - start < 1:
- end_workers = self._get_workers()
- LOG.info('workers: %r' % end_workers)
-
- if start_workers != end_workers:
- break
-
- time.sleep(.1)
+ # Wait at most 5 seconds to respawn a worker
+ cond = lambda: start_workers != self._get_workers()
+ timeout = 5
+ self._wait(cond, timeout)
# Make sure worker pids don't match
+ end_workers = self._get_workers()
+ LOG.info('workers: %r' % end_workers)
self.assertNotEqual(start_workers, end_workers)
def _terminate_with_signal(self, sig):
@@ -168,17 +170,13 @@ class ServiceLauncherTest(utils.BaseTestCase):
os.kill(self.pid, sig)
- # loop and check if all processes are killed (for 1 second max)
- start = time.time()
- while time.time() - start < 1:
- workers = self._get_workers()
- LOG.info('workers: %r' % workers)
-
- if not workers:
- break
-
- time.sleep(.1)
+ # Wait at most 5 seconds to kill all workers
+ cond = lambda: not self._get_workers()
+ timeout = 5
+ self._wait(cond, timeout)
+ workers = self._get_workers()
+ LOG.info('workers: %r' % workers)
self.assertFalse(workers, 'No OS processes left.')
def test_terminate_sigkill(self):
diff --git a/tests/unit/test_timeutils.py b/tests/unit/test_timeutils.py
index 8236032..1407f29 100644
--- a/tests/unit/test_timeutils.py
+++ b/tests/unit/test_timeutils.py
@@ -27,14 +27,10 @@ from openstack.common import timeutils
class TimeUtilsTest(unittest.TestCase):
def setUp(self):
- utc_timezone = iso8601.iso8601.UTC
self.skynet_self_aware_time_str = '1997-08-29T06:14:00Z'
- self.skynet_self_aware_time = datetime.datetime(1997, 8, 29, 6, 14, 0,
- tzinfo=utc_timezone)
- self.one_minute_before = datetime.datetime(1997, 8, 29, 6, 13, 0,
- tzinfo=iso8601.iso8601.UTC)
- self.one_minute_after = datetime.datetime(1997, 8, 29, 6, 15, 0,
- tzinfo=iso8601.iso8601.UTC)
+ self.skynet_self_aware_time = datetime.datetime(1997, 8, 29, 6, 14, 0)
+ self.one_minute_before = datetime.datetime(1997, 8, 29, 6, 13, 0)
+ self.one_minute_after = datetime.datetime(1997, 8, 29, 6, 15, 0)
self.skynet_self_aware_time_perfect_str = '1997-08-29T06:14:00.000000'
self.skynet_self_aware_time_perfect = datetime.datetime(1997, 8, 29,
6, 14, 0)
@@ -50,7 +46,9 @@ class TimeUtilsTest(unittest.TestCase):
def test_parse_isotime(self):
expect = timeutils.parse_isotime(self.skynet_self_aware_time_str)
- self.assertEqual(self.skynet_self_aware_time, expect)
+ skynet_self_aware_time_utc = self.skynet_self_aware_time.replace(
+ tzinfo=iso8601.iso8601.UTC)
+ self.assertEqual(skynet_self_aware_time_utc, expect)
def test_strtime(self):
expect = timeutils.strtime(self.skynet_self_aware_time_perfect)
@@ -67,31 +65,52 @@ class TimeUtilsTest(unittest.TestCase):
t = timeutils.parse_strtime(s)
self.assertEqual(orig_t, t)
- def test_is_older_than(self):
+ def _test_is_older_than(self, fn):
+ strptime = datetime.datetime.strptime
with mock.patch('datetime.datetime') as datetime_mock:
datetime_mock.utcnow.return_value = self.skynet_self_aware_time
- expect_true = timeutils.is_older_than(self.one_minute_before, 59)
+ datetime_mock.strptime = strptime
+ expect_true = timeutils.is_older_than(fn(self.one_minute_before),
+ 59)
self.assertTrue(expect_true)
- expect_false = timeutils.is_older_than(self.one_minute_before, 60)
+ expect_false = timeutils.is_older_than(fn(self.one_minute_before),
+ 60)
self.assertFalse(expect_false)
- expect_false = timeutils.is_older_than(self.one_minute_before, 61)
+ expect_false = timeutils.is_older_than(fn(self.one_minute_before),
+ 61)
self.assertFalse(expect_false)
- def test_is_newer_than(self):
+ def test_is_older_than_datetime(self):
+ self._test_is_older_than(lambda x: x)
+
+ def test_is_older_than_str(self):
+ self._test_is_older_than(timeutils.strtime)
+
+ def _test_is_newer_than(self, fn):
+ strptime = datetime.datetime.strptime
with mock.patch('datetime.datetime') as datetime_mock:
datetime_mock.utcnow.return_value = self.skynet_self_aware_time
- expect_true = timeutils.is_newer_than(self.one_minute_after, 59)
+ datetime_mock.strptime = strptime
+ expect_true = timeutils.is_newer_than(fn(self.one_minute_after),
+ 59)
self.assertTrue(expect_true)
- expect_false = timeutils.is_newer_than(self.one_minute_after, 60)
+ expect_false = timeutils.is_newer_than(fn(self.one_minute_after),
+ 60)
self.assertFalse(expect_false)
- expect_false = timeutils.is_newer_than(self.one_minute_after, 61)
+ expect_false = timeutils.is_newer_than(fn(self.one_minute_after),
+ 61)
self.assertFalse(expect_false)
+ def test_is_newer_than_datetime(self):
+ self._test_is_newer_than(lambda x: x)
+
+ def test_is_newer_than_str(self):
+ self._test_is_newer_than(timeutils.strtime)
+
def test_utcnow_ts(self):
skynet_self_aware_timestamp = 872835240
dt = datetime.datetime.utcfromtimestamp(skynet_self_aware_timestamp)
- expect = dt.replace(tzinfo=iso8601.iso8601.UTC)
- self.assertEqual(self.skynet_self_aware_time, expect)
+ self.assertEqual(self.skynet_self_aware_time, dt)
with mock.patch('datetime.datetime') as datetime_mock:
datetime_mock.utcnow.return_value = self.skynet_self_aware_time
ts = timeutils.utcnow_ts()