salt/tests/unit/utils/event_test.py

273 lines
9.2 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
'''
:codeauthor: :email:`Pedro Algarvio (pedro@algarvio.me)`
:copyright: © 2012-2013 by the SaltStack Team, see AUTHORS for more details
:license: Apache 2.0, see LICENSE for more details.
tests.unit.utils.event_test
~~~~~~~~~~~~~~~~~~~~~~~~~~~
'''
# Import python libs
import os
import hashlib
2013-12-02 22:26:13 +00:00
import time
from contextlib import contextmanager
from multiprocessing import Process
# Import Salt Testing libs
2013-12-03 01:22:51 +00:00
from salttesting import expectedFailure
from salttesting import TestCase
from salttesting.helpers import ensure_in_syspath
ensure_in_syspath('../../')
# Import salt libs
import integration
from salt.utils import event
SOCK_DIR = os.path.join(integration.TMP, 'test-socks')
2013-12-03 20:30:16 +00:00
2013-12-02 22:26:13 +00:00
@contextmanager
def eventpublisher_process():
2013-12-03 20:30:16 +00:00
proc = event.EventPublisher({'sock_dir': SOCK_DIR})
2013-12-02 22:26:13 +00:00
proc.start()
try:
if os.environ.get('TRAVIS_PYTHON_VERSION', None) is not None:
# Travis is slow
time.sleep(10)
else:
time.sleep(2)
2013-12-02 22:26:13 +00:00
yield
finally:
proc.terminate()
proc.join()
2013-12-03 20:30:16 +00:00
class EventSender(Process):
def __init__(self, data, tag, wait):
super(EventSender, self).__init__()
self.data = data
self.tag = tag
self.wait = wait
def run(self):
me = event.MasterEvent(sock_dir=SOCK_DIR)
time.sleep(self.wait)
me.fire_event(self.data, self.tag)
# Wait a few seconds before tearing down the zmq context
if os.environ.get('TRAVIS_PYTHON_VERSION', None) is not None:
# Travis is slow
time.sleep(10)
else:
time.sleep(2)
2013-12-03 20:30:16 +00:00
@contextmanager
def eventsender_process(data, tag, wait=0):
proc = EventSender(data, tag, wait)
proc.start()
try:
yield
finally:
proc.terminate()
proc.join()
class TestSaltEvent(TestCase):
2013-12-03 01:22:51 +00:00
def setUp(self):
if not os.path.exists(SOCK_DIR):
os.makedirs(SOCK_DIR)
2013-12-02 22:26:13 +00:00
def assertGotEvent(self, evt, data, msg=None):
self.assertIsNotNone(evt, msg)
for k, v in data.items():
self.assertIn(k, evt, msg)
self.assertEqual(data[k], evt[k], msg)
def test_master_event(self):
me = event.MasterEvent(SOCK_DIR)
self.assertEqual(
me.puburi, 'ipc://{0}'.format(
os.path.join(SOCK_DIR, 'master_event_pub.ipc')
)
)
self.assertEqual(
me.pulluri,
'ipc://{0}'.format(
os.path.join(SOCK_DIR, 'master_event_pull.ipc')
)
)
def test_minion_event(self):
opts = dict(id='foo', sock_dir=SOCK_DIR)
id_hash = hashlib.md5(opts['id']).hexdigest()
me = event.MinionEvent(**opts)
self.assertEqual(
me.puburi,
'ipc://{0}'.format(
os.path.join(
SOCK_DIR, 'minion_event_{0}_pub.ipc'.format(id_hash)
)
)
)
self.assertEqual(
me.pulluri,
'ipc://{0}'.format(
os.path.join(
SOCK_DIR, 'minion_event_{0}_pull.ipc'.format(id_hash)
)
)
)
def test_minion_event_tcp_ipc_mode(self):
opts = dict(id='foo', ipc_mode='tcp')
me = event.MinionEvent(**opts)
self.assertEqual(me.puburi, 'tcp://127.0.0.1:4510')
self.assertEqual(me.pulluri, 'tcp://127.0.0.1:4511')
def test_minion_event_no_id(self):
me = event.MinionEvent(sock_dir=SOCK_DIR)
id_hash = hashlib.md5('').hexdigest()
self.assertEqual(
me.puburi,
'ipc://{0}'.format(
os.path.join(
SOCK_DIR, 'minion_event_{0}_pub.ipc'.format(id_hash)
)
)
)
self.assertEqual(
me.pulluri,
'ipc://{0}'.format(
os.path.join(
SOCK_DIR, 'minion_event_{0}_pull.ipc'.format(id_hash)
)
)
)
2013-12-02 22:26:13 +00:00
def test_event_subscription(self):
2013-12-03 01:22:51 +00:00
'''Test a single event is received'''
with eventpublisher_process():
2013-12-02 22:26:13 +00:00
me = event.MasterEvent(sock_dir=SOCK_DIR)
2013-12-03 01:22:51 +00:00
me.subscribe()
2013-12-03 20:30:16 +00:00
me.fire_event({'data': 'foo1'}, 'evt1')
2013-12-02 22:26:13 +00:00
evt1 = me.get_event(tag='evt1')
2013-12-03 20:30:16 +00:00
self.assertGotEvent(evt1, {'data': 'foo1'})
2013-12-02 22:26:13 +00:00
2013-12-03 22:44:52 +00:00
def test_event_timeout(self):
'''Test no event is received if the timeout is reached'''
with eventpublisher_process():
me = event.MasterEvent(sock_dir=SOCK_DIR)
me.subscribe()
2013-12-06 17:50:24 +00:00
me.fire_event({'data': 'foo1'}, 'evt1')
2013-12-03 22:44:52 +00:00
evt1 = me.get_event(tag='evt1')
2013-12-06 17:50:24 +00:00
self.assertGotEvent(evt1, {'data': 'foo1'})
2013-12-03 22:44:52 +00:00
evt2 = me.get_event(tag='evt1')
self.assertIsNone(evt2)
2013-12-03 01:22:51 +00:00
def test_event_subscription_matching(self):
'''Test a subscription startswith matching'''
with eventpublisher_process():
me = event.MasterEvent(sock_dir=SOCK_DIR)
me.subscribe()
2013-12-03 20:30:16 +00:00
me.fire_event({'data': 'foo1'}, 'evt1')
2013-12-03 01:22:51 +00:00
evt1 = me.get_event(tag='evt1')
2013-12-03 20:30:16 +00:00
self.assertGotEvent(evt1, {'data': 'foo1'})
2013-12-03 01:22:51 +00:00
def test_event_subscription_matching_all(self):
'''Test a subscription matching'''
with eventpublisher_process():
me = event.MasterEvent(sock_dir=SOCK_DIR)
me.subscribe()
2013-12-03 20:30:16 +00:00
me.fire_event({'data': 'foo1'}, 'evt1')
2013-12-03 01:22:51 +00:00
evt1 = me.get_event(tag='')
2013-12-03 20:30:16 +00:00
self.assertGotEvent(evt1, {'data': 'foo1'})
2013-12-03 01:22:51 +00:00
def test_event_not_subscribed(self):
'''Test get event ignores non-subscribed events'''
with eventpublisher_process():
me = event.MasterEvent(sock_dir=SOCK_DIR)
2013-12-03 01:22:51 +00:00
me.subscribe()
2013-12-03 20:30:16 +00:00
with eventsender_process({'data': 'foo1'}, 'evt1', 5):
me.fire_event({'data': 'foo1'}, 'evt2')
evt1 = me.get_event(tag='evt1', wait=10)
2013-12-03 20:30:16 +00:00
self.assertGotEvent(evt1, {'data': 'foo1'})
def test_event_multiple_subscriptions(self):
'''Test multiple subscriptions do not interfere'''
with eventpublisher_process():
2013-12-02 22:26:13 +00:00
me = event.MasterEvent(sock_dir=SOCK_DIR)
2013-12-03 01:22:51 +00:00
me.subscribe()
2013-12-03 20:30:16 +00:00
with eventsender_process({'data': 'foo1'}, 'evt1', 5):
me.fire_event({'data': 'foo1'}, 'evt2')
evt1 = me.get_event(tag='evt1', wait=10)
2013-12-03 20:30:16 +00:00
self.assertGotEvent(evt1, {'data': 'foo1'})
def test_event_multiple_clients(self):
2013-12-03 01:45:18 +00:00
'''Test event is received by multiple clients'''
with eventpublisher_process():
me1 = event.MasterEvent(sock_dir=SOCK_DIR)
2013-12-03 01:22:51 +00:00
me1.subscribe()
me2 = event.MasterEvent(sock_dir=SOCK_DIR)
2013-12-03 01:22:51 +00:00
me2.subscribe()
2013-12-03 20:30:16 +00:00
me1.fire_event({'data': 'foo1'}, 'evt1')
evt1 = me1.get_event(tag='evt1')
2013-12-03 20:30:16 +00:00
self.assertGotEvent(evt1, {'data': 'foo1'})
evt2 = me2.get_event(tag='evt1')
2013-12-03 20:30:16 +00:00
self.assertGotEvent(evt2, {'data': 'foo1'})
def test_event_nested_subs(self):
'''Test nested event subscriptions do not drop events, issue #8580'''
with eventpublisher_process():
me = event.MasterEvent(sock_dir=SOCK_DIR)
2013-12-03 01:22:51 +00:00
me.subscribe()
2013-12-03 20:30:16 +00:00
me.fire_event({'data': 'foo1'}, 'evt1')
2013-12-02 22:26:13 +00:00
me.fire_event({'data': 'foo2'}, 'evt2')
evt2 = me.get_event(tag='evt2')
evt1 = me.get_event(tag='evt1')
2013-12-03 20:30:16 +00:00
self.assertGotEvent(evt2, {'data': 'foo2'})
self.assertGotEvent(evt1, {'data': 'foo1'})
2013-12-02 22:26:13 +00:00
2013-12-03 01:22:51 +00:00
@expectedFailure
def test_event_nested_sub_all(self):
'''Test nested event subscriptions do not drop events, get event for all tags'''
# Show why not to call get_event(tag='')
with eventpublisher_process():
me = event.MasterEvent(sock_dir=SOCK_DIR)
me.subscribe()
2013-12-03 20:30:16 +00:00
me.fire_event({'data': 'foo1'}, 'evt1')
2013-12-03 01:22:51 +00:00
me.fire_event({'data': 'foo2'}, 'evt2')
evt2 = me.get_event(tag='')
evt1 = me.get_event(tag='')
2013-12-03 20:30:16 +00:00
self.assertGotEvent(evt2, {'data': 'foo2'})
self.assertGotEvent(evt1, {'data': 'foo1'})
2013-12-03 01:22:51 +00:00
def test_event_many(self):
2013-12-02 22:26:13 +00:00
'''Test a large number of events, one at a time'''
with eventpublisher_process():
2013-12-02 22:26:13 +00:00
me = event.MasterEvent(sock_dir=SOCK_DIR)
2013-12-03 01:22:51 +00:00
me.subscribe()
2013-12-02 22:26:13 +00:00
for i in xrange(500):
2013-12-03 20:30:16 +00:00
me.fire_event({'data': '{0}'.format(i)}, 'testevents')
2013-12-02 22:26:13 +00:00
evt = me.get_event(tag='testevents')
2013-12-03 20:30:16 +00:00
self.assertGotEvent(evt, {'data': '{0}'.format(i)}, 'Event {0}'.format(i))
2013-12-02 22:26:13 +00:00
def test_event_many_backlog(self):
2013-12-02 22:26:13 +00:00
'''Test a large number of events, send all then recv all'''
with eventpublisher_process():
2013-12-02 22:26:13 +00:00
me = event.MasterEvent(sock_dir=SOCK_DIR)
2013-12-03 01:22:51 +00:00
me.subscribe()
# Must not exceed zmq HWM
2013-12-02 22:26:13 +00:00
for i in xrange(500):
2013-12-03 20:30:16 +00:00
me.fire_event({'data': '{0}'.format(i)}, 'testevents')
2013-12-02 22:26:13 +00:00
for i in xrange(500):
evt = me.get_event(tag='testevents')
2013-12-03 20:30:16 +00:00
self.assertGotEvent(evt, {'data': '{0}'.format(i)}, 'Event {0}'.format(i))
2013-12-02 22:26:13 +00:00
if __name__ == '__main__':
from integration import run_tests
run_tests(TestSaltEvent, needs_daemon=False)