def test_select_destination_with_old_client(self, from_primitives, mock_get_ac, mock_rfrs, mock_process): fake_spec = objects.RequestSpec() fake_spec.instance_uuid = uuids.instance from_primitives.return_value = fake_spec mock_p_sums = mock.Mock() fake_alloc_reqs = fakes.get_fake_alloc_reqs() place_res = (fake_alloc_reqs, mock_p_sums, "42.0") mock_get_ac.return_value = place_res mock_rfrs.return_value.cpu_pinning_requested = False expected_alloc_reqs_by_rp_uuid = { cn.uuid: [fake_alloc_reqs[x]] for x, cn in enumerate(fakes.COMPUTE_NODES) } with mock.patch.object(self.manager.driver, 'select_destinations') as select_destinations: self.manager.select_destinations( self.context, request_spec='fake_spec', filter_properties='fake_props', instance_uuids=[fake_spec.instance_uuid]) mock_process.assert_called_once_with(self.context, fake_spec) select_destinations.assert_called_once_with( self.context, fake_spec, [fake_spec.instance_uuid], expected_alloc_reqs_by_rp_uuid, mock_p_sums, "42.0", False) mock_get_ac.assert_called_once_with(self.context, mock_rfrs.return_value)
def test_select_destination_with_4_3_client(self, mock_get_ac, mock_rfrs, mock_process, cpu_pinning_requested=False): fake_spec = objects.RequestSpec() mock_p_sums = mock.Mock() fake_alloc_reqs = fakes.get_fake_alloc_reqs() place_res = (fake_alloc_reqs, mock_p_sums, "42.0") mock_get_ac.return_value = place_res mock_rfrs.return_value.cpu_pinning_requested = cpu_pinning_requested expected_alloc_reqs_by_rp_uuid = { cn.uuid: [fake_alloc_reqs[x]] for x, cn in enumerate(fakes.COMPUTE_NODES) } with mock.patch.object(self.manager.driver, 'select_destinations') as select_destinations: self.manager.select_destinations(self.context, spec_obj=fake_spec) mock_process.assert_called_once_with(self.context, fake_spec) select_destinations.assert_called_once_with( self.context, fake_spec, None, expected_alloc_reqs_by_rp_uuid, mock_p_sums, "42.0", False) mock_rfrs.assert_called_once_with(self.context, fake_spec, mock.ANY, enable_pinning_translate=True) mock_get_ac.assert_called_once_with(self.context, mock_rfrs.return_value)
def test_select_destination(self, mock_get_ac, mock_rfrs, mock_process): fake_spec = objects.RequestSpec() fake_spec.instance_uuid = uuids.instance fake_version = "9.42" mock_p_sums = mock.Mock() fake_alloc_reqs = fakes.get_fake_alloc_reqs() place_res = (fake_alloc_reqs, mock_p_sums, fake_version) mock_get_ac.return_value = place_res mock_rfrs.return_value.cpu_pinning_requested = False expected_alloc_reqs_by_rp_uuid = { cn.uuid: [fake_alloc_reqs[x]] for x, cn in enumerate(fakes.COMPUTE_NODES) } with mock.patch.object(self.manager.driver, 'select_destinations' ) as select_destinations: self.manager.select_destinations(self.context, spec_obj=fake_spec, instance_uuids=[fake_spec.instance_uuid]) mock_process.assert_called_once_with(self.context, fake_spec) select_destinations.assert_called_once_with( self.context, fake_spec, [fake_spec.instance_uuid], expected_alloc_reqs_by_rp_uuid, mock_p_sums, fake_version, False) mock_get_ac.assert_called_once_with( self.context, mock_rfrs.return_value) # Now call select_destinations() with True values for the params # introduced in RPC version 4.5 select_destinations.reset_mock() self.manager.select_destinations(None, spec_obj=fake_spec, instance_uuids=[fake_spec.instance_uuid], return_objects=True, return_alternates=True) select_destinations.assert_called_once_with(None, fake_spec, [fake_spec.instance_uuid], expected_alloc_reqs_by_rp_uuid, mock_p_sums, fake_version, True)
def test_select_destination_return_objects(self, mock_get_ac, mock_rfrs, mock_process): fake_spec = objects.RequestSpec() fake_spec.instance_uuid = uuids.instance fake_version = "9.42" mock_p_sums = mock.Mock() fake_alloc_reqs = fakes.get_fake_alloc_reqs() place_res = (fake_alloc_reqs, mock_p_sums, fake_version) mock_get_ac.return_value = place_res mock_rfrs.return_value.cpu_pinning_requested = False expected_alloc_reqs_by_rp_uuid = { cn.uuid: [fake_alloc_reqs[x]] for x, cn in enumerate(fakes.COMPUTE_NODES) } with mock.patch.object(self.manager.driver, 'select_destinations') as select_destinations: sel_obj = objects.Selection(service_host="fake_host", nodename="fake_node", compute_node_uuid=uuids.compute_node, cell_uuid=uuids.cell, limits=None) select_destinations.return_value = [[sel_obj]] # Pass True; should get the Selection object back. dests = self.manager.select_destinations( None, spec_obj=fake_spec, instance_uuids=[fake_spec.instance_uuid], return_objects=True, return_alternates=True) sel_host = dests[0][0] self.assertIsInstance(sel_host, objects.Selection) mock_process.assert_called_once_with(None, fake_spec) # Since both return_objects and return_alternates are True, the # driver should have been called with True for return_alternates. select_destinations.assert_called_once_with( None, fake_spec, [fake_spec.instance_uuid], expected_alloc_reqs_by_rp_uuid, mock_p_sums, fake_version, True) # Now pass False for return objects, but keep return_alternates as # True. Verify that the manager converted the Selection object back # to a dict. select_destinations.reset_mock() dests = self.manager.select_destinations( None, spec_obj=fake_spec, instance_uuids=[fake_spec.instance_uuid], return_objects=False, return_alternates=True) sel_host = dests[0] self.assertIsInstance(sel_host, dict) # Even though return_alternates was passed as True, since # return_objects was False, the driver should have been called with # return_alternates as False. select_destinations.assert_called_once_with( None, fake_spec, [fake_spec.instance_uuid], expected_alloc_reqs_by_rp_uuid, mock_p_sums, fake_version, False)
def test_select_destination_with_pcpu_fallback(self, mock_get_ac, mock_rfrs, mock_process, mock_log): """Check that we make a second request to placement if we've got a PCPU request. """ self.flags(disable_fallback_pcpu_query=False, group='workarounds') # mock the result from placement. In reality, the two calls we expect # would return two different results, but we don't care about that. All # we want to check is that it _was_ called twice fake_spec = objects.RequestSpec() mock_p_sums = mock.Mock() fake_alloc_reqs = fakes.get_fake_alloc_reqs() place_res = (fake_alloc_reqs, mock_p_sums, "42.0") mock_get_ac.return_value = place_res pcpu_rreq = mock.Mock() pcpu_rreq.cpu_pinning_requested = True vcpu_rreq = mock.Mock() mock_rfrs.side_effect = [pcpu_rreq, vcpu_rreq] # as above, the two allocation requests against each compute node would # be different in reality, and not all compute nodes might have two # allocation requests, but that doesn't matter for this simple test expected_alloc_reqs_by_rp_uuid = { cn.uuid: [fake_alloc_reqs[x], fake_alloc_reqs[x]] for x, cn in enumerate(fakes.COMPUTE_NODES) } with mock.patch.object(self.manager.driver, 'select_destinations') as select_destinations: self.manager.select_destinations(self.context, spec_obj=fake_spec) select_destinations.assert_called_once_with( self.context, fake_spec, None, expected_alloc_reqs_by_rp_uuid, mock_p_sums, "42.0", False) mock_process.assert_called_once_with(self.context, fake_spec) mock_log.assert_called_with( 'Requesting fallback allocation candidates with VCPU instead of ' 'PCPU') mock_rfrs.assert_has_calls([ mock.call(self.context, fake_spec, mock.ANY, enable_pinning_translate=True), mock.call(self.context, fake_spec, mock.ANY, enable_pinning_translate=False), ]) mock_get_ac.assert_has_calls([ mock.call(self.context, pcpu_rreq), mock.call(self.context, vcpu_rreq), ])