Skip to content

Commit 9f18d59

Browse files
committed
Jobs framework
1 parent 217ff49 commit 9f18d59

File tree

1 file changed

+208
-0
lines changed

1 file changed

+208
-0
lines changed

test/test_jobs.py

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0.
3+
import pdb
4+
5+
from awscrt import io, mqtt, mqtt5, mqtt_request_response
6+
import awsiot
7+
from awsiot import iotjobs
8+
9+
import boto3
10+
from concurrent.futures import Future
11+
import os
12+
import time
13+
import unittest
14+
import uuid
15+
16+
TIMEOUT = 30.0
17+
18+
19+
def create_client_id():
20+
return f"test-{uuid.uuid4().hex}"
21+
22+
23+
def _get_env_variable(env_name):
24+
env_data = os.environ.get(env_name)
25+
if not env_data:
26+
raise unittest.SkipTest(f"test requires env var: {env_name}")
27+
return env_data
28+
29+
30+
class JobsTestCallbacks():
31+
def __init__(self):
32+
self.future_connection_success = Future()
33+
self.future_stopped = Future()
34+
35+
def ws_handshake_transform(self, transform_args):
36+
transform_args.set_done()
37+
38+
def on_publish_received(self, publish_received_data: mqtt5.PublishReceivedData):
39+
pass
40+
41+
def on_lifecycle_stopped(self, lifecycle_stopped: mqtt5.LifecycleStoppedData):
42+
if self.future_stopped:
43+
self.future_stopped.set_result(None)
44+
45+
def on_lifecycle_attempting_connect(self, lifecycle_attempting_connect: mqtt5.LifecycleAttemptingConnectData):
46+
pass
47+
48+
def on_lifecycle_connection_success(self, lifecycle_connection_success: mqtt5.LifecycleConnectSuccessData):
49+
if self.future_connection_success:
50+
self.future_connection_success.set_result(lifecycle_connection_success)
51+
52+
def on_lifecycle_connection_failure(self, lifecycle_connection_failure: mqtt5.LifecycleConnectFailureData):
53+
if self.future_connection_success:
54+
if self.future_connection_success.done():
55+
pass
56+
else:
57+
self.future_connection_success.set_exception(lifecycle_connection_failure.exception)
58+
59+
def on_lifecycle_disconnection(self, lifecycle_disconnect_data: mqtt5.LifecycleDisconnectData):
60+
pass
61+
62+
class TestContext():
63+
def __init__(self):
64+
self.region = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_REGION")
65+
self.thing_name = None
66+
self.thing_group_name = None
67+
self.thing_group_arn = None
68+
self.job_id = None
69+
70+
class JobsServiceTest(unittest.TestCase):
71+
72+
def _create_protocol_client5(self):
73+
74+
input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST")
75+
input_cert = _get_env_variable("AWS_TEST_MQTT5_IOT_CERTIFICATE_PATH")
76+
input_key = _get_env_variable("AWS_TEST_MQTT5_IOT_KEY_PATH")
77+
78+
client_options = mqtt5.ClientOptions(
79+
host_name=input_host_name,
80+
port=8883
81+
)
82+
83+
tls_ctx_options = io.TlsContextOptions.create_client_with_mtls_from_path(
84+
input_cert,
85+
input_key
86+
)
87+
client_options.tls_ctx = io.ClientTlsContext(tls_ctx_options)
88+
89+
client_options.connect_options = mqtt5.ConnectPacket()
90+
client_options.connect_options.client_id = create_client_id()
91+
92+
callbacks = JobsTestCallbacks()
93+
client_options.on_lifecycle_event_stopped_fn = callbacks.on_lifecycle_stopped
94+
client_options.on_lifecycle_event_connection_success_fn = callbacks.on_lifecycle_connection_success
95+
client_options.on_lifecycle_event_connection_failure_fn = callbacks.on_lifecycle_connection_failure
96+
client_options.on_lifecycle_event_stopped_fn = callbacks.on_lifecycle_stopped
97+
98+
protocol_client = mqtt5.Client(client_options)
99+
protocol_client.start()
100+
101+
callbacks.future_connection_success.result(TIMEOUT)
102+
103+
return protocol_client, callbacks
104+
105+
def _shutdown_protocol_client5(self, protocol_client, callbacks):
106+
107+
protocol_client.stop()
108+
callbacks.future_stopped.result(TIMEOUT)
109+
110+
def _create_protocol_client311(self):
111+
112+
input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST")
113+
input_cert = _get_env_variable("AWS_TEST_MQTT5_IOT_CERTIFICATE_PATH")
114+
input_key = _get_env_variable("AWS_TEST_MQTT5_IOT_KEY_PATH")
115+
116+
tls_ctx_options = io.TlsContextOptions.create_client_with_mtls_from_path(
117+
input_cert,
118+
input_key
119+
)
120+
tls_ctx = io.ClientTlsContext(tls_ctx_options)
121+
122+
client = mqtt.Client(None, tls_ctx)
123+
124+
protocol_client = mqtt.Connection(
125+
client=client,
126+
client_id=create_client_id(),
127+
host_name=input_host_name,
128+
port=8883,
129+
ping_timeout_ms=10000,
130+
keep_alive_secs=30
131+
)
132+
protocol_client.connect().result(TIMEOUT)
133+
134+
return protocol_client
135+
136+
def _shutdown_protocol_client311(self, protocol_client):
137+
protocol_client.disconnect().result(TIMEOUT)
138+
139+
def _create_jobs_client(
140+
self,
141+
protocol_client,
142+
max_request_response_subscriptions,
143+
max_streaming_subscriptions,
144+
operation_timeout_seconds):
145+
rr_client_options = mqtt_request_response.ClientOptions(
146+
max_request_response_subscriptions, max_streaming_subscriptions)
147+
rr_client_options.operation_timeout_in_seconds = operation_timeout_seconds
148+
149+
jobs_client = iotjobs.IotJobsClientV2(protocol_client, rr_client_options)
150+
151+
return jobs_client
152+
153+
def _do_jobs_creation_test5(self, test_callable):
154+
(protocol_client, callbacks) = self._create_protocol_client5()
155+
156+
test_callable(protocol_client)
157+
158+
self._shutdown_protocol_client5(protocol_client, callbacks)
159+
160+
def _do_jobs_creation_test311(self, test_callable):
161+
protocol_client = self._create_protocol_client311()
162+
163+
test_callable(protocol_client)
164+
165+
self._shutdown_protocol_client311(protocol_client)
166+
167+
def _do_jobs_operation_test5(self, test_callable):
168+
(protocol_client, callbacks) = self._create_protocol_client5()
169+
identity_client = self._create_jobs_client(protocol_client, 2, 2, 30)
170+
171+
test_callable(identity_client)
172+
173+
self._shutdown_protocol_client5(protocol_client, callbacks)
174+
175+
def _do_jobs_operation_test311(self, test_callable):
176+
protocol_client = self._create_protocol_client311()
177+
identity_client = self._create_jobs_client(protocol_client, 2, 2, 30)
178+
179+
test_callable(identity_client)
180+
181+
self._shutdown_protocol_client311(protocol_client)
182+
183+
def _tear_down(self, test_context):
184+
pass
185+
186+
187+
# ==============================================================
188+
# CREATION SUCCESS TEST CASES
189+
# ==============================================================
190+
def test_client_creation_success5(self):
191+
self._do_jobs_creation_test5(lambda protocol_client: self._create_jobs_client(protocol_client, 2, 2, 30))
192+
193+
def test_client_creation_success311(self):
194+
self._do_jobs_creation_test311(lambda protocol_client: self._create_jobs_client(protocol_client, 2, 2, 30))
195+
196+
# ==============================================================
197+
# CREATION FAILURE TEST CASES
198+
# ==============================================================
199+
def test_client_creation_failure_no_protocol_client(self):
200+
self.assertRaises(Exception, self._create_jobs_client, None, 2, 2, 30)
201+
202+
# ==============================================================
203+
# REQUEST RESPONSE OPERATION TEST CASES
204+
# ==============================================================
205+
206+
207+
if __name__ == 'main':
208+
unittest.main()

0 commit comments

Comments
 (0)