Skip to content

Delete Test Resources for Device Advisor #285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion builder.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"DA_TOPIC": "test/da",
"DA_SHADOW_PROPERTY": "datest",
"DA_SHADOW_VALUE_SET": "ON",
"DA_SHADOW_VALUE_DEFAULT": "OFF"
"DA_SHADOW_VALUE_DEFAULT": "OFF",
"DA_S3_NAME": "aws-iot-sdk-deviceadvisor-logs"
},
"hosts": {
"ubuntu": {
Expand Down
29 changes: 26 additions & 3 deletions deviceadvisor/script/DATestRun.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import json
import os
import subprocess
import platform
import re
from time import sleep

##############################################
Expand All @@ -20,13 +20,28 @@ def delete_thing_with_certi(thingName, certiId, certiArn):
os.remove(os.environ["DA_CERTI"])
os.remove(os.environ["DA_KEY"])

# Export the testing log and upload it to S3 bucket
def process_logs(log_group, log_stream, thing_name):
logs_client = boto3.client('logs')
response = logs_client.get_log_events(
logGroupName=log_group,
logStreamName=log_stream
)
log_file = thing_name + ".log"
f = open(log_file, 'w')
for event in response["events"]:
f.write(event['message'])
f.close()
s3.Bucket(os.environ['DA_S3_NAME']).upload_file(log_file, log_file)
os.remove(log_file)

##############################################
# Initialize variables
# create aws clients
client = boto3.client('iot')
dataClient = boto3.client('iot-data')
deviceAdvisor = boto3.client('iotdeviceadvisor')
s3 = boto3.resource('s3')

# load test config
f = open('deviceadvisor/script/DATestConfig.json')
Expand Down Expand Up @@ -204,10 +219,18 @@ def delete_thing_with_certi(thingName, certiId, certiArn):
# If the test finalizing or store the test result
elif (test_result_responds['status'] != 'RUNNING'):
test_result[test_name] = test_result_responds['status']
if(test_result[test_name] == "PASS"):
delete_thing_with_certi(thing_name, certificate_id ,certificate_arn )
# If the test failed, upload the logs to S3 before clean up
if(test_result[test_name] != "PASS"):
log_url = test_result_responds['testResult']['groups'][0]['tests'][0]['logUrl']
group_string = re.search('group=(.*);', log_url)
log_group = group_string.group(1)
stream_string = re.search('stream=(.*)', log_url)
log_stream = stream_string.group(1)
process_logs(log_group, log_stream, thing_name)
delete_thing_with_certi(thing_name, certificate_id ,certificate_arn )
break
except Exception as e:
delete_thing_with_certi(thing_name, certificate_id ,certificate_arn)
print("[Device Advisor]Error: Failed to test: "+ test_name)

##############################################
Expand Down