From d8c3a6d2655a4ccc64ebf46a856319e2221a9072 Mon Sep 17 00:00:00 2001 From: "Daniel P. Berrange" Date: Tue, 12 Feb 2013 12:21:06 +0000 Subject: Add basic infrastructure for compute driver async events This introduces the basic infrastructure to allow compute drivers to emit asychronous events for processing by the virt API. Initially the only event defined is a "lifecycle" event which allows the virt API to immediately detect the start and stop of virtual domain instances, without needing to frequently poll the compute API. The base compute driver gains a method enabling the manage to register a callback to receive events, and another method to allow driver subclasses to emit events. Blueprint: compute-driver-events Change-Id: Ic9abcd3f829c106e840538a01376862ab5b3485b Signed-off-by: Daniel P. Berrange --- nova/tests/test_virt_drivers.py | 67 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) (limited to 'nova/tests') diff --git a/nova/tests/test_virt_drivers.py b/nova/tests/test_virt_drivers.py index a94fdc3c5..b0ce14750 100644 --- a/nova/tests/test_virt_drivers.py +++ b/nova/tests/test_virt_drivers.py @@ -28,6 +28,7 @@ from nova import test from nova.tests import fake_libvirt_utils from nova.tests.image import fake as fake_image from nova.tests import utils as test_utils +from nova.virt import event as virtevent from nova.virt import fake LOG = logging.getLogger(__name__) @@ -544,6 +545,72 @@ class _VirtDriverTestCase(_FakeDriverBackendTestCase): def test_remove_from_aggregate(self): self.connection.remove_from_aggregate(self.ctxt, 'aggregate', 'host') + def test_events(self): + got_events = [] + + def handler(event): + got_events.append(event) + + self.connection.register_event_listener(handler) + + event1 = virtevent.LifecycleEvent( + "cef19ce0-0ca2-11df-855d-b19fbce37686", + virtevent.EVENT_LIFECYCLE_STARTED) + event2 = virtevent.LifecycleEvent( + "cef19ce0-0ca2-11df-855d-b19fbce37686", + virtevent.EVENT_LIFECYCLE_PAUSED) + + self.connection.emit_event(event1) + self.connection.emit_event(event2) + want_events = [event1, event2] + self.assertEqual(want_events, got_events) + + event3 = virtevent.LifecycleEvent( + "cef19ce0-0ca2-11df-855d-b19fbce37686", + virtevent.EVENT_LIFECYCLE_RESUMED) + event4 = virtevent.LifecycleEvent( + "cef19ce0-0ca2-11df-855d-b19fbce37686", + virtevent.EVENT_LIFECYCLE_STOPPED) + + self.connection.emit_event(event3) + self.connection.emit_event(event4) + + want_events = [event1, event2, event3, event4] + self.assertEqual(want_events, got_events) + + def test_event_bad_object(self): + # Passing in something which does not inherit + # from virtevent.Event + + def handler(event): + pass + + self.connection.register_event_listener(handler) + + badevent = { + "foo": "bar" + } + + self.assertRaises(ValueError, + self.connection.emit_event, + badevent) + + def test_event_bad_callback(self): + # Check that if a callback raises an exception, + # it does not propagate back out of the + # 'emit_event' call + + def handler(event): + raise Exception("Hit Me!") + + self.connection.register_event_listener(handler) + + event1 = virtevent.LifecycleEvent( + "cef19ce0-0ca2-11df-855d-b19fbce37686", + virtevent.EVENT_LIFECYCLE_STARTED) + + self.connection.emit_event(event1) + class AbstractDriverTestCase(_VirtDriverTestCase, test.TestCase): def setUp(self): -- cgit