Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c611792

Browse files
authored
Better reliability for flaky tests (GoogleCloudPlatform#1960)
* Increases reliability for flaky tests * Replaces checks where device or gateway ID could affect test outcome.
1 parent 7f0aadf commit c611792

File tree

5 files changed

+36
-12
lines changed

5 files changed

+36
-12
lines changed

iot/api-client/beta-features/gateway/gateway_test.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ def test_create_gateway(iot_topic, capsys):
168168
out, _ = capsys.readouterr()
169169

170170
assert 'Created gateway' in out
171-
assert '400' not in out
172171

173172

174173
def test_list_gateways(iot_topic, capsys):
@@ -195,7 +194,6 @@ def test_list_gateways(iot_topic, capsys):
195194
out, _ = capsys.readouterr()
196195

197196
assert 'Gateway ID: {}'.format(gateway_id) in out
198-
assert '400' not in out
199197

200198

201199
def test_bind_device_to_gateway_and_unbind(iot_topic, capsys):
@@ -233,7 +231,7 @@ def test_bind_device_to_gateway_and_unbind(iot_topic, capsys):
233231

234232
assert 'Device Bound' in out
235233
assert 'Device unbound' in out
236-
assert '400' not in out
234+
assert 'HttpError 404' not in out
237235

238236

239237
def test_gateway_listen_for_bound_device_configs(iot_topic, capsys):

iot/api-client/manager/manager_test.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
# Add command receiver for bootstrapping device registry / device for testing
2020
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'mqtt_example')) # noqa
21+
from gcp_devrel.testing.flaky import flaky
2122
from google.cloud import pubsub
2223
import pytest
2324

@@ -278,14 +279,24 @@ def test_add_patch_delete_es256(test_topic, capsys):
278279
service_account_json, project_id, cloud_region, registry_id)
279280

280281

282+
@flaky
281283
def test_send_command(test_topic, capsys):
282284
device_id = device_id_template.format('RSA256')
283285
manager.create_registry(
284286
service_account_json, project_id, cloud_region, pubsub_topic,
285287
registry_id)
286-
manager.create_rs256_device(
287-
service_account_json, project_id, cloud_region, registry_id,
288-
device_id, rsa_cert_path)
288+
289+
exists = False
290+
devices = manager.list_devices(
291+
service_account_json, project_id, cloud_region, registry_id)
292+
for device in devices:
293+
if device.get('id') == device_id:
294+
exists = True
295+
296+
if not exists:
297+
manager.create_rs256_device(
298+
service_account_json, project_id, cloud_region, registry_id,
299+
device_id, rsa_cert_path)
289300

290301
# Exercize the functionality
291302
client = cloudiot_mqtt_example.get_client(

iot/api-client/manager/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
cryptography==2.4.2
2+
flaky==3.4.0
3+
gcp-devrel-py-tools==0.0.15
24
google-api-python-client==1.7.5
35
google-auth-httplib2==0.0.3
46
google-auth==1.6.1

iot/api-client/mqtt_example/cloudiot_mqtt_example_test.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
# Add manager for bootstrapping device registry / device for testing
2222
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'manager')) # noqa
23+
from gcp_devrel.testing.flaky import flaky
2324
import manager
2425

2526
import pytest
@@ -178,14 +179,24 @@ def test_config(test_topic, capsys):
178179
assert '/devices/{}/config'.format(device_id) in out
179180

180181

182+
@flaky
181183
def test_receive_command(capsys):
182184
device_id = device_id_template.format('RSA256')
183185
manager.create_registry(
184186
service_account_json, project_id, cloud_region, pubsub_topic,
185187
registry_id)
186-
manager.create_rs256_device(
187-
service_account_json, project_id, cloud_region, registry_id,
188-
device_id, rsa_cert_path)
188+
189+
exists = False
190+
devices = manager.list_devices(
191+
service_account_json, project_id, cloud_region, registry_id)
192+
for device in devices:
193+
if device.get('id') == device_id:
194+
exists = True
195+
196+
if not exists:
197+
manager.create_rs256_device(
198+
service_account_json, project_id, cloud_region, registry_id,
199+
device_id, rsa_cert_path)
189200

190201
# Exercize the functionality
191202
client = cloudiot_mqtt_example.get_client(
@@ -195,7 +206,7 @@ def test_receive_command(capsys):
195206
client.loop_start()
196207

197208
# Pre-process commands
198-
for i in range(1, 3):
209+
for i in range(1, 5):
199210
client.loop()
200211
time.sleep(1)
201212

@@ -204,7 +215,7 @@ def test_receive_command(capsys):
204215
device_id, 'me want cookies')
205216

206217
# Process commands
207-
for i in range(1, 3):
218+
for i in range(1, 5):
208219
client.loop()
209220
time.sleep(1)
210221

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
cryptography==2.4.2
2+
flaky==3.4.0
3+
gcp-devrel-py-tools==0.0.15
14
google-api-python-client==1.7.5
25
google-auth-httplib2==0.0.3
36
google-auth==1.6.1
47
google-cloud-pubsub==0.39.0
5-
cryptography==2.4.2
68
pyjwt==1.6.4
79
paho-mqtt==1.4.0

0 commit comments

Comments
 (0)