Skip to content

Commit

Permalink
feat: Add InfluxDb module (#975)
Browse files Browse the repository at this point in the history
Co-authored-by: Andre Hofmeister <9199345+HofmeisterAn@users.noreply.github.com>
  • Loading branch information
MelomanG and HofmeisterAn authored Aug 17, 2023
1 parent 49c1fa4 commit 7808ac4
Show file tree
Hide file tree
Showing 15 changed files with 438 additions and 3 deletions.
14 changes: 14 additions & 0 deletions Testcontainers.sln
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.Elasticsearc
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.EventStoreDb", "src\Testcontainers.EventStoreDb\Testcontainers.EventStoreDb.csproj", "{84D707E0-C9FA-4327-85DC-0AFEBEA73572}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.InfluxDb", "src\Testcontainers.InfluxDb\Testcontainers.InfluxDb.csproj", "{8F483B83-7BD4-4BD5-9F03-DFC26E1CE678}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.K3s", "src\Testcontainers.K3s\Testcontainers.K3s.csproj", "{111B840F-9DB0-4166-83E6-0580FD418F07}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.Kafka", "src\Testcontainers.Kafka\Testcontainers.Kafka.csproj", "{E93E40CE-59AA-4561-9B4C-E7B0A686326E}"
Expand Down Expand Up @@ -87,6 +89,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.Elasticsearc
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.EventStoreDb.Tests", "tests\Testcontainers.EventStoreDb.Tests\Testcontainers.EventStoreDb.Tests.csproj", "{64F8E9B9-78FD-4E13-BDDF-0340E2D4E1D0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.InfluxDb.Tests", "tests\Testcontainers.InfluxDb.Tests\Testcontainers.InfluxDb.Tests.csproj", "{B45B0EF2-5852-4ED3-904A-8FC62A3253D7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.K3s.Tests", "tests\Testcontainers.K3s.Tests\Testcontainers.K3s.Tests.csproj", "{F0F40AE2-70FF-4191-ADDA-26A19E0D1A0F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.Kafka.Tests", "tests\Testcontainers.Kafka.Tests\Testcontainers.Kafka.Tests.csproj", "{6F2AEE03-629A-4B49-BD5B-25CA3C61FFB7}"
Expand Down Expand Up @@ -174,6 +178,10 @@ Global
{84D707E0-C9FA-4327-85DC-0AFEBEA73572}.Debug|Any CPU.Build.0 = Debug|Any CPU
{84D707E0-C9FA-4327-85DC-0AFEBEA73572}.Release|Any CPU.ActiveCfg = Release|Any CPU
{84D707E0-C9FA-4327-85DC-0AFEBEA73572}.Release|Any CPU.Build.0 = Release|Any CPU
{8F483B83-7BD4-4BD5-9F03-DFC26E1CE678}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8F483B83-7BD4-4BD5-9F03-DFC26E1CE678}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8F483B83-7BD4-4BD5-9F03-DFC26E1CE678}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8F483B83-7BD4-4BD5-9F03-DFC26E1CE678}.Release|Any CPU.Build.0 = Release|Any CPU
{111B840F-9DB0-4166-83E6-0580FD418F07}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{111B840F-9DB0-4166-83E6-0580FD418F07}.Debug|Any CPU.Build.0 = Debug|Any CPU
{111B840F-9DB0-4166-83E6-0580FD418F07}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -290,6 +298,10 @@ Global
{64F8E9B9-78FD-4E13-BDDF-0340E2D4E1D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{64F8E9B9-78FD-4E13-BDDF-0340E2D4E1D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{64F8E9B9-78FD-4E13-BDDF-0340E2D4E1D0}.Release|Any CPU.Build.0 = Release|Any CPU
{B45B0EF2-5852-4ED3-904A-8FC62A3253D7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B45B0EF2-5852-4ED3-904A-8FC62A3253D7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B45B0EF2-5852-4ED3-904A-8FC62A3253D7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B45B0EF2-5852-4ED3-904A-8FC62A3253D7}.Release|Any CPU.Build.0 = Release|Any CPU
{F0F40AE2-70FF-4191-ADDA-26A19E0D1A0F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F0F40AE2-70FF-4191-ADDA-26A19E0D1A0F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F0F40AE2-70FF-4191-ADDA-26A19E0D1A0F}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -392,6 +404,7 @@ Global
{2EAFA567-9F68-4C52-9DBC-8F3EC11BB2CE} = {673F23AE-7694-4BB9-ABD4-136D6C13634E}
{641DDEA5-B6E0-41E6-BA11-7A28C0913127} = {673F23AE-7694-4BB9-ABD4-136D6C13634E}
{84D707E0-C9FA-4327-85DC-0AFEBEA73572} = {673F23AE-7694-4BB9-ABD4-136D6C13634E}
{8F483B83-7BD4-4BD5-9F03-DFC26E1CE678} = {673F23AE-7694-4BB9-ABD4-136D6C13634E}
{111B840F-9DB0-4166-83E6-0580FD418F07} = {673F23AE-7694-4BB9-ABD4-136D6C13634E}
{E93E40CE-59AA-4561-9B4C-E7B0A686326E} = {673F23AE-7694-4BB9-ABD4-136D6C13634E}
{AA8834A3-82A7-4E83-8E4C-88D37F74056A} = {673F23AE-7694-4BB9-ABD4-136D6C13634E}
Expand Down Expand Up @@ -421,6 +434,7 @@ Global
{101515E6-74C1-40F9-85C8-871F742A378D} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
{DD5B3678-468F-4D73-AECE-705E3D66CD43} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
{64F8E9B9-78FD-4E13-BDDF-0340E2D4E1D0} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
{B45B0EF2-5852-4ED3-904A-8FC62A3253D7} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
{F0F40AE2-70FF-4191-ADDA-26A19E0D1A0F} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
{6F2AEE03-629A-4B49-BD5B-25CA3C61FFB7} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
{4827D606-89D5-4E00-8341-47A6E95817BA} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
Expand Down
1 change: 1 addition & 0 deletions docs/modules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ await moduleNameContainer.StartAsync();
| DynamoDB | `amazon/dynamodb-local:1.21.0` | [NuGet](https://www.nuget.org/packages/Testcontainers.DynamoDb) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.DynamoDb) |
| Elasticsearch | `elasticsearch:8.6.1` | [NuGet](https://www.nuget.org/packages/Testcontainers.Elasticsearch) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.Elasticsearch) |
| EventStoreDB | `eventstore/eventstore:22.10.1-buster-slim` | [NuGet](https://www.nuget.org/packages/Testcontainers.EventStoreDb) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.EventStoreDb) |
| InfluxDB | `influxdb:2.7` | [NuGet](https://www.nuget.org/packages/Testcontainers.InfluxDb) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.InfluxDb) |
| K3s | `rancher/k3s:v1.26.2-k3s1` | [NuGet](https://www.nuget.org/packages/Testcontainers.K3s) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.K3s) |
| Kafka | `confluentinc/cp-kafka:6.1.9` | [NuGet](https://www.nuget.org/packages/Testcontainers.Kafka) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.Kafka) |
| Keycloak | `quay.io/keycloak/keycloak:21.1` | [NuGet](https://www.nuget.org/packages/Testcontainers.Keycloak) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.Keycloak) |
Expand Down
1 change: 1 addition & 0 deletions src/Testcontainers.InfluxDb/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
root = true
168 changes: 168 additions & 0 deletions src/Testcontainers.InfluxDb/InfluxDbBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
namespace Testcontainers.InfluxDb;

/// <inheritdoc cref="ContainerBuilder{TBuilderEntity, TContainerEntity, TConfigurationEntity}" />
[PublicAPI]
public sealed class InfluxDbBuilder : ContainerBuilder<InfluxDbBuilder, InfluxDbContainer, InfluxDbConfiguration>
{
public const string InfluxDbImage = "influxdb:2.7";

public const ushort InfluxDbPort = 8086;

public const string DefaultUsername = "username";

public const string DefaultPassword = "password";

public const string DefaultOrganization = "organization";

public const string DefaultBucket = "bucket";

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

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

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

/// <summary>
/// Sets the InfluxDb username.
/// </summary>
/// <param name="username">The InfluxDb username.</param>
/// <returns>A configured instance of <see cref="InfluxDbBuilder" />.</returns>
public InfluxDbBuilder WithUsername(string username)
{
return Merge(DockerResourceConfiguration, new InfluxDbConfiguration(username: username))
.WithEnvironment("DOCKER_INFLUXDB_INIT_USERNAME", username);
}

/// <summary>
/// Sets the InfluxDb password.
/// </summary>
/// <param name="password">The InfluxDb password.</param>
/// <returns>A configured instance of <see cref="InfluxDbBuilder" />.</returns>
public InfluxDbBuilder WithPassword(string password)
{
return Merge(DockerResourceConfiguration, new InfluxDbConfiguration(password: password))
.WithEnvironment("DOCKER_INFLUXDB_INIT_PASSWORD", password);
}

/// <summary>
/// Sets the InfluxDb organization.
/// </summary>
/// <param name="organization">The InfluxDb organization.</param>
/// <returns>A configured instance of <see cref="InfluxDbBuilder" />.</returns>
public InfluxDbBuilder WithOrganization(string organization)
{
return Merge(DockerResourceConfiguration, new InfluxDbConfiguration(organization: organization))
.WithEnvironment("DOCKER_INFLUXDB_INIT_ORG", organization);
}

/// <summary>
/// Sets the InfluxDb bucket.
/// </summary>
/// <param name="bucket">The InfluxDb bucket.</param>
/// <returns>A configured instance of <see cref="InfluxDbBuilder" />.</returns>
public InfluxDbBuilder WithBucket(string bucket)
{
return Merge(DockerResourceConfiguration, new InfluxDbConfiguration(bucket: bucket))
.WithEnvironment("DOCKER_INFLUXDB_INIT_BUCKET", bucket);
}

/// <summary>
/// Sets the InfluxDb admin token.
/// </summary>
/// <param name="adminToken">The InfluxDb admin token.</param>
/// <returns>A configured instance of <see cref="InfluxDbBuilder" />.</returns>
public InfluxDbBuilder WithAdminToken(string adminToken)
{
return Merge(DockerResourceConfiguration, new InfluxDbConfiguration(adminToken: adminToken))
.WithEnvironment("DOCKER_INFLUXDB_INIT_ADMIN_TOKEN", adminToken);
}

/// <summary>
/// Sets the InfluxDb retention.
/// </summary>
/// <param name="retention">The InfluxDb retention.</param>
/// <returns>A configured instance of <see cref="InfluxDbBuilder" />.</returns>
public InfluxDbBuilder WithRetention(string retention)
{
return Merge(DockerResourceConfiguration, new InfluxDbConfiguration(retention: retention))
.WithEnvironment("DOCKER_INFLUXDB_INIT_RETENTION", retention);
}

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

/// <inheritdoc />
protected override InfluxDbBuilder Init()
{
return base.Init()
.WithImage(InfluxDbImage)
.WithPortBinding(InfluxDbPort, true)
.WithEnvironment("DOCKER_INFLUXDB_INIT_MODE", "setup")
.WithUsername(DefaultUsername)
.WithPassword(DefaultPassword)
.WithOrganization(DefaultOrganization)
.WithBucket(DefaultBucket)
.WithWaitStrategy(Wait.ForUnixContainer().UntilHttpRequestIsSucceeded(request =>
request.ForPort(InfluxDbPort).ForPath("/ping").ForStatusCode(HttpStatusCode.NoContent)));
}

/// <inheritdoc />
protected override void Validate()
{
base.Validate();

_ = Guard.Argument(DockerResourceConfiguration.Username, nameof(DockerResourceConfiguration.Username))
.NotNull()
.NotEmpty();

_ = Guard.Argument(DockerResourceConfiguration.Password, nameof(DockerResourceConfiguration.Password))
.NotNull()
.NotEmpty();

_ = Guard.Argument(DockerResourceConfiguration.Organization, nameof(DockerResourceConfiguration.Organization))
.NotNull()
.NotEmpty();

_ = Guard.Argument(DockerResourceConfiguration.Bucket, nameof(DockerResourceConfiguration.Bucket))
.NotNull()
.NotEmpty();
}

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

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

/// <inheritdoc />
protected override InfluxDbBuilder Merge(InfluxDbConfiguration oldValue, InfluxDbConfiguration newValue)
{
return new InfluxDbBuilder(new InfluxDbConfiguration(oldValue, newValue));
}
}
107 changes: 107 additions & 0 deletions src/Testcontainers.InfluxDb/InfluxDbConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
namespace Testcontainers.InfluxDb;

/// <inheritdoc cref="ContainerConfiguration" />
[PublicAPI]
public sealed class InfluxDbConfiguration : ContainerConfiguration
{
/// <summary>
/// Initializes a new instance of the <see cref="InfluxDbConfiguration" /> class.
/// </summary>
/// <param name="username">The InfluxDb username.</param>
/// <param name="password">The InfluxDb password.</param>
/// <param name="organization">The InfluxDb organization.</param>
/// <param name="bucket">The InfluxDb bucket.</param>
/// <param name="adminToken">The InfluxDb admin token.</param>
/// <param name="retention">The InfluxDb retention.</param>
public InfluxDbConfiguration(
string username = null,
string password = null,
string organization = null,
string bucket = null,
string adminToken = null,
string retention = null)
{
Username = username;
Password = password;
Organization = organization;
Bucket = bucket;
AdminToken = adminToken;
Retention = retention;
}

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

/// <summary>
/// Initializes a new instance of the <see cref="InfluxDbConfiguration" /> class.
/// </summary>
/// <param name="oldValue">The old Docker resource configuration.</param>
/// <param name="newValue">The new Docker resource configuration.</param>
public InfluxDbConfiguration(InfluxDbConfiguration oldValue, InfluxDbConfiguration newValue)
: base(oldValue, newValue)
{
Username = BuildConfiguration.Combine(oldValue.Username, newValue.Username);
Password = BuildConfiguration.Combine(oldValue.Password, newValue.Password);
Organization = BuildConfiguration.Combine(oldValue.Organization, newValue.Organization);
Bucket = BuildConfiguration.Combine(oldValue.Bucket, newValue.Bucket);
AdminToken = BuildConfiguration.Combine(oldValue.AdminToken, newValue.AdminToken);
Retention = BuildConfiguration.Combine(oldValue.Retention, newValue.Retention);
}

/// <summary>
/// Gets the InfluxDb username.
/// </summary>
public string Username { get; }

/// <summary>
/// Gets the InfluxDb password.
/// </summary>
public string Password { get; }

/// <summary>
/// Gets the InfluxDb organization.
/// </summary>
public string Organization { get; }

/// <summary>
/// Gets the InfluxDb bucket.
/// </summary>
public string Bucket { get; }

/// <summary>
/// Gets the InfluxDb admin token.
/// </summary>
public string AdminToken { get; }

/// <summary>
/// Gets the InfluxDb retention.
/// </summary>
public string Retention { get; }
}
25 changes: 25 additions & 0 deletions src/Testcontainers.InfluxDb/InfluxDbContainer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace Testcontainers.InfluxDb;

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

/// <summary>
/// Gets the InfluxDb address.
/// </summary>
/// <returns>The InfluxDb address.</returns>
public string GetAddress()
{
return new UriBuilder(Uri.UriSchemeHttp, Hostname, GetMappedPublicPort(InfluxDbBuilder.InfluxDbPort)).ToString();
}
}
13 changes: 13 additions & 0 deletions src/Testcontainers.InfluxDb/Testcontainers.InfluxDb.csproj
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>
9 changes: 9 additions & 0 deletions src/Testcontainers.InfluxDb/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
global using System;
global using System.Net;
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;
Loading

0 comments on commit 7808ac4

Please sign in to comment.