Skip to content

Commit

Permalink
feat(#626): Support MySQL root password configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielHabenicht committed Oct 24, 2022
1 parent e6dbb63 commit d351697
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class MySqlTestcontainerConfiguration : TestcontainerDatabaseConfiguratio

private const int MySqlPort = 3306;

private const string RootUsername = "root";

/// <summary>
/// Initializes a new instance of the <see cref="MySqlTestcontainerConfiguration" /> class.
/// </summary>
Expand Down Expand Up @@ -39,15 +41,37 @@ public override string Database
/// <inheritdoc />
public override string Username
{
get => this.Environments["MYSQL_USER"];
set => this.Environments["MYSQL_USER"] = value;
get
{
string username;
if (this.Environments.TryGetValue("MYSQL_USER", out username))
{
return username;
}
else
{
return RootUsername;
}
}
set
{
// Only set the username if it is not "root", as mysql does not allow it.
if (value != RootUsername)
{
this.Environments["MYSQL_USER"] = value;
}
}
}

/// <inheritdoc />
public override string Password
{
get => this.Environments["MYSQL_PASSWORD"];
set => this.Environments["MYSQL_PASSWORD"] = value;
set
{
this.Environments["MYSQL_ROOT_PASSWORD"] = value;
this.Environments["MYSQL_PASSWORD"] = value;
}
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ namespace DotNet.Testcontainers.Tests.Fixtures
using JetBrains.Annotations;
using MySqlConnector;

[UsedImplicitly]
public sealed class MySqlFixture : DatabaseFixture<MySqlTestcontainer, DbConnection>
public abstract class MySqlBaseFixture : DatabaseFixture<MySqlTestcontainer, DbConnection>
{
private readonly TestcontainerDatabaseConfiguration configuration = new MySqlTestcontainerConfiguration { Database = "db", Username = "mysql", Password = "mysql" };
private readonly TestcontainerDatabaseConfiguration configuration;

public MySqlFixture()
public MySqlBaseFixture(string username, string password)
{
this.configuration = new MySqlTestcontainerConfiguration { Database = "db", Username = username, Password = password };
this.Container = new TestcontainersBuilder<MySqlTestcontainer>()
.WithDatabase(this.configuration)
.Build();
Expand All @@ -41,4 +41,20 @@ public override void Dispose()
this.configuration.Dispose();
}
}

[UsedImplicitly]
public sealed class MySqlNormalUserFixture : MySqlBaseFixture
{
public MySqlNormalUserFixture() : base("mysql", "mysql")
{
}
}

[UsedImplicitly]
public sealed class MySqlRootUserFixture : MySqlBaseFixture
{
public MySqlRootUserFixture() : base("root", "root")
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,37 @@ namespace DotNet.Testcontainers.Tests.Unit
using Xunit;

[Collection(nameof(Testcontainers))]
public sealed class MySqlTestcontainerTest : IClassFixture<MySqlFixture>
public static class MySqlTestcontainerTest
{
private readonly MySqlFixture mySqlFixture;

public MySqlTestcontainerTest(MySqlFixture mySqlFixture)
[Collection(nameof(Testcontainers))]
public sealed class MySqlTestcontainerRootUserTest : IClassFixture<MySqlRootUserFixture>
{
this.mySqlFixture = mySqlFixture;
}
private readonly MySqlRootUserFixture mySqlFixture;

[Fact]
public async Task ConnectionEstablished()
{
// Given
var connection = this.mySqlFixture.Connection;
public MySqlTestcontainerRootUserTest(MySqlRootUserFixture mySqlFixture)
{
this.mySqlFixture = mySqlFixture;
}

// When
await connection.OpenAsync()
.ConfigureAwait(false);
[Fact]
public async Task ConnectionEstablished()
{
// Given
var connection = this.mySqlFixture.Connection;

// Then
Assert.Equal(ConnectionState.Open, connection.State);
}
// When
await connection.OpenAsync()
.ConfigureAwait(false);

[Fact]
public async Task ExecScriptInRunningContainer()
{
// Given
const string script = @"
// 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
Expand All @@ -42,28 +45,93 @@ public async Task ExecScriptInRunningContainer()
SELECT * FROM MyTable;
";

// When
var result = await this.mySqlFixture.Container.ExecScriptAsync(script)
.ConfigureAwait(false);
// When
var result = await this.mySqlFixture.Container.ExecScriptAsync(script)
.ConfigureAwait(false);

// Then
Assert.Equal(0, result.ExitCode);
Assert.Contains("MyName", result.Stdout);
// Then
Assert.DoesNotContain("ERROR", result.Stderr);
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.mySqlFixture.Container.ExecScriptAsync(script)
.ConfigureAwait(false);

// Then
Assert.NotEqual(0, result.ExitCode);
Assert.Contains("ERROR 1064 (42000)", result.Stderr);
}
}

[Fact]
public async Task ThrowErrorInRunningContainerWithInvalidScript()
[Collection(nameof(Testcontainers))]
public sealed class MySqlTestcontainerNormalUserTest : IClassFixture<MySqlNormalUserFixture>
{
// Given
const string script = "invalid SQL command";
private readonly MySqlNormalUserFixture mySqlFixture;

public MySqlTestcontainerNormalUserTest(MySqlNormalUserFixture mySqlFixture)
{
this.mySqlFixture = mySqlFixture;
}

[Fact]
public async Task ConnectionEstablished()
{
// Given
var connection = this.mySqlFixture.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.mySqlFixture.Container.ExecScriptAsync(script)
.ConfigureAwait(false);

// Then
Assert.DoesNotContain("ERROR", result.Stderr);
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.mySqlFixture.Container.ExecScriptAsync(script)
.ConfigureAwait(false);
// When
var result = await this.mySqlFixture.Container.ExecScriptAsync(script)
.ConfigureAwait(false);

// Then
Assert.NotEqual(0, result.ExitCode);
Assert.Contains("ERROR 1064 (42000)", result.Stderr);
// Then
Assert.NotEqual(0, result.ExitCode);
Assert.Contains("ERROR 1064 (42000)", result.Stderr);
}
}
}
}

0 comments on commit d351697

Please sign in to comment.