Skip to content

Commit

Permalink
Add Device Advisor Jobs tests (#491)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfod authored Oct 23, 2023
1 parent 7b0d918 commit 18f78a0
Show file tree
Hide file tree
Showing 6 changed files with 374 additions and 16 deletions.
9 changes: 8 additions & 1 deletion deviceadvisor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,16 @@ for named shadows.

### Named Shadow Update

Validates if your device reads all update messages received and synchronizes the device's state to match the desired
Validates if a device reads all update messages received and synchronizes the device's state to match the desired
state properties.

This test executes
[Device updates reported state to desired state (Happy case)](https://docs.aws.amazon.com/iot/latest/developerguide/device-advisor-tests-shadow.html#update)
for named shadows.

### Job Execution

Validates if a device is able to receive updates using AWS IoT Jobs, and publish the status of successful updates.

This test executes
[Device can complete a job execution](https://docs.aws.amazon.com/iot/latest/developerguide/device-advisor-tests-job-execution.html).
5 changes: 5 additions & 0 deletions deviceadvisor/script/DATestConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
"test_suite_id" : "6s2hyjq6dkmb",
"test_exe_path" : "ShadowUpdate",
"cmd_args" : "--named-shadow"
},
{
"test_name" : "Job Execution",
"test_suite_id" : "6y02cc8096vl",
"test_exe_path" : "JobExecution"
}
]
}
33 changes: 20 additions & 13 deletions deviceadvisor/script/DATestRun.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ def process_logs(log_group, log_stream, thing_name):
f.close()

try:
secrets_client = boto3.client(
"secretsmanager", region_name=os.environ["AWS_DEFAULT_REGION"])
s3_bucket_name = secrets_client.get_secret_value(
SecretId="ci/DeviceAdvisor/s3bucket")["SecretString"]
s3.Bucket(s3_bucket_name).upload_file(log_file, log_file)
Expand Down Expand Up @@ -72,6 +70,8 @@ def sleep_with_backoff(base, max):
deviceAdvisor = boto3.client(
'iotdeviceadvisor', region_name=os.environ["AWS_DEFAULT_REGION"])
s3 = boto3.resource('s3', region_name=os.environ["AWS_DEFAULT_REGION"])
secrets_client = boto3.client(
"secretsmanager", region_name=os.environ["AWS_DEFAULT_REGION"])
except Exception:
print("[Device Advisor] Error: could not create boto3 clients.", file=sys.stderr)
exit(-1)
Expand Down Expand Up @@ -113,35 +113,44 @@ def sleep_with_backoff(base, max):

disabled = test_suite.get('disabled', False)
if disabled:
print(f"[Device Advisor] Info: "
"{test_name} test suite is disabled, skipping", file=sys.stderr)
print("[Device Advisor] Info: "
f"{test_name} test suite is disabled, skipping", file=sys.stderr)
continue

##############################################
# create a test thing
thing_name = "DATest_" + str(uuid.uuid4())
try:
thing_group = secrets_client.get_secret_value(
SecretId="ci/DeviceAdvisor/thing_group")["SecretString"]
# create_thing_response:
# {
# 'thingName': 'string',
# 'thingArn': 'string',
# 'thingId': 'string'
# }
print("[Device Advisor] Info: Started to create thing...", file=sys.stderr)
print("[Device Advisor] Info: Started to create thing "
f"'{thing_name}'", file=sys.stderr)
create_thing_response = client.create_thing(
thingName=thing_name
)
os.environ["DA_THING_NAME"] = thing_name

except Exception:
print("[Device Advisor] Error: Failed to create thing: " +
thing_name, file=sys.stderr)
# Some tests (e.g. Jobs) require the tested things to be a part of the DA group thing.
client.add_thing_to_thing_group(
thingGroupName=thing_group,
thingName=thing_name,
)

except Exception as e:
print(f"[Device Advisor] Error: Failed to create thing '{thing_name}'; "
f"exception: {e}", file=sys.stderr)
exit(-1)

##############################################
# create certificate and keys used for testing
try:
print("[Device Advisor]Info: Started to create certificate...",
print("[Device Advisor] Info: Started to create certificate...",
file=sys.stderr)
# create_cert_response:
# {
Expand Down Expand Up @@ -187,8 +196,6 @@ def sleep_with_backoff(base, max):
##############################################
# attach policy to certificate
try:
secrets_client = boto3.client(
"secretsmanager", region_name=os.environ["AWS_DEFAULT_REGION"])
policy_name = secrets_client.get_secret_value(
SecretId="ci/DeviceAdvisor/policy_name")["SecretString"]
client.attach_policy(
Expand Down Expand Up @@ -352,8 +359,8 @@ def sleep_with_backoff(base, max):

except Exception as e:
delete_thing_with_certi(thing_name, certificate_id, certificate_arn)
print("[Device Advisor]Error: Failed to test: " +
test_name + ", exception: " + e, file=sys.stderr)
print(f"[Device Advisor] Error: Failed to test: {test_name}; exception: {e}",
file=sys.stderr)
did_at_least_one_test_fail = True
sleep_with_backoff(BACKOFF_BASE, BACKOFF_MAX)

Expand Down
7 changes: 5 additions & 2 deletions deviceadvisor/tests/DATestUtils/DATestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
public class DATestUtils {

public enum TestType {
CONNECT, SUB_PUB, SHADOW
CONNECT, SUB_PUB, SHADOW, JOBS
}

private final static String ENV_ENDPONT = "DA_ENDPOINT";
Expand Down Expand Up @@ -47,10 +47,13 @@ public static Boolean init(TestType type)
{
return false;
}
else if (type == TestType.SHADOW && (thing_name.isEmpty() || shadowProperty.isEmpty() || shadowValue.isEmpty()))
if (type == TestType.SHADOW && (thing_name.isEmpty() || shadowProperty.isEmpty() || shadowValue.isEmpty()))
{
return false;
}
if (type == TestType.JOBS && thing_name.isEmpty()) {
return false;
}
return true;
}
}
46 changes: 46 additions & 0 deletions deviceadvisor/tests/JobExecution/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>software.amazon.awssdk.iotdevicesdk</groupId>
<artifactId>JobExecution</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>${project.groupId}:${project.artifactId}</name>
<description>Java bindings for the AWS IoT Core Service</description>
<url>https://github.com/awslabs/aws-iot-device-sdk-java-v2</url>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk.iotdevicesdk</groupId>
<artifactId>aws-iot-device-sdk</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>../DATestUtils</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Loading

0 comments on commit 18f78a0

Please sign in to comment.