def test_teamcity_release_reservation_zero(make_teamcity_response,
                                           sample_tc_pool_used, monkeypatch):
    first_tc_response = make_teamcity_response({"name": "value", "value": "0"})
    # Not examined so it can be blank
    second_tc_response = make_teamcity_response({})
    assert '' != sample_tc_pool_used[1].used_for
    with patch('Teamcity.tc_allocator.teamcity_request') as teamcity_request:
        teamcity_request.side_effect = [first_tc_response, second_tc_response]
        monkeypatch.setattr(MockDevice, 'get_share_state', lambda _: False)
        tc_allocator.teamcity_release_reservation(
            resource=sample_tc_pool_used[1])
        assert 1 == teamcity_request.call_count
        assert '' == sample_tc_pool_used[1].used_for
def build_reservation(request, build_id: int):
    try:
        used_for = f"Teamcity_ID={build_id}"
        resource = Resource.objects.get(used_for=used_for)
        if request.method == "DELETE":
            teamcity_release_reservation(resource=resource)
            return Response(status=status.HTTP_204_NO_CONTENT)
        return HttpResponseRedirect(
            reverse('api:show_reservation',
                    kwargs={'resource_pk': resource.pk}))
    except Resource.DoesNotExist as e:
        return HttpResponseNotFound(
            "That resource reservation for that build was not found")
def test_teamcity_release_reservation_negative(make_teamcity_response,
                                               sample_tc_pool_used,
                                               monkeypatch):
    # In Teamcity -1 means infinite quota. In our use case this is never desired so we should reset to zero to
    # prevent problems
    first_tc_response = make_teamcity_response({
        "name": "value",
        "value": "-1"
    })
    # Not examined so it can be blank
    second_tc_response = make_teamcity_response({})
    assert '' != sample_tc_pool_used[1].used_for
    with patch('Teamcity.tc_allocator.teamcity_request') as teamcity_request:
        teamcity_request.side_effect = [first_tc_response, second_tc_response]
        monkeypatch.setattr(MockDevice, 'get_share_state', lambda _: False)
        tc_allocator.teamcity_release_reservation(
            resource=sample_tc_pool_used[1])
        assert 2 == teamcity_request.call_count
        assert '' == sample_tc_pool_used[1].used_for