-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabfile.py
112 lines (100 loc) · 4.09 KB
/
fabfile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
from __future__ import with_statement
from fabric.api import local, abort, lcd
import os
import time
import requests
import sys
import glob
PATH = os.path.abspath(os.path.dirname(__file__))
def add_to_PYTHONPATH():
print "Checking if endpoint/ is already in the PYTHONPATH..."
python_bin = os.path.dirname(sys.executable)
python_lib = os.path.join(python_bin, '..', 'lib/')
python_version = glob.glob(os.path.join(python_lib + 'python*'))
python_site_packages = os.path.join(python_version[0], 'site-packages/')
if not os.path.exists(os.path.join(python_site_packages, 'endpoint.pth')):
print "Adding endpoint/ to the PYTHONPATH..."
with lcd(os.path.join(python_site_packages)):
local('touch endpoint.pth')
local('echo "%s" >> endpoint.pth' % os.path.join(PATH, '..'))
else:
print "It's already there. No changes have been applied."
def test_suite():
"""
Run the ENDPOINT application's tests suite.
"""
with lcd(PATH):
local("python run_mock.py &")
time.sleep(3)
local("nosetests")
local("pkill -n python run_mock.py")
def supervise(spec_file="tests", mode='one_time', endpoint='', interval=60, test_mode=False):
"""
Supervise endpoints and inform in case of any problem.
There're two modes availables:
- one_time: Check the endpoints just a single time.
- strict: Check the endpoints every <interval> seconds.
Also, there's a special "mock" option which will simulate a server
with several predefined endpoints.
"""
successes = []
errors = []
sys.path.append(os.path.join(PATH,'..'))
if not os.path.exists(os.path.join(PATH, spec_file)):
abort("YML file not found.")
try:
interval = int(interval)
except ValueError:
abort("Interval parameter is not a number.")
with lcd(PATH):
print "Running server..."
local("python run_server.py %s &" % spec_file)
time.sleep(3)
if test_mode:
print "Running mock server..."
local("python run_mock.py &")
time.sleep(3)
if mode == 'one_time':
if _process_response(spec_file, endpoint, successes, errors):
_print_output(successes, errors)
elif mode =='strict':
counter = 0
while True:
if _process_response(spec_file, endpoint, successes, errors):
_print_output(successes, errors)
print "Next check will be in %d seconds. This is the attempt %d." % (interval, counter)
counter += 1
successes, errors = [],[]
time.sleep(interval)
else:
abort("Sorry but this mode is not currently supported.")
print "Stopping server..."
local("killall python run_server.py")
def _process_response(spec_file, endpoint, successes, errors):
from endpoint.settings import SERVER
print "Making the requests..."
response = requests.get("http://%s:%s/%s" %
(SERVER['host'], SERVER['port'], os.path.join(spec_file, endpoint)))
if response.text == '[]' or response.text == '{}':
print "Empty response. Are you sure that the specification file exists?"
return False
else:
if isinstance(response.json(), list):
for endpoint in response.json():
if endpoint['tests_passed'] == False:
errors.append(endpoint)
elif endpoint['tests_passed'] == True:
successes.append(endpoint)
else:
if response.json()['tests_passed'] == False:
errors.append(response.json())
else:
successes.append(response.json())
return True
def _print_output(successes, errors):
if len(errors) == 0:
print "Congrats, the endpoints work fine."
else:
print "Sorry but we encountered some problems in the endpoints."
print "%s" % str(errors)
print " # Results: %d successes and %d errors" % (len(successes), len(errors))