|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using Aspire.Components.Common.Tests; |
| 5 | +using Aspire.Hosting.Utils; |
| 6 | +using Microsoft.Extensions.Configuration; |
| 7 | +using Microsoft.Extensions.DependencyInjection; |
| 8 | +using Microsoft.Extensions.Hosting; |
| 9 | +using Microsoft.Extensions.Logging; |
| 10 | +using Npgsql; |
| 11 | +using Polly; |
| 12 | +using Xunit; |
| 13 | +using Xunit.Abstractions; |
| 14 | + |
| 15 | +namespace Aspire.Hosting.PostgreSQL.Tests; |
| 16 | + |
| 17 | +public class PostgresFunctionalTests(ITestOutputHelper testOutputHelper) |
| 18 | +{ |
| 19 | + [Fact] |
| 20 | + [RequiresDocker] |
| 21 | + public async Task VerifyPostgresResource() |
| 22 | + { |
| 23 | + var builder = CreateDistributedApplicationBuilder(); |
| 24 | + |
| 25 | + var postgresDbName = "db1"; |
| 26 | + |
| 27 | + var postgres = builder.AddPostgres("pg").WithEnvironment("POSTGRES_DB", postgresDbName); |
| 28 | + var db = postgres.AddDatabase(postgresDbName); |
| 29 | + |
| 30 | + using var app = builder.Build(); |
| 31 | + |
| 32 | + await app.StartAsync(); |
| 33 | + |
| 34 | + var hb = Host.CreateApplicationBuilder(); |
| 35 | + |
| 36 | + hb.Configuration.AddInMemoryCollection(new Dictionary<string, string?> |
| 37 | + { |
| 38 | + [$"ConnectionStrings:{db.Resource.Name}"] = await db.Resource.ConnectionStringExpression.GetValueAsync(default) |
| 39 | + }); |
| 40 | + |
| 41 | + hb.AddNpgsqlDataSource(db.Resource.Name); |
| 42 | + |
| 43 | + using var host = hb.Build(); |
| 44 | + |
| 45 | + await host.StartAsync(); |
| 46 | + |
| 47 | + var pipeline = new ResiliencePipelineBuilder() |
| 48 | + .AddRetry(new() { MaxRetryAttempts = 10, Delay = TimeSpan.FromSeconds(1), ShouldHandle = new PredicateBuilder().Handle<NpgsqlException>() }) |
| 49 | + .AddTimeout(TimeSpan.FromSeconds(5)) |
| 50 | + .Build(); |
| 51 | + |
| 52 | + await pipeline.ExecuteAsync( |
| 53 | + async token => |
| 54 | + { |
| 55 | + using var connection = host.Services.GetRequiredService<NpgsqlConnection>(); |
| 56 | + await connection.OpenAsync(token); |
| 57 | + |
| 58 | + var command = connection.CreateCommand(); |
| 59 | + command.CommandText = $"SELECT 1"; |
| 60 | + var results = await command.ExecuteReaderAsync(token); |
| 61 | + |
| 62 | + Assert.True(results.HasRows); |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + [Theory] |
| 67 | + [InlineData(true)] |
| 68 | + [InlineData(false)] |
| 69 | + [RequiresDocker] |
| 70 | + public async Task WithDataShouldPersistStateBetweenUsages(bool useVolume) |
| 71 | + { |
| 72 | + var postgresDbName = "tempdb"; |
| 73 | + |
| 74 | + string? volumeName = null; |
| 75 | + string? bindMountPath = null; |
| 76 | + |
| 77 | + var pipeline = new ResiliencePipelineBuilder() |
| 78 | + .AddRetry(new() { MaxRetryAttempts = 10, Delay = TimeSpan.FromSeconds(1), ShouldHandle = new PredicateBuilder().Handle<NpgsqlException>() }) |
| 79 | + .AddTimeout(TimeSpan.FromSeconds(5)) |
| 80 | + .Build(); |
| 81 | + |
| 82 | + try |
| 83 | + { |
| 84 | + var builder1 = CreateDistributedApplicationBuilder(); |
| 85 | + |
| 86 | + var username = "postgres"; |
| 87 | + var password = "p@ssw0rd1"; |
| 88 | + |
| 89 | + var usernameParameter = builder1.AddParameter("user"); |
| 90 | + var passwordParameter = builder1.AddParameter("pwd"); |
| 91 | + builder1.Configuration["Parameters:user"] = username; |
| 92 | + builder1.Configuration["Parameters:pwd"] = password; |
| 93 | + var postgres1 = builder1.AddPostgres("pg", usernameParameter, passwordParameter).WithEnvironment("POSTGRES_DB", postgresDbName); |
| 94 | + |
| 95 | + var db1 = postgres1.AddDatabase(postgresDbName); |
| 96 | + |
| 97 | + if (useVolume) |
| 98 | + { |
| 99 | + // Use a deterministic volume name to prevent them from exhausting the machines if deletion fails |
| 100 | + volumeName = VolumeNameGenerator.CreateVolumeName(postgres1, nameof(WithDataShouldPersistStateBetweenUsages)); |
| 101 | + |
| 102 | + // If the volume already exists (because of a crashing previous run), try to delete it |
| 103 | + DockerUtils.AttemptDeleteDockerVolume(volumeName); |
| 104 | + postgres1.WithDataVolume(volumeName); |
| 105 | + } |
| 106 | + else |
| 107 | + { |
| 108 | + bindMountPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); |
| 109 | + postgres1.WithDataBindMount(bindMountPath); |
| 110 | + } |
| 111 | + |
| 112 | + using (var app = builder1.Build()) |
| 113 | + { |
| 114 | + await app.StartAsync(); |
| 115 | + |
| 116 | + try |
| 117 | + { |
| 118 | + var hb = Host.CreateApplicationBuilder(); |
| 119 | + |
| 120 | + hb.Configuration.AddInMemoryCollection(new Dictionary<string, string?> |
| 121 | + { |
| 122 | + [$"ConnectionStrings:{db1.Resource.Name}"] = await db1.Resource.ConnectionStringExpression.GetValueAsync(default) |
| 123 | + }); |
| 124 | + |
| 125 | + hb.AddNpgsqlDataSource(db1.Resource.Name); |
| 126 | + |
| 127 | + using (var host = hb.Build()) |
| 128 | + { |
| 129 | + await host.StartAsync(); |
| 130 | + |
| 131 | + await pipeline.ExecuteAsync( |
| 132 | + async token => |
| 133 | + { |
| 134 | + using var connection = host.Services.GetRequiredService<NpgsqlConnection>(); |
| 135 | + await connection.OpenAsync(token); |
| 136 | + |
| 137 | + var command = connection.CreateCommand(); |
| 138 | + command.CommandText = $"CREATE TABLE cars (brand VARCHAR(255)); INSERT INTO cars (brand) VALUES ('BatMobile'); SELECT * FROM cars;"; |
| 139 | + var results = await command.ExecuteReaderAsync(token); |
| 140 | + |
| 141 | + Assert.True(results.HasRows); |
| 142 | + }); |
| 143 | + } |
| 144 | + } |
| 145 | + finally |
| 146 | + { |
| 147 | + // Stops the container, or the Volume/mount would still be in use |
| 148 | + await app.StopAsync(); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + var builder2 = CreateDistributedApplicationBuilder(); |
| 153 | + usernameParameter = builder2.AddParameter("user"); |
| 154 | + passwordParameter = builder2.AddParameter("pwd"); |
| 155 | + builder2.Configuration["Parameters:user"] = username; |
| 156 | + builder2.Configuration["Parameters:pwd"] = password; |
| 157 | + |
| 158 | + var postgres2 = builder2.AddPostgres("pg", usernameParameter, passwordParameter); |
| 159 | + var db2 = postgres2.AddDatabase(postgresDbName); |
| 160 | + |
| 161 | + if (useVolume) |
| 162 | + { |
| 163 | + postgres2.WithDataVolume(volumeName); |
| 164 | + } |
| 165 | + else |
| 166 | + { |
| 167 | + postgres2.WithDataBindMount(bindMountPath!); |
| 168 | + } |
| 169 | + |
| 170 | + using (var app = builder2.Build()) |
| 171 | + { |
| 172 | + await app.StartAsync(); |
| 173 | + try |
| 174 | + { |
| 175 | + var hb = Host.CreateApplicationBuilder(); |
| 176 | + |
| 177 | + hb.Configuration.AddInMemoryCollection(new Dictionary<string, string?> |
| 178 | + { |
| 179 | + [$"ConnectionStrings:{db2.Resource.Name}"] = await db2.Resource.ConnectionStringExpression.GetValueAsync(default) |
| 180 | + }); |
| 181 | + |
| 182 | + hb.AddNpgsqlDataSource(db2.Resource.Name); |
| 183 | + |
| 184 | + using (var host = hb.Build()) |
| 185 | + { |
| 186 | + await host.StartAsync(); |
| 187 | + |
| 188 | + await pipeline.ExecuteAsync( |
| 189 | + async token => |
| 190 | + { |
| 191 | + using var connection = host.Services.GetRequiredService<NpgsqlConnection>(); |
| 192 | + await connection.OpenAsync(token); |
| 193 | + |
| 194 | + var command = connection.CreateCommand(); |
| 195 | + command.CommandText = $"SELECT * FROM cars;"; |
| 196 | + var results = await command.ExecuteReaderAsync(token); |
| 197 | + |
| 198 | + Assert.True(results.HasRows); |
| 199 | + }); |
| 200 | + } |
| 201 | + |
| 202 | + } |
| 203 | + finally |
| 204 | + { |
| 205 | + // Stops the container, or the Volume/mount would still be in use |
| 206 | + await app.StopAsync(); |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + } |
| 211 | + finally |
| 212 | + { |
| 213 | + if (volumeName is not null) |
| 214 | + { |
| 215 | + DockerUtils.AttemptDeleteDockerVolume(volumeName); |
| 216 | + } |
| 217 | + |
| 218 | + if (bindMountPath is not null) |
| 219 | + { |
| 220 | + try |
| 221 | + { |
| 222 | + File.Delete(bindMountPath); |
| 223 | + } |
| 224 | + catch |
| 225 | + { |
| 226 | + // Don't fail test if we can't clean the temporary folder |
| 227 | + } |
| 228 | + } |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + private TestDistributedApplicationBuilder CreateDistributedApplicationBuilder() |
| 233 | + { |
| 234 | + var builder = TestDistributedApplicationBuilder.CreateWithTestContainerRegistry(); |
| 235 | + builder.Services.AddXunitLogging(testOutputHelper); |
| 236 | + return builder; |
| 237 | + } |
| 238 | +} |
0 commit comments