3
3
import json
4
4
import os
5
5
import subprocess
6
- import platform
6
+ import re
7
+ import random
7
8
from time import sleep
8
9
9
10
##############################################
@@ -20,13 +21,37 @@ def delete_thing_with_certi(thingName, certiId, certiArn):
20
21
os .remove (os .environ ["DA_CERTI" ])
21
22
os .remove (os .environ ["DA_KEY" ])
22
23
24
+ # Export the testing log and upload it to S3 bucket
25
+ def process_logs (log_group , log_stream , thing_name ):
26
+ logs_client = boto3 .client ('logs' )
27
+ response = logs_client .get_log_events (
28
+ logGroupName = log_group ,
29
+ logStreamName = log_stream
30
+ )
31
+ log_file = thing_name + ".log"
32
+ f = open (log_file , 'w' )
33
+ for event in response ["events" ]:
34
+ f .write (event ['message' ])
35
+ f .close ()
36
+ s3 .Bucket (os .environ ['DA_S3_NAME' ]).upload_file (log_file , log_file )
37
+ os .remove (log_file )
38
+ print ("[Device Advisor] Device Advisor Log file uploaded to " + log_file )
39
+
40
+ # Sleep for a random time
41
+ def sleep_with_backoff (base , max ):
42
+ sleep (random .randint (base , max ))
23
43
24
44
##############################################
25
45
# Initialize variables
26
46
# create aws clients
27
47
client = boto3 .client ('iot' )
28
48
dataClient = boto3 .client ('iot-data' )
29
49
deviceAdvisor = boto3 .client ('iotdeviceadvisor' )
50
+ s3 = boto3 .resource ('s3' )
51
+
52
+ # const
53
+ BACKOFF_BASE = 5
54
+ BACKOFF_MAX = 10
30
55
31
56
# load test config
32
57
f = open ('deviceadvisor/script/DATestConfig.json' )
@@ -159,6 +184,7 @@ def delete_thing_with_certi(thingName, certiId, certiArn):
159
184
# 'createdAt': datetime(2015, 1, 1)
160
185
# }
161
186
print ("[Device Advisor]Info: Start device advisor test: " + test_name )
187
+ sleep_with_backoff (BACKOFF_BASE , BACKOFF_MAX )
162
188
test_start_response = deviceAdvisor .start_suite_run (
163
189
suiteDefinitionId = DATestConfig ['test_suite_ids' ][test_name ],
164
190
suiteRunConfiguration = {
@@ -175,8 +201,8 @@ def delete_thing_with_certi(thingName, certiId, certiArn):
175
201
os .environ ['DA_ENDPOINT' ] = endpoint_response ['endpoint' ]
176
202
177
203
while True :
178
- # sleep for 1s every loop to avoid TooManyRequestsException
179
- sleep ( 1 )
204
+ # Add backoff to avoid TooManyRequestsException
205
+ sleep_with_backoff ( BACKOFF_BASE , BACKOFF_MAX )
180
206
test_result_responds = deviceAdvisor .get_suite_run (
181
207
suiteDefinitionId = DATestConfig ['test_suite_ids' ][test_name ],
182
208
suiteRunId = test_start_response ['suiteRunId' ]
@@ -204,10 +230,18 @@ def delete_thing_with_certi(thingName, certiId, certiArn):
204
230
# If the test finalizing or store the test result
205
231
elif (test_result_responds ['status' ] != 'RUNNING' ):
206
232
test_result [test_name ] = test_result_responds ['status' ]
207
- if (test_result [test_name ] == "PASS" ):
208
- delete_thing_with_certi (thing_name , certificate_id ,certificate_arn )
233
+ # If the test failed, upload the logs to S3 before clean up
234
+ if (test_result [test_name ] != "PASS" ):
235
+ log_url = test_result_responds ['testResult' ]['groups' ][0 ]['tests' ][0 ]['logUrl' ]
236
+ group_string = re .search ('group=(.*);' , log_url )
237
+ log_group = group_string .group (1 )
238
+ stream_string = re .search ('stream=(.*)' , log_url )
239
+ log_stream = stream_string .group (1 )
240
+ process_logs (log_group , log_stream , thing_name )
241
+ delete_thing_with_certi (thing_name , certificate_id ,certificate_arn )
209
242
break
210
243
except Exception as e :
244
+ delete_thing_with_certi (thing_name , certificate_id ,certificate_arn )
211
245
print ("[Device Advisor]Error: Failed to test: " + test_name )
212
246
213
247
##############################################
0 commit comments