Skip to content

Commit 0da69c9

Browse files
Enhance IMDS Tests for Improved Code Coverage (#5045)
code coverage improvements Co-authored-by: Gladwin Johnson <[email protected]>
1 parent aef501f commit 0da69c9

File tree

1 file changed

+13
-6
lines changed
  • tests/Microsoft.Identity.Test.Unit/ManagedIdentityTests

1 file changed

+13
-6
lines changed

tests/Microsoft.Identity.Test.Unit/ManagedIdentityTests/ImdsTests.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ namespace Microsoft.Identity.Test.Unit.ManagedIdentityTests
1717
[TestClass]
1818
public class ImdsTests : TestBase
1919
{
20-
[TestMethod]
21-
public async Task ImdsBadRequestTestAsync()
20+
[DataTestMethod]
21+
[DataRow(HttpStatusCode.BadRequest, ImdsManagedIdentitySource.IdentityUnavailableError, 1, DisplayName = "BadRequest - Identity Unavailable")]
22+
[DataRow(HttpStatusCode.BadGateway, ImdsManagedIdentitySource.GatewayError, 1, DisplayName = "BadGateway - Gateway Error")]
23+
[DataRow(HttpStatusCode.GatewayTimeout, ImdsManagedIdentitySource.GatewayError, 4, DisplayName = "GatewayTimeout - Gateway Error Retries")]
24+
public async Task ImdsErrorHandlingTestAsync(HttpStatusCode statusCode, string expectedErrorSubstring, int expectedAttempts)
2225
{
2326
using (new EnvVariableContext())
2427
using (var httpManager = new MockHttpManager(isManagedIdentity: true))
25-
2628
{
2729
SetEnvironmentVariables(ManagedIdentitySource.Imds, "http://169.254.169.254");
2830

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

3537
var mi = miBuilder.Build();
3638

37-
httpManager.AddManagedIdentityMockHandler(ManagedIdentityTests.ImdsEndpoint, ManagedIdentityTests.Resource, MockHelpers.GetMsiImdsErrorResponse(),
38-
ManagedIdentitySource.Imds, statusCode: HttpStatusCode.BadRequest);
39+
// Adding multiple mock handlers to simulate retries for GatewayTimeout
40+
for (int i = 0; i < expectedAttempts; i++)
41+
{
42+
httpManager.AddManagedIdentityMockHandler(ManagedIdentityTests.ImdsEndpoint, ManagedIdentityTests.Resource,
43+
MockHelpers.GetMsiImdsErrorResponse(), ManagedIdentitySource.Imds, statusCode: statusCode);
44+
}
3945

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

4451
Assert.IsNotNull(ex);
4552
Assert.AreEqual(ManagedIdentitySource.Imds.ToString(), ex.AdditionalExceptionData[MsalException.ManagedIdentitySource]);
4653
Assert.AreEqual(MsalError.ManagedIdentityRequestFailed, ex.ErrorCode);
47-
Assert.IsTrue(ex.Message.Contains(ImdsManagedIdentitySource.IdentityUnavailableError), $"The error message is not as expected. Error message: {ex.Message}. Expected message: {ImdsManagedIdentitySource.IdentityUnavailableError}");
54+
Assert.IsTrue(ex.Message.Contains(expectedErrorSubstring), $"The error message is not as expected. Error message: {ex.Message}. Expected message should contain: {expectedErrorSubstring}");
4855
}
4956
}
5057
}

0 commit comments

Comments
 (0)