-
-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1550fe6
commit f5d471c
Showing
7 changed files
with
225 additions
and
3 deletions.
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
66 changes: 66 additions & 0 deletions
66
src/Testcontainers/Configurations/Modules/Databases/MariaDbTestcontainerConfiguration.cs
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,66 @@ | ||
namespace DotNet.Testcontainers.Configurations | ||
{ | ||
using DotNet.Testcontainers.Builders; | ||
using JetBrains.Annotations; | ||
|
||
/// <inheritdoc cref="TestcontainerDatabaseConfiguration" /> | ||
[PublicAPI] | ||
public class MariaDbTestcontainerConfiguration : TestcontainerDatabaseConfiguration | ||
{ | ||
private const string MariaDbImage = "mariadb:10.8"; | ||
|
||
private const int MariaDbPort = 3306; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="MariaDbTestcontainerConfiguration" /> class. | ||
/// </summary> | ||
public MariaDbTestcontainerConfiguration() | ||
: this(MariaDbImage) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="MariaDbTestcontainerConfiguration" /> class. | ||
/// </summary> | ||
/// <param name="image">The Docker image.</param> | ||
public MariaDbTestcontainerConfiguration(string image) | ||
: base(image, MariaDbPort) | ||
{ | ||
this.Environments["MYSQL_ALLOW_EMPTY_PASSWORD"] = "yes"; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override string Database | ||
{ | ||
get => this.Environments["MYSQL_DATABASE"]; | ||
set => this.Environments["MYSQL_DATABASE"] = value; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override string Username | ||
{ | ||
get => this.Environments["MYSQL_USER"]; | ||
set => this.Environments["MYSQL_USER"] = value; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override string Password | ||
{ | ||
get => this.Environments["MYSQL_PASSWORD"]; | ||
set => this.Environments["MYSQL_PASSWORD"] = value; | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets this specifies the password that will be set for the MariaDB root superuser account. | ||
/// </summary> | ||
public string RootPassword | ||
{ | ||
get => this.Environments["MARIADB_ROOT_PASSWORD"]; | ||
set => this.Environments["MARIADB_ROOT_PASSWORD"] = value; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override IWaitForContainerOS WaitStrategy => Wait.ForUnixContainer() | ||
.UntilCommandIsCompleted("mysql", "--host=localhost", $"--port={this.DefaultPort}", $"--user={this.Username}", $"--password={this.Password}", "--protocol=TCP", "--execute=SHOW DATABASES"); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/Testcontainers/Containers/Modules/Databases/MariaDbTestcontainer.cs
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,45 @@ | ||
namespace DotNet.Testcontainers.Containers | ||
{ | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using DotNet.Testcontainers.Configurations; | ||
using JetBrains.Annotations; | ||
using Microsoft.Extensions.Logging; | ||
|
||
/// <inheritdoc cref="TestcontainerDatabase" /> | ||
[PublicAPI] | ||
public sealed class MariaDbTestcontainer : TestcontainerDatabase | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="MariaDbTestcontainer" /> class. | ||
/// </summary> | ||
/// <param name="configuration">The Testcontainers configuration.</param> | ||
/// <param name="logger">The logger.</param> | ||
internal MariaDbTestcontainer(ITestcontainersConfiguration configuration, ILogger logger) | ||
: base(configuration, logger) | ||
{ | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override string ConnectionString | ||
=> $"Server={this.Hostname};Port={this.Port};Database={this.Database};Uid={this.Username};Pwd={this.Password};"; | ||
|
||
/// <summary> | ||
/// Executes a SQL script in the database container. | ||
/// </summary> | ||
/// <param name="scriptContent">The content of the SQL script to be executed.</param> | ||
/// <param name="ct">Cancellation token.</param> | ||
/// <returns>Task that completes when the script has been executed.</returns> | ||
public override async Task<ExecResult> ExecScriptAsync(string scriptContent, CancellationToken ct = default) | ||
{ | ||
var tempScriptFile = this.GetTempScriptFile(); | ||
|
||
await this.CopyFileAsync(tempScriptFile, Encoding.Default.GetBytes(scriptContent), 493, 0, 0, ct) | ||
.ConfigureAwait(false); | ||
|
||
return await this.ExecAsync(new[] { "mysql", $"--host={this.Hostname}", $"--port={this.ContainerPort}", $"--user={this.Username}", $"--password={this.Password}", this.Database, $"--execute=source {tempScriptFile}" }, ct) | ||
.ConfigureAwait(false); | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
tests/Testcontainers.Tests/Fixtures/Containers/Unix/Modules/Databases/MariaDbFixture.cs
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,42 @@ | ||
namespace DotNet.Testcontainers.Tests.Fixtures | ||
{ | ||
using System.Data.Common; | ||
using System.Threading.Tasks; | ||
using DotNet.Testcontainers.Builders; | ||
using DotNet.Testcontainers.Configurations; | ||
using DotNet.Testcontainers.Containers; | ||
using MySqlConnector; | ||
|
||
public sealed class MariaDbFixture : DatabaseFixture<MariaDbTestcontainer, DbConnection> | ||
{ | ||
private readonly TestcontainerDatabaseConfiguration configuration = new MariaDbTestcontainerConfiguration { Database = "db", Username = "mysql", Password = "mysql" }; | ||
|
||
public MariaDbFixture() | ||
{ | ||
this.Container = new TestcontainersBuilder<MariaDbTestcontainer>() | ||
.WithDatabase(this.configuration) | ||
.Build(); | ||
} | ||
|
||
public override async Task InitializeAsync() | ||
{ | ||
await this.Container.StartAsync() | ||
.ConfigureAwait(false); | ||
|
||
this.Connection = new MySqlConnection(this.Container.ConnectionString); | ||
} | ||
|
||
public override async Task DisposeAsync() | ||
{ | ||
this.Connection.Dispose(); | ||
|
||
await this.Container.DisposeAsync() | ||
.ConfigureAwait(false); | ||
} | ||
|
||
public override void Dispose() | ||
{ | ||
this.configuration.Dispose(); | ||
} | ||
} | ||
} |
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
69 changes: 69 additions & 0 deletions
69
...s/Testcontainers.Tests/Unit/Containers/Unix/Modules/Databases/MariaDbTestcontainerTest.cs
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,69 @@ | ||
namespace DotNet.Testcontainers.Tests.Unit | ||
{ | ||
using System.Data; | ||
using System.Threading.Tasks; | ||
using DotNet.Testcontainers.Tests.Fixtures; | ||
using Xunit; | ||
|
||
[Collection(nameof(Testcontainers))] | ||
public sealed class MariaDbTestcontainerTest : IClassFixture<MariaDbFixture> | ||
{ | ||
private readonly MariaDbFixture mariaDbFixture; | ||
|
||
public MariaDbTestcontainerTest(MariaDbFixture mariaDbFixture) | ||
{ | ||
this.mariaDbFixture = mariaDbFixture; | ||
} | ||
|
||
[Fact] | ||
public async Task ConnectionEstablished() | ||
{ | ||
// Given | ||
var connection = this.mariaDbFixture.Connection; | ||
|
||
// When | ||
await connection.OpenAsync() | ||
.ConfigureAwait(false); | ||
|
||
// Then | ||
Assert.Equal(ConnectionState.Open, connection.State); | ||
} | ||
|
||
[Fact] | ||
public async Task ExecScriptInRunningContainer() | ||
{ | ||
// Given | ||
const string script = @" | ||
CREATE TABLE MyTable ( | ||
id INT(6) UNSIGNED PRIMARY KEY, | ||
name VARCHAR(30) NOT NULL | ||
); | ||
INSERT INTO MyTable (id, name) VALUES (1, 'MyName'); | ||
SELECT * FROM MyTable; | ||
"; | ||
|
||
// When | ||
var result = await this.mariaDbFixture.Container.ExecScriptAsync(script) | ||
.ConfigureAwait(false); | ||
|
||
// Then | ||
Assert.Equal(0, result.ExitCode); | ||
Assert.Contains("MyName", result.Stdout); | ||
} | ||
|
||
[Fact] | ||
public async Task ThrowErrorInRunningContainerWithInvalidScript() | ||
{ | ||
// Given | ||
const string script = "invalid SQL command"; | ||
|
||
// When | ||
var result = await this.mariaDbFixture.Container.ExecScriptAsync(script) | ||
.ConfigureAwait(false); | ||
|
||
// Then | ||
Assert.Equal(0, result.ExitCode); // exit code is 0 because MariaDB docker image does not have a proper error handler | ||
Assert.Contains("ERROR 1064 (42000)", result.Stderr); | ||
} | ||
} | ||
} |