diff --git a/azext_iot/monitor/telemetry.py b/azext_iot/monitor/telemetry.py index c57dd60c4..616f2579e 100644 --- a/azext_iot/monitor/telemetry.py +++ b/azext_iot/monitor/telemetry.py @@ -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: @@ -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] diff --git a/azext_iot/tests/central/__init__.py b/azext_iot/tests/central/__init__.py index ca43a8a9e..589861f28 100644 --- a/azext_iot/tests/central/__init__.py +++ b/azext_iot/tests/central/__init__.py @@ -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( diff --git a/azext_iot/tests/central/test_iot_central_devices_int.py b/azext_iot/tests/central/test_iot_central_devices_int.py index de82f536f..4388b3c7a 100644 --- a/azext_iot/tests/central/test_iot_central_devices_int.py +++ b/azext_iot/tests/central/test_iot_central_devices_int.py @@ -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 ) @@ -249,8 +250,6 @@ 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, @@ -258,7 +257,11 @@ def test_central_device_template_methods_CRD(self): 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( @@ -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) @@ -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 ) @@ -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 ) diff --git a/azext_iot/tests/central/test_iot_central_int.py b/azext_iot/tests/central/test_iot_central_int.py index 192612482..0307f8f4a 100644 --- a/azext_iot/tests/central/test_iot_central_int.py +++ b/azext_iot/tests/central/test_iot_central_int.py @@ -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 @@ -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 ) @@ -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 ) @@ -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 ) @@ -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,