Skip to content

Commit 641c151

Browse files
committed
Add test_download_target_custom_download_handler
Add a test case to test_updater which tests incorrect custom_download_handler implementations. Signed-off-by: Teodora Sechkova <[email protected]>
1 parent a08b439 commit 641c151

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

tests/test_updater.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,73 @@ def test_6_get_one_valid_targetinfo(self):
12131213

12141214

12151215

1216+
def test_download_target_custom_download_handler(self):
1217+
# Create temporary directory (destination directory of downloaded targets)
1218+
# that will be passed as an argument to 'download_target()'.
1219+
destination_directory = self.make_temp_directory()
1220+
target_filepaths = \
1221+
list(self.repository_updater.metadata['current']['targets']['targets'].keys())
1222+
1223+
# Get the target info, which is an argument to 'download_target()'.
1224+
target_filepath = target_filepaths.pop()
1225+
targetinfo = self.repository_updater.get_one_valid_targetinfo(target_filepath)
1226+
download_filepath = os.path.join(destination_directory, target_filepath)
1227+
1228+
# Test passing a handler with correct signature but incorrect implementation
1229+
def test_1(param_1, param2):
1230+
pass
1231+
1232+
self.assertRaises(tuf.exceptions.NoWorkingMirrorError,
1233+
self.repository_updater.download_target, targetinfo, destination_directory,
1234+
custom_download_handler=test_1)
1235+
1236+
# Checks if the file is permanently stored
1237+
self.assertFalse(os.path.exists(download_filepath))
1238+
1239+
# Test passing a handler with incorrect number of parameters
1240+
def test_2(param_1, param_2, param_3):
1241+
pass
1242+
1243+
with self.assertRaises(tuf.exceptions.NoWorkingMirrorError) as cm:
1244+
self.repository_updater.download_target(targetinfo, destination_directory,
1245+
custom_download_handler=test_2)
1246+
1247+
for _, mirror_error in six.iteritems(cm.exception.mirror_errors):
1248+
self.assertTrue(isinstance(mirror_error, TypeError))
1249+
1250+
# Checks if the file is permanently stored
1251+
self.assertFalse(os.path.exists(download_filepath))
1252+
1253+
# Test passing a handler throwing an unexpected error
1254+
def test_3(param_1, param2):
1255+
raise IOError
1256+
1257+
with self.assertRaises(tuf.exceptions.NoWorkingMirrorError) as cm:
1258+
self.repository_updater.download_target(targetinfo, destination_directory,
1259+
custom_download_handler=test_3)
1260+
1261+
for _, mirror_error in six.iteritems(cm.exception.mirror_errors):
1262+
self.assertTrue(isinstance(mirror_error, IOError))
1263+
1264+
# Checks if the file has been successfully downloaded
1265+
self.assertFalse(os.path.exists(download_filepath))
1266+
1267+
# Test: default case.
1268+
self.repository_updater.download_target(targetinfo, destination_directory)
1269+
1270+
# Checks if the file has been successfully downloaded
1271+
self.assertTrue(os.path.exists(download_filepath))
1272+
1273+
# Test: normal case.
1274+
self.repository_updater.download_target(targetinfo, destination_directory,
1275+
custom_download_handler=tuf.download.safe_download)
1276+
1277+
# Checks if the file has been successfully downloaded
1278+
self.assertTrue(os.path.exists(download_filepath))
1279+
1280+
1281+
1282+
12161283
def test_6_download_target(self):
12171284
# Create temporary directory (destination directory of downloaded targets)
12181285
# that will be passed as an argument to 'download_target()'.

0 commit comments

Comments
 (0)