Skip to content

Commit

Permalink
Enhance IMDS Tests for Improved Code Coverage (#5045)
Browse files Browse the repository at this point in the history
code coverage improvements

Co-authored-by: Gladwin Johnson <gljohns@microsoft.com>
  • Loading branch information
gladjohn and GladwinJohnson authored Jan 3, 2025
1 parent aef501f commit 0da69c9
Showing 1 changed file with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ namespace Microsoft.Identity.Test.Unit.ManagedIdentityTests
[TestClass]
public class ImdsTests : TestBase
{
[TestMethod]
public async Task ImdsBadRequestTestAsync()
[DataTestMethod]
[DataRow(HttpStatusCode.BadRequest, ImdsManagedIdentitySource.IdentityUnavailableError, 1, DisplayName = "BadRequest - Identity Unavailable")]
[DataRow(HttpStatusCode.BadGateway, ImdsManagedIdentitySource.GatewayError, 1, DisplayName = "BadGateway - Gateway Error")]
[DataRow(HttpStatusCode.GatewayTimeout, ImdsManagedIdentitySource.GatewayError, 4, DisplayName = "GatewayTimeout - Gateway Error Retries")]
public async Task ImdsErrorHandlingTestAsync(HttpStatusCode statusCode, string expectedErrorSubstring, int expectedAttempts)
{
using (new EnvVariableContext())
using (var httpManager = new MockHttpManager(isManagedIdentity: true))

{
SetEnvironmentVariables(ManagedIdentitySource.Imds, "http://169.254.169.254");

Expand All @@ -34,17 +36,22 @@ public async Task ImdsBadRequestTestAsync()

var mi = miBuilder.Build();

httpManager.AddManagedIdentityMockHandler(ManagedIdentityTests.ImdsEndpoint, ManagedIdentityTests.Resource, MockHelpers.GetMsiImdsErrorResponse(),
ManagedIdentitySource.Imds, statusCode: HttpStatusCode.BadRequest);
// Adding multiple mock handlers to simulate retries for GatewayTimeout
for (int i = 0; i < expectedAttempts; i++)
{
httpManager.AddManagedIdentityMockHandler(ManagedIdentityTests.ImdsEndpoint, ManagedIdentityTests.Resource,
MockHelpers.GetMsiImdsErrorResponse(), ManagedIdentitySource.Imds, statusCode: statusCode);
}

// Expecting a MsalServiceException indicating an error
MsalServiceException ex = await Assert.ThrowsExceptionAsync<MsalServiceException>(async () =>
await mi.AcquireTokenForManagedIdentity(ManagedIdentityTests.Resource)
.ExecuteAsync().ConfigureAwait(false)).ConfigureAwait(false);

Assert.IsNotNull(ex);
Assert.AreEqual(ManagedIdentitySource.Imds.ToString(), ex.AdditionalExceptionData[MsalException.ManagedIdentitySource]);
Assert.AreEqual(MsalError.ManagedIdentityRequestFailed, ex.ErrorCode);
Assert.IsTrue(ex.Message.Contains(ImdsManagedIdentitySource.IdentityUnavailableError), $"The error message is not as expected. Error message: {ex.Message}. Expected message: {ImdsManagedIdentitySource.IdentityUnavailableError}");
Assert.IsTrue(ex.Message.Contains(expectedErrorSubstring), $"The error message is not as expected. Error message: {ex.Message}. Expected message should contain: {expectedErrorSubstring}");
}
}
}
Expand Down

0 comments on commit 0da69c9

Please sign in to comment.