kumquat-buildroot/support/testing/tests/download/gitremote.py
Ricardo Martincoski 4a40d36f13 support/testing: switch to Python 3 only
Python 2.7 will not be maintained past 2020.

Many scripts on the tree are used during the build and should keep
Python 2 compatibility for a while.
This is not the case for the runtime test infra. It's meant to be run in
modern distros only, so it can safely switch to support Python 3 only.

An advantage of this approach is to have less scenarios to test in.
Otherwise every change to the test infra or runtime tests would need to
be tested against both versions of the interpreter, increasing the
effort of the developers, to ensure the compatibility to Python 2 was
not broken.

In order to accomplish the change to Python 3:
 - change the shebang for run-tests;
 - use Python 3 urllib as a drop-in replacement for Python 2 urllib2;
 - when writing the downloaded binary files, explicitly open the output
   file as binary;
 - when subprocess is used to retrieve the text output from commands,
   explicitly ask for text output. For this, use 'universal_newlines'
   because 'text' was added only on Python 3.7;
 - when pexpect is used to retrieve the text output from qemu or git,
   explicitly ask for text output using 'encoding';
 - the code using csv currently follows the example in the documentation
   for the Python 2 module, change it to follow the example in the
   documentation for the Python 3 module;
 - fix the relative import for test_git.py to be Python 3 compliant.

Signed-off-by: Ricardo Martincoski <ricardo.martincoski@gmail.com>
Cc: Arnout Vandecappelle <arnout@mind.be>
Cc: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
Cc: Yann E. MORIN <yann.morin.1998@free.fr>
Tested-by: Romain Naour <romain.naour@smile.fr>
Tested-by: Nicolas Carrier <nicolas.carrier@orolia.com>
Signed-off-by: Nicolas Carrier <nicolas.carrier@orolia.com>
Signed-off-by: Arnout Vandecappelle (Essensium/Mind) <arnout@mind.be>
2019-10-28 22:14:07 +01:00

48 lines
1.9 KiB
Python

# subprocess does not kill the child daemon when a test case fails by raising
# an exception. So use pexpect instead.
import infra
import pexpect
GIT_REMOTE_PORT_INITIAL = 9418
GIT_REMOTE_PORT_LAST = GIT_REMOTE_PORT_INITIAL + 99
class GitRemote(object):
def __init__(self, builddir, serveddir, logtofile):
"""
Start a local git server.
In order to support test cases in parallel, select the port the
server will listen to in runtime. Since there is no reliable way
to allocate the port prior to starting the server (another
process in the host machine can use the port between it is
selected from a list and it is really allocated to the server)
try to start the server in a port and in the case it is already
in use, try the next one in the allowed range.
"""
self.daemon = None
self.port = None
self.logfile = infra.open_log_file(builddir, "gitremote", logtofile)
daemon_cmd = ["git", "daemon", "--reuseaddr", "--verbose",
"--listen=localhost", "--export-all",
"--base-path={}".format(serveddir)]
for port in range(GIT_REMOTE_PORT_INITIAL, GIT_REMOTE_PORT_LAST + 1):
cmd = daemon_cmd + ["--port={port}".format(port=port)]
self.logfile.write("> starting git remote with '{}'\n".format(" ".join(cmd)))
self.daemon = pexpect.spawn(cmd[0], cmd[1:], logfile=self.logfile,
encoding='utf-8')
ret = self.daemon.expect(["Ready to rumble",
"Address already in use"])
if ret == 0:
self.port = port
return
raise SystemError("Could not find a free port to run git remote")
def stop(self):
if self.daemon is None:
return
self.daemon.terminate(force=True)