Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Event Hubs] Upgrade management package #37568

Merged
merged 5 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" />
<PackageReference Include="Microsoft.Azure.Management.EventHub" />
<PackageReference Include="Microsoft.Azure.Management.ResourceManager" />
<PackageReference Include="Microsoft.Azure.Management.Storage" />
<PackageReference Include="Microsoft.Azure.Services.AppAuthentication" />
<PackageReference Include="Polly" />
<PackageReference Include="Azure.ResourceManager.EventHubs" />
<PackageReference Include="CommandLineParser" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Management.EventHub" />
<PackageReference Include="Microsoft.Azure.Management.ResourceManager" />
<PackageReference Include="Microsoft.Azure.Services.AppAuthentication" />
<PackageReference Include="Azure.ResourceManager.EventHubs" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Moq" />
<PackageReference Include="NUnit" />
<PackageReference Include="NUnit3TestAdapter" />
<PackageReference Include="Polly" />
<PackageReference Include="System.Memory.Data" />
<PackageReference Include="System.Net.WebSockets.Client" />
<PackageReference Include="System.Threading.Tasks.Extensions" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Azure.Messaging.EventHubs.Consumer;
using Microsoft.Azure.Management.EventHub;
using Microsoft.Azure.Management.EventHub.Models;
using Microsoft.Azure.Management.ResourceManager;
using Microsoft.Rest;

namespace Azure.Messaging.EventHubs.Tests
{
Expand All @@ -26,9 +22,6 @@ public sealed class EventHubScope : IAsyncDisposable
/// <summary>The manager for common live test resource operations.</summary>
private static readonly LiveResourceManager ResourceManager = new LiveResourceManager();

/// <summary>The location of the Azure Resource Manager for the active cloud environment.</summary>
private static readonly Uri AzureResourceManagerUri = new Uri(EventHubsTestEnvironment.Instance.ResourceManagerUrl);

/// <summary>Serves as a sentinel flag to denote when the instance has been disposed.</summary>
private volatile bool _disposed = false;

Expand Down Expand Up @@ -56,6 +49,7 @@ public sealed class EventHubScope : IAsyncDisposable
/// Initializes a new instance of the <see cref="EventHubScope"/> class.
/// </summary>
///
/// <param name="eventHubResourceId">The ARM resource identifier of the Event Hub that was created.</param>
jsquire marked this conversation as resolved.
Show resolved Hide resolved
/// <param name="eventHubHame">The name of the Event Hub that was created.</param>
/// <param name="consumerGroups">The set of consumer groups associated with the Event Hub; the default consumer group is not included, as it is implicitly created.</param>
/// <param name="shouldRemoveEventHubAtScopeCompletion">Indicates whether the Event Hub should be removed when the current scope is completed.</param>
Expand Down Expand Up @@ -86,14 +80,9 @@ public async ValueTask DisposeAsync()
return;
}

var resourceGroup = EventHubsTestEnvironment.Instance.ResourceGroup;
var eventHubNamespace = EventHubsTestEnvironment.Instance.EventHubsNamespace;
var token = await ResourceManager.AcquireManagementTokenAsync().ConfigureAwait(false);
var client = new EventHubManagementClient(AzureResourceManagerUri, new TokenCredentials(token)) { SubscriptionId = EventHubsTestEnvironment.Instance.SubscriptionId };

try
{
await ResourceManager.CreateRetryPolicy().ExecuteAsync(() => client.EventHubs.DeleteAsync(resourceGroup, eventHubNamespace, EventHubName)).ConfigureAwait(false);
await ResourceManager.DeleteEventHubAsync(EventHubName).ConfigureAwait(false);
}
catch
{
Expand All @@ -104,10 +93,6 @@ public async ValueTask DisposeAsync()
// If an Event Hub fails to be deleted, removing of the associated namespace at the end of the
// test run will also remove the orphan.
}
finally
{
client?.Dispose();
}

_disposed = true;
}
Expand Down Expand Up @@ -136,25 +121,6 @@ public static Task<EventHubScope> CreateAsync(int partitionCount,
IEnumerable<string> consumerGroups,
[CallerMemberName] string caller = "") => BuildScope(partitionCount, consumerGroups, caller);

/// <summary>
/// Performs the tasks needed to remove an ephemeral Event Hubs namespace used as a container for Event Hub instances
/// for a specific test run.
/// </summary>
///
/// <param name="namespaceName">The name of the namespace to delete.</param>
///
public static async Task DeleteNamespaceAsync(string namespaceName)
{
var subscription = EventHubsTestEnvironment.Instance.SubscriptionId;
var resourceGroup = EventHubsTestEnvironment.Instance.ResourceGroup;
var token = await ResourceManager.AcquireManagementTokenAsync().ConfigureAwait(false);

using (var client = new EventHubManagementClient(AzureResourceManagerUri, new TokenCredentials(token)) { SubscriptionId = subscription })
{
await ResourceManager.CreateRetryPolicy().ExecuteAsync(() => client.Namespaces.DeleteAsync(resourceGroup, namespaceName)).ConfigureAwait(false);
}
}

/// <summary>
/// Builds a new scope based on the Event Hub that is named using <see cref="EventHubsTestEnvironment.Instance.EventHubName" />, if specified. If not,
/// a new EventHub is created for the scope.
Expand Down Expand Up @@ -189,22 +155,11 @@ private static Task<EventHubScope> BuildScope(int partitionCount,
///
/// <returns>The <see cref="EventHubScope" /> that will be used in a given test run.</returns>
///
private static async Task<EventHubScope> BuildScopeFromExistingEventHub()
{
var token = await ResourceManager.AcquireManagementTokenAsync().ConfigureAwait(false);

using (var client = new EventHubManagementClient(AzureResourceManagerUri, new TokenCredentials(token)) { SubscriptionId = EventHubsTestEnvironment.Instance.SubscriptionId })
{
var consumerGroups = client.ConsumerGroups.ListByEventHub
(
EventHubsTestEnvironment.Instance.ResourceGroup,
EventHubsTestEnvironment.Instance.EventHubsNamespace,
EventHubsTestEnvironment.Instance.EventHubNameOverride
);

return new EventHubScope(EventHubsTestEnvironment.Instance.EventHubNameOverride, consumerGroups.Select(c => c.Name).ToList(), shouldRemoveEventHubAtScopeCompletion: false);
}
}
private static Task<EventHubScope> BuildScopeFromExistingEventHub() => Task.FromResult(
new EventHubScope(
EventHubsTestEnvironment.Instance.EventHubNameOverride,
ResourceManager.QueryEventHubConsumerGroupNames(EventHubsTestEnvironment.Instance.EventHubNameOverride),
shouldRemoveEventHubAtScopeCompletion: false));

/// <summary>
/// Performs the tasks needed to create a new Event Hub instance with the requested
Expand All @@ -221,34 +176,29 @@ private static async Task<EventHubScope> BuildScopeWithNewEventHub(int partition
IEnumerable<string> consumerGroups,
[CallerMemberName] string caller = "")
{
// Use the name of the test along with some randomization as the name of the ephemeral Event Hub. This
// allows us to identify which test is associated with it, while preventing test run failures when cleanup
// fails.

caller = (caller.Length < 16) ? caller : caller.Substring(0, 15);

var groups = (consumerGroups ?? Enumerable.Empty<string>()).ToList();
var resourceGroup = EventHubsTestEnvironment.Instance.ResourceGroup;
var eventHubNamespace = EventHubsTestEnvironment.Instance.EventHubsNamespace;
var token = await ResourceManager.AcquireManagementTokenAsync().ConfigureAwait(false);
var eventHubName = $"{ Guid.NewGuid().ToString("D").Substring(0, 13) }-{ caller }";
var groups = consumerGroups?.ToList() ?? new List<string>();
var eventHub = await ResourceManager.CreateEventHubAsync(eventHubName, partitionCount, groups).ConfigureAwait(false);

string CreateName() => $"{ Guid.NewGuid().ToString("D").Substring(0, 13) }-{ caller }";
// There is a race condition in which ARM has created the new Event Hub but it is not yet visible to the Event Hubs
// service. Introduce a short delay to allow for the service to get access to the new resource.

using (var client = new EventHubManagementClient(AzureResourceManagerUri, new TokenCredentials(token)) { SubscriptionId = EventHubsTestEnvironment.Instance.SubscriptionId })
{
var eventHub = new Eventhub(partitionCount: partitionCount);
eventHub = await ResourceManager.CreateRetryPolicy<Eventhub>().ExecuteAsync(() => client.EventHubs.CreateOrUpdateAsync(resourceGroup, eventHubNamespace, CreateName(), eventHub)).ConfigureAwait(false);

var consumerPolicy = ResourceManager.CreateRetryPolicy<ConsumerGroup>();

await Task.WhenAll
(
consumerGroups.Select(groupName =>
{
var group = new ConsumerGroup(name: groupName);
return consumerPolicy.ExecuteAsync(() => client.ConsumerGroups.CreateOrUpdateAsync(resourceGroup, eventHubNamespace, eventHub.Name, groupName, group));
})
).ConfigureAwait(false);

groups.Insert(0, EventHubConsumerClient.DefaultConsumerGroupName);
return new EventHubScope(eventHub.Name, groups, shouldRemoveEventHubAtScopeCompletion: true);
}
await Task.Delay(TimeSpan.FromSeconds(5)).ConfigureAwait(false);

// The default consumer group is always present; include it as part of the scope.

groups.Insert(0, EventHubConsumerClient.DefaultConsumerGroupName);

return new EventHubScope(
eventHubName,
groups,
shouldRemoveEventHubAtScopeCompletion: true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core.TestFramework;

namespace Azure.Messaging.EventHubs.Tests
Expand Down
Loading