From a32ec0c8e2924aca80b14fccc175f5fc360ec992 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Tue, 26 Sep 2017 13:26:20 +0300 Subject: Fix: use random port when running tests --- apifw.yarn | 15 ++++++++++----- apitest.py | 4 ++-- randport | 28 ++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 7 deletions(-) create mode 100755 randport diff --git a/apifw.yarn b/apifw.yarn index 3e69c38..a914ab9 100644 --- a/apifw.yarn +++ b/apifw.yarn @@ -63,10 +63,12 @@ It's a silly name. Please suggest something better. # FIXME: It would be good for the test suite to pick a random free # port. But that's not simple. export APITEST_LOG="$DATADIR/apitest.log" - gunicorn3 --daemon --bind 127.0.0.1:12765 -p "$DATADIR/pid" \ + "$SRCDIR/randport" > "$DATADIR/port" + port="$(cat "$DATADIR/port")" + gunicorn3 --daemon --bind "127.0.0.1:$port" -p "$DATADIR/pid" \ --log-file "$DATADIR/log" --log-level=debug \ apitest:app - while ! curl -s http://127.0.0.1:12765/version > /dev/null + while ! curl -s "http://127.0.0.1:$port/version" > /dev/null do # Sleep in Debian can take a fractional second arg. sleep 0.1 @@ -76,20 +78,23 @@ It's a silly name. Please suggest something better. kill "$(cat "$DATADIR/pid")" IMPLEMENTS WHEN client requests GET /version without token - curl -sv "http://127.0.0.1:12765/version" > "$DATADIR/out" 2> "$DATADIR/err" + port="$(cat "$DATADIR/port")" + curl -sv "http://127.0.0.1:$port/version" > "$DATADIR/out" 2> "$DATADIR/err" IMPLEMENTS WHEN client requests GET /version using token token="$(cat "$DATADIR/token")" + port="$(cat "$DATADIR/port")" curl -sv -H "Authorization: Bearer $token" \ - "http://127.0.0.1:12765/version" > "$DATADIR/out" 2> "$DATADIR/err" + "http://127.0.0.1:$port/version" > "$DATADIR/out" 2> "$DATADIR/err" IMPLEMENTS WHEN client uploads a fake jpg token="$(cat "$DATADIR/token")" + port="$(cat "$DATADIR/port")" curl -sv -H "Authorization: Bearer $token" \ -H "Content-type: application/jpeg" \ -d "fake jpg" \ -X PUT \ - "http://127.0.0.1:12765/upload" > "$DATADIR/out" 2> "$DATADIR/err" + "http://127.0.0.1:$port/upload" > "$DATADIR/out" 2> "$DATADIR/err" IMPLEMENTS WHEN client gets an authorization token with scope "(.+)" iss="$(cat "$DATADIR/iss")" diff --git a/apitest.py b/apitest.py index 6e1867a..946e9e1 100644 --- a/apitest.py +++ b/apitest.py @@ -47,7 +47,7 @@ class Api(apifw.Api): }, ] - def version(self, content_type, body): + def version(self, content_type, body, **kwargs): return apifw.Response({ 'status': apifw.HTTP_OK, 'body': 'version: 4.2', @@ -56,7 +56,7 @@ class Api(apifw.Api): }, }) - def upload(self, content_type, body): + def upload(self, content_type, body, **kwargs): return apifw.Response({ 'status': apifw.HTTP_OK, 'body': 'thank you for %s\n' % body.decode('ascii'), diff --git a/randport b/randport new file mode 100755 index 0000000..d523401 --- /dev/null +++ b/randport @@ -0,0 +1,28 @@ +#!/usr/bin/python3 +# +# Copyright (C) 2017 Lars Wirzenius +# +# Pick a random port that is free to be listened on. For testing. + + +import errno +import random +import socket +import sys + + +MAX = 1000 +for i in range(MAX): + port = random.randint(1025, 2**15-1) + s = socket.socket() + try: + s.bind(('0.0.0.0', port)) + except OSError as e: + if e.errno == errno.EADDRINUSE: + continue + raise + break +else: + sys.stderr.write("Can't find a free port\n") + sys.exit(1) +sys.stdout.write('{}\n'.format(port)) -- cgit v1.2.1