|
|
|
@ -890,7 +890,26 @@ def test_publish_with_wait(galaxy_server, collection_artifact, monkeypatch):
|
|
|
|
% galaxy_server.api_server
|
|
|
|
% galaxy_server.api_server
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_download_file(tmp_path_factory, monkeypatch):
|
|
|
|
class MockHTTPResponses:
|
|
|
|
|
|
|
|
def __init__(self, responses: list[tuple[bytes, int | None]]) -> None:
|
|
|
|
|
|
|
|
self._body, self.length = responses[0]
|
|
|
|
|
|
|
|
self._responses = responses[1:]
|
|
|
|
|
|
|
|
self._stream = BytesIO(self._body)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def read(self, size: int = -1) -> bytes:
|
|
|
|
|
|
|
|
data = self._stream.read(size)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if not data:
|
|
|
|
|
|
|
|
# reset the stream to simulate retries
|
|
|
|
|
|
|
|
if self._responses:
|
|
|
|
|
|
|
|
self._body, self.length = self._responses[0]
|
|
|
|
|
|
|
|
del self._responses[0]
|
|
|
|
|
|
|
|
self._stream = BytesIO(self._body)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_download_file(tmp_path_factory: pytest.TempPathFactory, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
|
|
temp_dir = to_bytes(tmp_path_factory.mktemp('test-ÅÑŚÌβŁÈ Collections'))
|
|
|
|
temp_dir = to_bytes(tmp_path_factory.mktemp('test-ÅÑŚÌβŁÈ Collections'))
|
|
|
|
|
|
|
|
|
|
|
|
data = b"\x00\x01\x02\x03"
|
|
|
|
data = b"\x00\x01\x02\x03"
|
|
|
|
@ -898,7 +917,7 @@ def test_download_file(tmp_path_factory, monkeypatch):
|
|
|
|
sha256_hash.update(data)
|
|
|
|
sha256_hash.update(data)
|
|
|
|
|
|
|
|
|
|
|
|
mock_open = MagicMock()
|
|
|
|
mock_open = MagicMock()
|
|
|
|
mock_open.return_value = BytesIO(data)
|
|
|
|
mock_open.return_value = MockHTTPResponses([(data, len(data))])
|
|
|
|
monkeypatch.setattr(collection.concrete_artifact_manager, 'open_url', mock_open)
|
|
|
|
monkeypatch.setattr(collection.concrete_artifact_manager, 'open_url', mock_open)
|
|
|
|
|
|
|
|
|
|
|
|
expected = temp_dir
|
|
|
|
expected = temp_dir
|
|
|
|
@ -913,13 +932,13 @@ def test_download_file(tmp_path_factory, monkeypatch):
|
|
|
|
assert mock_open.mock_calls[0][1][0] == 'http://google.com/file'
|
|
|
|
assert mock_open.mock_calls[0][1][0] == 'http://google.com/file'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_download_file_hash_mismatch(tmp_path_factory, monkeypatch):
|
|
|
|
def test_download_file_hash_mismatch(tmp_path_factory: pytest.TempPathFactory, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
|
|
temp_dir = to_bytes(tmp_path_factory.mktemp('test-ÅÑŚÌβŁÈ Collections'))
|
|
|
|
temp_dir = to_bytes(tmp_path_factory.mktemp('test-ÅÑŚÌβŁÈ Collections'))
|
|
|
|
|
|
|
|
|
|
|
|
data = b"\x00\x01\x02\x03"
|
|
|
|
data = b"\x00\x01\x02\x03"
|
|
|
|
|
|
|
|
|
|
|
|
mock_open = MagicMock()
|
|
|
|
mock_open = MagicMock()
|
|
|
|
mock_open.return_value = BytesIO(data)
|
|
|
|
mock_open.return_value = MockHTTPResponses([(data, len(data))])
|
|
|
|
monkeypatch.setattr(collection.concrete_artifact_manager, 'open_url', mock_open)
|
|
|
|
monkeypatch.setattr(collection.concrete_artifact_manager, 'open_url', mock_open)
|
|
|
|
|
|
|
|
|
|
|
|
expected = "Mismatch artifact hash with downloaded file"
|
|
|
|
expected = "Mismatch artifact hash with downloaded file"
|
|
|
|
@ -927,6 +946,35 @@ def test_download_file_hash_mismatch(tmp_path_factory, monkeypatch):
|
|
|
|
collection._download_file('http://google.com/file', temp_dir, 'bad', True)
|
|
|
|
collection._download_file('http://google.com/file', temp_dir, 'bad', True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_download_file_incomplete_read(
|
|
|
|
|
|
|
|
tmp_path_factory: pytest.TempPathFactory,
|
|
|
|
|
|
|
|
monkeypatch: pytest.MonkeyPatch
|
|
|
|
|
|
|
|
) -> None:
|
|
|
|
|
|
|
|
temp_dir = to_bytes(tmp_path_factory.mktemp('test-ÅÑŚÌβŁÈ Collections'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
incomplete_data = b"\x00\x01\x02\x03"
|
|
|
|
|
|
|
|
data = incomplete_data + b"\x04"
|
|
|
|
|
|
|
|
sha256_hash = sha256(data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Test retries succeed
|
|
|
|
|
|
|
|
responses = [(incomplete_data, len(data)), (data, len(data))]
|
|
|
|
|
|
|
|
mock_open = MagicMock(return_value=MockHTTPResponses(responses))
|
|
|
|
|
|
|
|
monkeypatch.setattr(collection.concrete_artifact_manager, 'open_url', mock_open)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
collection._download_file('http://google.com/file', temp_dir, sha256_hash.hexdigest(), True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Test error is correct
|
|
|
|
|
|
|
|
mock_open.return_value = MockHTTPResponses([(data, len(data) + 1)])
|
|
|
|
|
|
|
|
monkeypatch.setattr(collection.concrete_artifact_manager, 'open_url', mock_open)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
expected_error = re.escape(
|
|
|
|
|
|
|
|
"Downloading http://google.com/file failed: "
|
|
|
|
|
|
|
|
"IncompleteRead(5 bytes read, 1 more expected)"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
with pytest.raises(AnsibleError, match=expected_error):
|
|
|
|
|
|
|
|
collection._download_file('http://google.com/file', temp_dir, sha256_hash.hexdigest(), True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_extract_tar_file_invalid_hash(tmp_tarfile):
|
|
|
|
def test_extract_tar_file_invalid_hash(tmp_tarfile):
|
|
|
|
temp_dir, tfile, filename, dummy = tmp_tarfile
|
|
|
|
temp_dir, tfile, filename, dummy = tmp_tarfile
|
|
|
|
|
|
|
|
|
|
|
|
|