Skip to content

Commit fcbfe38

Browse files
committed
Simplify the test download_handler implementation
Simplify the test implementation of a custom_download_handler to match the latest function description in Updater. Reduce the function parameters which now takes only a 'url' Signed-off-by: Teodora Sechkova <[email protected]>
1 parent 67eeb63 commit fcbfe38

File tree

1 file changed

+8
-33
lines changed

1 file changed

+8
-33
lines changed

tests/test_updater.py

+8-33
Original file line numberDiff line numberDiff line change
@@ -1228,7 +1228,7 @@ def test_download_target_custom_download_handler(self):
12281228
download_filepath = os.path.join(destination_directory, target_filepath)
12291229

12301230
# Test passing a handler with correct signature but incorrect implementation
1231-
def test_1(param_1, param2):
1231+
def test_1(param_1):
12321232
pass
12331233

12341234
self.assertRaises(tuf.exceptions.NoWorkingMirrorError,
@@ -1239,7 +1239,7 @@ def test_1(param_1, param2):
12391239
self.assertFalse(os.path.exists(download_filepath))
12401240

12411241
# Test passing a handler with incorrect number of parameters
1242-
def test_2(param_1, param_2, param_3):
1242+
def test_2(param_1, param_2):
12431243
pass
12441244

12451245
with self.assertRaises(tuf.exceptions.NoWorkingMirrorError) as cm:
@@ -1253,7 +1253,7 @@ def test_2(param_1, param_2, param_3):
12531253
self.assertFalse(os.path.exists(download_filepath))
12541254

12551255
# Test passing a handler throwing an unexpected error
1256-
def test_3(param_1, param2):
1256+
def test_3(param_1):
12571257
raise IOError
12581258

12591259
with self.assertRaises(tuf.exceptions.NoWorkingMirrorError) as cm:
@@ -1272,13 +1272,8 @@ def test_3(param_1, param2):
12721272
# Checks if the file has been successfully downloaded
12731273
self.assertTrue(os.path.exists(download_filepath))
12741274

1275-
# Define a simple custom download handler mimicking
1276-
# the tuf.download call stack:
1277-
# safe_download()
1278-
# _download_file()
1279-
# _download_fixed_amount_of_data()
1280-
# _check_downloaded_length()
1281-
def download_handler(url, required_length):
1275+
# Define a simple custom download handler
1276+
def download_handler(url):
12821277
url = six.moves.urllib.parse.unquote(url).replace('\\', '/')
12831278
temp_file = tempfile.TemporaryFile()
12841279

@@ -1287,32 +1282,12 @@ def download_handler(url, required_length):
12871282
with session.get(url, stream=True,
12881283
timeout=tuf.settings.SOCKET_TIMEOUT) as response:
12891284

1290-
# Check response status.
1285+
# Check response status
12911286
response.raise_for_status()
12921287

1293-
# Download fixed amount of data
1294-
average_download_speed = 0
1295-
number_of_bytes_received = 0
1296-
1297-
start_time = timeit.default_timer()
1298-
1288+
# Read the raw socket response
12991289
for chunk in response.iter_content(chunk_size=tuf.settings.CHUNK_SIZE):
1300-
number_of_bytes_received += len(chunk)
13011290
temp_file.write(chunk)
1302-
if number_of_bytes_received >= required_length:
1303-
break
1304-
1305-
stop_time = timeit.default_timer()
1306-
average_download_speed = number_of_bytes_received / (stop_time - start_time)
1307-
if average_download_speed < tuf.settings.MIN_AVERAGE_DOWNLOAD_SPEED:
1308-
break
1309-
1310-
#Check downloaded length
1311-
if number_of_bytes_received != required_length:
1312-
if average_download_speed < tuf.settings.MIN_AVERAGE_DOWNLOAD_SPEED:
1313-
raise tuf.exceptions.SlowRetrievalError(average_download_speed)
1314-
1315-
raise tuf.exceptions.DownloadLengthMismatchError(required_length, number_of_bytes_received)
13161291

13171292
except Exception:
13181293
temp_file.close()
@@ -1321,7 +1296,7 @@ def download_handler(url, required_length):
13211296
else:
13221297
return temp_file
13231298

1324-
#Test passing a custom download function
1299+
# Test passing a custom download function
13251300
self.repository_updater.download_target(targetinfo, destination_directory,
13261301
custom_download_handler=download_handler)
13271302

0 commit comments

Comments
 (0)