-
Notifications
You must be signed in to change notification settings - Fork 305
Fix PostgreSQL health check connection string parsing #3083
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
Open
Copilot
wants to merge
9
commits into
main
Choose a base branch
from
copilot/fix-health-check-postgres
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+189
−5
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d4424e0
Initial plan
Copilot 66b22b8
Fix PostgreSQL health check connection string handling
Copilot d858f2d
Add exception handling to NormalizeConnectionString method
Copilot 209087a
Merge branch 'main' into copilot/fix-health-check-postgres
Aniruddh25 26096f1
Merge branch 'main' into copilot/fix-health-check-postgres
souvikghosh04 d94b703
Add MySQL support, logging, and unit tests for NormalizeConnectionString
Copilot e0927eb
Merge branch 'main' into copilot/fix-health-check-postgres
Aniruddh25 4a83f33
Refactor tests to use DataRow attributes and mask passwords
Copilot d190e77
Merge branch 'main' into copilot/fix-health-check-postgres
Aniruddh25 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
155 changes: 155 additions & 0 deletions
155
src/Service.Tests/UnitTests/HealthCheckUtilitiesUnitTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #nullable enable | ||
|
|
||
| using System; | ||
| using Azure.DataApiBuilder.Config.ObjectModel; | ||
| using Azure.DataApiBuilder.Service.HealthCheck; | ||
| using Microsoft.Extensions.Logging; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using Moq; | ||
|
|
||
| namespace Azure.DataApiBuilder.Service.Tests.UnitTests | ||
| { | ||
| /// <summary> | ||
| /// Unit tests for health check utility methods. | ||
| /// </summary> | ||
| [TestClass] | ||
| public class HealthCheckUtilitiesUnitTests | ||
| { | ||
| /// <summary> | ||
| /// Tests that connection strings are properly normalized for supported database types. | ||
| /// </summary> | ||
| [TestMethod] | ||
| [DataRow( | ||
| DatabaseType.PostgreSQL, | ||
| "Host=localhost;Port=5432;Database=testdb;Username=testuser;Password=XXXX", | ||
| "Host=localhost", | ||
| "Database=testdb", | ||
| DisplayName = "PostgreSQL connection string normalization")] | ||
| [DataRow( | ||
| DatabaseType.MSSQL, | ||
| "Server=localhost;Database=testdb;User Id=testuser;Password=XXXX", | ||
| "Data Source=localhost", | ||
| "Initial Catalog=testdb", | ||
| DisplayName = "MSSQL connection string normalization")] | ||
| [DataRow( | ||
| DatabaseType.DWSQL, | ||
| "Server=localhost;Database=testdb;User Id=testuser;Password=XXXX", | ||
| "Data Source=localhost", | ||
| "Initial Catalog=testdb", | ||
| DisplayName = "DWSQL connection string normalization")] | ||
| [DataRow( | ||
| DatabaseType.MySQL, | ||
| "Server=localhost;Port=3306;Database=testdb;Uid=testuser;Pwd=XXXX", | ||
| "Server=localhost", | ||
| "Database=testdb", | ||
| DisplayName = "MySQL connection string normalization")] | ||
| public void NormalizeConnectionString_SupportedDatabases_Success( | ||
| DatabaseType dbType, | ||
| string connectionString, | ||
| string expectedServerPart, | ||
| string expectedDatabasePart) | ||
| { | ||
| // Act | ||
| string result = HealthCheck.Utilities.NormalizeConnectionString(connectionString, dbType); | ||
|
|
||
| // Assert | ||
| Assert.IsNotNull(result); | ||
| Assert.IsTrue(result.Contains(expectedServerPart)); | ||
| Assert.IsTrue(result.Contains(expectedDatabasePart)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Tests that unsupported database types return the original connection string. | ||
| /// </summary> | ||
| [TestMethod] | ||
| public void NormalizeConnectionString_UnsupportedType_ReturnsOriginal() | ||
| { | ||
| // Arrange | ||
| string connectionString = "AccountEndpoint=https://test.documents.azure.com:443/;AccountKey=test"; | ||
| DatabaseType dbType = DatabaseType.CosmosDB_NoSQL; | ||
|
|
||
| // Act | ||
| string result = HealthCheck.Utilities.NormalizeConnectionString(connectionString, dbType); | ||
|
|
||
| // Assert | ||
| Assert.AreEqual(connectionString, result); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Tests that malformed connection strings are handled gracefully. | ||
| /// </summary> | ||
| [TestMethod] | ||
| [DataRow(DatabaseType.PostgreSQL, true, DisplayName = "PostgreSQL malformed string with logger")] | ||
| [DataRow(DatabaseType.MSSQL, true, DisplayName = "MSSQL malformed string with logger")] | ||
| [DataRow(DatabaseType.MySQL, false, DisplayName = "MySQL malformed string without logger")] | ||
| public void NormalizeConnectionString_MalformedString_ReturnsOriginal( | ||
| DatabaseType dbType, | ||
| bool useLogger) | ||
| { | ||
| // Arrange | ||
| string malformedConnectionString = "InvalidConnectionString;NoEquals"; | ||
| Mock<ILogger>? mockLogger = useLogger ? new Mock<ILogger>() : null; | ||
|
|
||
| // Act | ||
| string result = HealthCheck.Utilities.NormalizeConnectionString( | ||
| malformedConnectionString, | ||
| dbType, | ||
| mockLogger?.Object); | ||
|
|
||
| // Assert | ||
| Assert.AreEqual(malformedConnectionString, result); | ||
| if (useLogger && mockLogger != null) | ||
| { | ||
| mockLogger.Verify( | ||
| x => x.Log( | ||
| LogLevel.Warning, | ||
| It.IsAny<EventId>(), | ||
| It.Is<It.IsAnyType>((v, t) => true), | ||
| It.IsAny<Exception>(), | ||
| It.Is<Func<It.IsAnyType, Exception?, string>>((v, t) => true)), | ||
| Times.Once); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Tests that PostgreSQL connection strings with lowercase keywords are normalized correctly. | ||
| /// This is the specific bug that was reported - lowercase 'host' was not supported. | ||
| /// </summary> | ||
| [TestMethod] | ||
| public void NormalizeConnectionString_PostgreSQL_LowercaseKeywords_Success() | ||
| { | ||
| // Arrange | ||
| string connectionString = "host=localhost;port=5432;database=mydb;username=myuser;password=XXXX"; | ||
| DatabaseType dbType = DatabaseType.PostgreSQL; | ||
|
|
||
| // Act | ||
| string result = HealthCheck.Utilities.NormalizeConnectionString(connectionString, dbType); | ||
|
|
||
| // Assert | ||
| Assert.IsNotNull(result); | ||
| // NpgsqlConnectionStringBuilder should normalize lowercase keywords to proper format | ||
| Assert.IsTrue(result.Contains("Host=localhost") || result.Contains("host=localhost")); | ||
| Assert.IsTrue(result.Contains("Database=mydb") || result.Contains("database=mydb")); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Tests that empty connection strings are handled gracefully. | ||
| /// </summary> | ||
| [TestMethod] | ||
| public void NormalizeConnectionString_EmptyString_ReturnsEmpty() | ||
| { | ||
| // Arrange | ||
| string connectionString = string.Empty; | ||
| DatabaseType dbType = DatabaseType.PostgreSQL; | ||
|
|
||
| // Act | ||
| string result = HealthCheck.Utilities.NormalizeConnectionString(connectionString, dbType); | ||
|
|
||
| // Assert | ||
| Assert.AreEqual(string.Empty, result); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.