'Tests that we clean up previously-allocated instances if not all
instances could be scheduled'

@mock.patch('nova.scheduler.filter_scheduler.FilterScheduler._cleanup_allocations')
@mock.patch('nova.scheduler.filter_scheduler.FilterScheduler._claim_resources')
@mock.patch('nova.scheduler.filter_scheduler.FilterScheduler._get_all_host_states')
@mock.patch('nova.scheduler.filter_scheduler.FilterScheduler._get_sorted_hosts')
def test_schedule_not_all_instance_clean_claimed(self, mock_get_hosts, mock_get_all_states, mock_claim, mock_cleanup):

    spec_obj = objects.RequestSpec(num_instances=2, flavor=objects.Flavor(memory_mb=512, root_gb=512, ephemeral_gb=0, swap=0, vcpus=1), project_id=uuids.project_id, instance_group=None)
    host_state = mock.Mock(spec=host_manager.HostState, host=mock.sentinel.host, uuid=uuids.cn1)
    all_host_states = [host_state]
    mock_get_all_states.return_value = all_host_states
    mock_get_hosts.side_effect = [all_host_states, []]
    mock_claim.return_value = True
    instance_uuids = [uuids.instance1, uuids.instance2]
    alloc_reqs_by_rp_uuid = {uuids.cn1: [mock.sentinel.alloc_req]}
    ctx = mock.Mock()
    self.driver._schedule(ctx, spec_obj, instance_uuids, alloc_reqs_by_rp_uuid, mock.sentinel.provider_summaries)
    mock_cleanup.assert_called_once_with([uuids.instance1])
