Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 488daf6

Browse filesBrowse files
gguussJon Wayne Parrott
authored andcommitted
Adds MQTT device tests (GoogleCloudPlatform#1268)
* Adds MQTT device tests * Fix lint
1 parent 8f00674 commit 488daf6
Copy full SHA for 488daf6

File tree

Expand file treeCollapse file tree

6 files changed

+2164
-0
lines changed
Filter options
Expand file treeCollapse file tree

6 files changed

+2164
-0
lines changed
+171Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
# Copyright 2017 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
import os
16+
import sys
17+
import time
18+
19+
from google.cloud import pubsub
20+
21+
# Add manager for bootstrapping device registry / device for testing
22+
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'manager')) # noqa
23+
import manager
24+
25+
import pytest
26+
27+
import cloudiot_mqtt_example
28+
29+
30+
cloud_region = 'us-central1'
31+
device_id_template = 'test-device-{}'
32+
ca_cert_path = 'resources/roots.pem'
33+
rsa_cert_path = 'resources/rsa_cert.pem'
34+
rsa_private_path = 'resources/rsa_private.pem'
35+
topic_id = 'test-device-events-{}'.format(int(time.time()))
36+
37+
project_id = os.environ['GCLOUD_PROJECT']
38+
service_account_json = os.environ['GOOGLE_APPLICATION_CREDENTIALS']
39+
40+
pubsub_topic = 'projects/{}/topics/{}'.format(project_id, topic_id)
41+
registry_id = 'test-registry-{}'.format(int(time.time()))
42+
43+
44+
@pytest.fixture(scope='module')
45+
def test_topic():
46+
topic = manager.create_iot_topic(project_id, topic_id)
47+
48+
yield topic
49+
50+
pubsub_client = pubsub.PublisherClient()
51+
topic_path = pubsub_client.topic_path(project_id, topic_id)
52+
pubsub_client.delete_topic(topic_path)
53+
54+
55+
def test_event(test_topic, capsys):
56+
device_id = device_id_template.format('RSA256')
57+
manager.open_registry(
58+
service_account_json, project_id, cloud_region, pubsub_topic,
59+
registry_id)
60+
61+
manager.create_rs256_device(
62+
service_account_json, project_id, cloud_region, registry_id,
63+
device_id, rsa_cert_path)
64+
65+
manager.get_device(
66+
service_account_json, project_id, cloud_region, registry_id,
67+
device_id)
68+
69+
sub_topic = 'events'
70+
mqtt_topic = '/devices/{}/{}'.format(device_id, sub_topic)
71+
72+
client = cloudiot_mqtt_example.get_client(
73+
project_id, cloud_region, registry_id, device_id,
74+
rsa_private_path, 'RS256', ca_cert_path,
75+
'mqtt.googleapis.com', 443)
76+
77+
client.publish(mqtt_topic, 'just test', qos=1)
78+
time.sleep(2)
79+
client.loop_stop()
80+
81+
manager.get_state(
82+
service_account_json, project_id, cloud_region, registry_id,
83+
device_id)
84+
85+
manager.delete_device(
86+
service_account_json, project_id, cloud_region, registry_id,
87+
device_id)
88+
89+
manager.delete_registry(
90+
service_account_json, project_id, cloud_region, registry_id)
91+
92+
out, _ = capsys.readouterr()
93+
assert 'on_publish' in out
94+
95+
96+
def test_state(test_topic, capsys):
97+
device_id = device_id_template.format('RSA256')
98+
manager.open_registry(
99+
service_account_json, project_id, cloud_region, pubsub_topic,
100+
registry_id)
101+
102+
manager.create_rs256_device(
103+
service_account_json, project_id, cloud_region, registry_id,
104+
device_id, rsa_cert_path)
105+
106+
manager.get_device(
107+
service_account_json, project_id, cloud_region, registry_id,
108+
device_id)
109+
110+
sub_topic = 'state'
111+
mqtt_topic = '/devices/{}/{}'.format(device_id, sub_topic)
112+
113+
client = cloudiot_mqtt_example.get_client(
114+
project_id, cloud_region, registry_id, device_id,
115+
rsa_private_path, 'RS256', ca_cert_path,
116+
'mqtt.googleapis.com', 443)
117+
client.publish(mqtt_topic, 'state test', qos=1)
118+
time.sleep(3)
119+
client.loop_stop()
120+
121+
manager.get_state(
122+
service_account_json, project_id, cloud_region, registry_id,
123+
device_id)
124+
125+
manager.delete_device(
126+
service_account_json, project_id, cloud_region, registry_id,
127+
device_id)
128+
129+
manager.delete_registry(
130+
service_account_json, project_id, cloud_region, registry_id)
131+
132+
out, _ = capsys.readouterr()
133+
assert 'on_publish' in out
134+
assert 'c3RhdGUgdGVzdA' in out
135+
136+
137+
def test_config(test_topic, capsys):
138+
device_id = device_id_template.format('RSA256')
139+
manager.open_registry(
140+
service_account_json, project_id, cloud_region, pubsub_topic,
141+
registry_id)
142+
143+
manager.create_rs256_device(
144+
service_account_json, project_id, cloud_region, registry_id,
145+
device_id, rsa_cert_path)
146+
147+
manager.get_device(
148+
service_account_json, project_id, cloud_region, registry_id,
149+
device_id)
150+
151+
client = cloudiot_mqtt_example.get_client(
152+
project_id, cloud_region, registry_id, device_id,
153+
rsa_private_path, 'RS256', ca_cert_path,
154+
'mqtt.googleapis.com', 443)
155+
time.sleep(5)
156+
client.loop_stop()
157+
158+
manager.get_state(
159+
service_account_json, project_id, cloud_region, registry_id,
160+
device_id)
161+
162+
manager.delete_device(
163+
service_account_json, project_id, cloud_region, registry_id,
164+
device_id)
165+
166+
manager.delete_registry(
167+
service_account_json, project_id, cloud_region, registry_id)
168+
169+
out, _ = capsys.readouterr()
170+
assert "Received message" in out
171+
assert '/devices/{}/config'.format(device_id) in out
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
google-api-python-client==1.6.4
2+
google-auth-httplib2==0.0.3
3+
google-auth==1.2.1
4+
google-cloud-pubsub==0.29.3
15
cryptography==2.1.4
26
pyjwt==1.5.3
37
paho-mqtt==1.3.1
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Test public certificate files
2+
3+
The public certificates in this folder are only provided for testing and should
4+
not be used for registering your devices.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.