diff --git a/Testcontainers.sln b/Testcontainers.sln index 49143c3ac..94af5e2c3 100644 --- a/Testcontainers.sln +++ b/Testcontainers.sln @@ -25,6 +25,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.Bigtable", " EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.ClickHouse", "src\Testcontainers.ClickHouse\Testcontainers.ClickHouse.csproj", "{B061A78E-536E-4CA1-8401-234D5FBFBAB7}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.CockroachDb", "src\Testcontainers.CockroachDb\Testcontainers.CockroachDb.csproj", "{8D9871C6-5A39-4F0B-A15A-E87D34F3EA73}" +EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.Consul", "src\Testcontainers.Consul\Testcontainers.Consul.csproj", "{51ED33B9-B688-401E-85F2-329D3C935BD1}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.CosmosDb", "src\Testcontainers.CosmosDb\Testcontainers.CosmosDb.csproj", "{A724806F-8C94-4438-8011-04A9A1575318}" @@ -107,6 +109,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.Bigtable.Tes EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.ClickHouse.Tests", "tests\Testcontainers.ClickHouse.Tests\Testcontainers.ClickHouse.Tests.csproj", "{9D0A0B32-4921-400C-99CB-8650677E3E44}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.CockroachDb.Tests", "tests\Testcontainers.CockroachDb.Tests\Testcontainers.CockroachDb.Tests.csproj", "{685E6D9A-B05E-41D9-A08E-5F3CA7733F7D}" +EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.Commons", "tests\Testcontainers.Commons\Testcontainers.Commons.csproj", "{2478673C-B063-469D-ABD1-0C3E0A25541B}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.Consul.Tests", "tests\Testcontainers.Consul.Tests\Testcontainers.Consul.Tests.csproj", "{91B23679-A2A5-4132-8AFA-740CE40A88B6}" @@ -220,6 +224,10 @@ Global {B061A78E-536E-4CA1-8401-234D5FBFBAB7}.Debug|Any CPU.Build.0 = Debug|Any CPU {B061A78E-536E-4CA1-8401-234D5FBFBAB7}.Release|Any CPU.ActiveCfg = Release|Any CPU {B061A78E-536E-4CA1-8401-234D5FBFBAB7}.Release|Any CPU.Build.0 = Release|Any CPU + {8D9871C6-5A39-4F0B-A15A-E87D34F3EA73}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8D9871C6-5A39-4F0B-A15A-E87D34F3EA73}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8D9871C6-5A39-4F0B-A15A-E87D34F3EA73}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8D9871C6-5A39-4F0B-A15A-E87D34F3EA73}.Release|Any CPU.Build.0 = Release|Any CPU {51ED33B9-B688-401E-85F2-329D3C935BD1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {51ED33B9-B688-401E-85F2-329D3C935BD1}.Debug|Any CPU.Build.0 = Debug|Any CPU {51ED33B9-B688-401E-85F2-329D3C935BD1}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -384,6 +392,10 @@ Global {9D0A0B32-4921-400C-99CB-8650677E3E44}.Debug|Any CPU.Build.0 = Debug|Any CPU {9D0A0B32-4921-400C-99CB-8650677E3E44}.Release|Any CPU.ActiveCfg = Release|Any CPU {9D0A0B32-4921-400C-99CB-8650677E3E44}.Release|Any CPU.Build.0 = Release|Any CPU + {685E6D9A-B05E-41D9-A08E-5F3CA7733F7D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {685E6D9A-B05E-41D9-A08E-5F3CA7733F7D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {685E6D9A-B05E-41D9-A08E-5F3CA7733F7D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {685E6D9A-B05E-41D9-A08E-5F3CA7733F7D}.Release|Any CPU.Build.0 = Release|Any CPU {2478673C-B063-469D-ABD1-0C3E0A25541B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {2478673C-B063-469D-ABD1-0C3E0A25541B}.Debug|Any CPU.Build.0 = Debug|Any CPU {2478673C-B063-469D-ABD1-0C3E0A25541B}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -552,6 +564,7 @@ Global {A9FF9C7F-BBA0-4B44-90B7-48A60F9E00F3} = {673F23AE-7694-4BB9-ABD4-136D6C13634E} {302EC1E0-AE75-4E99-A6BF-524F35338BC8} = {673F23AE-7694-4BB9-ABD4-136D6C13634E} {B061A78E-536E-4CA1-8401-234D5FBFBAB7} = {673F23AE-7694-4BB9-ABD4-136D6C13634E} + {8D9871C6-5A39-4F0B-A15A-E87D34F3EA73} = {673F23AE-7694-4BB9-ABD4-136D6C13634E} {51ED33B9-B688-401E-85F2-329D3C935BD1} = {673F23AE-7694-4BB9-ABD4-136D6C13634E} {A724806F-8C94-4438-8011-04A9A1575318} = {673F23AE-7694-4BB9-ABD4-136D6C13634E} {58E94721-2681-4D82-8D94-0B2F9DB0D575} = {673F23AE-7694-4BB9-ABD4-136D6C13634E} @@ -593,6 +606,7 @@ Global {03E60673-078A-4508-99AD-8537CE6F78F1} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF} {2E7B92E3-8526-4706-90F3-00F0F5C47C37} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF} {9D0A0B32-4921-400C-99CB-8650677E3E44} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF} + {685E6D9A-B05E-41D9-A08E-5F3CA7733F7D} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF} {2478673C-B063-469D-ABD1-0C3E0A25541B} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF} {91B23679-A2A5-4132-8AFA-740CE40A88B6} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF} {BD445A54-F411-4758-955E-397A1E98680C} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF} diff --git a/docs/modules/index.md b/docs/modules/index.md index c85f7fabc..e533faecf 100644 --- a/docs/modules/index.md +++ b/docs/modules/index.md @@ -29,6 +29,7 @@ await moduleNameContainer.StartAsync(); | 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) | | 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) | | Couchbase | `couchbase:community-7.0.2` | [NuGet](https://www.nuget.org/packages/Testcontainers.Couchbase) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.Couchbase) | | CouchDB | `couchdb:3.3` | [NuGet](https://www.nuget.org/packages/Testcontainers.CouchDb) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.CouchDb) | diff --git a/src/Testcontainers.CockroachDb/.editorconfig b/src/Testcontainers.CockroachDb/.editorconfig new file mode 100644 index 000000000..6f066619d --- /dev/null +++ b/src/Testcontainers.CockroachDb/.editorconfig @@ -0,0 +1 @@ +root = true \ No newline at end of file diff --git a/src/Testcontainers.CockroachDb/CockroachDbBuilder.cs b/src/Testcontainers.CockroachDb/CockroachDbBuilder.cs new file mode 100644 index 000000000..0b3283ff7 --- /dev/null +++ b/src/Testcontainers.CockroachDb/CockroachDbBuilder.cs @@ -0,0 +1,141 @@ +namespace Testcontainers.CockroachDb; + +/// +[PublicAPI] +public sealed class CockroachDbBuilder : ContainerBuilder +{ + public const string CockroachDbImage = "cockroachdb/cockroach:latest-v23.1"; + + public const ushort CockroachDbPort = 26257; + + public const ushort CockroachDbRestPort = 8080; + + public const string DefaultDatabase = "defaultdb"; + + public const string DefaultUsername = "root"; + + public const string DefaultPassword = ""; + + /// + /// Initializes a new instance of the class. + /// + public CockroachDbBuilder() + : this(new CockroachDbConfiguration()) + { + DockerResourceConfiguration = Init().DockerResourceConfiguration; + } + + /// + /// Initializes a new instance of the class. + /// + /// The Docker resource configuration. + private CockroachDbBuilder(CockroachDbConfiguration resourceConfiguration) + : base(resourceConfiguration) + { + DockerResourceConfiguration = resourceConfiguration; + } + + /// + protected override CockroachDbConfiguration DockerResourceConfiguration { get; } + + /// + /// Sets the CockroachDb database. + /// + /// The CockroachDb database. + /// A configured instance of . + public CockroachDbBuilder WithDatabase(string database) + { + return Merge(DockerResourceConfiguration, new CockroachDbConfiguration(database: database)) + .WithEnvironment("COCKROACH_DATABASE", database); + } + + /// + /// Sets the CockroachDb username. + /// + /// The CockroachDb username. + /// A configured instance of . + public CockroachDbBuilder WithUsername(string username) + { + return Merge(DockerResourceConfiguration, new CockroachDbConfiguration(username: username)) + .WithEnvironment("COCKROACH_USER", username); + } + + /// + /// Sets the CockroachDb password. + /// + /// The CockroachDb password. + /// A configured instance of . + public CockroachDbBuilder WithPassword(string password) + { + return Merge(DockerResourceConfiguration, new CockroachDbConfiguration(password: password)) + .WithEnvironment("COCKROACH_PASSWORD", password); + } + + /// + public override CockroachDbContainer Build() + { + Validate(); + return new CockroachDbContainer(DockerResourceConfiguration, TestcontainersSettings.Logger); + } + + /// + protected override CockroachDbBuilder Init() + { + return base.Init() + .WithImage(CockroachDbImage) + .WithPortBinding(CockroachDbPort, true) + .WithPortBinding(CockroachDbRestPort, true) + .WithDatabase(DefaultDatabase) + .WithUsername(DefaultUsername) + .WithPassword(DefaultPassword) + .WithCommand("start-single-node") + .WithCommand("--insecure") + .WithWaitStrategy(Wait.ForUnixContainer().UntilHttpRequestIsSucceeded(request => + request.ForPort(CockroachDbRestPort).ForPath("/health"))); + } + + /// + protected override void Validate() + { + base.Validate(); + + _ = Guard.Argument(DockerResourceConfiguration.Password, nameof(DockerResourceConfiguration.Password)) + .NotNull(); + } + + /// + protected override CockroachDbBuilder Clone(IResourceConfiguration resourceConfiguration) + { + return Merge(DockerResourceConfiguration, new CockroachDbConfiguration(resourceConfiguration)); + } + + /// + protected override CockroachDbBuilder Clone(IContainerConfiguration resourceConfiguration) + { + return Merge(DockerResourceConfiguration, new CockroachDbConfiguration(resourceConfiguration)); + } + + /// + protected override CockroachDbBuilder Merge(CockroachDbConfiguration oldValue, CockroachDbConfiguration newValue) + { + return new CockroachDbBuilder(new CockroachDbConfiguration(oldValue, newValue)); + } + + /// + private sealed class WaitUntil : IWaitUntil + { + private static readonly string[] LineEndings = ["\r\n", "\n"]; + + /// + public async Task UntilAsync(IContainer container) + { + var (stdout, stderr) = await container.GetLogsAsync(timestampsEnabled: false) + .ConfigureAwait(false); + + return 2.Equals(Array.Empty() + .Concat(stdout.Split(LineEndings, StringSplitOptions.RemoveEmptyEntries)) + .Concat(stderr.Split(LineEndings, StringSplitOptions.RemoveEmptyEntries)) + .Count(line => line.Contains("database system is ready to accept connections"))); + } + } +} \ No newline at end of file diff --git a/src/Testcontainers.CockroachDb/CockroachDbConfiguration.cs b/src/Testcontainers.CockroachDb/CockroachDbConfiguration.cs new file mode 100644 index 000000000..4ef034b9d --- /dev/null +++ b/src/Testcontainers.CockroachDb/CockroachDbConfiguration.cs @@ -0,0 +1,80 @@ +namespace Testcontainers.CockroachDb; + +/// +[PublicAPI] +public sealed class CockroachDbConfiguration : ContainerConfiguration +{ + /// + /// Initializes a new instance of the class. + /// + /// The CockroachDb database. + /// The CockroachDb username. + /// The CockroachDb password. + public CockroachDbConfiguration( + string database = null, + string username = null, + string password = null) + { + Database = database; + Username = username; + Password = password; + } + + /// + /// Initializes a new instance of the class. + /// + /// The Docker resource configuration. + public CockroachDbConfiguration(IResourceConfiguration resourceConfiguration) + : base(resourceConfiguration) + { + // Passes the configuration upwards to the base implementations to create an updated immutable copy. + } + + /// + /// Initializes a new instance of the class. + /// + /// The Docker resource configuration. + public CockroachDbConfiguration(IContainerConfiguration resourceConfiguration) + : base(resourceConfiguration) + { + // Passes the configuration upwards to the base implementations to create an updated immutable copy. + } + + /// + /// Initializes a new instance of the class. + /// + /// The Docker resource configuration. + public CockroachDbConfiguration(CockroachDbConfiguration resourceConfiguration) + : this(new CockroachDbConfiguration(), resourceConfiguration) + { + // Passes the configuration upwards to the base implementations to create an updated immutable copy. + } + + /// + /// Initializes a new instance of the class. + /// + /// The old Docker resource configuration. + /// The new Docker resource configuration. + public CockroachDbConfiguration(CockroachDbConfiguration oldValue, CockroachDbConfiguration newValue) + : base(oldValue, newValue) + { + Database = BuildConfiguration.Combine(oldValue.Database, newValue.Database); + Username = BuildConfiguration.Combine(oldValue.Username, newValue.Username); + Password = BuildConfiguration.Combine(oldValue.Password, newValue.Password); + } + + /// + /// Gets the CockroachDb database. + /// + public string Database { get; } + + /// + /// Gets the CockroachDb username. + /// + public string Username { get; } + + /// + /// Gets the CockroachDb password. + /// + public string Password { get; } +} \ No newline at end of file diff --git a/src/Testcontainers.CockroachDb/CockroachDbContainer.cs b/src/Testcontainers.CockroachDb/CockroachDbContainer.cs new file mode 100644 index 000000000..84deffa1e --- /dev/null +++ b/src/Testcontainers.CockroachDb/CockroachDbContainer.cs @@ -0,0 +1,51 @@ +namespace Testcontainers.CockroachDb; + +/// +[PublicAPI] +public sealed class CockroachDbContainer : DockerContainer, IDatabaseContainer +{ + private readonly CockroachDbConfiguration _configuration; + + /// + /// Initializes a new instance of the class. + /// + /// The container configuration. + /// The logger. + public CockroachDbContainer(CockroachDbConfiguration configuration, ILogger logger) + : base(configuration, logger) + { + _configuration = configuration; + } + + /// + /// Gets the CockroachDb connection string. + /// + /// The CockroachDb connection string. + public string GetConnectionString() + { + var properties = new Dictionary(); + properties.Add("Host", Hostname); + properties.Add("Port", GetMappedPublicPort(CockroachDbBuilder.CockroachDbPort).ToString()); + properties.Add("Database", _configuration.Database); + properties.Add("Username", _configuration.Username); + properties.Add("Password", _configuration.Password); + return string.Join(";", properties.Select(property => string.Join("=", property.Key, property.Value))); + } + + /// + /// Executes the SQL script in the CockroachDb container. + /// + /// The content of the SQL script to execute. + /// Cancellation token. + /// Task that completes when the SQL script has been executed. + public async Task 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[] { "cockroach", "sql", "--insecure", "--file", scriptFilePath }, ct) + .ConfigureAwait(false); + } +} \ No newline at end of file diff --git a/src/Testcontainers.CockroachDb/Testcontainers.CockroachDb.csproj b/src/Testcontainers.CockroachDb/Testcontainers.CockroachDb.csproj new file mode 100644 index 000000000..87905ccf0 --- /dev/null +++ b/src/Testcontainers.CockroachDb/Testcontainers.CockroachDb.csproj @@ -0,0 +1,12 @@ + + + netstandard2.0;netstandard2.1 + latest + + + + + + + + \ No newline at end of file diff --git a/src/Testcontainers.CockroachDb/Usings.cs b/src/Testcontainers.CockroachDb/Usings.cs new file mode 100644 index 000000000..a01abde62 --- /dev/null +++ b/src/Testcontainers.CockroachDb/Usings.cs @@ -0,0 +1,14 @@ +global using System; +global using System.Collections.Generic; +global using System.IO; +global using System.Linq; +global using System.Text; +global using System.Threading; +global using System.Threading.Tasks; +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; \ No newline at end of file diff --git a/src/Testcontainers.PostgreSql/PostgreSqlBuilder.cs b/src/Testcontainers.PostgreSql/PostgreSqlBuilder.cs index fc7e5be11..53a3844b6 100644 --- a/src/Testcontainers.PostgreSql/PostgreSqlBuilder.cs +++ b/src/Testcontainers.PostgreSql/PostgreSqlBuilder.cs @@ -123,7 +123,7 @@ protected override PostgreSqlBuilder Merge(PostgreSqlConfiguration oldValue, Pos /// private sealed class WaitUntil : IWaitUntil { - private static readonly string[] LineEndings = { "\r\n", "\n" }; + private static readonly string[] LineEndings = ["\r\n", "\n"]; /// public async Task UntilAsync(IContainer container) diff --git a/tests/Testcontainers.CockroachDb.Tests/.editorconfig b/tests/Testcontainers.CockroachDb.Tests/.editorconfig new file mode 100644 index 000000000..6f066619d --- /dev/null +++ b/tests/Testcontainers.CockroachDb.Tests/.editorconfig @@ -0,0 +1 @@ +root = true \ No newline at end of file diff --git a/tests/Testcontainers.CockroachDb.Tests/CockroachDbContainerTest.cs b/tests/Testcontainers.CockroachDb.Tests/CockroachDbContainerTest.cs new file mode 100644 index 000000000..14ce462a4 --- /dev/null +++ b/tests/Testcontainers.CockroachDb.Tests/CockroachDbContainerTest.cs @@ -0,0 +1,45 @@ +namespace Testcontainers.CockroachDb; + +public sealed class CockroachDbContainerTest : IAsyncLifetime +{ + private readonly CockroachDbContainer _cockroachDbContainer = new CockroachDbBuilder().Build(); + + public Task InitializeAsync() + { + return _cockroachDbContainer.StartAsync(); + } + + public Task DisposeAsync() + { + return _cockroachDbContainer.DisposeAsync().AsTask(); + } + + [Fact] + [Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))] + public void ConnectionStateReturnsOpen() + { + // Given + using DbConnection connection = new NpgsqlConnection(_cockroachDbContainer.GetConnectionString()); + + // When + connection.Open(); + + // Then + Assert.Equal(ConnectionState.Open, connection.State); + } + + [Fact] + [Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))] + public async Task ExecScriptReturnsSuccessful() + { + // Given + const string scriptContent = "SELECT 1;"; + + // When + var execResult = await _cockroachDbContainer.ExecScriptAsync(scriptContent) + .ConfigureAwait(true); + + // When + Assert.True(0L.Equals(execResult.ExitCode), execResult.Stderr); + } +} \ No newline at end of file diff --git a/tests/Testcontainers.CockroachDb.Tests/Testcontainers.CockroachDb.Tests.csproj b/tests/Testcontainers.CockroachDb.Tests/Testcontainers.CockroachDb.Tests.csproj new file mode 100644 index 000000000..ed5cac687 --- /dev/null +++ b/tests/Testcontainers.CockroachDb.Tests/Testcontainers.CockroachDb.Tests.csproj @@ -0,0 +1,18 @@ + + + net8.0 + false + false + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/Testcontainers.CockroachDb.Tests/Usings.cs b/tests/Testcontainers.CockroachDb.Tests/Usings.cs new file mode 100644 index 000000000..576c631bf --- /dev/null +++ b/tests/Testcontainers.CockroachDb.Tests/Usings.cs @@ -0,0 +1,6 @@ +global using System.Data; +global using System.Data.Common; +global using System.Threading.Tasks; +global using DotNet.Testcontainers.Commons; +global using Npgsql; +global using Xunit; \ No newline at end of file