Skip to content

Commit

Permalink
Fix tests for central and python 3.9 (#440)
Browse files Browse the repository at this point in the history
* fix tests for central and py3.9
* fix lint
* fix device template methods tests
  • Loading branch information
lucadruda authored Sep 29, 2021
1 parent 71f3f32 commit 3830b64
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 8 deletions.
19 changes: 15 additions & 4 deletions azext_iot/monitor/telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ def start_multiple_monitors(
loop = get_loop()

# TODO: Remove direct usage of loop arguments by Python 3.10
future = asyncio.gather(*coroutines, loop=loop, return_exceptions=True) # pylint: disable=deprecated-argument
# pylint: disable=deprecated-argument
future = asyncio.gather(*coroutines, loop=loop, return_exceptions=True)
result = None

try:
Expand All @@ -74,9 +75,19 @@ def start_multiple_monitors(
result = loop.run_until_complete(future)
except KeyboardInterrupt:
print("Stopping event monitor...", flush=True)
for t in asyncio.Task.all_tasks(): # pylint: disable=no-member
t.cancel()
loop.run_forever()
try:
# TODO: remove when deprecating
# pylint: disable=no-member
tasks = (
asyncio.Task.all_tasks()
if sys.version_info < (3, 9)
else asyncio.all_tasks()
)
for t in tasks: # pylint: disable=no-member
t.cancel()
loop.run_forever()
except RuntimeError:
pass # no running loop anymore
finally:
if result:
errors = result[0]
Expand Down
20 changes: 20 additions & 0 deletions azext_iot/tests/central/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,26 @@ def _delete_device_template(self, api_version, template_id):
return
except Exception as e:
error = e
# delete associated devices if any.
command = "iot central device list --app-id {}".format(APP_ID)
devices = self.cmd(
command, api_version=api_version
).get_output_in_json()

if devices:
for device in devices:
device_template = device[
"instanceOf"
if api_version == ApiVersion.preview.value
else "template"
]
if device_template == template_id:
self.cmd(
"iot central device delete --app-id {} --device-id {}".format(
APP_ID, device["id"]
),
api_version=api_version,
)
time.sleep(10)

raise CLIError(
Expand Down
13 changes: 9 additions & 4 deletions azext_iot/tests/central/test_iot_central_devices_int.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ def test_central_device_template_methods_CRD(self):
).get_output_in_json()

start_dev_temp_count = len(start_device_template_list)

(template_id, template_name) = self._create_device_template(
api_version=self._api_version
)
Expand Down Expand Up @@ -249,16 +250,18 @@ def test_central_device_template_methods_CRD(self):
template_id=template_id, api_version=self._api_version
)

# template can't be deleted if any device exists for it. Assert accordingly

deleted_device_template_list = self.cmd(
"iot central device-template list --app-id {}".format(APP_ID),
api_version=self._api_version,
).get_output_in_json()

deleted_dev_temp_count = len(deleted_device_template_list)

assert deleted_dev_temp_count == start_dev_temp_count
# if template existed before this run then a successfull deletion reduces the number of templates
assert (
deleted_dev_temp_count == start_dev_temp_count
or deleted_dev_temp_count == (start_dev_temp_count - 1)
)

if (
next(
Expand Down Expand Up @@ -288,7 +291,7 @@ def test_central_device_template_methods_CRD(self):
def test_central_device_groups_list(self):
result = self._list_device_groups()
# assert object is empty or populated but not null
assert result is not None and (result == {} or bool(result) is True)
assert result is not None and (result == [] or bool(result) is True)

def test_central_device_registration_info_registered(self):
(template_id, _) = self._create_device_template(api_version=self._api_version)
Expand All @@ -303,6 +306,7 @@ def test_central_device_registration_info_registered(self):
result = self.cmd(command, api_version=self._api_version)

self._delete_device(device_id=device_id, api_version=self._api_version)

self._delete_device_template(
template_id=template_id, api_version=self._api_version
)
Expand Down Expand Up @@ -463,6 +467,7 @@ def test_central_device_should_start_failover_and_failback(self):

# Cleanup
self._delete_device(device_id=device_id, api_version=self._api_version)

self._delete_device_template(
template_id=template_id, api_version=self._api_version
)
Expand Down
6 changes: 6 additions & 0 deletions azext_iot/tests/central/test_iot_central_int.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@ def test_central_monitor_events(self):
)

self._delete_device(device_id=device_id, api_version=self._api_version)

self._delete_device_template(
template_id=template_id, api_version=self._api_version
)

assert '"Bool": true' in output
assert device_id in output

Expand Down Expand Up @@ -109,6 +111,7 @@ def test_central_validate_messages_success(self):
output = self._get_validate_messages_output(device_id, enqueued_time)

self._delete_device(device_id=device_id, api_version=self._api_version)

self._delete_device_template(
template_id=template_id, api_version=self._api_version
)
Expand Down Expand Up @@ -184,6 +187,7 @@ def test_central_validate_messages_issues_detected(self):
)

self._delete_device(device_id=device_id, api_version=self._api_version)

self._delete_device_template(
template_id=template_id, api_version=self._api_version
)
Expand Down Expand Up @@ -273,6 +277,7 @@ def test_central_run_command_root_level(self):
show_command_result = self.cmd(command, api_version=self._api_version)

self._delete_device(device_id=device_id, api_version=self._api_version)

self._delete_device_template(
template_id=template_id, api_version=self._api_version
)
Expand Down Expand Up @@ -309,6 +314,7 @@ def test_central_run_command_component(self):
show_command_result = self.cmd(command, api_version=self._api_version)

self._delete_device(device_id=device_id, api_version=self._api_version)

self._delete_device_template(
template_id=template_id,
api_version=self._api_version,
Expand Down

0 comments on commit 3830b64

Please sign in to comment.