Skip to content

http: raise error on unsuccessful http statuses on exists#4785

Merged
skshetry merged 4 commits into
treeverse:masterfrom
damakuno:master
Nov 4, 2020
Merged

http: raise error on unsuccessful http statuses on exists#4785
skshetry merged 4 commits into
treeverse:masterfrom
damakuno:master

Conversation

@damakuno
Copy link
Copy Markdown
Contributor

@damakuno damakuno commented Oct 25, 2020

This PR is to fix #4288 . It is still a work in progress as I haven't written any tests for the changes I have implemented, and I need some guidance on where/how to write tests for the project. Do I add to the unit test script in dvc/tests/unit/remote/test_http.py?

I'm not sure if this requires documentation, but I'll create a PR for it if needed.

@damakuno
Copy link
Copy Markdown
Contributor Author

damakuno commented Oct 26, 2020

I'm a bit stumped on writing the tests. If my understanding is correct I need to test 3 scenarios:

  1. When the response returns 404
  2. When the response returns 200 OK
  3. Other types of responses that will raise a HTTPError

I'm just not sure how to mock responses from the requests module?
It's my first time writing tests for this project, any pointers are very much appreciated.
I'm thinking about something like this, with comments on things I have yet to implement.
in dvc/tests/unit/remote/test_http.py:

...
def test_exists(dvc):
    from dvc.path_info import URLInfo
    config = {
        "url": "http://127.0.0.1",
        "path_info": "file.html"
    }
    path_info = URLInfo(config["url"])
    tree = HTTPTree(dvc, config)

    # mock a successful response that returns 200 OK
    assert tree.exists(path_info) is True
    # mock a response that returns 404
    assert tree.exists(path_info) is False
    # mock a HTTP Error response that is not 404 to 
    with pytest.raises(HTTPError):
        tree.exists(path_info)

P.S. I'll keep digging and try to figure something out, so I'm still working on it!

@skshetry
Copy link
Copy Markdown
Collaborator

skshetry commented Nov 2, 2020

@damakuno, sadly we don't have a good way to test errors in http remote, as we are using this hack for tests:
https://github.com/iterative/dvc/blob/59d5c73c5e5fd6e0c5bba7f6fc7ba5677380ed40/tests/utils/httpd.py#L11

The corresponding pytest http fixture is here.

The short-term solution can be mocking, but it's a code smell. But, it's the only one that I see for now:

def test_exists(dvc, http, mocker):
    tree = HTTPTree(dvc, http.config)

    resp = requests.Response()
    resp.status_code = requests.codes.FORBIDDEN
    resp.raw = io.StringIO("boo!!!")

    mocker.patch.object(tree, "request", return_value=resp)
    with pytest.raises(HTTPError):
        tree.exists(http / "file.txt")

Comment thread dvc/tree/http.py Outdated
@damakuno damakuno marked this pull request as ready for review November 3, 2020 03:28
@damakuno
Copy link
Copy Markdown
Contributor Author

damakuno commented Nov 3, 2020

How do I resolve the failing code coverage check? I'm not quite sure what's the issue

@efiop efiop requested a review from skshetry November 3, 2020 15:32
@skshetry
Copy link
Copy Markdown
Collaborator

skshetry commented Nov 3, 2020

@damakuno, nothing to do with the coverage, as you have already added the tests.

Comment thread tests/unit/remote/test_http.py Outdated
Comment thread tests/unit/remote/test_http.py Outdated
@damakuno damakuno changed the title [WIP] add check for HTTP status code 404 add check for HTTP status code 404 Nov 4, 2020
@skshetry skshetry changed the title add check for HTTP status code 404 http: raise error on unsuccessful http statuses on exists Nov 4, 2020
Copy link
Copy Markdown
Collaborator

@skshetry skshetry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @damakuno for contributing. The changes look good, and can be merged. I added a comment to clarify on the test. 🎉

@skshetry skshetry added the bugfix fixes bug label Nov 4, 2020
@skshetry skshetry merged commit d1441ee into treeverse:master Nov 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix fixes bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

http: do not ignore HTTP statuses when working with HTTP(S) remotes

3 participants