|
1 | 1 | // Copyright Subatomix Research Inc. |
2 | 2 | // SPDX-License-Identifier: MIT |
3 | 3 |
|
4 | | -using System.Net; |
| 4 | +using Subatomix.Testing.SqlServerIntegration; |
5 | 5 |
|
6 | 6 | namespace PSql.Deploy.Integration; |
7 | 7 |
|
8 | 8 | [SetUpFixture] |
9 | 9 | public static class IntegrationTestsSetup |
10 | 10 | { |
11 | | - private const string |
12 | | - PasswordName = "MSSQL_SA_PASSWORD", |
13 | | - ServerPipe = @"\\.\pipe\sql\query"; |
14 | | - |
15 | | - private const ushort |
16 | | - ServerPort = 1433; |
17 | | - |
18 | | - private static SqlServerContainer? _container; |
19 | | - private static Target? _setupTarget; |
20 | | - private static Target? _testTarget; |
| 11 | + private static TemporaryDatabase? _database; |
| 12 | + private static Target? _target; |
21 | 13 |
|
22 | 14 | internal static Target Target |
23 | | - => _testTarget ?? throw new InvalidOperationException("Target not initialized."); |
| 15 | + => _target ?? throw OnSetUpNotExecuted(); |
24 | 16 |
|
25 | 17 | [OneTimeSetUp] |
26 | | - public static async Task SetUp() |
| 18 | + public static void SetUp() |
27 | 19 | { |
28 | | - var connectionString = new SqlConnectionStringBuilder { DataSource = "." }; |
29 | | - var credential = null as NetworkCredential; |
30 | | - |
31 | | - var password = Environment |
32 | | - .GetEnvironmentVariable(PasswordName) |
33 | | - .NullIfEmpty(); |
34 | | - |
35 | | - if (password is not null) |
36 | | - { |
37 | | - // Scenario A: Environment variable MSSQL_SA_PASSWORD present. |
38 | | - // => Assume that a local SQL Server default instance is running. |
39 | | - // Use the given password to authenticate as SA. |
40 | | - credential = new NetworkCredential("sa", password); |
41 | | - } |
42 | | - else if (IsLocalSqlServerListening()) |
43 | | - { |
44 | | - // Scenario B: Process listening on port 1433 or named pipe. |
45 | | - // => Assume that a local SQL Server default instance is running |
46 | | - // and supports integrated authentication. Assume that the |
47 | | - // current user has suffucient privileges to run tests. |
48 | | - connectionString.IntegratedSecurity = true; |
49 | | - } |
50 | | - else |
51 | | - { |
52 | | - // Scenario C: No process listening on port 1433 or named pipe. |
53 | | - // => Start an ephemeral SQL Server container on port 1433 using a |
54 | | - // generated SA password. |
55 | | - _container = new SqlServerContainer(ServerPort); |
56 | | - credential = _container.Credential; |
57 | | - } |
58 | | - |
59 | | - connectionString.Encrypt = SqlConnectionEncryptOption.Optional; |
60 | | - connectionString.ApplicationName = "PSql.Deploy.Tests"; |
| 20 | + TestSqlServer.SetUp(); |
61 | 21 |
|
62 | | - _setupTarget = new(connectionString.ToString(), credential); |
63 | | - |
64 | | - connectionString.InitialCatalog = "PSqlDeployTest"; |
65 | | - |
66 | | - _testTarget = new(connectionString.ToString(), credential); |
67 | | - |
68 | | - await CreateTestDatabaseAsync(); |
| 22 | + _database = TestSqlServer.CreateTemporaryDatabase("PSqlDeployTest"); |
| 23 | + _target = new(_database.ConnectionString, TestSqlServer.Credential); |
69 | 24 | } |
70 | 25 |
|
71 | 26 |
|
72 | | - private static bool IsLocalSqlServerListening() |
73 | | - { |
74 | | - return TcpPort.IsListening(ServerPort) |
75 | | - || OperatingSystem.IsWindows() && File.Exists(ServerPipe); |
76 | | - } |
77 | | - |
78 | 27 | [OneTimeTearDown] |
79 | | - public static async Task TearDown() |
80 | | - { |
81 | | - try |
82 | | - { |
83 | | - await RemoveTestDatabaseAsync(); |
84 | | - } |
85 | | - finally |
86 | | - { |
87 | | - _container?.Dispose(); |
88 | | - _container = null; |
89 | | - } |
90 | | - } |
91 | | - |
92 | | - private static async Task CreateTestDatabaseAsync() |
| 28 | + public static void TearDown() |
93 | 29 | { |
94 | | - if (_setupTarget is null) |
95 | | - return; |
96 | | - |
97 | | - await using var connection = new SqlTestTargetConnection(_setupTarget); |
| 30 | + _database = null; |
| 31 | + _target = null; |
98 | 32 |
|
99 | | - await connection.OpenAsync(CancellationToken.None); |
100 | | - await connection.RemoveDatabaseAsync("PSqlDeployTest"); |
101 | | - await connection.CreateDatabaseAsync("PSqlDeployTest"); |
| 33 | + TestSqlServer.TearDown(); |
102 | 34 | } |
103 | 35 |
|
104 | | - private static async Task RemoveTestDatabaseAsync() |
| 36 | + private static Exception OnSetUpNotExecuted() |
105 | 37 | { |
106 | | - if (_setupTarget is null) |
107 | | - return; |
108 | | - |
109 | | - await using var connection = new SqlTestTargetConnection(_setupTarget); |
110 | | - |
111 | | - await connection.OpenAsync(CancellationToken.None); |
112 | | - await connection.RemoveDatabaseAsync("PSqlDeployTest"); |
| 38 | + return new InvalidOperationException( |
| 39 | + nameof(IntegrationTestsSetup) + "." + nameof(SetUp) + " has not executed." |
| 40 | + ); |
113 | 41 | } |
114 | 42 | } |
0 commit comments