From 1633239cf4c630dec7b27fb0fcbe582fbe1393d5 Mon Sep 17 00:00:00 2001 From: Damon Barry Date: Thu, 2 Jul 2026 15:13:31 -0700 Subject: [PATCH] Delete leaf device identities in one-time test teardown --- .../Context.cs | 4 + .../Device.cs | 133 +++++++----------- .../DeviceWithCustomCertificates.cs | 37 ++--- .../PlugAndPlay.cs | 18 +-- .../SetupFixture.cs | 5 + .../X509Device.cs | 18 +-- 6 files changed, 82 insertions(+), 133 deletions(-) diff --git a/test/Microsoft.Azure.Devices.Edge.Test.Common/Context.cs b/test/Microsoft.Azure.Devices.Edge.Test.Common/Context.cs index 5ec5a1a6795..d626fdb23e5 100644 --- a/test/Microsoft.Azure.Devices.Edge.Test.Common/Context.cs +++ b/test/Microsoft.Azure.Devices.Edge.Test.Common/Context.cs @@ -6,6 +6,8 @@ namespace Microsoft.Azure.Devices.Edge.Test.Common using System.IO; using System.Linq; using System.Net; + using System.Threading; + using System.Threading.Tasks; using Microsoft.Azure.Devices.Edge.Test.Common; using Microsoft.Azure.Devices.Edge.Util; using Microsoft.Extensions.Configuration; @@ -138,6 +140,8 @@ IEnumerable GetAndValidateRegistries() public Dictionary DeleteList { get; } = new Dictionary(); + public Dictionary LeafDeleteList { get; } = new Dictionary(); + public Option DpsIdScope { get; } public Option DpsGroupKey { get; } diff --git a/test/Microsoft.Azure.Devices.Edge.Test/Device.cs b/test/Microsoft.Azure.Devices.Edge.Test/Device.cs index 9b3224fc146..4f2b0be57a9 100644 --- a/test/Microsoft.Azure.Devices.Edge.Test/Device.cs +++ b/test/Microsoft.Azure.Devices.Edge.Test/Device.cs @@ -39,18 +39,12 @@ public async Task QuickstartCerts() Option.None(), this.device.NestedEdge.IsNestedEdge); - await TryFinally.DoAsync( - async () => - { - DateTime seekTime = DateTime.Now; - await leaf.SendEventAsync(token); - await leaf.WaitForEventsReceivedAsync(seekTime, token); - await leaf.InvokeDirectMethodAsync(token); - }, - async () => - { - await leaf.DeleteIdentityAsync(token); - }); + Context.Current.LeafDeleteList.TryAdd(leafDeviceId, leaf); + + DateTime seekTime = DateTime.Now; + await leaf.SendEventAsync(token); + await leaf.WaitForEventsReceivedAsync(seekTime, token); + await leaf.InvokeDirectMethodAsync(token); } [Test] @@ -79,19 +73,12 @@ public async Task QuickstartChangeSasKey() Option.None(), this.device.NestedEdge.IsNestedEdge); - await TryFinally.DoAsync( - async () => - { - DateTime seekTime = DateTime.Now; - await leaf.SendEventAsync(token); - await leaf.WaitForEventsReceivedAsync(seekTime, token); - await leaf.InvokeDirectMethodAsync(token); - }, - async () => - { - await leaf.Close(); - await leaf.DeleteIdentityAsync(token); - }); + Context.Current.LeafDeleteList.TryAdd(leafDeviceId, leaf); + + DateTime seekTime = DateTime.Now; + await leaf.SendEventAsync(token); + await leaf.WaitForEventsReceivedAsync(seekTime, token); + await leaf.InvokeDirectMethodAsync(token); // Re-create the leaf with the same device ID, for our purposes this is // the equivalent of updating the SAS keys @@ -109,19 +96,10 @@ await TryFinally.DoAsync( Option.None(), this.device.NestedEdge.IsNestedEdge); - await TryFinally.DoAsync( - async () => - { - DateTime seekTime = DateTime.Now; - await leafUpdated.SendEventAsync(token); - await leafUpdated.WaitForEventsReceivedAsync(seekTime, token); - await leafUpdated.InvokeDirectMethodAsync(token); - }, - async () => - { - await leafUpdated.Close(); - await leafUpdated.DeleteIdentityAsync(token); - }); + seekTime = DateTime.Now; + await leafUpdated.SendEventAsync(token); + await leafUpdated.WaitForEventsReceivedAsync(seekTime, token); + await leafUpdated.InvokeDirectMethodAsync(token); } [Test] @@ -155,40 +133,33 @@ public async Task RouteMessageL3LeafToL4Module() Option.None(), Context.Current.NestedEdge); - await TryFinally.DoAsync( - async () => - { - // Send a message from the leaf device - DateTime seekTime = DateTime.Now; - await leaf.SendEventAsync(token); - Log.Verbose($"Sent message from {leafDeviceId}"); - - // Verify that the message was received/resent by the relayer module on L4 - await Profiler.Run( - () => this.IotHub.ReceiveEventsAsync( - parentDeviceId, - seekTime, - data => - { - data.SystemProperties.TryGetValue("iothub-connection-device-id", out object devId); - data.SystemProperties.TryGetValue("iothub-connection-module-id", out object modId); - data.Properties.TryGetValue("leaf-message-id", out object msgId); - - Log.Verbose($"Received event for '{devId + "/" + modId}' with message ID '{msgId}'"); - - return devId != null && devId.ToString().Equals(parentDeviceId) - && modId.ToString().Equals(relayerModuleId); - }, - token), - "Received events from module '{Device}' on Event Hub '{EventHub}'", - parentDeviceId + "/" + relayerModuleId, - this.IotHub.EventHubName); - }, - async () => - { - await leaf.Close(); - await leaf.DeleteIdentityAsync(token); - }); + Context.Current.LeafDeleteList.TryAdd(leafDeviceId, leaf); + + // Send a message from the leaf device + DateTime seekTime = DateTime.Now; + await leaf.SendEventAsync(token); + Log.Verbose($"Sent message from {leafDeviceId}"); + + // Verify that the message was received/resent by the relayer module on L4 + await Profiler.Run( + () => this.IotHub.ReceiveEventsAsync( + parentDeviceId, + seekTime, + data => + { + data.SystemProperties.TryGetValue("iothub-connection-device-id", out object devId); + data.SystemProperties.TryGetValue("iothub-connection-module-id", out object modId); + data.Properties.TryGetValue("leaf-message-id", out object msgId); + + Log.Verbose($"Received event for '{devId + "/" + modId}' with message ID '{msgId}'"); + + return devId != null && devId.ToString().Equals(parentDeviceId) + && modId.ToString().Equals(relayerModuleId); + }, + token), + "Received events from module '{Device}' on Event Hub '{EventHub}'", + parentDeviceId + "/" + relayerModuleId, + this.IotHub.EventHubName); } [Test] @@ -226,18 +197,12 @@ public async Task DisableReenableParentEdge() Option.None(), this.device.NestedEdge.IsNestedEdge); - await TryFinally.DoAsync( - async () => - { - DateTime seekTime = DateTime.Now; - await leaf.SendEventAsync(token); - await leaf.WaitForEventsReceivedAsync(seekTime, token); - await leaf.InvokeDirectMethodAsync(token); - }, - async () => - { - await leaf.DeleteIdentityAsync(token); - }); + Context.Current.LeafDeleteList.TryAdd(leafDeviceId, leaf); + + DateTime seekTime = DateTime.Now; + await leaf.SendEventAsync(token); + await leaf.WaitForEventsReceivedAsync(seekTime, token); + await leaf.InvokeDirectMethodAsync(token); } } } diff --git a/test/Microsoft.Azure.Devices.Edge.Test/DeviceWithCustomCertificates.cs b/test/Microsoft.Azure.Devices.Edge.Test/DeviceWithCustomCertificates.cs index 5f311963e54..06f6806ae27 100644 --- a/test/Microsoft.Azure.Devices.Edge.Test/DeviceWithCustomCertificates.cs +++ b/test/Microsoft.Azure.Devices.Edge.Test/DeviceWithCustomCertificates.cs @@ -45,6 +45,8 @@ public async Task TransparentGateway( token, Option.None(), this.device.NestedEdge.IsNestedEdge); + + Context.Current.LeafDeleteList.TryAdd(leafDeviceId, leaf); } catch (Exception) when (!parentId.HasValue) { @@ -58,18 +60,10 @@ public async Task TransparentGateway( Assert.NotNull(leaf); - await TryFinally.DoAsync( - async () => - { - DateTime seekTime = DateTime.Now; - await leaf.SendEventAsync(token); - await leaf.WaitForEventsReceivedAsync(seekTime, token); - await leaf.InvokeDirectMethodAsync(token); - }, - async () => - { - await leaf.DeleteIdentityAsync(token); - }); + DateTime seekTime = DateTime.Now; + await leaf.SendEventAsync(token); + await leaf.WaitForEventsReceivedAsync(seekTime, token); + await leaf.InvokeDirectMethodAsync(token); } [Test] @@ -112,6 +106,8 @@ public async Task GrandparentScopeDevice( token, Option.None(), this.device.NestedEdge.IsNestedEdge); + + Context.Current.LeafDeleteList.TryAdd(leafDeviceId, leaf); } catch (Exception) when (!parentId.HasValue) { @@ -125,19 +121,10 @@ public async Task GrandparentScopeDevice( Assert.NotNull(leaf); - await TryFinally.DoAsync( - async () => - { - DateTime seekTime = DateTime.Now; - await leaf.SendEventAsync(token); - await leaf.WaitForEventsReceivedAsync(seekTime, token); - await leaf.InvokeDirectMethodAsync(token); - }, - async () => - { - await leaf.DeleteIdentityAsync(token); - await Task.CompletedTask; - }); + DateTime seekTime = DateTime.Now; + await leaf.SendEventAsync(token); + await leaf.WaitForEventsReceivedAsync(seekTime, token); + await leaf.InvokeDirectMethodAsync(token); } } } diff --git a/test/Microsoft.Azure.Devices.Edge.Test/PlugAndPlay.cs b/test/Microsoft.Azure.Devices.Edge.Test/PlugAndPlay.cs index 45eaf14d4f3..5fd4017c7b4 100644 --- a/test/Microsoft.Azure.Devices.Edge.Test/PlugAndPlay.cs +++ b/test/Microsoft.Azure.Devices.Edge.Test/PlugAndPlay.cs @@ -48,18 +48,12 @@ public async Task PlugAndPlayDeviceClient(Protocol protocol) Option.Some(TestModelId), Context.Current.NestedEdge); - await TryFinally.DoAsync( - async () => - { - DateTime seekTime = DateTime.Now; - await leaf.SendEventAsync(token); - await leaf.WaitForEventsReceivedAsync(seekTime, token); - await this.ValidateIdentity(leafDeviceId, Option.None(), TestModelId, token); - }, - async () => - { - await leaf.DeleteIdentityAsync(token); - }); + Context.Current.LeafDeleteList.TryAdd(leafDeviceId, leaf); + + DateTime seekTime = DateTime.Now; + await leaf.SendEventAsync(token); + await leaf.WaitForEventsReceivedAsync(seekTime, token); + await this.ValidateIdentity(leafDeviceId, Option.None(), TestModelId, token); } [TestCase(Protocol.Mqtt)] diff --git a/test/Microsoft.Azure.Devices.Edge.Test/SetupFixture.cs b/test/Microsoft.Azure.Devices.Edge.Test/SetupFixture.cs index be8e67f3966..17d554e4cd0 100644 --- a/test/Microsoft.Azure.Devices.Edge.Test/SetupFixture.cs +++ b/test/Microsoft.Azure.Devices.Edge.Test/SetupFixture.cs @@ -124,6 +124,11 @@ public Task AfterAllAsync() => TryFinally.DoAsync( await device.MaybeDeleteIdentityAsync(token); } + foreach (var leaf in Context.Current.LeafDeleteList.Values) + { + await leaf.DeleteIdentityAsync(token); + } + // Remove packages installed by this run. await this.daemon.UninstallAsync(token); diff --git a/test/Microsoft.Azure.Devices.Edge.Test/X509Device.cs b/test/Microsoft.Azure.Devices.Edge.Test/X509Device.cs index 767c5cb999c..cdc388e3e54 100644 --- a/test/Microsoft.Azure.Devices.Edge.Test/X509Device.cs +++ b/test/Microsoft.Azure.Devices.Edge.Test/X509Device.cs @@ -38,18 +38,12 @@ public async Task X509ManualProvision() Option.None(), Context.Current.NestedEdge); - await TryFinally.DoAsync( - async () => - { - DateTime seekTime = DateTime.Now; - await leaf.SendEventAsync(token); - await leaf.WaitForEventsReceivedAsync(seekTime, token); - await leaf.InvokeDirectMethodAsync(token); - }, - async () => - { - await leaf.DeleteIdentityAsync(token); - }); + Context.Current.LeafDeleteList.TryAdd(leafDeviceId, leaf); + + DateTime seekTime = DateTime.Now; + await leaf.SendEventAsync(token); + await leaf.WaitForEventsReceivedAsync(seekTime, token); + await leaf.InvokeDirectMethodAsync(token); } } }