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

Commit

Permalink
Add functions to start/stop/update stagecraft mock
Browse files Browse the repository at this point in the history
Add two functions to start/update and stop a StagecraftService based on
a behave context.
  • Loading branch information
robyoung committed May 18, 2014
1 parent 2e03a80 commit f1089e7
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 7 deletions.
24 changes: 21 additions & 3 deletions features/support/stagecraft.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
import os

from flask import Flask, Response, abort, json, request
from multiprocessing import Process
import requests

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
Expand All @@ -15,8 +32,8 @@ def __init__(self, port, routes):
@self.__app.route('/', defaults={'path': ''})
@self.__app.route('/<path:path>')
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:
Expand All @@ -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):
Expand Down
62 changes: 58 additions & 4 deletions features/support/tests/test_stagecraft.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit f1089e7

Please sign in to comment.