forked from rcwoolley/device-cloud-python
-
Notifications
You must be signed in to change notification settings - Fork 2
/
validate_script.py
executable file
·522 lines (438 loc) · 17.3 KB
/
validate_script.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#!/usr/bin/env python
'''
Copyright (c) 2016-2017 Wind River Systems, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
OR CONDITIONS OF ANY KIND, either express or implied.
'''
"""
A script that executes an application which publishes known values to the Cloud
with the purpose of validating the abilities of the host to run the device_cloud agent.
In the Cloud there must be a thing definition with:
- method "pass_action" with one string parameter "param"
- method "fail_action" with no parameters
- property "property"
- attribute "attribute"
- alarm "alarm" with a possible state of 1
There must also be an associated application with "validat" in the name so the
token can be discovered.
"""
import getpass
import json
import os
import requests
import subprocess
import sys
import time
import platform
from datetime import datetime
if sys.version_info.major == 2:
input = raw_input
app_file = "validate_app.py"
cloud = ""
validate_app = None
default_thing_def = "hdc_validate_def"
default_app_name = "hdc_validate_app"
def _send(data, session_id=None):
headers = None
if session_id:
headers = {"sessionId":session_id}
datastr = json.dumps(data)
r = requests.post("https://"+cloud+"/api", headers=headers, data=datastr)
if r.status_code == 200:
try:
rjson = r.json()
if "auth" in rjson:
ret = rjson["auth"]
elif "cmd" in rjson:
ret = rjson["cmd"]
return ret
except Exception as error:
print(error)
return {"success":False, "content":r.content,"status_code":r.status_code}
def get_alarm(session_id, thing_key, alarm_name):
"""
Retrieve the last value of a sent alarm
"""
data_params = {"thingKey":thing_key,"key":alarm_name}
data = {"cmd":{"command":"alarm.current","params":data_params}}
return _send(data, session_id)
def get_app(session_id, name):
"""
Retreive a list of applications and their tokens
"""
data_params = {"name":name}
data = {"cmd":{"command":"app.find", "params":data_params}}
return _send(data, session_id)
def get_attribute(session_id, thing_key, attr_name):
"""
Retrieve the last value of a sent attribute
"""
data_params = {"thingKey":thing_key,"key":attr_name}
data = {"cmd":{"command":"attribute.current","params":data_params}}
return _send(data, session_id)
def get_files(session_id, thing_key):
"""
Retrieve a list of a specified thing's files
"""
data_params = {"thingKey":thing_key}
data = {"cmd":{"command":"file.list","params":data_params}}
return _send(data, session_id)
def get_location(session_id, thing_key):
"""
Retrieve last location of a specified thing
"""
data_params = {"thingKey":thing_key}
data = {"cmd":{"command":"location.current","params":data_params}}
return _send(data, session_id)
def get_logs(session_id, thing_key, start=None):
"""
Retrive logs from the Cloud
"""
data_params = {"thingKey":thing_key}
if start:
data_params.update({"start":start})
data = {"cmd":{"command":"log.list","params":data_params}}
return _send(data, session_id)
def get_property(session_id, thing_key, prop_name):
"""
Retrieve the last value of a sent property
"""
data_params = {"thingKey":thing_key,"key":prop_name}
data = {"cmd":{"command":"property.current","params":data_params}}
return _send(data, session_id)
def get_session(username, password):
"""
Get session for future communications with Cloud
"""
data_params = {"username":username,"password":password}
data = {"auth":{"command":"api.authenticate","params":data_params}}
return _send(data)
def get_thing(session_id, thing_key):
"""
Get information about a specific thing
"""
data_params = {"key":thing_key}
data = {"cmd":{"command":"thing.find","params":data_params}}
return _send(data, session_id)
def delete_thing(session_id, thing_key):
"""
delete the test thing
"""
data_params = {"key":thing_key}
data = {"cmd":{"command":"thing.delete","params":data_params}}
return _send(data, session_id)
def method_exec(session_id, thing_key, method_name, params=None):
"""
Execute Method
"""
data_params = {"thingKey":thing_key,"method":method_name,"ackTimeout":30}
if params:
data_params["params"] = params
data = {"cmd":{"command":"method.exec","params":data_params}}
return _send(data, session_id)
def stop_app(proc):
"""
Stop application by sending a newline to stdin
"""
if proc:
if proc.poll() is None:
out = proc.communicate(b"\n")
proc.wait()
def error_quit(*args):
"""
Print error messages, stop application, and exit with error code 1
"""
for arg in args:
if arg.__class__.__name__ == "str":
print(arg)
elif arg.__class__.__name__ == "Popen":
stop_app(arg)
sys.exit(1)
def strtotime(string):
"""
Convert string to a datetime object
"""
return datetime.strptime(string, "%Y-%m-%dT%H:%M:%S.%fZ")
def timetostr(dtime):
"""
Convert datetime into a string
"""
return dtime.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
def check_for_match(haystack, needle):
found = False
for x in range(len(haystack)):
if needle in haystack[x]['msg']:
print("Log: \"{}\" - OK".format(haystack[x]['msg']))
found = True
return found
def main():
"""
Main function to validate abilites of host
"""
global cloud
start_time = datetime.utcnow()
fails = []
default_device_id = "travisci-session-py-"
py_ver = platform.python_version()
default_device_id += py_ver.replace(".","")
if not os.path.isfile(app_file):
error_quit("Could not find app file {}.".format(app_file))
cloud = os.environ.get("HDCADDRESS")
username = os.environ.get("HDCUSERNAME")
password = os.environ.get("HDCPASSWORD")
token = os.environ.get("HDCAPPTOKEN")
# Get Cloud credentials
if not cloud:
cloud = input("Cloud Address: ")
if not username:
username = input("Username: ")
if not password:
password = getpass.getpass("Password: ")
# Ensure Cloud address is formatted correctly for later use
cloud = cloud.split("://")[-1]
cloud = cloud.split("/")[0]
print("Cloud: {}".format(cloud))
# Start a session with the Cloud
session_id = ""
session_info = get_session(username, password)
if session_info.get("success") is True:
session_id = session_info["params"].get("sessionId")
if session_id:
print("Session ID: {} - OK".format(session_id))
else:
error_quit("Failed to get session id.")
# Look for the app token created for this validation test.
# This token is looked for by name, so as long as the cloud has a validation
# app set up, the token does not need to be retrieved manually.
# if HDCAPPTOKEN was in the env use it. Otherwise, query it.
if not token:
validateapps = []
app_info = get_app(session_id, default_app_name)
#print(json.dumps(app_info, indent=2, sort_keys=True))
if app_info.get("success") is True:
app_list = app_info.get("params")
token = app_list["token"]
print("Token: {} - OK".format(token))
else:
error_quit("Either app does not exist or user does not have\n"
"permission to query the token.\n"
" * Please export HDCAPPTOKEN=<app token> and run again")
# Generate config for app with retrieved token
generate = subprocess.Popen("./generate_config.py -f validate.cfg "
"-c "+cloud+" -p 8883 "
"-t "+token, shell=True,
stdout=subprocess.PIPE, stdin=subprocess.PIPE)
ret = generate.wait()
if ret != 0:
error_quit("Failed to generate connection config for validation app.")
# Remove the downloaded file if it exists from a previous validation
if os.path.isfile(os.path.abspath("validate_download")):
os.remove(os.path.abspath("validate_download"))
# delete the thing in the cloud so that we don't have 100s of new
# instances of test apps. Write the device_id here and then check
# the cloud. Subsequent code will use the device_id. This test
# would normally be run in a docker instance with a new device_id each time.
if os.path.isfile("device_id"):
print("file device_id exists, using it")
with open("device_id", "r") as did_file:
device_id = did_file.read().strip()
thing_key = device_id + "-iot-validate-app"
else:
with open("device_id", "w") as did_file:
did_file.write( default_device_id )
device_id = default_device_id
thing_key = device_id + "-iot-validate-app"
print("Deleting thing key {} for this test".format(thing_key))
thing_info = delete_thing(session_id, thing_key)
#print(json.dumps(thing_info, indent=2, sort_keys=True))
time.sleep(2)
# Start app
validate_app = subprocess.Popen("."+os.sep+app_file,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
# Check to make sure thing is connected in Cloud
thing = None
connected = False
print("Checking thing key %s for connection" % thing_key)
for i in range(10):
thing_info = get_thing(session_id, thing_key)
#print(json.dumps(thing_info, indent=2, sort_keys=True))
if thing_info.get("success") is True:
thing = thing_info.get("params")
connected = thing.get("connected")
break
time.sleep(1)
if connected is True:
print("Connected - OK")
else:
error_quit("Thing not connected - FAIL", validate_app)
# Check that the expected property value was published to the Cloud
prop = None
for i in range(10):
prop_info = get_property(session_id, thing_key, "property")
#print(json.dumps(prop_info, indent=2, sort_keys=True))
if prop_info.get("success") is True:
prop = prop_info.get("params")
if prop:
if (strtotime(prop["ts"]) > start_time and
strtotime(prop["ts"]) < datetime.utcnow()):
if prop["value"] == 12.34:
print("Property: {} - OK".format(prop["value"]))
break
else:
print("Wrong property value: {} != 12.34 "
"- FAIL".format(prop["value"]))
fails.append("Property value")
else:
print("Property timestamp out of range of application - FAIL")
fails.append("Property time")
else:
print("Property not found in Cloud - FAIL")
fails.append("Property retrieval")
# Check that the expected atribute value was published to the Cloud
attr = None
for i in range(10):
attr_info = get_attribute(session_id, thing_key, "attribute")
#print(json.dumps(attr_info, indent=2, sort_keys=True))
if attr_info.get("success") is True:
attr = attr_info.get("params")
if attr:
if (strtotime(attr["ts"]) > start_time and
strtotime(attr["ts"]) < datetime.utcnow()):
if attr["value"] == "text and such":
print("Attribute: \"{}\" - OK".format(attr["value"]))
break
else:
print("Wrong attribute value: {} != \"text and such\" - FAIL".format(attr["value"]))
fails.append("Attribute value")
else:
print("Attribute timestamp out of range of application - FAIL")
fails.append("Attribute time")
else:
print("Attribute not found in Cloud - FAIL")
fails.append("Attribute retrieval")
# Check that the expected location was published to the Cloud
loc = None
for i in range(10):
loc_info = get_location(session_id, thing_key)
#print(json.dumps(loc_info, indent=2, sort_keys=True))
if loc_info.get("success") is True:
loc = loc_info.get("params")
if loc:
errors = []
if loc["lat"] != 45.351603:
errors.append("lat: {} != 45.351603".format(loc["lat"]))
if loc["lng"] != -75.918713:
errors.append("lng: {} != ".format(loc["lng"]))
if loc["heading"] != 12.34:
errors.append("heading: {} != 12.34".format(loc["heading"]))
if loc["altitude"] != 1.0:
errors.append("altitude: {} != 1.0".format(loc["altitude"]))
if loc["speed"] != 2.0:
errors.append("speed: {} != 2.0".format(loc["speed"]))
if loc["fixAcc"] != 3.0:
errors.append("fixAcc: {} != 3.0".format(loc["fixAcc"]))
if loc["fixType"] != "crystal ball":
errors.append("fix_type: {} != \"crystal ball\"".format(loc["fixType"]))
if errors:
print("Wrong location: {} - FAIL".format(", ".join(errors)))
fails.append("Location")
else:
print("Location: lat:{}, lng:{}, etc... - OK".format(loc["lat"], loc["lng"]))
break
else:
print("Location not found in Cloud - FAIL")
fails.append("Location retrieval")
# Check that the expected log was published to the Cloud.
# load the json obj and walk it to make sure
# recheck for logs if at first it fails
logs = None
found = False
for i in range(10):
logs_info = get_logs(session_id, thing_key, start=timetostr(start_time))
found = check_for_match(logs_info['params']['result'], "logs and such")
if found == True:
break
time.sleep(1)
if found == False:
print("No logs for this thing in the specified time frame - FAIL")
fails.append("Log retrieval")
# Check that the pass action executes and returns successfully
pass_act_info = method_exec(session_id, thing_key, "pass_action",
{"param":"value"})
#print(json.dumps(pass_act_info, indent=2, sort_keys=True))
if pass_act_info.get("success") is True:
print("Pass action success: True - OK")
else:
print("Pass action failed to complete successfully - FAIL")
fails.append("Action successful execution")
# Check that the fail action executes and returns a failure successfully
fail_act_info = method_exec(session_id, thing_key, "fail_action")
#print(json.dumps(fail_act_info, indent=2, sort_keys=True))
if fail_act_info.get("success") is False:
errmsgs = fail_act_info.get("errorMessages")
if len(errmsgs) == 1 and errmsgs[0] == "fail and such":
print("Fail action error message: \"fail and such\" - OK")
else:
print("Fail action error message: \"{}\" != \"fail and such\" - "
"FAIL".format(errmsgs[0]))
fails.append("Action error message")
else:
print("Fail action did not return failure - FAIL")
fails.append("Action failure")
# Check that a file was successfully uploaded to the Cloud
tries = 50
while tries > 0:
tries -= 1
time.sleep(0.5)
files_info = get_files(session_id, thing_key)
loop_done = False
if files_info.get("success") is True:
# there may be more than one file returned
file_results = files_info["params"]["result"]
for file in file_results:
if file["fileName"] == "validate_upload":
print("File uploaded: validate_upload - OK")
loop_done = True
break
if loop_done == True:
break
else:
if tries == 0:
print("File list could not be retrieved - FAIL")
fails.append("File list retrieval")
# Check that a file was successfully downloaded from the Cloud
tries = 50
while tries > 0:
tries -= 1
time.sleep(0.5)
if os.path.isfile(os.path.abspath("validate_download")):
print("File downloaded: validate_download - OK")
break
else:
if tries == 0:
print("Could not find downloaded file - FAIL")
fails.append("Download file")
stop_app(validate_app)
return fails
if __name__ == "__main__":
fails = []
try:
fails = main()
except Exception as error:
stop_app(validate_app)
raise(error)
if not fails:
print("\n\nAll passed! Success.")
sys.exit(0)
else:
print("\n\nFailed on the following:")
for fail in fails:
print(fail)
sys.exit(1)