Skip to content

Commit

Permalink
Ensure that OAuthServerMock is running before proceeding test
Browse files Browse the repository at this point in the history
  • Loading branch information
jirutka committed Dec 1, 2015
1 parent d8a0b6d commit 000d8df
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
4 changes: 1 addition & 3 deletions integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,10 @@ def oaas(request):
extra_conf = marker.kwargs if marker else {}
config = merge_dicts(proxy_conf, oaas_state, extra_conf)

process = OAuthServerMock(config=config, port=oaas_port, server='cherrypy')
process = BottleServer(OAuthServerMock(config), port=oaas_port)
process.start()
request.addfinalizer(process.terminate)

sleep(0.1)


@fixture(scope='module')
def http():
Expand Down
42 changes: 36 additions & 6 deletions integration/support/oaas_mock.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
from . import free_tcp_port

from base64 import b64decode
from bottle import Bottle, ConfigDict, HTTPError, LocalRequest, redirect
import json
from multiprocessing import Process

__all__ = ['OAuthServerMock']
from bottle import Bottle, ConfigDict, HTTPError, LocalRequest, abort, redirect
import requests
from requests import ConnectionError
from retry import retry

__all__ = ['OAuthServerMock', 'BottleServer']


def OAuthServerMock(config, **run_opts):
def OAuthServerMock(config):
conf = ConfigDict().load_dict(config)
app = Bottle()
request = LocalRequest()

@app.get('/')
def get_root():
abort(200, 'OK')

@app.get('/authorize')
def get_authorize():
query = request.query
Expand Down Expand Up @@ -91,10 +101,30 @@ def get_userinfo():
def handle_error(error):
return error.body

def _run():
app.run(**run_opts)
return app


class BottleServer(Process):

def __init__(self, bottle_app, port=free_tcp_port(), server='cherrypy',
check_url=None, bottle_opts={}):
opts = {'port': port, 'server': server, **bottle_opts}

super().__init__(target=bottle_app.run, kwargs=opts, daemon=True)
self._check_url = check_url or "http://%s:%d" % (opts.get('host', 'localhost'), port)

def start(self):
super().start()

try: # sanity check
self._request_check_url()
except ConnectionError as e:
self.terminate()
raise e

return Process(target=_run, daemon=True)
@retry(ConnectionError, tries=20, delay=0.1)
def _request_check_url(self):
return requests.get(self._check_url)


class OAASError(HTTPError):
Expand Down

0 comments on commit 000d8df

Please sign in to comment.