-
Notifications
You must be signed in to change notification settings - Fork 256
Add default testcontainer support in the tests #3753
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,18 @@ | ||
| using System.Globalization; | ||
| using Microsoft.Extensions.Configuration; | ||
| using Testcontainers.PostgreSql; | ||
|
|
||
| namespace Microsoft.EntityFrameworkCore.TestUtilities; | ||
|
|
||
| public static class TestEnvironment | ||
| { | ||
| public static IConfiguration Config { get; } | ||
|
|
||
| public static string ConnectionString { get; } | ||
|
|
||
| // Keep a reference to prevent GC from collecting (and finalizing/stopping) the container while tests are running. | ||
| private static readonly PostgreSqlContainer? _postgreSqlContainer; | ||
|
|
||
| static TestEnvironment() | ||
| { | ||
| var configBuilder = new ConfigurationBuilder() | ||
|
|
@@ -18,13 +24,37 @@ static TestEnvironment() | |
| Config = configBuilder.Build() | ||
| .GetSection("Test:Npgsql"); | ||
|
|
||
| Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); | ||
| } | ||
| if (Config["DefaultConnection"] is { } connectionString) | ||
| { | ||
| Console.WriteLine("Using connection string configured via Test:Npgsql:DefaultConnection: " + connectionString); | ||
|
|
||
| private const string DefaultConnectionString = "Server=localhost;Username=npgsql_tests;Password=npgsql_tests;Port=5432"; | ||
| ConnectionString = connectionString; | ||
| } | ||
|
Comment on lines
+27
to
+32
|
||
| else | ||
| { | ||
| Console.WriteLine("No connection string configured via Test:Npgsql:DefaultConnection, starting up testcontainer..."); | ||
|
|
||
| public static string DefaultConnection | ||
| => Config["DefaultConnection"] ?? DefaultConnectionString; | ||
| _postgreSqlContainer = new PostgreSqlBuilder("postgres:latest") | ||
| .WithCommand("-c", "max_connections=200") | ||
| .Build(); | ||
| _postgreSqlContainer.StartAsync().GetAwaiter().GetResult(); | ||
roji marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ConnectionString = _postgreSqlContainer.GetConnectionString(); | ||
|
Comment on lines
+35
to
+41
|
||
|
|
||
| AppDomain.CurrentDomain.ProcessExit += (_, _) => | ||
| { | ||
| try | ||
| { | ||
| _postgreSqlContainer?.DisposeAsync().AsTask().GetAwaiter().GetResult(); | ||
| } | ||
| catch | ||
| { | ||
| // Ignore exceptions during process-exit cleanup. | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); | ||
| } | ||
|
|
||
| private static Version? _postgresVersion; | ||
|
|
||
|
|
||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Console.WriteLinelogs the full configured connection string, which may include credentials (e.g., passwords) and can leak secrets in CI logs or developer output. Consider avoiding logging the connection string entirely, or at least redact sensitive keys (Password/Username) before writing to output.