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

fix: Get sqlcmd utility file path from container instead of const file path #1221

Merged
merged 7 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 11 additions & 3 deletions src/Testcontainers.MsSql/MsSqlBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,20 @@ private MsSqlBuilder WithUsername(string username)
/// </remarks>
private sealed class WaitUntil : IWaitUntil
{
private readonly string[] _command = { "/opt/mssql-tools/bin/sqlcmd", "-Q", "SELECT 1;" };

/// <inheritdoc />
public async Task<bool> UntilAsync(IContainer container)
{
var execResult = await container.ExecAsync(_command)
var hasMsSql18Tools = await container.ExecAsync(new[] { "[", "-f" , "/opt/mssql-tools18/bin/sqlcmd", "]" })
.ConfigureAwait(false);

string[] command = [
hasMsSql18Tools.ExitCode == 0 ? "/opt/mssql-tools18/bin/sqlcmd" : "/opt/mssql-tools/bin/sqlcmd",
outofrange-consulting marked this conversation as resolved.
Show resolved Hide resolved
"-C",
"-Q",
"SELECT 1;",
];

var execResult = await container.ExecAsync(command)
.ConfigureAwait(false);

return 0L.Equals(execResult.ExitCode);
Expand Down
20 changes: 19 additions & 1 deletion src/Testcontainers.MsSql/MsSqlContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,25 @@ public async Task<ExecResult> ExecScriptAsync(string scriptContent, Cancellation
await CopyAsync(Encoding.Default.GetBytes(scriptContent), scriptFilePath, Unix.FileMode644, ct)
.ConfigureAwait(false);

return await ExecAsync(new[] { "/opt/mssql-tools/bin/sqlcmd", "-b", "-r", "1", "-U", _configuration.Username, "-P", _configuration.Password, "-i", scriptFilePath }, ct)
var hasMsSql18Tools = await ExecAsync(new[] { "[", "-f" , "/opt/mssql-tools18/bin/sqlcmd", "]" }, ct)
.ConfigureAwait(false);

var command = new[]
{
hasMsSql18Tools.ExitCode == 0 ? "/opt/mssql-tools18/bin/sqlcmd" : "/opt/mssql-tools/bin/sqlcmd",
"-C",
"-b",
"-r",
"1",
"-U",
_configuration.Username,
"-P",
_configuration.Password,
"-i",
scriptFilePath,
};

return await ExecAsync(command, ct)
.ConfigureAwait(false);
}
}
29 changes: 27 additions & 2 deletions tests/Testcontainers.MsSql.Tests/MsSqlContainerTest.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
using JetBrains.Annotations;

namespace Testcontainers.MsSql;

public sealed class MsSqlContainerTest : IAsyncLifetime
public abstract class MsSqlContainerTest : IAsyncLifetime
{
private readonly MsSqlContainer _msSqlContainer = new MsSqlBuilder().Build();
private readonly MsSqlContainer _msSqlContainer;

private MsSqlContainerTest(MsSqlContainer msSqlContainer)
{
_msSqlContainer = msSqlContainer;
}

public Task InitializeAsync()
{
return _msSqlContainer.StartAsync();
Expand Down Expand Up @@ -43,4 +50,22 @@ public async Task ExecScriptReturnsSuccessful()
Assert.True(0L.Equals(execResult.ExitCode), execResult.Stderr);
Assert.Empty(execResult.Stderr);
}

[UsedImplicitly]
public sealed class MsSqlDefaultConfiguration : MsSqlContainerTest
{
public MsSqlDefaultConfiguration()
: base(new MsSqlBuilder().Build())
{
}
}

[UsedImplicitly]
public sealed class MsSqlWithMsSqlTools18Configuration : MsSqlContainerTest
{
public MsSqlWithMsSqlTools18Configuration()
: base(new MsSqlBuilder().WithImage("mcr.microsoft.com/mssql/server:2022-CU14-ubuntu-22.04").Build())
{
}
}
}
Loading