-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
75 lines (59 loc) · 1.63 KB
/
server.py
File metadata and controls
75 lines (59 loc) · 1.63 KB
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
import base64
import json
import os
import traceback
import uuid
import sys
import flask
import flask.json
import gevent.wsgi
import pymysql
import redis
app = flask.Flask(__name__)
relationships = json.loads(base64.b64decode(os.environ["PLATFORM_RELATIONSHIPS"]))
@app.route('/')
def root():
tests = {}
tests["mysql"] = wrap_test(test_mysql, relationships["mysql"][0])
tests["redis"] = wrap_test(test_redis, relationships["redis"][0])
return flask.json.jsonify(tests)
def wrap_test(callback, *args, **kwargs):
try:
result = callback(*args, **kwargs)
return {
"status": "OK",
"return": result,
}
except Exception:
return {
"status": "ERROR",
"error": traceback.format_exception(*sys.exc_info())
}
def test_mysql(instance):
connection = pymysql.connect(
host=instance["host"],
user=instance["username"],
password=instance["password"],
db=instance["path"],
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor,
)
try:
with connection.cursor() as cursor:
cursor.execute("SELECT 1")
cursor.fetchone()
finally:
connection.close()
def test_redis(instance):
r = redis.StrictRedis(
host=instance["host"],
port=instance["port"],
db=0,
)
key_name = "foo-%s" + str(uuid.uuid4())
value = "bar"
r.set(key_name, "bar")
assert value == r.get(key_name)
if __name__ == "__main__":
http_server = gevent.wsgi.WSGIServer(('127.0.0.1', int(os.environ["PORT"])), app)
http_server.serve_forever()