示例#1
0
def test_rpc_client_http_req_kwarg_timeout_overrides_default():
    method_fn = mock.Mock()

    rpc_client = marathon.RpcClient('http://base/url')
    rpc_client.http_req(method_fn, 'some/path', timeout=42)

    method_fn.assert_called_with('http://base/url/some/path', timeout=42)
示例#2
0
def test_rpc_client_http_req_set_timeout_in_constructor():
    method_fn = mock.Mock()

    rpc_client = marathon.RpcClient('http://base/url', 24)
    rpc_client.http_req(method_fn, 'some/path')

    method_fn.assert_called_with('http://base/url/some/path', timeout=24)
示例#3
0
    def test_case(base_url, path, full_url):
        method_fn = mock.Mock()

        rpc_client = marathon.RpcClient(base_url)
        rpc_client.http_req(method_fn, path)

        method_fn.assert_called_with(full_url, timeout=http.DEFAULT_TIMEOUT)
示例#4
0
    def test_case(expected):
        def method_fn(*args, **kwargs):
            return expected

        rpc_client = marathon.RpcClient('http://base/url')
        actual = rpc_client.http_req(method_fn, 'some/path')

        assert actual == expected
示例#5
0
def _assert_pod_feature_supported_raises_exception(head_fn, exception):
    rpc_client = marathon.RpcClient('http://base/url', timeout=22)
    marathon_client = marathon.Client(rpc_client)
    head_fn.side_effect = exception

    with pytest.raises(exception.__class__) as exception_info:
        marathon_client.pod_feature_supported()

    assert exception_info.value == exception
示例#6
0
def test_rpc_client_http_req_passes_kwargs_to_method_fn():
    method_fn = mock.Mock()

    rpc_client = marathon.RpcClient('http://base/url')
    rpc_client.http_req(method_fn, 'some/path', foo='bar', baz=42)

    method_fn.assert_called_with('http://base/url/some/path',
                                 foo='bar',
                                 baz=42,
                                 timeout=http.DEFAULT_TIMEOUT)
示例#7
0
def test_pod_feature_supported_gets_404_response(head_fn):
    mock_response = mock.create_autospec(requests.Response)
    mock_response.status_code = 404
    head_fn.side_effect = DCOSHTTPException(mock_response)

    rpc_client = marathon.RpcClient('http://base/url', timeout=24)
    marathon_client = marathon.Client(rpc_client)

    assert not marathon_client.pod_feature_supported()
    head_fn.assert_called_with('http://base/url/v2/pods', timeout=24)
示例#8
0
    def invoke_test_case(status_code):
        mock_response = mock.create_autospec(requests.Response)
        mock_response.status_code = status_code
        head_fn.return_value = mock_response

        rpc_client = marathon.RpcClient('http://base/url', timeout=42)
        marathon_client = marathon.Client(rpc_client)
        is_supported = marathon_client.pod_feature_supported()

        head_fn.assert_called_with('http://base/url/v2/pods', timeout=42)

        return is_supported
示例#9
0
def test_rpc_client_http_req_propagates_method_fn_exception_1():
    request = requests.Request(method='ANY', url='http://arbitrary/url')
    response = requests.Response()
    response.status_code = 403
    response.reason = 'Forbidden'
    response.request = request

    def method_fn(*args, **kwargs):
        raise DCOSHTTPException(response)

    rpc_client = marathon.RpcClient('http://base/url')
    with pytest.raises(DCOSException) as e:
        rpc_client.http_req(method_fn, 'some/path')

    expected_message = marathon.RpcClient.response_error_message(
        status_code=403,
        reason='Forbidden',
        request_method='ANY',
        request_url='http://arbitrary/url',
        json_body=None)
    assert str(e).endswith(expected_message)
示例#10
0
    def test_case(head_fn, status_code):
        request = requests.Request(method='ANY', url='http://arbitrary/url')
        mock_response = mock.create_autospec(requests.Response)
        mock_response.status_code = status_code
        mock_response.reason = 'Arbitrary Reason'
        mock_response.request = request
        mock_response.json.side_effect = ValueError('empty body')
        head_fn.side_effect = DCOSHTTPException(mock_response)

        rpc_client = marathon.RpcClient('http://does/not/matter')
        marathon_client = marathon.Client(rpc_client)

        with pytest.raises(DCOSException) as exception_info:
            marathon_client.pod_feature_supported()

        message = marathon.RpcClient.response_error_message(
            status_code,
            reason='Arbitrary Reason',
            request_method='ANY',
            request_url='http://arbitrary/url',
            json_body=None)
        assert str(exception_info.value).endswith(message)
示例#11
0
def test_rpc_client_http_req_propagates_method_fn_exception_2():
    request = requests.Request(method='NONE', url='http://host/path')
    # Need the mock so that the json() method can be overridden
    response = mock.create_autospec(requests.Response)
    response.status_code = 422
    response.reason = 'Something Bad'
    response.request = request
    response.json.return_value = {'message': 'BOOM!'}

    def method_fn(*args, **kwargs):
        raise DCOSHTTPException(response)

    rpc_client = marathon.RpcClient('http://base/url')
    with pytest.raises(DCOSException) as e:
        rpc_client.http_req(method_fn, 'some/path')

    expected_message = marathon.RpcClient.response_error_message(
        status_code=422,
        reason='Something Bad',
        request_method='None',
        request_url='http://host/path',
        json_body={'message': 'BOOM!'})
    assert str(e).endswith(expected_message)