-
-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Andre Hofmeister <[email protected]>
- Loading branch information
1 parent
0c6588e
commit 560b64f
Showing
13 changed files
with
391 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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 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 |
This file contains 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,117 @@ | ||
namespace Testcontainers.Nats; | ||
|
||
/// <inheritdoc cref="ContainerBuilder{TBuilderEntity, TContainerEntity, TConfigurationEntity}" /> | ||
[PublicAPI] | ||
public sealed class NatsBuilder : ContainerBuilder<NatsBuilder, NatsContainer, NatsConfiguration> | ||
{ | ||
public const string NatsImage = "nats:2.9"; | ||
|
||
public const ushort NatsClientPort = 4222; | ||
|
||
public const ushort NatsClusterRoutingPort = 6222; | ||
|
||
public const ushort NatsHttpManagementPort = 8222; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="NatsBuilder" /> class. | ||
/// </summary> | ||
public NatsBuilder() | ||
: this(new NatsConfiguration()) | ||
{ | ||
DockerResourceConfiguration = Init().DockerResourceConfiguration; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="NatsBuilder" /> class. | ||
/// </summary> | ||
/// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
private NatsBuilder(NatsConfiguration resourceConfiguration) | ||
: base(resourceConfiguration) | ||
{ | ||
DockerResourceConfiguration = resourceConfiguration; | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override NatsConfiguration DockerResourceConfiguration { get; } | ||
|
||
/// <summary> | ||
/// Sets the Nats username. | ||
/// </summary> | ||
/// <param name="username">The Nats username.</param> | ||
/// <returns>A configured instance of <see cref="NatsBuilder" />.</returns> | ||
public NatsBuilder WithUsername(string username) | ||
{ | ||
return Merge(DockerResourceConfiguration, new NatsConfiguration(username: username)) | ||
.WithCommand("--user", username); | ||
} | ||
|
||
/// <summary> | ||
/// Sets the Nats password. | ||
/// </summary> | ||
/// <param name="password">The Nats password.</param> | ||
/// <returns>A configured instance of <see cref="NatsBuilder" />.</returns> | ||
public NatsBuilder WithPassword(string password) | ||
{ | ||
return Merge(DockerResourceConfiguration, new NatsConfiguration(password: password)) | ||
.WithCommand("--pass", password); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override NatsContainer Build() | ||
{ | ||
Validate(); | ||
return new NatsContainer(DockerResourceConfiguration, TestcontainersSettings.Logger); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override NatsBuilder Init() | ||
{ | ||
return base.Init() | ||
.WithImage(NatsImage) | ||
.WithPortBinding(NatsClientPort, true) | ||
.WithPortBinding(NatsHttpManagementPort, true) | ||
.WithPortBinding(NatsClusterRoutingPort, true) | ||
.WithUsername(string.Empty) | ||
.WithPassword(string.Empty) | ||
.WithCommand("--http_port", NatsHttpManagementPort.ToString()) | ||
.WithCommand("--jetstream") | ||
.WithCommand("--debug") | ||
.WithCommand("--trace") | ||
.WithWaitStrategy(Wait.ForUnixContainer().UntilMessageIsLogged("Server is ready")); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override void Validate() | ||
{ | ||
const string message = "Missing username or password. Both must be specified."; | ||
|
||
base.Validate(); | ||
|
||
_ = Guard.Argument(DockerResourceConfiguration.Username, nameof(DockerResourceConfiguration.Username)) | ||
.NotNull(); | ||
|
||
_ = Guard.Argument(DockerResourceConfiguration.Password, nameof(DockerResourceConfiguration.Password)) | ||
.NotNull(); | ||
|
||
_ = Guard.Argument(DockerResourceConfiguration, "Credentials") | ||
.ThrowIf(argument => 1.Equals(new[] { argument.Value.Username, argument.Value.Password }.Count(string.IsNullOrWhiteSpace)), argument => new ArgumentException(message, argument.Name)); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override NatsBuilder Clone(IResourceConfiguration<CreateContainerParameters> resourceConfiguration) | ||
{ | ||
return Merge(DockerResourceConfiguration, new NatsConfiguration(resourceConfiguration)); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override NatsBuilder Clone(IContainerConfiguration resourceConfiguration) | ||
{ | ||
return Merge(DockerResourceConfiguration, new NatsConfiguration(resourceConfiguration)); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override NatsBuilder Merge(NatsConfiguration oldValue, NatsConfiguration newValue) | ||
{ | ||
return new NatsBuilder(new NatsConfiguration(oldValue, newValue)); | ||
} | ||
} |
This file contains 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,71 @@ | ||
namespace Testcontainers.Nats; | ||
|
||
/// <inheritdoc cref="ContainerConfiguration" /> | ||
[PublicAPI] | ||
public sealed class NatsConfiguration : ContainerConfiguration | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="NatsConfiguration" /> class. | ||
/// </summary> | ||
/// <param name="username">The Nats username.</param> | ||
/// <param name="password">The Nats password.</param> | ||
public NatsConfiguration( | ||
string username = null, | ||
string password = null) | ||
{ | ||
Username = username; | ||
Password = password; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="NatsConfiguration" /> class. | ||
/// </summary> | ||
/// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
public NatsConfiguration(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="NatsConfiguration" /> class. | ||
/// </summary> | ||
/// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
public NatsConfiguration(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="NatsConfiguration" /> class. | ||
/// </summary> | ||
/// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
public NatsConfiguration(NatsConfiguration resourceConfiguration) | ||
: this(new NatsConfiguration(), resourceConfiguration) | ||
{ | ||
// Passes the configuration upwards to the base implementations to create an updated immutable copy. | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="NatsConfiguration" /> class. | ||
/// </summary> | ||
/// <param name="oldValue">The old Docker resource configuration.</param> | ||
/// <param name="newValue">The new Docker resource configuration.</param> | ||
public NatsConfiguration(NatsConfiguration oldValue, NatsConfiguration newValue) | ||
: base(oldValue, newValue) | ||
{ | ||
Username = BuildConfiguration.Combine(oldValue.Username, newValue.Username); | ||
Password = BuildConfiguration.Combine(oldValue.Password, newValue.Password); | ||
} | ||
|
||
/// <summary> | ||
/// The Nats username. | ||
/// </summary> | ||
public string Username { get; } | ||
|
||
/// <summary> | ||
/// The Nats password. | ||
/// </summary> | ||
public string Password { get; } | ||
} |
This file contains 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,43 @@ | ||
namespace Testcontainers.Nats; | ||
|
||
/// <inheritdoc cref="DockerContainer" /> | ||
[PublicAPI] | ||
public sealed class NatsContainer : DockerContainer | ||
{ | ||
private readonly NatsConfiguration _configuration; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="NatsContainer" /> class. | ||
/// </summary> | ||
/// <param name="configuration">The container configuration.</param> | ||
/// <param name="logger">The logger.</param> | ||
public NatsContainer(NatsConfiguration configuration, ILogger logger) | ||
: base(configuration, logger) | ||
{ | ||
_configuration = configuration; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the Nats connection string. | ||
/// </summary> | ||
/// <remarks> | ||
/// If both username and password are set in the builder configuration, they will be included in the connection string. | ||
/// </remarks> | ||
/// <returns>A Nats connection string in the format: <c>nats://hostname:port</c>.</returns> | ||
public string GetConnectionString() | ||
{ | ||
var endpoint = new UriBuilder("nats://", Hostname, GetMappedPublicPort(NatsBuilder.NatsClientPort)); | ||
endpoint.UserName = Uri.EscapeDataString(_configuration.Username); | ||
endpoint.Password = Uri.EscapeDataString(_configuration.Password); | ||
return endpoint.ToString(); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the Nats monitoring endpoint. | ||
/// </summary> | ||
/// <returns>An HTTP address in the format: <c>http://hostname:port</c>.</returns> | ||
public string GetManagementEndpoint() | ||
{ | ||
return new UriBuilder(Uri.UriSchemeHttp, Hostname, GetMappedPublicPort(NatsBuilder.NatsHttpManagementPort)).ToString(); | ||
} | ||
} |
This file contains 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,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks> | ||
<LangVersion>latest</LangVersion> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All"/> | ||
<PackageReference Include="JetBrains.Annotations" Version="2022.3.1" PrivateAssets="All"/> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="$(SolutionDir)src/Testcontainers/Testcontainers.csproj"/> | ||
</ItemGroup> | ||
</Project> |
This file contains 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 @@ | ||
global using System; | ||
global using System.Linq; | ||
global using Docker.DotNet.Models; | ||
global using DotNet.Testcontainers; | ||
global using DotNet.Testcontainers.Builders; | ||
global using DotNet.Testcontainers.Configurations; | ||
global using DotNet.Testcontainers.Containers; | ||
global using JetBrains.Annotations; | ||
global using Microsoft.Extensions.Logging; |
This file contains 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 |
Oops, something went wrong.