generated from microsoft/MLOpsPython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smoke_test_scoring_service.py
91 lines (74 loc) · 2.42 KB
/
smoke_test_scoring_service.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
import argparse
import requests
import time
from azureml.core import Workspace
from azureml.core.webservice import AksWebservice, AciWebservice
from ml_service.util.env_variables import Env
import secrets
input = {"data": [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]]}
output_len = 2
def call_web_service(e, service_type, service_name):
aml_workspace = Workspace.get(
name=e.workspace_name,
subscription_id=e.subscription_id,
resource_group=e.resource_group
)
print("Fetching service")
headers = {}
if service_type == "ACI":
service = AciWebservice(aml_workspace, service_name)
else:
service = AksWebservice(aml_workspace, service_name)
if service.auth_enabled:
service_keys = service.get_keys()
headers['Authorization'] = 'Bearer ' + service_keys[0]
print("Testing service")
print(". url: %s" % service.scoring_uri)
output = call_web_app(service.scoring_uri, headers)
return output
def call_web_app(url, headers):
# Generate an HTTP 'traceparent' distributed tracing header
# (per the W3C Trace Context proposed specification).
headers['traceparent'] = "00-{0}-{1}-00".format(
secrets.token_hex(16), secrets.token_hex(8))
retries = 600
for i in range(retries):
try:
response = requests.post(
url, json=input, headers=headers)
response.raise_for_status()
return response.json()
except requests.exceptions.HTTPError as e:
if i == retries - 1:
raise e
print(e)
print("Retrying...")
time.sleep(1)
def main():
parser = argparse.ArgumentParser("smoke_test_scoring_service.py")
parser.add_argument(
"--type",
type=str,
choices=["AKS", "ACI", "Webapp"],
required=True,
help="type of service"
)
parser.add_argument(
"--service",
type=str,
required=True,
help="Name of the image to test"
)
args = parser.parse_args()
e = Env()
if args.type == "Webapp":
output = call_web_app(args.service, {})
else:
output = call_web_service(e, args.type, args.service)
print("Verifying service output")
assert "result" in output
assert len(output["result"]) == output_len
print("Smoke test successful.")
if __name__ == '__main__':
main()