Skip to content

Commit b1cb6e8

Browse files
committed
Add test_fetcher
Add unit test for fetcher.py Signed-off-by: Teodora Sechkova <[email protected]>
1 parent e82fd14 commit b1cb6e8

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

tests/test_fetcher.py

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
<Program>
5+
test_download.py
6+
7+
<Author>
8+
Teodora Sechkova [email protected]
9+
10+
<Started>
11+
December 18, 2020.
12+
13+
<Copyright>
14+
See LICENSE-MIT OR LICENSE for licensing information.
15+
16+
<Purpose>
17+
Unit test for 'fetcher.py'.
18+
"""
19+
20+
import logging
21+
import os
22+
import io
23+
import sys
24+
import unittest
25+
import tempfile
26+
27+
import tuf
28+
import tuf.unittest_toolbox as unittest_toolbox
29+
import tuf.exceptions
30+
import tuf.fetcher
31+
32+
from tests import utils
33+
34+
35+
logger = logging.getLogger(__name__)
36+
37+
38+
class TestFetcher(unittest_toolbox.Modified_TestCase):
39+
def setUp(self):
40+
"""
41+
Create a temporary file and launch a simple server in the
42+
current working directory.
43+
"""
44+
45+
unittest_toolbox.Modified_TestCase.setUp(self)
46+
47+
# Making a temporary file.
48+
current_dir = os.getcwd()
49+
target_filepath = self.make_temp_data_file(directory=current_dir)
50+
self.target_fileobj = open(target_filepath, 'r')
51+
self.file_contents = self.target_fileobj.read()
52+
self.file_length = len(self.file_contents)
53+
54+
# Launch a SimpleHTTPServer (serves files in the current dir).
55+
self.server_process_handler = utils.TestServerProcess(log=logger)
56+
57+
rel_target_filepath = os.path.basename(target_filepath)
58+
self.url = 'http://localhost:' \
59+
+ str(self.server_process_handler.port) + '/' + rel_target_filepath
60+
61+
# Create a temporary file where the target file chunks are written
62+
# during fetching
63+
self.temp_file = tempfile.TemporaryFile()
64+
self.fetcher = tuf.fetcher.RequestsFetcher()
65+
66+
67+
# Stop server process and perform clean up.
68+
def tearDown(self):
69+
unittest_toolbox.Modified_TestCase.tearDown(self)
70+
71+
# Cleans the resources and flush the logged lines (if any).
72+
self.server_process_handler.clean()
73+
74+
self.target_fileobj.close()
75+
self.temp_file.close()
76+
77+
78+
# Test: Normal case.
79+
def test_fetch(self):
80+
81+
for chunk in self.fetcher.fetch(self.url, self.file_length):
82+
self.temp_file.write(chunk)
83+
84+
self.temp_file.seek(0)
85+
temp_file_data = self.temp_file.read().decode('utf-8')
86+
self.assertEqual(self.file_contents, temp_file_data)
87+
self.assertEqual(self.file_length, len(temp_file_data))
88+
89+
90+
# Test if fetcher downloads file up to a required length
91+
def test_fetch_restricted_length(self):
92+
for chunk in self.fetcher.fetch(self.url, self.file_length-4):
93+
self.temp_file.write(chunk)
94+
95+
self.temp_file.seek(0, io.SEEK_END)
96+
self.assertEqual(self.temp_file.tell(), self.file_length-4)
97+
98+
99+
# Test if fetcher does not downlad more than actual file length
100+
def test_fetch_upper_length(self):
101+
for chunk in self.fetcher.fetch(self.url, self.file_length+4):
102+
self.temp_file.write(chunk)
103+
104+
self.temp_file.seek(0, io.SEEK_END)
105+
self.assertEqual(self.temp_file.tell(), self.file_length)
106+
107+
108+
# Test incorrect URL parsing
109+
def test_url_parsing(self):
110+
with self.assertRaises(tuf.exceptions.URLParsingError) as cm:
111+
for chunk in self.fetcher.fetch(self.random_string(), self.file_length):
112+
self.temp_file.write(chunk)
113+
114+
115+
116+
# Run unit test.
117+
if __name__ == '__main__':
118+
utils.configure_test_logging(sys.argv)
119+
unittest.main()

0 commit comments

Comments
 (0)