forked from open-lambda/open-lambda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboss_test.py
127 lines (99 loc) · 3.6 KB
/
boss_test.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import os, sys
import json
import time
from subprocess import run
import requests
api_key = None
boss_port = 5000
def read_json(path):
with open(path, encoding="utf-8") as f:
return json.load(f)
def write_json(path, data):
with open(path, "w", encoding="utf-8") as f:
return json.dump(data, f, indent=2)
def boss_get(resource, check=True):
url = f"http://localhost:{boss_port}/{resource}"
resp = requests.get(url)
if check:
resp.raise_for_status()
return resp.text
def boss_post(resource, data, check=True):
url = f"http://localhost:{boss_port}/{resource}"
resp = requests.post(url, headers={"api_key": api_key}, data=data)
if check:
resp.raise_for_status()
return resp
def boss_invoke(lambda_name, data, check=True):
url = f"http://localhost:{boss_port}/run/{lambda_name}"
resp = requests.post(url, data=data)
if check:
resp.raise_for_status()
return resp
def tester(platform):
global api_key, boss_port
print(f"Testing {platform}")
# PART 0: clear existing config
if os.path.exists("boss.json"):
run(["rm", "boss.json"]).check_returncode()
# PART 1: config and launch
# should create new config file
run(["./ol", "new-boss"]).check_returncode()
assert os.path.exists("boss.json")
# should have options platform (e.g., "aws", etc) and scaling ("manual" or "auto")
config = read_json("boss.json")
assert "platform" in config
assert "scaling" in config
config["platform"] = platform
config["scaling"] = "manual"
write_json("boss.json", config)
# config should contain randomly generate secret API key
assert "api_key" in config
assert "boss_port" in config
api_key = config["api_key"]
boss_port = config["boss_port"]
boss_port = 5000
# should be able to start boss as background process
run(["./ol", "boss", "--detach"]).check_returncode()
time.sleep(1) # TODO: better ping
# PART 2: scaling
# should start with zero workers
status = boss_get("status")
status = json.loads(status)
assert len(status["workers"]) == 0
# start a worker (because we're chose "manual" scaling)
boss_post("scaling/worker_count", "1")
# there should be a worker, though probably not ready
status = boss_get("status")
status = json.loads(status)
assert len(status["workers"]) == 1
# {workers: [{"name": worker1, "state": ready}, {"name": worker2, "state": ready}]}
# wait until it is ready (up to 3 minutes)
t0 = t1 = time.time()
while t1 - t0 < 180:
time.sleep(1)
status = boss_get("status")
status = json.loads(status)
assert len(status["workers"]) == 1
if status["workers"][0]["state"] == "ready":
break
t1 = time.time()
# PART 3: registry
lambda1_name = "hi"
code = ["def f(event):", "\treturn 'hello'"]
boss_post("registry/upload", {"name": lambda1_name, "code": "\n".join(code)})
# PART 4: load balancing
# does it forward the request to a worker and give us the proper response?
result = boss_invoke(f"run/{lambda1_name}", None).json()
assert result == 'hello'
# if we should down all workers, do we get an error code back?
boss_post("scaling/worker_count", 0)
assert len(status["workers"]) == 0
resp = boss_invoke(f"run/{lambda1_name}", None, check=False)
assert resp.status_code != 200
def main():
if len(sys.argv) < 2:
print("Usage: python3 boss-test.py (aws|azure|gcp) [platform2, ...]")
for platform in sys.argv[1:]:
tester(platform)
if __name__ == "__main__":
main()