-
-
Notifications
You must be signed in to change notification settings - Fork 346
Expand file tree
/
Copy pathMsSqlContainer.cs
More file actions
84 lines (71 loc) · 3.39 KB
/
MsSqlContainer.cs
File metadata and controls
84 lines (71 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
namespace Testcontainers.MsSql;
/// <inheritdoc cref="DockerContainer" />
[PublicAPI]
public sealed class MsSqlContainer : DockerContainer, IDatabaseContainer
{
private static readonly string[] FindSqlCmdFilePath = { "/bin/sh", "-c", "find /opt/mssql-tools*/bin/sqlcmd -type f -print -quit" };
private readonly Lazy<Task<string>> _lazySqlCmdFilePath;
private readonly MsSqlConfiguration _configuration;
/// <summary>
/// Initializes a new instance of the <see cref="MsSqlContainer" /> class.
/// </summary>
/// <param name="configuration">The container configuration.</param>
public MsSqlContainer(MsSqlConfiguration configuration)
: base(configuration)
{
_lazySqlCmdFilePath = new Lazy<Task<string>>(FindSqlCmdFilePathAsync);
_configuration = configuration;
}
/// <summary>
/// Gets the MsSql connection string.
/// </summary>
/// <returns>The MsSql connection string.</returns>
public string GetConnectionString()
{
var properties = new Dictionary<string, string>();
properties.Add("Server", Hostname + "," + GetMappedPublicPort(MsSqlBuilder.MsSqlPort));
properties.Add("Database", _configuration.Database);
properties.Add("User Id", _configuration.Username);
properties.Add("Password", _configuration.Password);
properties.Add("TrustServerCertificate", bool.TrueString);
return string.Join(";", properties.Select(property => string.Join("=", property.Key, property.Value)));
}
/// <summary>
/// Gets the <c>sqlcmd</c> utility file path.
/// </summary>
/// <remarks>
/// The file path represents the path from the container, not from the Docker or test host.
/// </remarks>
/// <param name="ct">Cancellation token.</param>
/// <returns>Task that completes when the <c>sqlcmd</c> utility file path has been found.</returns>
public Task<string> GetSqlCmdFilePathAsync(CancellationToken ct = default)
{
return _lazySqlCmdFilePath.Value;
}
/// <summary>
/// Executes the SQL script in the MsSql container.
/// </summary>
/// <param name="scriptContent">The content of the SQL script to execute.</param>
/// <param name="ct">Cancellation token.</param>
/// <returns>Task that completes when the SQL script has been executed.</returns>
public async Task<ExecResult> ExecScriptAsync(string scriptContent, CancellationToken ct = default)
{
var scriptFilePath = string.Join("/", string.Empty, "tmp", Guid.NewGuid().ToString("D"), Path.GetRandomFileName());
var sqlCmdFilePath = await GetSqlCmdFilePathAsync(ct)
.ConfigureAwait(false);
await CopyAsync(Encoding.Default.GetBytes(scriptContent), scriptFilePath, fileMode: Unix.FileMode644, ct: ct)
.ConfigureAwait(false);
return await ExecAsync(new[] { sqlCmdFilePath, "-C", "-b", "-r", "1", "-U", _configuration.Username, "-P", _configuration.Password, "-i", scriptFilePath }, ct)
.ConfigureAwait(false);
}
private async Task<string> FindSqlCmdFilePathAsync()
{
var findSqlCmdFilePathExecResult = await ExecAsync(FindSqlCmdFilePath)
.ConfigureAwait(false);
if (findSqlCmdFilePathExecResult.ExitCode == 0)
{
return findSqlCmdFilePathExecResult.Stdout.Trim();
}
throw new NotSupportedException("The sqlcmd binary could not be found.");
}
}