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

Adding cassandra #1

Merged
merged 13 commits into from
Feb 8, 2025
Merged
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 .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ jobs:
{ name: "Testcontainers.Azurite", runs-on: "ubuntu-22.04" },
{ name: "Testcontainers.BigQuery", runs-on: "ubuntu-22.04" },
{ name: "Testcontainers.Bigtable", runs-on: "ubuntu-22.04" },
{ name: "Testcontainers.Cassandra", runs-on: "ubuntu-22.04" },
{ name: "Testcontainers.ClickHouse", runs-on: "ubuntu-22.04" },
{ name: "Testcontainers.CockroachDb", runs-on: "ubuntu-22.04" },
{ name: "Testcontainers.Consul", runs-on: "ubuntu-22.04" },
Expand Down
3 changes: 2 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.2"/>
<PackageVersion Include="SharpZipLib" Version="1.4.2"/>
<PackageVersion Include="SSH.NET" Version="2024.1.0"/>
<PackageVersion Include="CassandraCSharpDriver" Version="3.21.0" />
<!-- Unit and integration test dependencies: -->
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1"/>
<PackageVersion Include="Microsoft.Extensions.Diagnostics.Testing" Version="8.10.0"/>
Expand Down Expand Up @@ -70,4 +71,4 @@
<PackageVersion Include="Selenium.WebDriver" Version="4.8.1"/>
<PackageVersion Include="StackExchange.Redis" Version="2.6.90"/>
</ItemGroup>
</Project>
</Project>
14 changes: 14 additions & 0 deletions Testcontainers.sln
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.WebDriver.Te
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.Xunit.Tests", "tests\Testcontainers.Xunit.Tests\Testcontainers.Xunit.Tests.csproj", "{E901DF14-6F05-4FC2-825A-3055FAD33561}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.Cassandra", "src\Testcontainers.Cassandra\Testcontainers.Cassandra.csproj", "{8495D757-5FD7-491C-B941-9D43B3DCF3C0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.Cassandra.Tests", "tests\Testcontainers.Cassandra.Tests\Testcontainers.Cassandra.Tests.csproj", "{C6A2B99E-BFD5-4510-83D7-A8844142F27D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -610,6 +614,14 @@ Global
{E901DF14-6F05-4FC2-825A-3055FAD33561}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E901DF14-6F05-4FC2-825A-3055FAD33561}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E901DF14-6F05-4FC2-825A-3055FAD33561}.Release|Any CPU.Build.0 = Release|Any CPU
{8495D757-5FD7-491C-B941-9D43B3DCF3C0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8495D757-5FD7-491C-B941-9D43B3DCF3C0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8495D757-5FD7-491C-B941-9D43B3DCF3C0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8495D757-5FD7-491C-B941-9D43B3DCF3C0}.Release|Any CPU.Build.0 = Release|Any CPU
{C6A2B99E-BFD5-4510-83D7-A8844142F27D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C6A2B99E-BFD5-4510-83D7-A8844142F27D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C6A2B99E-BFD5-4510-83D7-A8844142F27D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C6A2B99E-BFD5-4510-83D7-A8844142F27D}.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 @@ -710,5 +722,7 @@ Global
{DDB41BC8-5826-4D97-9C5F-001151E3FFD6} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
{EBA72C3B-57D5-43FF-A5B4-3D55B3B6D4C2} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
{E901DF14-6F05-4FC2-825A-3055FAD33561} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
{8495D757-5FD7-491C-B941-9D43B3DCF3C0} = {673F23AE-7694-4BB9-ABD4-136D6C13634E}
{C6A2B99E-BFD5-4510-83D7-A8844142F27D} = {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 @@ -28,6 +28,7 @@ await moduleNameContainer.StartAsync();
| Azurite | `mcr.microsoft.com/azure-storage/azurite:3.24.0` | [NuGet](https://www.nuget.org/packages/Testcontainers.Azurite) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.Azurite) |
| BigQuery | `ghcr.io/goccy/bigquery-emulator:0.4` | [NuGet](https://www.nuget.org/packages/Testcontainers.BigQuery) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.BigQuery) |
| Bigtable | `gcr.io/google.com/cloudsdktool/google-cloud-cli:446.0.1-emulators` | [NuGet](https://www.nuget.org/packages/Testcontainers.Bigtable) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.Bigtable) |
| Cassandra | `library/cassandra:5.0.3` | [NuGet](https://www.nuget.org/packages/Testcontainers.Cassandra) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.Cassandra) |
| ClickHouse | `clickhouse/clickhouse-server:23.6-alpine` | [NuGet](https://www.nuget.org/packages/Testcontainers.ClickHouse) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.ClickHouse) |
| CockroachDB | `cockroachdb:23.1.13` | [NuGet](https://www.nuget.org/packages/Testcontainers.CockroachDb) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.CockroachDb) |
| Consul | `consul:1.15` | [NuGet](https://www.nuget.org/packages/Testcontainers.Consul) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.Consul) |
Expand Down
1 change: 1 addition & 0 deletions src/Testcontainers.Cassandra/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
root = true
77 changes: 77 additions & 0 deletions src/Testcontainers.Cassandra/CassandraBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
namespace Testcontainers.Cassandra
{
/// <inheritdoc cref="ContainerBuilder{TBuilderEntity, TContainerEntity, TConfigurationEntity}" />
[PublicAPI]
public sealed class CassandraBuilder : ContainerBuilder<CassandraBuilder, CassandraContainer, CassandraConfiguration>
{
public const string CassandraImage = "library/cassandra:5.0.3";

public const ushort CqlPort = 9042;

public const string DefaultLocalDataCentre = "datacentre1";

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

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

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

/// <inheritdoc />
public override CassandraContainer Build()
{
return new CassandraContainer(DockerResourceConfiguration);
}

/// <inheritdoc />
protected override CassandraBuilder Init()
{
return base.Init()
.WithImage(CassandraImage)
.WithPortBinding(CqlPort, true)
.WithEnvironment("CASSANDRA_SNITCH", "GossipingPropertyFileSnitch")
.WithEnvironment("JVM_OPTS", "-Dcassandra.skip_wait_for_gossip_to_settle=0 -Dcassandra.initial_token=0")
.WithEnvironment("HEAP_NEWSIZE", "128M")
.WithEnvironment("MAX_HEAP_SIZE", "1024M")
.WithEnvironment("CASSANDRA_ENDPOINT_SNITCH", "GossipingPropertyFileSnitch")
.WithEnvironment("CASSANDRA_DC", DefaultLocalDataCentre)
.WithWaitStrategy(
Wait.ForUnixContainer()
.UntilPortIsAvailable(CqlPort)
.UntilMessageIsLogged("Startup complete"));
}

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

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

/// <inheritdoc />
protected override CassandraBuilder Merge(CassandraConfiguration oldValue, CassandraConfiguration newValue)
{
return new CassandraBuilder(new CassandraConfiguration(oldValue, newValue));
}
}
}
54 changes: 54 additions & 0 deletions src/Testcontainers.Cassandra/CassandraConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
namespace Testcontainers.Cassandra
{
/// <inheritdoc cref="ContainerConfiguration" />
[PublicAPI]
public sealed class CassandraConfiguration : ContainerConfiguration
{
/// <summary>
/// Initializes a new instance of the <see cref="CassandraConfiguration" /> class.
/// </summary>
public CassandraConfiguration()
{
}

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

/// <summary>
/// Initializes a new instance of the <see cref="CassandraConfiguration" /> class.
/// </summary>
/// <param name="oldValue">The old Docker resource configuration.</param>
/// <param name="newValue">The new Docker resource configuration.</param>
public CassandraConfiguration(CassandraConfiguration oldValue, CassandraConfiguration newValue)
: base(oldValue, newValue)
{
}
}
}
43 changes: 43 additions & 0 deletions src/Testcontainers.Cassandra/CassandraContainer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace Testcontainers.Cassandra
{
/// <inheritdoc cref="DockerContainer" />
[PublicAPI]
public class CassandraContainer : DockerContainer, IDatabaseContainer
{
private readonly CassandraConfiguration _configuration;

/// <inheritdoc cref="DockerContainer" />
public CassandraContainer(CassandraConfiguration configuration) : base(configuration)
{
_configuration = configuration;
}

public IPEndPoint GetEndPoint()
{
return new IPEndPoint(IPAddress.Loopback, GetMappedPublicPort(CassandraBuilder.CqlPort));
}

public string GetConnectionString()
{
throw new NotImplementedException();
}

/// <summary>
/// Executes the SQL script in the Cassandra container.
/// </summary>
/// <param name="scriptContent">The content of the CQL script to execute.</param>
/// <param name="ct">Cancellation token.</param>
/// <returns>Task that completes when the CQL script has been executed.</returns>
public async Task<ExecResult> ExecScriptAsync(string scriptContent, CancellationToken ct = default)
{
var scriptFilePath = string.Join("/", string.Empty, "tmp", Guid.NewGuid().ToString("D"), Path.GetRandomFileName());

await CopyAsync(Encoding.Default.GetBytes(scriptContent), scriptFilePath, Unix.FileMode644, ct)
.ConfigureAwait(false);

return await ExecAsync(new[] { "cqlsh", "-f", scriptFilePath }, ct)
.ConfigureAwait(false);
}

}
}
12 changes: 12 additions & 0 deletions src/Testcontainers.Cassandra/Testcontainers.Cassandra.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net8.0;net9.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.Cassandra/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
global using System;
global using System.IO;
global using System.Net;
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;
73 changes: 73 additions & 0 deletions tests/Testcontainers.Cassandra.Tests/CassandraContainerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
namespace Testcontainers.Cassandra.Tests
{
public sealed class CassandraContainerTest : IAsyncLifetime
{
private readonly CassandraContainer _cassandraContainer = new CassandraBuilder().Build();

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

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

[Fact]
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))]
public void ConnectionStateReturnsOpen()
{
// Given
var cluster = Cluster.Builder()
.AddContactPoint(_cassandraContainer.GetEndPoint())
.Build();

// When
var session = cluster.Connect();

// Then
Assert.True(session.GetState().GetConnectedHosts().First().IsUp);
}

[Fact]
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))]
public async Task DriverExecutesCqlStatementAndReturnResult()
{
// Given
const string selectFromSystemLocalStatement = "SELECT * FROM system.local WHERE key = ?;";
var cluster = Cluster.Builder()
.AddContactPoint(_cassandraContainer.GetEndPoint())
.Build();

// When
var session = await cluster.ConnectAsync();
var preparedStatement = await session.PrepareAsync(selectFromSystemLocalStatement);
var boundStatement = preparedStatement.Bind("local");
var result = await session.ExecuteAsync(boundStatement);

// Then
Assert.True(result.IsFullyFetched);
var resultRows = result.GetRows().ToList();
Assert.Single(resultRows);
Assert.Equal("COMPLETED", resultRows.First()["bootstrapped"]);
}

[Fact]
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))]
public async Task ExecScriptAsyncReturnsSuccess()
{
// Given
const string selectFromSystemLocalStatement = "SELECT * FROM system.local;";

// When
var execResult = await _cassandraContainer.ExecScriptAsync(selectFromSystemLocalStatement)
.ConfigureAwait(true);

// Then
Assert.True(0L.Equals(execResult.ExitCode), execResult.Stderr);
Assert.NotEmpty(execResult.Stdout);
Assert.Empty(execResult.Stderr);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net9.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="CassandraCSharpDriver" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Testcontainers.Cassandra\Testcontainers.Cassandra.csproj" />
<ProjectReference Include="..\Testcontainers.Commons\Testcontainers.Commons.csproj" />
</ItemGroup>
</Project>
5 changes: 5 additions & 0 deletions tests/Testcontainers.Cassandra.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
global using System.Linq;
global using System.Threading.Tasks;
global using Cassandra;
global using DotNet.Testcontainers.Commons;
global using Xunit;