Skip to content

Commit 0face13

Browse files
authored
Device Advisor Test Samples (#281)
* added device advisor samples
1 parent 9968fd2 commit 0face13

File tree

5 files changed

+175
-0
lines changed

5 files changed

+175
-0
lines changed

deviceadvisor/tests/da_test_utils.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0.
3+
import os
4+
from enum import Enum
5+
from uuid import uuid4
6+
7+
class TestType(Enum):
8+
CONNECT = 1
9+
SUB_PUB = 1
10+
SHADOW = 1
11+
12+
class DATestUtils:
13+
endpoint = os.getenv('DA_ENDPOINT')
14+
certificatePath = os.getenv('DA_CERTI')
15+
keyPath = os.getenv('DA_KEY')
16+
topic = os.getenv('DA_TOPIC')
17+
thing_name = os.getenv('DA_THING_NAME')
18+
shadowProperty = os.getenv('DA_SHADOW_PROPERTY')
19+
shadowValue = os.getenv('DA_SHADOW_VALUE_SET')
20+
client_id = "test-" + str(uuid4())
21+
22+
@classmethod
23+
def valid(cls, test_type):
24+
if (not (cls.endpoint and cls.certificatePath and cls.keyPath)):
25+
return False
26+
27+
if (not cls.topic and test_type == TestType.SUB_PUB):
28+
return False
29+
30+
if (not (cls.thing_name and cls.shadowProperty and cls.shadowValue) and test_type == TestType.SHADOW):
31+
return False
32+
33+
return True

deviceadvisor/tests/mqtt_connect.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0.
3+
4+
from awsiot import mqtt_connection_builder
5+
from da_test_utils import DATestUtils, TestType
6+
7+
# This sample used to test mqtt connection through device advisor
8+
9+
if __name__ == '__main__':
10+
# init variables
11+
utils = DATestUtils.valid(TestType.CONNECT)
12+
if not utils:
13+
quit(-1)
14+
15+
mqtt_connection = mqtt_connection_builder.mtls_from_path(
16+
endpoint=DATestUtils.endpoint,
17+
cert_filepath=DATestUtils.certificatePath,
18+
pri_key_filepath=DATestUtils.keyPath,
19+
client_id = DATestUtils.client_id)
20+
21+
connect_future = mqtt_connection.connect()
22+
23+
# Future.result() waits until a result is available
24+
connect_future.result()
25+
26+
disconnect_future = mqtt_connection.disconnect()
27+
disconnect_future.result()
28+
quit(0)

deviceadvisor/tests/mqtt_publish.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0.
3+
from awscrt import mqtt
4+
from awsiot import mqtt_connection_builder
5+
from da_test_utils import DATestUtils, TestType
6+
import json
7+
8+
if __name__ == '__main__':
9+
# validate environment variables
10+
utils = DATestUtils.valid(TestType.SUB_PUB)
11+
if not utils:
12+
quit(-1)
13+
14+
mqtt_connection = mqtt_connection_builder.mtls_from_path(
15+
endpoint=DATestUtils.endpoint,
16+
cert_filepath=DATestUtils.certificatePath,
17+
pri_key_filepath=DATestUtils.keyPath,
18+
client_id=DATestUtils.client_id)
19+
connect_future = mqtt_connection.connect()
20+
21+
# Future.result() waits until a result is available
22+
connect_future.result()
23+
24+
message = "Hello World"
25+
message_json = json.dumps(message)
26+
# Device advisor test will not return PUBACK, therefore we use AT_MOST_ONCE so that
27+
# we dont busy wait for PUBACK
28+
publish_future, packet_id = mqtt_connection.publish(
29+
topic=DATestUtils.topic,
30+
payload=message_json,
31+
qos=mqtt.QoS.AT_MOST_ONCE)
32+
publish_future.result()
33+
34+
# Disconnect
35+
disconnect_future = mqtt_connection.disconnect()
36+
disconnect_future.result()
37+
quit(0)

deviceadvisor/tests/mqtt_subscribe.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0.
3+
4+
from awscrt import io, mqtt, auth, http
5+
from awsiot import mqtt_connection_builder
6+
from da_test_utils import DATestUtils, TestType
7+
8+
if __name__ == '__main__':
9+
# init variables
10+
utils = DATestUtils.valid(TestType.SUB_PUB)
11+
if not utils:
12+
quit(-1)
13+
14+
mqtt_connection = mqtt_connection_builder.mtls_from_path(
15+
endpoint=DATestUtils.endpoint,
16+
cert_filepath=DATestUtils.certificatePath,
17+
pri_key_filepath=DATestUtils.keyPath,
18+
client_id = DATestUtils.client_id)
19+
20+
connect_future = mqtt_connection.connect()
21+
22+
# Future.result() waits until a result is available
23+
connect_future.result()
24+
25+
# Subscribe
26+
subscribe_future, packet_id = mqtt_connection.subscribe(
27+
topic=DATestUtils.topic,
28+
qos=mqtt.QoS.AT_LEAST_ONCE)
29+
30+
subscribe_future.result()
31+
32+
# Disconnect
33+
disconnect_future = mqtt_connection.disconnect()
34+
disconnect_future.result()
35+
quit(0)

deviceadvisor/tests/shadow_update.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0.
3+
4+
from awscrt import mqtt
5+
from awsiot import iotshadow, mqtt_connection_builder
6+
from concurrent.futures import Future
7+
from da_test_utils import DATestUtils, TestType
8+
from uuid import uuid4
9+
10+
if __name__ == '__main__':
11+
# init variables
12+
utils = DATestUtils.valid(TestType.SUB_PUB)
13+
if not utils:
14+
quit(-1)
15+
16+
mqtt_connection = mqtt_connection_builder.mtls_from_path(
17+
endpoint=DATestUtils.endpoint,
18+
cert_filepath=DATestUtils.certificatePath,
19+
pri_key_filepath=DATestUtils.keyPath,
20+
client_id = DATestUtils.client_id)
21+
22+
connect_future = mqtt_connection.connect()
23+
connect_future.result()
24+
shadow_client = iotshadow.IotShadowClient(mqtt_connection)
25+
26+
27+
# Publish shadow value
28+
request = iotshadow.UpdateShadowRequest(
29+
thing_name=DATestUtils.thing_name,
30+
state=iotshadow.ShadowState(
31+
reported={ DATestUtils.shadowProperty: DATestUtils.shadowValue },
32+
desired={ DATestUtils.shadowProperty: DATestUtils.shadowValue },
33+
)
34+
)
35+
# Device advisor test will not return PUBACK, therefore we use AT_MOST_ONCE so that
36+
# we dont busy wait for PUBACK
37+
shadow_future = shadow_client.publish_update_shadow(request, mqtt.QoS.AT_MOST_ONCE)
38+
shadow_future.result()
39+
40+
disconnect_future = mqtt_connection.disconnect()
41+
disconnect_future.result()
42+
quit(0)

0 commit comments

Comments
 (0)