remote: http: rename http error, refactor test

This commit is contained in:
Paweł Redzyński
2019-11-29 15:30:55 +01:00
parent 72abfb0572
commit 7218ff202c
5 changed files with 16 additions and 22 deletions
+3 -4
View File
@@ -350,9 +350,8 @@ class NoOutputInExternalRepoError(DvcException):
) )
class HTTPErrorStatusCodeException(DvcException): class HTTPError(DvcException):
def __init__(self, code, reason): def __init__(self, code, reason):
super(HTTPErrorStatusCodeException, self).__init__( super(HTTPError, self).__init__(
"Server responded with error status code: '{}' and message: " "HTTP error: '{} {}'".format(code, reason)
"'{}'".format(code, reason)
) )
+2 -5
View File
@@ -7,8 +7,7 @@ from funcy import cached_property, wrap_prop
from dvc.config import Config from dvc.config import Config
from dvc.config import ConfigError from dvc.config import ConfigError
from dvc.exceptions import DvcException from dvc.exceptions import DvcException, HTTPError
from dvc.exceptions import HTTPErrorStatusCodeException
from dvc.progress import Tqdm from dvc.progress import Tqdm
from dvc.remote.base import RemoteBASE from dvc.remote.base import RemoteBASE
from dvc.scheme import Schemes from dvc.scheme import Schemes
@@ -40,9 +39,7 @@ class RemoteHTTP(RemoteBASE):
def _download(self, from_info, to_file, name=None, no_progress_bar=False): def _download(self, from_info, to_file, name=None, no_progress_bar=False):
response = self._request("GET", from_info.url, stream=True) response = self._request("GET", from_info.url, stream=True)
if response.status_code != 200: if response.status_code != 200:
raise HTTPErrorStatusCodeException( raise HTTPError(response.status_code, response.reason)
response.status_code, response.reason
)
with Tqdm( with Tqdm(
total=None if no_progress_bar else self._content_length(from_info), total=None if no_progress_bar else self._content_length(from_info),
leave=False, leave=False,
+1 -2
View File
@@ -43,8 +43,7 @@ from tests.func.test_data_cloud import _should_test_ssh
from tests.func.test_data_cloud import get_ssh_url from tests.func.test_data_cloud import get_ssh_url
from tests.func.test_data_cloud import TEST_AWS_REPO_BUCKET from tests.func.test_data_cloud import TEST_AWS_REPO_BUCKET
from tests.func.test_data_cloud import TEST_GCP_REPO_BUCKET from tests.func.test_data_cloud import TEST_GCP_REPO_BUCKET
from tests.utils.httpd import ContentMD5Handler from tests.utils.httpd import StaticFileServer, ContentMD5Handler
from tests.utils.httpd import StaticFileServer
class TestRepro(TestDvc): class TestRepro(TestDvc):
+10 -10
View File
@@ -1,8 +1,12 @@
try:
from http.server import BaseHTTPRequestHandler
except ImportError:
from BaseHTTPServer import BaseHTTPRequestHandler
import pytest import pytest
from BaseHTTPServer import BaseHTTPRequestHandler
from dvc.config import ConfigError from dvc.config import ConfigError
from dvc.exceptions import HTTPErrorStatusCodeException from dvc.exceptions import HTTPError
from dvc.path_info import URLInfo from dvc.path_info import URLInfo
from dvc.remote.http import RemoteHTTP from dvc.remote.http import RemoteHTTP
from tests.utils.httpd import StaticFileServer from tests.utils.httpd import StaticFileServer
@@ -19,11 +23,10 @@ def test_no_traverse_compatibility(dvc_repo):
RemoteHTTP(dvc_repo, config) RemoteHTTP(dvc_repo, config)
@pytest.mark.parametrize("response_code", [404, 403, 500]) def test_download_fails_on_error_code(dvc_repo):
def test_download_fails_on_error_code(response_code, dvc_repo):
class ErrorStatusRequestHandler(BaseHTTPRequestHandler): class ErrorStatusRequestHandler(BaseHTTPRequestHandler):
def do_GET(self): def do_GET(self):
self.send_response(response_code, message="Error message") self.send_response(404, message="Not found")
self.end_headers() self.end_headers()
with StaticFileServer(ErrorStatusRequestHandler) as httpd: with StaticFileServer(ErrorStatusRequestHandler) as httpd:
@@ -31,9 +34,6 @@ def test_download_fails_on_error_code(response_code, dvc_repo):
config = {"url": url} config = {"url": url}
remote = RemoteHTTP(dvc_repo, config) remote = RemoteHTTP(dvc_repo, config)
import os
with pytest.raises(HTTPErrorStatusCodeException): with pytest.raises(HTTPError):
remote._download( remote._download(URLInfo(url) / "file.txt", "file.txt")
URLInfo(os.path.join(url, "file.txt")), "file.txt"
)
-1
View File
@@ -47,7 +47,6 @@ class StaticFileServer:
def __init__(self, handler_class=ETagHandler): def __init__(self, handler_class=ETagHandler):
self._lock.acquire() self._lock.acquire()
self.response_handler = handler_class
self._httpd = HTTPServer(("localhost", 0), handler_class) self._httpd = HTTPServer(("localhost", 0), handler_class)
self._thread = None self._thread = None