-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathspeedtest-cli-jsontest
76 lines (70 loc) · 2.63 KB
/
speedtest-cli-jsontest
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
import subprocess
import jsonschema
# Call speedtest and capture the output as a string
output = subprocess.check_output(["/var/lib/vastai_kaalia/version_263/speedtest-cli", "--json"])
# Define the JSON schema that describes the expected structure
schema = {
"type": "object",
"properties": {
"client": {
"type": "object",
"properties": {
"rating": {"type": "string"},
"loggedin": {"type": "string"},
"isprating": {"type": "string"},
"ispdlavg": {"type": "string"},
"ip": {"type": "string"},
"isp": {"type": "string"},
"lon": {"type": "string"},
"ispulavg": {"type": "string"},
"country": {"type": "string"},
"lat": {"type": "string"},
},
"required": ["rating", "loggedin", "isprating", "ispdlavg", "ip", "isp", "lon", "ispulavg", "country", "lat"]
},
"bytes_sent": {"type": "number"},
"download": {"type": "number"},
"timestamp": {"type": "string"},
"share": {"type": ["null", "string"]},
"bytes_received": {"type": "number"},
"ping": {"type": "number"},
"upload": {"type": "number"},
"server": {
"type": "object",
"properties": {
"latency": {"type": "number"},
"name": {"type": "string"},
"url": {"type": "string"},
"country": {"type": "string"},
"lon": {"type": "string"},
"cc": {"type": "string"},
"host": {"type": "string"},
"sponsor": {"type": "string"},
"lat": {"type": "string"},
"id": {"type": "string"},
"d": {"type": "number"},
},
"required": ["latency", "name", "url", "country", "lon", "cc", "host", "sponsor", "lat", "id", "d"]
}
},
"required": ["client", "bytes_sent", "download", "timestamp", "share", "bytes_received", "ping", "upload", "server"]
}
# Load the output as JSON
try:
data = json.loads(output)
except json.JSONDecodeError as e:
print("Error: Invalid JSON response")
print("JSONDecodeError:", e)
print("Output:", output.decode())
exit()
# Validate the JSON response against the schema
try:
jsonschema.validate(data, schema)
except jsonschema.exceptions.ValidationError as e:
print("Error: JSON response does not have expected structure")
print("ValidationError:", e)
exit()
print("Success: JSON response has expected structure")