Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NXDRIVE-2607: Handle invalid data returned by the server when asking for a batchId #2630

Merged
merged 1 commit into from Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changes/5.1.2.md
Expand Up @@ -6,6 +6,7 @@ Release date: `2021-xx-xx`

- [NXDRIVE-2581](https://jira.nuxeo.com/browse/NXDRIVE-2581): Add the synchronization feature
- [NXDRIVE-2606](https://jira.nuxeo.com/browse/NXDRIVE-2606): Catch `TypeError` in `RemoteFileInfo.from_dict()`
- [NXDRIVE-2607](https://jira.nuxeo.com/browse/NXDRIVE-2607): Handle invalid data returned by the server when asking for a `batchId`

### Direct Edit

Expand Down
8 changes: 7 additions & 1 deletion nxdrive/client/uploader/__init__.py
Expand Up @@ -2,6 +2,7 @@
Uploader used by the Remote client for all upload stuff.
"""
from abc import abstractmethod
from json import JSONDecodeError
from logging import getLogger
from pathlib import Path
from time import monotonic_ns
Expand Down Expand Up @@ -103,7 +104,12 @@ def _get_transfer(
handler = UP_AMAZON_S3 if Feature.s3 and uploads.has_s3() else ""

# Create a new batch
batch = uploads.batch(handler=handler)
try:
batch = uploads.batch(handler=handler)
except JSONDecodeError as exc:
err = "Cannot parse the batch response: invalid data from the server"
log.warning(err)
raise HTTPError(status=500, message=err) from exc

# Remove eventual obsolete upload (it happens when an upload using S3 has invalid metadatas)
is_direct_transfer = kwargs.get("is_direct_transfer", False)
Expand Down
41 changes: 41 additions & 0 deletions tests/old_functional/test_transfer.py
@@ -1,9 +1,11 @@
"""
Test pause/resume transfers in different scenarii.
"""
import re
from unittest.mock import patch

import pytest
import responses
from nuxeo.exceptions import HTTPError
from requests.exceptions import ConnectionError

Expand Down Expand Up @@ -616,6 +618,45 @@ def upload(*args, **kwargs):
assert not list(dao.get_uploads())
assert self.remote_1.exists("/test.bin")

def test_server_error_upload_invalid_batch_response(self):
"""Test a server error happening when asking for a batchId."""
remote = self.remote_1
dao = self.engine_1.dao
# Locally create a file that will be uploaded remotely
self.local_1.make_file("/", "test.bin", content=b"0" * 1024)

# There is no upload, right now
assert not list(dao.get_uploads())

# Alter the first request done to get a batchId
url = remote.client.host + remote.uploads.endpoint
with ensure_no_exception(), responses.RequestsMock() as rsps:
# Other requests should not be altered
rsps.add_passthru(re.compile(fr"^(?!{url}).*"))

html = (
'<!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.1//EN http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'
'<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">'
"<head><title>The page is temporarily unavailable</title>"
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />'
)
rsps.add(responses.POST, url, body=html)

self.wait_sync()

assert not self.remote_1.exists("/test.bin")
assert len(dao.get_errors(limit=0)) == 1

# Reset the error
for state in dao.get_errors():
dao.reset_error(state)

# Resync and check the file exist
with ensure_no_exception():
self.wait_sync()
assert not list(dao.get_uploads())
assert self.remote_1.exists("/test.bin")

def test_chunk_upload_error(self):
"""Test a server error happening while uploading chunks."""

Expand Down
3 changes: 3 additions & 0 deletions tools/deps/requirements-tests.txt
Expand Up @@ -237,6 +237,9 @@ regex==2021.4.4 \
--hash=sha256:97f29f57d5b84e73fbaf99ab3e26134e6687348e95ef6b48cfd2c06807005a07 \
--hash=sha256:52ba3d3f9b942c49d7e4bc105bb28551c44065f139a65062ab7912bef10c9afb
# via black
responses==0.13.2 \
--hash=sha256:75529f9bea08276cea43545dcb6129f137c299d6a12269485a753785c869e0e2 \
--hash=sha256:0f0ab4717728d33dae8e66deea61eecc1e38f0398e35249e3963ff74cfc8d0d8
selenium==3.141.0 ; sys_platform == "win32" \
--hash=sha256:2d7131d7bc5a5b99a2d9b04aaf2612c411b03b8ca1b1ee8d3de5845a9be2cb3c
text-unidecode==1.3 \
Expand Down