Skip to content

Commit d6c7f26

Browse files
authored
Delete Test Resources for Device Advisor (#285)
* update DA script: now it uploads the logs to s3 before delete the testing thing. * make sure delete test resource before exit * add backoff for device advisor request * update the print statement for logs path
1 parent 6c111aa commit d6c7f26

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

builder.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"DA_TOPIC": "test/da",
1818
"DA_SHADOW_PROPERTY": "datest",
1919
"DA_SHADOW_VALUE_SET": "ON",
20-
"DA_SHADOW_VALUE_DEFAULT": "OFF"
20+
"DA_SHADOW_VALUE_DEFAULT": "OFF",
21+
"DA_S3_NAME": "aws-iot-sdk-deviceadvisor-logs"
2122
},
2223
"hosts": {
2324
"ubuntu": {

deviceadvisor/script/DATestRun.py

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import json
44
import os
55
import subprocess
6-
import platform
6+
import re
7+
import random
78
from time import sleep
89

910
##############################################
@@ -20,13 +21,37 @@ def delete_thing_with_certi(thingName, certiId, certiArn):
2021
os.remove(os.environ["DA_CERTI"])
2122
os.remove(os.environ["DA_KEY"])
2223

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))
2343

2444
##############################################
2545
# Initialize variables
2646
# create aws clients
2747
client = boto3.client('iot')
2848
dataClient = boto3.client('iot-data')
2949
deviceAdvisor = boto3.client('iotdeviceadvisor')
50+
s3 = boto3.resource('s3')
51+
52+
# const
53+
BACKOFF_BASE = 5
54+
BACKOFF_MAX = 10
3055

3156
# load test config
3257
f = open('deviceadvisor/script/DATestConfig.json')
@@ -159,6 +184,7 @@ def delete_thing_with_certi(thingName, certiId, certiArn):
159184
# 'createdAt': datetime(2015, 1, 1)
160185
# }
161186
print("[Device Advisor]Info: Start device advisor test: " + test_name)
187+
sleep_with_backoff(BACKOFF_BASE, BACKOFF_MAX)
162188
test_start_response = deviceAdvisor.start_suite_run(
163189
suiteDefinitionId=DATestConfig['test_suite_ids'][test_name],
164190
suiteRunConfiguration={
@@ -175,8 +201,8 @@ def delete_thing_with_certi(thingName, certiId, certiArn):
175201
os.environ['DA_ENDPOINT'] = endpoint_response['endpoint']
176202

177203
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)
180206
test_result_responds = deviceAdvisor.get_suite_run(
181207
suiteDefinitionId=DATestConfig['test_suite_ids'][test_name],
182208
suiteRunId=test_start_response['suiteRunId']
@@ -204,10 +230,18 @@ def delete_thing_with_certi(thingName, certiId, certiArn):
204230
# If the test finalizing or store the test result
205231
elif (test_result_responds['status'] != 'RUNNING'):
206232
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 )
209242
break
210243
except Exception as e:
244+
delete_thing_with_certi(thing_name, certificate_id ,certificate_arn)
211245
print("[Device Advisor]Error: Failed to test: "+ test_name)
212246

213247
##############################################

0 commit comments

Comments
 (0)