Merge pull request #985 from lukpueh/quickfix-win-py27-tests

Fix failing AppVeyor Python2.7 tests
This commit is contained in:
lukpueh 2020-03-03 12:04:53 +01:00 committed by GitHub
commit bb94304eb8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 78 deletions

View file

@ -124,7 +124,6 @@
packages = find_packages(exclude=['tests']),
scripts = [
'tuf/scripts/repo.py',
'tuf/scripts/client.py',
'tuf/scripts/simple_server.py',
'tuf/scripts/client.py'
]
)

View file

@ -35,8 +35,10 @@
import sys
import random
import platform
import six
from six.moves.SimpleHTTPServer import SimpleHTTPRequestHandler
PORT = 0
@ -55,7 +57,22 @@ def _port_gen():
else:
PORT = _port_gen()
Handler = six.moves.SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = six.moves.socketserver.TCPServer(('', PORT), Handler)
class QuietHTTPRequestHandler(SimpleHTTPRequestHandler):
"""A SimpleHTTPRequestHandler that does not write incoming requests to
stderr. """
def log_request(self, code='-', size='-'):
pass
# NOTE: On Windows/Python2 tests that use this simple_server.py in a
# subprocesses hang after a certain amount of requests (~68), if a PIPE is
# passed as Popen's stderr argument. As a simple workaround we silence the
# server on those Windows/Py2 to not fill the buffer.
if six.PY2 and platform.system() == 'Windows':
handler = QuietHTTPRequestHandler
else:
handler = SimpleHTTPRequestHandler
httpd = six.moves.socketserver.TCPServer(('', PORT), handler)
httpd.serve_forever()

View file

@ -128,8 +128,8 @@ def setUp(self):
while self.SERVER_PORT == self.SERVER_PORT2:
self.SERVER_PORT2 = random.SystemRandom().randint(30000, 45000)
command = ['python', '-m', 'tuf.scripts.simple_server', str(self.SERVER_PORT)]
command2 = ['python', '-m', 'tuf.scripts.simple_server', str(self.SERVER_PORT2)]
command = ['python', '-m', 'tests.simple_server', str(self.SERVER_PORT)]
command2 = ['python', '-m', 'tests.simple_server', str(self.SERVER_PORT2)]
self.server_process = subprocess.Popen(command, stderr=subprocess.PIPE,
cwd=self.repository_directory)

View file

@ -98,7 +98,7 @@ def setUpClass(cls):
# as a delegated role 'targets/role1', three target files, five key files,
# etc.
cls.SERVER_PORT = random.randint(30000, 45000)
command = ['python', '-m', 'tuf.scripts.simple_server', str(cls.SERVER_PORT)]
command = ['python', '-m', 'tests.simple_server', str(cls.SERVER_PORT)]
cls.server_process = subprocess.Popen(command, stderr=subprocess.PIPE)
logger.info('\n\tServer process started.')
logger.info('\tServer process id: '+str(cls.server_process.pid))
@ -1094,7 +1094,7 @@ def test_6_get_one_valid_targetinfo(self):
# The SimpleHTTPServer started in the setupclass has a tendency to
# timeout in Windows after a few tests.
SERVER_PORT = random.randint(30000, 45000)
command = ['python', '-m', 'tuf.scripts.simple_server', str(SERVER_PORT)]
command = ['python', '-m', 'tests.simple_server', str(SERVER_PORT)]
server_process = subprocess.Popen(command, stderr=subprocess.PIPE)
# NOTE: Following error is raised if a delay is not long enough:
@ -1361,7 +1361,7 @@ def test_7_updated_targets(self):
# The SimpleHTTPServer started in the setupclass has a tendency to
# timeout in Windows after a few tests.
SERVER_PORT = random.randint(30000, 45000)
command = ['python', '-m', 'tuf.scripts.simple_server', str(SERVER_PORT)]
command = ['python', '-m', 'tests.simple_server', str(SERVER_PORT)]
server_process = subprocess.Popen(command, stderr=subprocess.PIPE)
# NOTE: Following error is raised if a delay is not long enough to allow
@ -1493,7 +1493,7 @@ def test_8_remove_obsolete_targets(self):
# The SimpleHTTPServer started in the setupclass has a tendency to
# timeout in Windows after a few tests.
SERVER_PORT = random.randint(30000, 45000)
command = ['python', '-m', 'tuf.scripts.simple_server', str(SERVER_PORT)]
command = ['python', '-m', 'tests.simple_server', str(SERVER_PORT)]
server_process = subprocess.Popen(command, stderr=subprocess.PIPE)
# NOTE: Following error is raised if a delay is not long enough to allow
@ -1877,8 +1877,8 @@ def setUp(self):
self.SERVER_PORT = 30001
self.SERVER_PORT2 = 30002
command = ['python', '-m', 'tuf.scripts.simple_server', str(self.SERVER_PORT)]
command2 = ['python', '-m', 'tuf.scripts.simple_server', str(self.SERVER_PORT2)]
command = ['python', '-m', 'tests.simple_server', str(self.SERVER_PORT)]
command2 = ['python', '-m', 'tests.simple_server', str(self.SERVER_PORT2)]
self.server_process = subprocess.Popen(command, stderr=subprocess.PIPE,
cwd=self.repository_directory)

View file

@ -1,66 +0,0 @@
#!/usr/bin/env python
# Copyright 2012 - 2017, New York University and the TUF contributors
# SPDX-License-Identifier: MIT OR Apache-2.0
"""
<Program>
simple_server.py
<Author>
Konstantin Andrianov.
<Started>
February 15, 2012.
<Copyright>
See LICENSE-MIT OR LICENSE for licensing information.
<Purpose>
This is a basic server that was designed to be used in conjunction with
test_download.py to test download.py module.
<Reference>
SimpleHTTPServer:
https://docs.python.org/2/library/simplehttpserver.html
"""
# Help with Python 3 compatibility, where the print statement is a function, an
# implicit relative import is invalid, and the '/' operator performs true
# division. Example: print 'hello world' raises a 'SyntaxError' exception.
from __future__ import print_function
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
import sys
import random
import six
PORT = 0
def _port_gen():
return random.SystemRandom().randint(30000, 45000)
if len(sys.argv) > 1:
try:
PORT = int(sys.argv[1])
# Enforce arbitrarily chosen port range.
if PORT < 30000 or PORT > 45000:
raise ValueError
except ValueError:
PORT = _port_gen()
else:
PORT = _port_gen()
if __name__ == '__main__':
Handler = six.moves.SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = six.moves.socketserver.TCPServer(('', PORT), Handler)
httpd.serve_forever()