def test_event_lifecycle(self): got_events = [] # Validate that libvirt events are correctly translated # to Nova events def handler(event): got_events.append(event) hostimpl = host.Host("qemu:///system", lifecycle_event_handler=handler) conn = hostimpl.get_connection() hostimpl._init_events_pipe() fake_dom_xml = """ <domain type='kvm'> <uuid>cef19ce0-0ca2-11df-855d-b19fbce37686</uuid> <devices> <disk type='file'> <source file='filename'/> </disk> </devices> </domain> """ dom = fakelibvirt.Domain(conn, fake_dom_xml, False) hostimpl._event_lifecycle_callback( conn, dom, fakelibvirt.VIR_DOMAIN_EVENT_STOPPED, 0, hostimpl) hostimpl._dispatch_events() self.assertEqual(len(got_events), 1) self.assertIsInstance(got_events[0], event.LifecycleEvent) self.assertEqual(got_events[0].uuid, "cef19ce0-0ca2-11df-855d-b19fbce37686") self.assertEqual(got_events[0].transition, event.EVENT_LIFECYCLE_STOPPED)
def test_event_dispatch(self): # Validate that the libvirt self-pipe for forwarding # events between threads is working sanely def handler(event): got_events.append(event) hostimpl = host.Host("qemu:///system", lifecycle_event_handler=handler) got_events = [] hostimpl._init_events_pipe() event1 = event.LifecycleEvent("cef19ce0-0ca2-11df-855d-b19fbce37686", event.EVENT_LIFECYCLE_STARTED) event2 = event.LifecycleEvent("cef19ce0-0ca2-11df-855d-b19fbce37686", event.EVENT_LIFECYCLE_PAUSED) hostimpl._queue_event(event1) hostimpl._queue_event(event2) hostimpl._dispatch_events() want_events = [event1, event2] self.assertEqual(want_events, got_events) event3 = event.LifecycleEvent("cef19ce0-0ca2-11df-855d-b19fbce37686", event.EVENT_LIFECYCLE_RESUMED) event4 = event.LifecycleEvent("cef19ce0-0ca2-11df-855d-b19fbce37686", event.EVENT_LIFECYCLE_STOPPED) hostimpl._queue_event(event3) hostimpl._queue_event(event4) hostimpl._dispatch_events() want_events = [event1, event2, event3, event4] self.assertEqual(want_events, got_events)
def setUp(self): super(IptablesFirewallTestCase, self).setUp() self.useFixture(lock_fixture.ExternalLockFixture()) self.useFixture(fakelibvirt.FakeLibvirtFixture()) self.fw = firewall.IptablesFirewallDriver( FakeVirtAPI(), host=host.Host("qemu:///system"))
def setUp(self): super(NWFilterTestCase, self).setUp() self.useFixture(fakelibvirt.FakeLibvirtFixture()) self.fw = firewall.NWFilterFirewall( FakeVirtAPI(), host=host.Host("qemu:///system"))
def test_event_delayed_cleanup(self): hostimpl = host.Host("xen:///", lifecycle_event_handler=lambda e: None) uuid = "cef19ce0-0ca2-11df-855d-b19fbce37686" ev = event.LifecycleEvent(uuid, event.EVENT_LIFECYCLE_STARTED) gt_mock = mock.Mock() hostimpl._events_delayed[uuid] = gt_mock hostimpl._event_emit_delayed(ev) gt_mock.cancel.assert_called_once_with() self.assertNotIn(uuid, hostimpl._events_delayed.keys())
def test_event_emit_delayed_call_delayed_pending(self, spawn_after_mock): hostimpl = host.Host("xen:///", lifecycle_event_handler=lambda e: None) uuid = "cef19ce0-0ca2-11df-855d-b19fbce37686" gt_mock = mock.Mock() hostimpl._events_delayed[uuid] = gt_mock ev = event.LifecycleEvent(uuid, event.EVENT_LIFECYCLE_STOPPED) hostimpl._event_emit_delayed(ev) gt_mock.cancel.assert_called_once_with() self.assertTrue(spawn_after_mock.called)
def test_event_emit_delayed_call_now(self): got_events = [] def handler(event): got_events.append(event) hostimpl = host.Host("qemu:///system", lifecycle_event_handler=handler) ev = event.LifecycleEvent("cef19ce0-0ca2-11df-855d-b19fbce37686", event.EVENT_LIFECYCLE_STOPPED) hostimpl._event_emit_delayed(ev) self.assertEqual(1, len(got_events)) self.assertEqual(ev, got_events[0])
def test_get_domain_info(self, mock_has_min_version): test_host = host.Host("qemu:///system") domain = mock.MagicMock() expected = [power_state.RUNNING, 512, 512, None, None] race = fakelibvirt.make_libvirtError( fakelibvirt.libvirtError, 'ERR', error_code=fakelibvirt.VIR_ERR_OPERATION_FAILED, error_message='cannot read cputime for domain') mock_has_min_version.return_value = True domain.info.return_value = expected actual = compat.get_domain_info(fakelibvirt, test_host, domain) self.assertEqual(actual, expected) self.assertEqual(domain.info.call_count, 1) domain.info.reset_mock() domain.info.side_effect = race self.assertRaises(fakelibvirt.libvirtError, compat.get_domain_info, fakelibvirt, test_host, domain) self.assertEqual(domain.info.call_count, 1) domain.info.reset_mock() mock_has_min_version.return_value = False domain.info.side_effect = [race, expected] actual = compat.get_domain_info(fakelibvirt, test_host, domain) self.assertEqual(actual, expected) self.assertEqual(domain.info.call_count, 2) domain.info.reset_mock() domain.info.side_effect = race self.assertRaises(fakelibvirt.libvirtError, compat.get_domain_info, fakelibvirt, test_host, domain) self.assertEqual(domain.info.call_count, 2)
def setUp(self): super(HostTestCase, self).setUp() self.useFixture(fakelibvirt.FakeLibvirtFixture()) self.host = host.Host("qemu:///system")
def test_event_emit_delayed_call_delayed(self, spawn_after_mock): hostimpl = host.Host("xen:///", lifecycle_event_handler=lambda e: None) ev = event.LifecycleEvent("cef19ce0-0ca2-11df-855d-b19fbce37686", event.EVENT_LIFECYCLE_STOPPED) hostimpl._event_emit_delayed(ev) spawn_after_mock.assert_called_once_with(15, hostimpl._event_emit, ev)