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 setUp(self): super(GuestTestCase, self).setUp() self.useFixture(fakelibvirt.FakeLibvirtFixture()) self.host = host.Host("qemu:///system") self.context = context.get_admin_context() self.domain = mock.Mock(spec=fakelibvirt.virDomain) self.guest = libvirt_guest.Guest(self.domain)
def setUp(self): super(MigrationMonitorTestCase, self).setUp() self.useFixture(fakelibvirt.FakeLibvirtFixture()) flavor = objects.Flavor(memory_mb=2048, swap=0, vcpu_weight=None, root_gb=1, id=2, name=u'm1.small', ephemeral_gb=0, rxtx_factor=1.0, flavorid=u'1', vcpus=1, extra_specs={}) instance = { 'id': 1, 'uuid': '32dfcb37-5af1-552b-357c-be8c3aa38310', 'memory_kb': '1024000', 'basepath': '/some/path', 'bridge_name': 'br100', 'display_name': "Acme webserver", 'vcpus': 2, 'project_id': 'fake', 'bridge': 'br101', 'image_ref': '155d900f-4e14-4e4c-a73d-069cbf4541e6', 'root_gb': 10, 'ephemeral_gb': 20, 'instance_type_id': '5', # m1.small 'extra_specs': {}, 'system_metadata': { 'image_disk_format': 'raw', }, 'flavor': flavor, 'new_flavor': None, 'old_flavor': None, 'pci_devices': objects.PciDeviceList(), 'numa_topology': None, 'config_drive': None, 'vm_mode': None, 'kernel_id': None, 'ramdisk_id': None, 'os_type': 'linux', 'user_id': '838a72b0-0d54-4827-8fd6-fb1227633ceb', 'ephemeral_key_uuid': None, 'vcpu_model': None, 'host': 'fake-host', 'task_state': None, } self.instance = objects.Instance(**instance) self.conn = fakelibvirt.Connection("qemu:///system") self.dom = fakelibvirt.Domain(self.conn, "<domain/>", True) self.host = host.Host("qemu:///system") self.guest = libvirt_guest.Guest(self.dom)
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 setUp(self): super(GuestBlockTestCase, self).setUp() self.useFixture(nova_fixtures.LibvirtFixture()) self.host = host.Host("qemu:///system") self.context = context.get_admin_context() self.domain = mock.Mock(spec=fakelibvirt.virDomain) self.guest = libvirt_guest.Guest(self.domain) self.gblock = self.guest.get_block_device('vda')
def test_event_emit_delayed_call_delayed(self): ev = event.LifecycleEvent("cef19ce0-0ca2-11df-855d-b19fbce37686", event.EVENT_LIFECYCLE_STOPPED) for uri in ("qemu:///system", "xen:///"): spawn_after_mock = mock.Mock() greenthread.spawn_after = spawn_after_mock hostimpl = host.Host(uri, lifecycle_event_handler=lambda e: None) hostimpl._event_emit_delayed(ev) spawn_after_mock.assert_called_once_with(15, hostimpl._event_emit, ev)
def setUp(self): super(GuestTestCase, self).setUp() self.useFixture(fakelibvirt.FakeLibvirtFixture()) self.host = host.Host("qemu:///system") self.context = context.get_admin_context() self.domain = mock.Mock(spec=fakelibvirt.virDomain) self.guest = libvirt_guest.Guest(self.domain) # Make RetryDecorator not actually sleep on retries self.useFixture(service_fixture.SleepFixture())
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_conn_event_up_down(self, mock_conn, mock_test_conn): handler = mock.MagicMock() h = host.Host("qemu:///system", conn_event_handler=handler) mock_conn.side_effect = (mock.MagicMock(), fakelibvirt.libvirtError('test')) mock_test_conn.return_value = False h.get_connection() self.assertRaises(exception.HypervisorUnavailable, h.get_connection) h._dispatch_conn_event() h._dispatch_conn_event() handler.assert_has_calls( [mock.call(True, None), mock.call(False, StringMatcher())])
def test_event_dispatch(self, mock_spawn_after): # 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] self.assertEqual(want_events, got_events) # STOPPED is delayed so it's handled separately mock_spawn_after.assert_called_once_with( hostimpl._lifecycle_delay, hostimpl._event_emit, event4)
def test_conn_event_fail(self, mock_conn): handler = mock.MagicMock() h = host.Host("qemu:///system", conn_event_handler=handler) mock_conn.side_effect = fakelibvirt.libvirtError('test') self.assertRaises(exception.HypervisorUnavailable, h.get_connection) h._dispatch_conn_event() handler.assert_called_once_with(False, StringMatcher()) # Attempt to get a second connection, and assert that we don't add # queue a second callback. Note that we can't call # _dispatch_conn_event() and assert no additional call to the handler # here as above. This is because we haven't added an event, so it would # block. We mock the helper method which queues an event for callback # instead. with mock.patch.object(h, '_queue_conn_event_handler') as mock_queue: self.assertRaises(exception.HypervisorUnavailable, h.get_connection) mock_queue.assert_not_called()
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, libvirt.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_lifecycle(self): got_events = [] # Validate that libvirt events are correctly translated # to Nova events def spawn_after(seconds, func, *args, **kwargs): got_events.append(args[0]) return mock.Mock(spec=greenthread.GreenThread) greenthread.spawn_after = mock.Mock(side_effect=spawn_after) hostimpl = host.Host("qemu:///system", lifecycle_event_handler=lambda e: None) 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 setUp(self): super(LibvirtVolumeBaseTestCase, self).setUp() self.useFixture(nova_fixtures.LibvirtFixture()) self.fake_host = host.Host("qemu:///system") self.connr = { 'ip': '127.0.0.1', 'initiator': 'fake_initiator', 'host': 'fake_host' } self.disk_info = { "bus": "virtio", "dev": "vde", "type": "disk", } self.name = 'volume-00000001' self.location = '10.0.2.15:3260' self.iqn = 'iqn.2010-10.org.openstack:%s' % self.name self.vol = {'id': 1, 'name': self.name} self.uuid = '875a8070-d0b9-4949-8b31-104d125c9a64' self.user = '******'
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(NWFilterTestCase, self).setUp() self.useFixture(fakelibvirt.FakeLibvirtFixture()) self.fw = firewall.NWFilterFirewall(host=host.Host("qemu:///system"))
def setUp(self): super(HostTestCase, self).setUp() self.useFixture(fakelibvirt.FakeLibvirtFixture()) self.host = host.Host("qemu:///system")
def __init__(self): 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)
def setUp(self): super(GuestTestCase, self).setUp() self.useFixture(fakelibvirt.FakeLibvirtFixture()) self.host = host.Host("qemu:///system") self.context = context.get_admin_context()