Skip to content

Commit

Permalink
Use the startup callback to write the my.cnf configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
0xced committed Feb 15, 2024
1 parent 925f426 commit a23cebb
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/Testcontainers.MySql/MySqlBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ protected override MySqlBuilder Init()
.WithPortBinding(MySqlPort, true)
.WithDatabase(DefaultDatabase)
.WithUsername(DefaultUsername)
.WithPassword(DefaultPassword);
.WithPassword(DefaultPassword)
.WithStartupCallback((container, ct) => container.WriteConfigurationFile(ct));
}

/// <inheritdoc />
Expand Down
30 changes: 16 additions & 14 deletions src/Testcontainers.MySql/MySqlContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace Testcontainers.MySql;
public sealed class MySqlContainer : DockerContainer, IDatabaseContainer
{
private readonly MySqlConfiguration _configuration;
private bool _hasConfigFile;

/// <summary>
/// Initializes a new instance of the <see cref="MySqlContainer" /> class.
Expand Down Expand Up @@ -46,20 +45,23 @@ public async Task<ExecResult> ExecScriptAsync(string scriptContent, Cancellation
await CopyAsync(Encoding.Default.GetBytes(scriptContent), scriptFilePath, Unix.FileMode644, ct)
.ConfigureAwait(false);

if (!_hasConfigFile)
{
var config = new StringWriter { NewLine = "\n" };
config.WriteLine("[client]");
config.WriteLine("protocol=TCP");
config.WriteLine($"user={_configuration.Username}");
config.WriteLine($"password={_configuration.Password}");
await CopyAsync(Encoding.Default.GetBytes(config.ToString()), "/etc/mysql/my.cnf", UnixFileModes.UserRead | UnixFileModes.UserWrite, ct)
.ConfigureAwait(false);

_hasConfigFile = true;
}

return await ExecAsync(new[] { "mysql", _configuration.Database, $"--execute=source {scriptFilePath};" }, ct)
.ConfigureAwait(false);
}

/// <summary>
/// Writes a configuration file so that calling <see cref="ExecScriptAsync"/> does not
/// produce a warning on stderr about using a password on the command line.
/// </summary>
/// <param name="ct">Cancellation token.</param>
/// <returns>Task that completes when the configuration file has been written.</returns>
internal Task WriteConfigurationFile(CancellationToken ct)
{
var config = new StringWriter { NewLine = "\n" };
config.WriteLine("[client]");
config.WriteLine("protocol=TCP");
config.WriteLine($"user={_configuration.Username}");
config.WriteLine($"password={_configuration.Password}");
return CopyAsync(Encoding.Default.GetBytes(config.ToString()), "/etc/mysql/my.cnf", UnixFileModes.UserRead | UnixFileModes.UserWrite, ct);
}
}
4 changes: 2 additions & 2 deletions src/Testcontainers/Builders/ContainerBuilder`3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,9 @@ public TBuilderEntity WithWaitStrategy(IWaitForContainerOS waitStrategy)
}

/// <inheritdoc />
public TBuilderEntity WithStartupCallback(Func<IContainer, CancellationToken, Task> startupCallback)
public TBuilderEntity WithStartupCallback(Func<TContainerEntity, CancellationToken, Task> startupCallback)
{
return Clone(new ContainerConfiguration(startupCallback: startupCallback));
return Clone(new ContainerConfiguration(startupCallback: (container, ct) => startupCallback((TContainerEntity)container, ct)));
}

/// <inheritdoc />
Expand Down
2 changes: 1 addition & 1 deletion src/Testcontainers/Builders/IContainerBuilder`2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,6 @@ public interface IContainerBuilder<out TBuilderEntity, out TContainerEntity> : I
/// <param name="startupCallback">The callback method to invoke.</param>
/// <returns>A configured instance of <typeparamref name="TBuilderEntity" />.</returns>
[PublicAPI]
TBuilderEntity WithStartupCallback(Func<IContainer, CancellationToken, Task> startupCallback);
TBuilderEntity WithStartupCallback(Func<TContainerEntity, CancellationToken, Task> startupCallback);
}
}

0 comments on commit a23cebb

Please sign in to comment.