From f1089e71c827f13781b8791269ac03e94ae1182e Mon Sep 17 00:00:00 2001 From: Rob Young Date: Sun, 18 May 2014 11:18:24 +0100 Subject: [PATCH] Add functions to start/stop/update stagecraft mock Add two functions to start/update and stop a StagecraftService based on a behave context. --- features/support/stagecraft.py | 24 +++++++-- features/support/tests/test_stagecraft.py | 62 +++++++++++++++++++++-- 2 files changed, 79 insertions(+), 7 deletions(-) diff --git a/features/support/stagecraft.py b/features/support/stagecraft.py index a1d87258..ccac4611 100644 --- a/features/support/stagecraft.py +++ b/features/support/stagecraft.py @@ -1,3 +1,5 @@ +import os + from flask import Flask, Response, abort, json, request from multiprocessing import Process import requests @@ -5,6 +7,21 @@ from features.support.support import wait_until +def create_or_update_stagecraft_service(context, port, routes): + if 'mock_stagecraft_service' not in context or not context.mock_stagecraft_service: + context.mock_stagecraft_service = StagecraftService(port, routes) + context.mock_stagecraft_service.start() + else: + context.mock_stagecraft_service.add_routes(routes) + return context.mock_stagecraft_service + + +def stop_stagecraft_service_if_running(context): + if 'mock_stagecraft_service' in context and context.mock_stagecraft_service: + context.mock_stagecraft_service.stop() + context.mock_stagecraft_service = None + + class StagecraftService(object): def __init__(self, port, routes): self.__port = port @@ -15,8 +32,8 @@ def __init__(self, port, routes): @self.__app.route('/', defaults={'path': ''}) @self.__app.route('/') def catch_all(path): - if path == "_is_fake_server_up": - return Response('Yes', 200) + if path == '_is_fake_server_up': + return Response('Yes: {}'.format(os.getpid()), 200) path_and_query = path if len(request.query_string) > 0: @@ -26,7 +43,8 @@ def catch_all(path): resp_item = self.__routes.get(key, None) if resp_item is None: - abort(404) + abort(404, 'Known routes: {}'.format( + ', '.join([path for _, path in self.__routes.keys()]))) return Response(json.dumps(resp_item), mimetype='application/json') def add_routes(self, routes): diff --git a/features/support/tests/test_stagecraft.py b/features/support/tests/test_stagecraft.py index 50f765cc..47b5bc4d 100644 --- a/features/support/tests/test_stagecraft.py +++ b/features/support/tests/test_stagecraft.py @@ -3,7 +3,61 @@ from nose.tools import assert_raises from hamcrest import assert_that, is_ -from ..stagecraft import StagecraftService +from ..stagecraft import StagecraftService, create_or_update_stagecraft_service, stop_stagecraft_service_if_running + + +class StubContext(object): + def __init__(self): + self._params = {} + + def __getattr__(self, key): + return self._params[key] + + def __setattr__(self, key, value): + if key.startswith('_'): + self.__dict__[key] = value + else: + self._params[key] = value + + def __contains__(self, key): + return key in self._params + +def test_create_stagecraft_service(): + context = StubContext() + service = create_or_update_stagecraft_service(context, 2012, {}) + + assert_that(context.mock_stagecraft_service.running(), is_(True)) + + service.stop() + + assert_that(context.mock_stagecraft_service.stopped(), is_(True)) + + +def test_update_stagecraft_service(): + context = StubContext() + + service1 = create_or_update_stagecraft_service(context, 8089, {}) + response = requests.get('http://localhost:8089/example') + assert_that(response.status_code, is_(404)) + + service2 = create_or_update_stagecraft_service(context, 8089, + {('GET', u'example'): {u'foo': u'bar'}}) + response = requests.get('http://localhost:8089/example') + assert_that(response.status_code, is_(200)) + + assert_that(service1, is_(service2)) + + service1.stop() + + +def test_stop_stagecraft_if_running(): + context = StubContext() + + service = create_or_update_stagecraft_service(context, 8089, {}) + + stop_stagecraft_service_if_running(context) + + assert_that(service.running(), is_(False)) class TestStagecraftService(object): def create_service(self): @@ -16,7 +70,7 @@ def test_service_catches_calls(self): service.start() response = requests.get('http://localhost:8089/example') service.stop() - + assert_that(response.json(), is_({u'foo': u'bar'})) def test_calls_fail_if_service_is_not_started(self): @@ -59,11 +113,11 @@ def test_new_routes_can_be_added_to_a_running_service(self): 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):