-
Notifications
You must be signed in to change notification settings - Fork 278
Abstract out the network IO #1250
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
84957c8
Add FetcherInterface class
sechkova 41ffe7a
Implement RequestsFetcher class
sechkova 815fe24
Move network IO logic to RequestsFetcher
sechkova 33b89a8
Add fetcher as parameter to Updater class
sechkova 6c49792
Update tests importing tuf.download
sechkova 7dc5ef6
Add test_fetcher
sechkova aaddbd3
Take out connection time from download speed calculation
sechkova 4f02e1e
Avoid 'localhost' lookup in tests
sechkova 29e3419
Apply a defensive data length check in fetch()
sechkova 50b3b19
Test downloading data in more than one chunk
sechkova 055280b
Close temp file in test_proxy_use.py
sechkova e9b294b
Add an HTTP error for Fetcher interface
1677ce0
Move fetcher components to make API boundary clearer
2af63cf
Add host address as a test level constant
sechkova 93c6573
Apply the new code style to fetcher docstrings
sechkova File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
#!/usr/bin/env python | ||
|
||
# Copyright 2021, New York University and the TUF contributors | ||
# SPDX-License-Identifier: MIT OR Apache-2.0 | ||
|
||
"""Unit test for RequestsFetcher. | ||
""" | ||
# Help with Python 2 compatibility, where the '/' operator performs | ||
# integer division. | ||
from __future__ import division | ||
|
||
import logging | ||
import os | ||
import io | ||
import sys | ||
import unittest | ||
import tempfile | ||
import math | ||
|
||
import tuf | ||
import tuf.exceptions | ||
import tuf.requests_fetcher | ||
import tuf.unittest_toolbox as unittest_toolbox | ||
|
||
from tests import utils | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class TestFetcher(unittest_toolbox.Modified_TestCase): | ||
def setUp(self): | ||
""" | ||
Create a temporary file and launch a simple server in the | ||
current working directory. | ||
""" | ||
|
||
unittest_toolbox.Modified_TestCase.setUp(self) | ||
|
||
# Making a temporary file. | ||
current_dir = os.getcwd() | ||
target_filepath = self.make_temp_data_file(directory=current_dir) | ||
self.target_fileobj = open(target_filepath, 'r') | ||
self.file_contents = self.target_fileobj.read() | ||
self.file_length = len(self.file_contents) | ||
|
||
# Launch a SimpleHTTPServer (serves files in the current dir). | ||
self.server_process_handler = utils.TestServerProcess(log=logger) | ||
|
||
rel_target_filepath = os.path.basename(target_filepath) | ||
self.url = 'http://' + utils.TEST_HOST_ADDRESS + ':' \ | ||
+ str(self.server_process_handler.port) + '/' + rel_target_filepath | ||
|
||
# Create a temporary file where the target file chunks are written | ||
# during fetching | ||
self.temp_file = tempfile.TemporaryFile() | ||
self.fetcher = tuf.requests_fetcher.RequestsFetcher() | ||
|
||
|
||
# Stop server process and perform clean up. | ||
def tearDown(self): | ||
unittest_toolbox.Modified_TestCase.tearDown(self) | ||
|
||
# Cleans the resources and flush the logged lines (if any). | ||
self.server_process_handler.clean() | ||
|
||
self.target_fileobj.close() | ||
self.temp_file.close() | ||
|
||
|
||
# Test: Normal case. | ||
def test_fetch(self): | ||
for chunk in self.fetcher.fetch(self.url, self.file_length): | ||
self.temp_file.write(chunk) | ||
|
||
self.temp_file.seek(0) | ||
temp_file_data = self.temp_file.read().decode('utf-8') | ||
self.assertEqual(self.file_contents, temp_file_data) | ||
|
||
# Test if fetcher downloads file up to a required length | ||
def test_fetch_restricted_length(self): | ||
for chunk in self.fetcher.fetch(self.url, self.file_length-4): | ||
self.temp_file.write(chunk) | ||
|
||
self.temp_file.seek(0, io.SEEK_END) | ||
joshuagl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.assertEqual(self.temp_file.tell(), self.file_length-4) | ||
|
||
|
||
# Test that fetcher does not download more than actual file length | ||
def test_fetch_upper_length(self): | ||
for chunk in self.fetcher.fetch(self.url, self.file_length+4): | ||
self.temp_file.write(chunk) | ||
|
||
self.temp_file.seek(0, io.SEEK_END) | ||
self.assertEqual(self.temp_file.tell(), self.file_length) | ||
|
||
|
||
# Test incorrect URL parsing | ||
def test_url_parsing(self): | ||
with self.assertRaises(tuf.exceptions.URLParsingError): | ||
self.fetcher.fetch(self.random_string(), self.file_length) | ||
|
||
|
||
# Test: Normal case with url data downloaded in more than one chunk | ||
def test_fetch_in_chunks(self): | ||
# Set smaller chunk size to ensure that the file will be downloaded | ||
# in more than one chunk | ||
default_chunk_size = tuf.settings.CHUNK_SIZE | ||
tuf.settings.CHUNK_SIZE = 4 | ||
|
||
# expected_chunks_count: 3 | ||
expected_chunks_count = math.ceil(self.file_length/tuf.settings.CHUNK_SIZE) | ||
self.assertEqual(expected_chunks_count, 3) | ||
|
||
chunks_count = 0 | ||
for chunk in self.fetcher.fetch(self.url, self.file_length): | ||
self.temp_file.write(chunk) | ||
chunks_count+=1 | ||
|
||
self.temp_file.seek(0) | ||
temp_file_data = self.temp_file.read().decode('utf-8') | ||
self.assertEqual(self.file_contents, temp_file_data) | ||
# Check that we calculate chunks as expected | ||
self.assertEqual(chunks_count, expected_chunks_count) | ||
|
||
# Restore default settings | ||
tuf.settings.CHUNK_SIZE = default_chunk_size | ||
|
||
|
||
|
||
# Run unit test. | ||
if __name__ == '__main__': | ||
utils.configure_test_logging(sys.argv) | ||
unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.