-
-
Notifications
You must be signed in to change notification settings - Fork 341
feat: Add Azure Event Hubs module #1342
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
Closed
rafek1241
wants to merge
40
commits into
testcontainers:develop
from
rafek1241:feature/add-azure-event-hubs
Closed
Changes from 22 commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
8fe0070
Support Azure EventHubs
WakaToa 3d5c226
Adjust EventHubsConfiguration
WakaToa ebccc8c
Add CPM for Azure.Messaging.EventHubs
WakaToa b264e42
better ordering of package version Azure.Messaging.EventHubs
WakaToa 2503ceb
fix: file-scope namespace
rafek1241 b411706
fix: global using
rafek1241 459dae9
Merge branch 'develop' into feature/add-azure-event-hubs
rafek1241 0ba4f84
feat: use same pattern for EULA license
rafek1241 79d4b40
chore: remove redundant reference to config builder
rafek1241 da84fea
fix: target frameworks
rafek1241 4a1383a
fix: target framework of the test project
rafek1241 87f4d32
chore: small refactor + add docs for better understanding
rafek1241 2c7d413
Revert "chore: remove redundant reference to config builder"
rafek1241 8ca479d
refactor: add validation of configuration builder
rafek1241 6b27965
chore: move waiting strategy to init()
rafek1241 a5116e2
fix: validation
rafek1241 de5d54b
fix: test name and network hardcoded name
rafek1241 99efb2c
feat: implement built-in and configurable azurite docker container in…
rafek1241 2d28534
fix: default namespace name according to documentation
rafek1241 f650ea2
build: workflow
rafek1241 40bd09a
refactor: azurite dependency
rafek1241 0758827
feat: tests to cover custom azurite case
rafek1241 f7121d1
revert: changes copied from PR !1335
rafek1241 a9fe768
refactor: move creation of dependent stuff in build() process
rafek1241 7eabf4e
Merge remote-tracking branch 'upstream/develop' into feature/add-azur…
rafek1241 21637c5
docs: azure eventhubs documentation page
rafek1241 72c6b52
fix: mkdocs
rafek1241 5cfa3d8
fix: docs
rafek1241 58a9dd9
docs: fix module name
rafek1241 c3bf5e0
Merge remote-tracking branch 'origin/develop' into fork/rafek1241/fea…
HofmeisterAn 53150dc
Merge remote-tracking branch 'origin/develop' into fork/rafek1241/fea…
HofmeisterAn 0b03f27
chore: Align EventHubs module implementation
HofmeisterAn cfb21b8
Merge branch 'develop' into feature/add-azure-event-hubs
HofmeisterAn bfe1996
Merge remote-tracking branch 'rafek1241/feature/add-azure-event-hubs'…
HofmeisterAn 34cc45d
chore: Remove WithNetworkAliases
HofmeisterAn d9f4bdc
Merge pull request #1 from testcontainers/fork/rafek1241/feature/add-…
rafek1241 4f932be
fix: format
rafek1241 73e6af7
fix: docs
rafek1241 e9df035
refactor: logic to have service configuration instead of Configuratio…
rafek1241 aee785d
tests(WIP): eventhubs kafka usage example
rafek1241 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| root = true |
63 changes: 63 additions & 0 deletions
63
src/Testcontainers.EventHubs/Configuration/ConfigurationBuilder.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| namespace Testcontainers.EventHubs.Configuration; | ||
|
|
||
| public class ConfigurationBuilder | ||
| { | ||
| private const string DefaultNamespace = "emulatorNs1"; | ||
| private const string DefaultLoggingType = "file"; | ||
|
|
||
| private readonly RootConfiguration _rootConfiguration = new RootConfiguration(); | ||
|
|
||
| private ConfigurationBuilder() | ||
| { | ||
| _rootConfiguration.UserConfig = new UserConfig | ||
| { | ||
| NamespaceConfig = | ||
| [ | ||
| new NamespaceConfig | ||
| { | ||
| Type = "EventHub", | ||
| Name = DefaultNamespace | ||
| }, | ||
| ], | ||
| LoggingConfig = new LoggingConfig() { Type = DefaultLoggingType }, | ||
| }; | ||
| } | ||
|
|
||
| public static ConfigurationBuilder Create() | ||
| { | ||
| return new ConfigurationBuilder(); | ||
| } | ||
|
|
||
| public ConfigurationBuilder WithEventHub(string entityName, int partitionCount, IEnumerable<string> consumerGroups) | ||
| { | ||
| var namespaceConfig = _rootConfiguration.UserConfig.NamespaceConfig.FirstOrDefault(x => x.Name == DefaultNamespace); | ||
| if (namespaceConfig == null) | ||
| { | ||
| throw new InvalidOperationException($"Default Namespace '{DefaultNamespace}' not found."); | ||
| } | ||
|
|
||
| namespaceConfig.Entities.Add(new Entity | ||
| { | ||
| Name = entityName, | ||
| PartitionCount = partitionCount, | ||
| ConsumerGroups = consumerGroups.Select(consumerGroupName => new ConsumerGroup { Name = consumerGroupName }).ToList(), | ||
| }); | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| public bool Validate() | ||
| { | ||
| return _rootConfiguration.UserConfig.NamespaceConfig.Count == 1 && | ||
| _rootConfiguration.UserConfig.NamespaceConfig.First().Entities.Count > 0 && | ||
| _rootConfiguration.UserConfig.NamespaceConfig.First().Entities.Count <= 10 && | ||
| _rootConfiguration.UserConfig.NamespaceConfig.First().Entities.All(entity => | ||
| entity.PartitionCount is > 0 and <= 32 && | ||
| entity.ConsumerGroups.Count is > 0 and <= 20); | ||
| } | ||
|
|
||
| public string Build() | ||
| { | ||
| return JsonSerializer.Serialize(_rootConfiguration); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| namespace Testcontainers.EventHubs.Configuration; | ||
|
|
||
| public record ConsumerGroup | ||
| { | ||
| public string Name { get; set; } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| namespace Testcontainers.EventHubs.Configuration; | ||
|
|
||
| public record Entity | ||
| { | ||
| public string Name { get; set; } | ||
| public int PartitionCount { get; set; } | ||
| public List<ConsumerGroup> ConsumerGroups { get; set; } = []; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| namespace Testcontainers.EventHubs.Configuration; | ||
|
|
||
| public record LoggingConfig | ||
| { | ||
| public string Type { get; set; } | ||
| } |
9 changes: 9 additions & 0 deletions
9
src/Testcontainers.EventHubs/Configuration/NamespaceConfig.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| namespace Testcontainers.EventHubs.Configuration; | ||
|
|
||
| public record NamespaceConfig | ||
| { | ||
| public string Type { get; set; } | ||
| public string Name { get; set; } | ||
|
|
||
| public List<Entity> Entities { get; set; } = []; | ||
| } |
10 changes: 10 additions & 0 deletions
10
src/Testcontainers.EventHubs/Configuration/RootConfiguration.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| namespace Testcontainers.EventHubs.Configuration; | ||
|
|
||
| /// <summary> | ||
| /// Azure Event Hubs emulator JSON configuration. | ||
| /// <seealso href="https://raw.githubusercontent.com/Azure/azure-event-hubs-emulator-installer/refs/heads/main/EventHub-Emulator/Schema/Config-schema.json"/> | ||
| /// </summary> | ||
| public record RootConfiguration | ||
| { | ||
| public UserConfig UserConfig { get; set; } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| namespace Testcontainers.EventHubs.Configuration; | ||
|
|
||
| public record UserConfig | ||
| { | ||
| public List<NamespaceConfig> NamespaceConfig { get; set; } = []; | ||
| public LoggingConfig LoggingConfig { get; set; } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| namespace Testcontainers.EventHubs; | ||
|
|
||
| /// <inheritdoc cref="ContainerBuilder{TBuilderEntity, TContainerEntity, TConfigurationEntity}" /> | ||
| [PublicAPI] | ||
| public sealed class EventHubsBuilder : ContainerBuilder<EventHubsBuilder, EventHubsContainer, EventHubsConfiguration> | ||
| { | ||
| public const string EventHubsImage = "mcr.microsoft.com/azure-messaging/eventhubs-emulator:latest"; | ||
|
|
||
| public const ushort EventHubsPort = 5672; | ||
rafek1241 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| public const string AzuriteNetworkAlias = "azurite"; | ||
|
|
||
| private const string AcceptLicenseAgreementEnvVar = "ACCEPT_EULA"; | ||
|
|
||
| private const string AcceptLicenseAgreement = "Y"; | ||
|
|
||
| private const string DeclineLicenseAgreement = "N"; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="EventHubsBuilder" /> class. | ||
| /// </summary> | ||
| public EventHubsBuilder() | ||
| : this(new EventHubsConfiguration()) | ||
| { | ||
| DockerResourceConfiguration = Init().DockerResourceConfiguration; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="EventHubsBuilder" /> class. | ||
| /// </summary> | ||
| /// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
| private EventHubsBuilder(EventHubsConfiguration resourceConfiguration) | ||
| : base(resourceConfiguration) | ||
| { | ||
| DockerResourceConfiguration = resourceConfiguration; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override EventHubsConfiguration DockerResourceConfiguration { get; } | ||
|
|
||
| /// <summary> | ||
| /// Sets the event hub configuration | ||
| /// </summary> | ||
| /// <param name="configurationBuilder"></param> | ||
| /// <returns></returns> | ||
| public EventHubsBuilder WithConfigurationBuilder(ConfigurationBuilder configurationBuilder) | ||
| { | ||
| var configBytes = Encoding.UTF8.GetBytes(configurationBuilder.Build()); | ||
|
|
||
| return Merge(DockerResourceConfiguration, | ||
| new EventHubsConfiguration(configurationBuilder: configurationBuilder)) | ||
| .WithResourceMapping(configBytes, "Eventhubs_Emulator/ConfigFiles/Config.json"); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Accepts the license agreement. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// When <paramref name="acceptLicenseAgreement" /> is set to <c>true</c>, the Azure Event Hubs Emulator <see href="https://github.com/Azure/azure-event-hubs-emulator-installer/blob/main/EMULATOR_EULA.md">license</see> is accepted. | ||
| /// </remarks> | ||
| /// <param name="acceptLicenseAgreement">A boolean value indicating whether the Azure Event Hubs Emulator license agreement is accepted.</param> | ||
| /// <returns>A configured instance of <see cref="EventHubsBuilder" />.</returns> | ||
| public EventHubsBuilder WithAcceptLicenseAgreement(bool acceptLicenseAgreement) | ||
| { | ||
| var licenseAgreement = acceptLicenseAgreement ? AcceptLicenseAgreement : DeclineLicenseAgreement; | ||
| return WithEnvironment(AcceptLicenseAgreementEnvVar, licenseAgreement); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Sets the Azurite container for the Event Hubs Emulator. | ||
| /// </summary> | ||
| /// <param name="azuriteContainer">docker container</param> | ||
| /// <param name="alias">network alias name that is used for connection to blob and table endpoints</param> | ||
| /// <returns></returns> | ||
| public EventHubsBuilder WithAzurite(AzuriteContainer azuriteContainer, | ||
| string alias) => | ||
| WithAzurite(azuriteContainer, false, alias); | ||
|
|
||
| private EventHubsBuilder WithAzurite(AzuriteContainer azuriteContainer, | ||
| bool isInternal, | ||
| string alias = AzuriteNetworkAlias) | ||
| { | ||
| var builder = Merge(DockerResourceConfiguration, | ||
| new EventHubsConfiguration(azuriteContainer: azuriteContainer)); | ||
|
|
||
| if (!isInternal) | ||
| { | ||
| builder = builder.DependsOn(azuriteContainer); | ||
| } | ||
|
|
||
| return builder | ||
| .WithEnvironment("BLOB_SERVER", alias) | ||
| .WithEnvironment("METADATA_SERVER", alias); | ||
| } | ||
|
|
||
| private EventHubsBuilder WithAzurite() | ||
| { | ||
| var azuriteContainer = new AzuriteBuilder() | ||
| .WithNetwork(DockerResourceConfiguration.Networks.Single()) | ||
| .WithNetworkAliases(AzuriteNetworkAlias) | ||
| .Build(); | ||
|
|
||
| return WithAzurite(azuriteContainer, true); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public override EventHubsContainer Build() | ||
| { | ||
| Validate(); | ||
| return new EventHubsContainer(DockerResourceConfiguration); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override void Validate() | ||
| { | ||
| base.Validate(); | ||
|
|
||
| _ = Guard.Argument(DockerResourceConfiguration, nameof(DockerResourceConfiguration.Image)) | ||
| .ThrowIf(argument => LicenseAgreementNotAccepted(argument.Value), | ||
| argument => throw new ArgumentException( | ||
| $"The image '{DockerResourceConfiguration.Image.FullName}' requires you to accept a license agreement.", | ||
| argument.Name)); | ||
|
|
||
| _ = Guard.Argument(DockerResourceConfiguration.ConfigurationBuilder, | ||
| nameof(DockerResourceConfiguration.ConfigurationBuilder)) | ||
| .NotNull() | ||
| .ThrowIf(x => !x.Value.Validate(), _ => throw new ArgumentException("ConfigurationBuilder is invalid.")); | ||
|
|
||
| return; | ||
|
|
||
| bool LicenseAgreementNotAccepted(EventHubsConfiguration value) => | ||
| !value.Environments.TryGetValue(AcceptLicenseAgreementEnvVar, out var licenseAgreementValue) || | ||
| !AcceptLicenseAgreement.Equals(licenseAgreementValue, StringComparison.Ordinal); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override EventHubsBuilder Init() | ||
| { | ||
| return base.Init() | ||
| .WithImage(EventHubsImage) | ||
| .WithNetwork(new NetworkBuilder().Build()) | ||
| .WithPortBinding(EventHubsPort, true) | ||
| .WithAzurite() | ||
| .WithWaitStrategy(Wait.ForUnixContainer() | ||
| .UntilMessageIsLogged("Emulator Service is Successfully Up!") | ||
| .AddCustomWaitStrategy(new WaitTwoSeconds())); | ||
| ; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override EventHubsBuilder Clone(IResourceConfiguration<CreateContainerParameters> resourceConfiguration) | ||
| { | ||
| return Merge(DockerResourceConfiguration, new EventHubsConfiguration(resourceConfiguration)); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override EventHubsBuilder Clone(IContainerConfiguration resourceConfiguration) | ||
| { | ||
| return Merge(DockerResourceConfiguration, new EventHubsConfiguration(resourceConfiguration)); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override EventHubsBuilder Merge(EventHubsConfiguration oldValue, | ||
| EventHubsConfiguration newValue) | ||
| { | ||
| return new EventHubsBuilder(new EventHubsConfiguration(oldValue, newValue)); | ||
| } | ||
|
|
||
| private sealed class WaitTwoSeconds : IWaitUntil | ||
| { | ||
| /// <inheritdoc /> | ||
| public async Task<bool> UntilAsync(IContainer container) | ||
| { | ||
| await Task.Delay(TimeSpan.FromSeconds(2)) | ||
| .ConfigureAwait(false); | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.