Skip to content
This repository has been archived by the owner on Mar 24, 2021. It is now read-only.

Commit

Permalink
Allow adding routes to a StagecraftService
Browse files Browse the repository at this point in the history
- Allow new routes to be added to a running StagecraftService
- Allow routes to be reset

This is going to be used to allow the admin feature tests to be migrated
to using stagecraft. To make this possible they will need to be able to
add both data-set routes and user routes to the same service.
  • Loading branch information
robyoung committed May 16, 2014
1 parent c87a511 commit 94c5ce7
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
32 changes: 22 additions & 10 deletions features/support/stagecraft.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@


class StagecraftService(object):
def __init__(self, port, url_response_dict):
def __init__(self, port, routes):
self.__port = port
self.__url_response_dict = url_response_dict
self.__routes = routes
self.__app = Flask('fake_stagecraft')
self.__proc = None

Expand All @@ -24,17 +24,36 @@ def catch_all(path):

key = (request.method, path_and_query)

resp_item = self.__url_response_dict.get(key, None)
resp_item = self.__routes.get(key, None)
if resp_item is None:
abort(404)
return Response(json.dumps(resp_item), mimetype='application/json')

def add_routes(self, routes):
self.__routes.update(routes)
self.restart()

def reset(self):
self.__routes = dict()
self.restart()

def start(self):
if self.stopped():
self.__proc = Process(target=self._run)
self.__proc.start()
wait_until(self.running)

def stop(self):
if self.running():
self.__proc.terminate()
self.__proc.join()
self.__proc = None
wait_until(self.stopped)

def restart(self):
self.stop()
self.start()

def running(self):
if self.__proc is None:
return False
Expand All @@ -47,13 +66,6 @@ def running(self):
def stopped(self):
return not self.running()

def stop(self):
if self.running():
self.__proc.terminate()
self.__proc.join()
self.__proc = None
wait_until(self.stopped)

def _run(self):
# reloading is disabled to stop the Flask webserver starting up twice
# when used in conjunction with multiprocessing
Expand Down
29 changes: 29 additions & 0 deletions features/support/tests/test_stagecraft.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ def test_calls_fail_after_service_is_stopped(self):
service.stop()
assert_raises(ConnectionError, requests.get, ('http://localhost:8089/example'))

def test_calls_succeed_after_service_is_restarted(self):
service = self.create_service()
service.restart()

response = requests.get('http://localhost:8089/example')

service.stop()
assert_that(response.status_code, is_(200))

def test_running_returns_true_if_the_service_is_running(self):
service = self.create_service()
assert_that(service.running(), is_(False))
Expand All @@ -45,3 +54,23 @@ def test_stopped_returns_true_if_the_service_is_not_running(self):
service.stop()
assert_that(service.stopped(), is_(True))

def test_new_routes_can_be_added_to_a_running_service(self):
service = self.create_service()
service.start()
service.add_routes({
('GET', u'foobar'): {u'bar': u'foo'}})

response = requests.get('http://localhost:8089/foobar')

service.stop()

assert_that(response.json(), is_({u'bar': u'foo'}))

def test_all_routes_can_be_removed_from_a_running_service(self):
service = self.create_service()
service.start()
service.reset()
response = requests.get('http://localhost:8089/example')
service.stop()

assert_that(response.status_code, is_(404))

0 comments on commit 94c5ce7

Please sign in to comment.