3939import random
4040import subprocess
4141import sys
42- import time
4342import unittest
4443
4544import tuf
4847import tuf .unittest_toolbox as unittest_toolbox
4948import tuf .exceptions
5049
50+ import utils
51+
5152import requests .exceptions
5253
5354import securesystemslib
54- import six
5555
5656logger = logging .getLogger (__name__ )
5757
@@ -81,13 +81,7 @@ def setUp(self):
8181 junk , rel_target_filepath = os .path .split (target_filepath )
8282 self .url = 'http://localhost:' + str (self .PORT )+ '/' + rel_target_filepath
8383
84- # Provide a delay long enough to allow the HTTPS servers to start.
85- # Encountered an error on one test system at delay value of 0.2s, so
86- # increasing to 0.5s. Further increasing to 2s due to occasional failures
87- # in other tests in similar circumstances on AppVeyor.
88- # Expect to see "Connection refused" if this delay is not long enough
89- # (though other issues could cause that).
90- time .sleep (2 )
84+ utils .wait_for_server ('localhost' , self .PORT )
9185
9286 # Computing hash of target file data.
9387 m = hashlib .md5 ()
@@ -102,6 +96,8 @@ def tearDown(self):
10296 if self .server_proc .returncode is None :
10397 logger .info ('\t Server process ' + str (self .server_proc .pid )+ ' terminated.' )
10498 self .server_proc .kill ()
99+ # Drop return values of communicate()
100+ self .server_proc .communicate ()
105101 self .target_fileobj .close ()
106102
107103
@@ -129,8 +125,8 @@ def test_download_url_to_tempfileobj_and_lengths(self):
129125 # the server-reported length of the file does not match the
130126 # required_length. 'updater.py' *does* verify the hashes of downloaded
131127 # content.
132- download .safe_download (self .url , self .target_data_length - 4 )
133- download .unsafe_download (self .url , self .target_data_length - 4 )
128+ download .safe_download (self .url , self .target_data_length - 4 ). close ()
129+ download .unsafe_download (self .url , self .target_data_length - 4 ). close ()
134130
135131 # We catch 'tuf.exceptions.DownloadLengthMismatchError' for safe_download()
136132 # because it will not download more bytes than requested (in this case, a
@@ -140,7 +136,7 @@ def test_download_url_to_tempfileobj_and_lengths(self):
140136
141137 # Calling unsafe_download() with a mismatched length should not raise an
142138 # exception.
143- download .unsafe_download (self .url , self .target_data_length + 1 )
139+ download .unsafe_download (self .url , self .target_data_length + 1 ). close ()
144140
145141
146142
@@ -277,12 +273,8 @@ def test_https_connection(self):
277273 expd_https_server_proc = popen_python (
278274 ['simple_https_server.py' , port4 , expired_cert_fname ])
279275
280- # Provide a delay long enough to allow the four HTTPS servers to start.
281- # Have encountered errors at 0.2s, 0.5s, and 2s, primarily on AppVeyor.
282- # Increasing to 4s for this test.
283- # Expect to see "Connection refused" if this delay is not long enough
284- # (though other issues could cause that).
285- time .sleep (3 )
276+ for port in range (self .PORT + 1 , self .PORT + 5 ):
277+ utils .wait_for_server ('localhost' , port )
286278
287279 relative_target_fpath = os .path .basename (target_filepath )
288280 good_https_url = 'https://localhost:' + port1 + '/' + relative_target_fpath
@@ -314,13 +306,13 @@ def test_https_connection(self):
314306 # trusting the good certs (trusting the bad cert instead). Expect failure
315307 # because even though the server's cert file is otherwise OK, we don't
316308 # trust it.
317- print ('Trying HTTPS download of target file: ' + good_https_url )
309+ logger . info ('Trying HTTPS download of target file: ' + good_https_url )
318310 with self .assertRaises (requests .exceptions .SSLError ):
319311 download .safe_download (good_https_url , target_data_length )
320312 with self .assertRaises (requests .exceptions .SSLError ):
321313 download .unsafe_download (good_https_url , target_data_length )
322314
323- print ('Trying HTTPS download of target file: ' + good2_https_url )
315+ logger . info ('Trying HTTPS download of target file: ' + good2_https_url )
324316 with self .assertRaises (requests .exceptions .SSLError ):
325317 download .safe_download (good2_https_url , target_data_length )
326318 with self .assertRaises (requests .exceptions .SSLError ):
@@ -372,6 +364,8 @@ def test_https_connection(self):
372364 if proc .returncode is None :
373365 logger .info ('Terminating server process ' + str (proc .pid ))
374366 proc .kill ()
367+ # drop return values
368+ proc .communicate ()
375369
376370
377371
0 commit comments