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

feat: Add pulsar module #1138

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<PackageVersion Include="Neo4j.Driver" Version="5.5.0"/>
<PackageVersion Include="Npgsql" Version="6.0.10"/>
<PackageVersion Include="Oracle.ManagedDataAccess.Core" Version="3.21.90"/>
<PackageVersion Include="DotPulsar" Version="3.1.2"/>
<PackageVersion Include="RabbitMQ.Client" Version="6.4.0"/>
<PackageVersion Include="RavenDB.Client" Version="5.4.100"/>
<PackageVersion Include="Selenium.WebDriver" Version="4.8.1"/>
Expand Down
14 changes: 14 additions & 0 deletions Testcontainers.sln
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.Tests", "tes
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.WebDriver.Tests", "tests\Testcontainers.WebDriver.Tests\Testcontainers.WebDriver.Tests.csproj", "{EBA72C3B-57D5-43FF-A5B4-3D55B3B6D4C2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.Pulsar", "src\Testcontainers.Pulsar\Testcontainers.Pulsar.csproj", "{59ABEA46-0099-4564-8AF4-1C96CEF5A52C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.Pulsar.Tests", "tests\Testcontainers.Pulsar.Tests\Testcontainers.Pulsar.Tests.csproj", "{9FE7BE58-F79F-47F3-A718-4A67CC5370F5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -568,6 +572,14 @@ Global
{EBA72C3B-57D5-43FF-A5B4-3D55B3B6D4C2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EBA72C3B-57D5-43FF-A5B4-3D55B3B6D4C2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EBA72C3B-57D5-43FF-A5B4-3D55B3B6D4C2}.Release|Any CPU.Build.0 = Release|Any CPU
{59ABEA46-0099-4564-8AF4-1C96CEF5A52C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{59ABEA46-0099-4564-8AF4-1C96CEF5A52C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{59ABEA46-0099-4564-8AF4-1C96CEF5A52C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{59ABEA46-0099-4564-8AF4-1C96CEF5A52C}.Release|Any CPU.Build.0 = Release|Any CPU
{9FE7BE58-F79F-47F3-A718-4A67CC5370F5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9FE7BE58-F79F-47F3-A718-4A67CC5370F5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9FE7BE58-F79F-47F3-A718-4A67CC5370F5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9FE7BE58-F79F-47F3-A718-4A67CC5370F5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{5365F780-0E6C-41F0-B1B9-7DC34368F80C} = {673F23AE-7694-4BB9-ABD4-136D6C13634E}
Expand Down Expand Up @@ -661,5 +673,7 @@ Global
{1A1983E6-5297-435F-B467-E8E1F11277D6} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
{27CDB869-A150-4593-958F-6F26E5391E7C} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
{EBA72C3B-57D5-43FF-A5B4-3D55B3B6D4C2} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
{59ABEA46-0099-4564-8AF4-1C96CEF5A52C} = {673F23AE-7694-4BB9-ABD4-136D6C13634E}
{9FE7BE58-F79F-47F3-A718-4A67CC5370F5} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
EndGlobalSection
EndGlobal
1 change: 1 addition & 0 deletions docs/modules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ await moduleNameContainer.StartAsync();
| Papercut | `jijiechen/papercut:latest` | [NuGet](https://www.nuget.org/packages/Testcontainers.Papercut) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.Papercut) |
| PostgreSQL | `postgres:15.1` | [NuGet](https://www.nuget.org/packages/Testcontainers.PostgreSql) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.PostgreSql) |
| PubSub | `gcr.io/google.com/cloudsdktool/google-cloud-cli:446.0.1-emulators` | [NuGet](https://www.nuget.org/packages/Testcontainers.PubSub) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.PubSub) |
| Pulsar | `apachepulsar/pulsar:3.2.0` | [NuGet](https://www.nuget.org/packages/Testcontainers.Pulsar) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.Pulsar) |
| RabbitMQ | `rabbitmq:3.11` | [NuGet](https://www.nuget.org/packages/Testcontainers.RabbitMq) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.RabbitMq) |
| RavenDB | `ravendb/ravendb:5.4-ubuntu-latest` | [NuGet](https://www.nuget.org/packages/Testcontainers.RavenDb) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.RavenDb) |
| Redis | `redis:7.0` | [NuGet](https://www.nuget.org/packages/Testcontainers.Redis) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.Redis) |
Expand Down
1 change: 1 addition & 0 deletions src/Testcontainers.Pulsar/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
root = true
80 changes: 80 additions & 0 deletions src/Testcontainers.Pulsar/PulsarBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
namespace Testcontainers.Pulsar;

/// <inheritdoc cref="ContainerBuilder{TBuilderEntity, TContainerEntity, TConfigurationEntity}" />
[PublicAPI]
public sealed class PulsarBuilder : ContainerBuilder<PulsarBuilder, PulsarContainer, PulsarConfiguration>
{
public const string PulsarImage = "apachepulsar/pulsar:3.2.0";

public const ushort BrokerPort = 6650;
public const ushort BrokerHttpPort = 8080;

/// <summary>
/// Initializes a new instance of the <see cref="PulsarBuilder" /> class.
/// </summary>
public PulsarBuilder()
: this(new PulsarConfiguration())
{
DockerResourceConfiguration = Init().DockerResourceConfiguration;
}

/// <summary>
/// Initializes a new instance of the <see cref="PulsarBuilder" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
private PulsarBuilder(PulsarConfiguration resourceConfiguration)
: base(resourceConfiguration)
{
DockerResourceConfiguration = resourceConfiguration;
}

/// <inheritdoc />
protected override PulsarConfiguration DockerResourceConfiguration { get; }

/// <inheritdoc />
public override PulsarContainer Build()
{
Validate();
return new PulsarContainer(DockerResourceConfiguration, TestcontainersSettings.Logger);
}

/// <inheritdoc />
protected override PulsarBuilder Init()
{
return base.Init()
.WithImage(PulsarImage)
.WithPortBinding(BrokerPort, true)
.WithPortBinding(BrokerHttpPort, true)
.WithEntrypoint("/bin/bash", "-c", "/pulsar/bin/apply-config-from-env.py /pulsar/conf/standalone.conf && bin/pulsar standalone --no-functions-worker -nss")
.WithWaitStrategy(Wait.ForUnixContainer()
.UntilPortIsAvailable(BrokerPort)
.UntilPortIsAvailable(BrokerHttpPort)
.UntilHttpRequestIsSucceeded(request => request.ForPort(BrokerHttpPort).ForPath("/admin/v2/clusters").ForResponseMessageMatching(IsNodeReadyAsync)));
}

/// <inheritdoc />
protected override PulsarBuilder Clone(IResourceConfiguration<CreateContainerParameters> resourceConfiguration)
{
return Merge(DockerResourceConfiguration, new PulsarConfiguration(resourceConfiguration));
}

/// <inheritdoc />
protected override PulsarBuilder Clone(IContainerConfiguration resourceConfiguration)
{
return Merge(DockerResourceConfiguration, new PulsarConfiguration(resourceConfiguration));
}

/// <inheritdoc />
protected override PulsarBuilder Merge(PulsarConfiguration oldValue, PulsarConfiguration newValue)
{
return new PulsarBuilder(new PulsarConfiguration(oldValue, newValue));
}

private async Task<bool> IsNodeReadyAsync(HttpResponseMessage response)
{
var content = await response.Content.ReadAsStringAsync()
.ConfigureAwait(false);

return "[\"standalone\"]".Equals(content, StringComparison.OrdinalIgnoreCase);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if PULSAR_PREFIX_clusterName env var is set then the wait strategy will fail. It should check the env var's value first.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As @entvex mentioned, there is already an open PR. We should focus on one implementation.

}
}
53 changes: 53 additions & 0 deletions src/Testcontainers.Pulsar/PulsarConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
namespace Testcontainers.Pulsar;

/// <inheritdoc cref="ContainerConfiguration" />
[PublicAPI]
public sealed class PulsarConfiguration : ContainerConfiguration
{
/// <summary>
/// Initializes a new instance of the <see cref="PulsarConfiguration" /> class.
/// </summary>
public PulsarConfiguration()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="PulsarConfiguration" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
public PulsarConfiguration(IResourceConfiguration<CreateContainerParameters> resourceConfiguration)
: base(resourceConfiguration)
{
// Passes the configuration upwards to the base implementations to create an updated immutable copy.
}

/// <summary>
/// Initializes a new instance of the <see cref="PulsarConfiguration" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
public PulsarConfiguration(IContainerConfiguration resourceConfiguration)
: base(resourceConfiguration)
{
// Passes the configuration upwards to the base implementations to create an updated immutable copy.
}

/// <summary>
/// Initializes a new instance of the <see cref="PulsarConfiguration" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
public PulsarConfiguration(PulsarConfiguration resourceConfiguration)
: this(new PulsarConfiguration(), resourceConfiguration)
{
// Passes the configuration upwards to the base implementations to create an updated immutable copy.
}

/// <summary>
/// Initializes a new instance of the <see cref="PulsarConfiguration" /> class.
/// </summary>
/// <param name="oldValue">The old Docker resource configuration.</param>
/// <param name="newValue">The new Docker resource configuration.</param>
public PulsarConfiguration(PulsarConfiguration oldValue, PulsarConfiguration newValue)
: base(oldValue, newValue)
{
}
}
25 changes: 25 additions & 0 deletions src/Testcontainers.Pulsar/PulsarContainer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace Testcontainers.Pulsar;

/// <inheritdoc cref="DockerContainer" />
[PublicAPI]
public sealed class PulsarContainer : DockerContainer
{
/// <summary>
/// Initializes a new instance of the <see cref="PulsarContainer" /> class.
/// </summary>
/// <param name="configuration">The container configuration.</param>
/// <param name="logger">The logger.</param>
public PulsarContainer(PulsarConfiguration configuration, ILogger logger)
: base(configuration, logger)
{
}

/// <summary>
/// Gets the service URL for the Pulsar cluster.
/// </summary>
/// <returns>The service URL.</returns>
public Uri GetServiceUrl()
{
return new Uri($"pulsar://{Hostname}:{GetMappedPublicPort(PulsarBuilder.BrokerPort)}");
}
}
12 changes: 12 additions & 0 deletions src/Testcontainers.Pulsar/Testcontainers.Pulsar.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0;net8.0;netstandard2.0;netstandard2.1</TargetFrameworks>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" VersionOverride="2023.3.0" PrivateAssets="All"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../Testcontainers/Testcontainers.csproj"/>
</ItemGroup>
</Project>
11 changes: 11 additions & 0 deletions src/Testcontainers.Pulsar/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
global using System;
global using System.Net.Http;
global using System.Text;
global using System.Threading;
global using System.Threading.Tasks;
global using Docker.DotNet.Models;
global using DotNet.Testcontainers.Builders;
global using DotNet.Testcontainers.Configurations;
global using DotNet.Testcontainers.Containers;
global using JetBrains.Annotations;
global using Microsoft.Extensions.Logging;
1 change: 1 addition & 0 deletions tests/Testcontainers.Pulsar.Tests/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
root = true
37 changes: 37 additions & 0 deletions tests/Testcontainers.Pulsar.Tests/PulsarContainerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
namespace Testcontainers.Pulsar;

public sealed class PulsarContainerTest : IAsyncLifetime
{
private readonly PulsarContainer _pulsarContainer = new PulsarBuilder().Build();

public Task InitializeAsync()
{
return _pulsarContainer.StartAsync();
}

public Task DisposeAsync()
{
return _pulsarContainer.DisposeAsync().AsTask();
}

[Fact]
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))]
public async Task ConsumerReturnsProducerMessage()
{
const string topic = "sample";

await using var client = PulsarClient.Builder().ServiceUrl(_pulsarContainer.GetServiceUrl()).Build();
await using var producer = client.NewProducer(Schema.String).Topic(topic).Create();
await using var consumer = client.NewConsumer(Schema.String).Topic(topic).SubscriptionName("sample-subscription").Create();

var message = Guid.NewGuid().ToString("D");

_ = await producer.Send(message).ConfigureAwait(true);

using var cts = new CancellationTokenSource();
cts.CancelAfter(TimeSpan.FromSeconds(15));
var result = await consumer.Receive(cts.Token).ConfigureAwait(true);

Assert.Equal(message, result.Value());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net8.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<IsPublishable>false</IsPublishable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk"/>
<PackageReference Include="coverlet.collector"/>
<PackageReference Include="xunit.runner.visualstudio"/>
<PackageReference Include="xunit"/>
<PackageReference Include="DotPulsar"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../../src/Testcontainers.Pulsar/Testcontainers.Pulsar.csproj"/>
<ProjectReference Include="../Testcontainers.Commons/Testcontainers.Commons.csproj"/>
</ItemGroup>
</Project>
7 changes: 7 additions & 0 deletions tests/Testcontainers.Pulsar.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
global using System;
global using System.Threading;
global using System.Threading.Tasks;
global using DotPulsar;
global using DotPulsar.Extensions;
global using DotNet.Testcontainers.Commons;
global using Xunit;
Loading