Skip to content

Commit e62cc4d

Browse files
Fixed + Improved all tests in ImdsV2Tests.cs
1 parent 0bd0aae commit e62cc4d

File tree

3 files changed

+94
-126
lines changed

3 files changed

+94
-126
lines changed

src/client/Microsoft.Identity.Client/ManagedIdentity/V2/ImdsV2ManagedIdentitySource.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ public static async Task<CsrMetadata> GetCsrMetadataAsync(RequestContext request
6666
}
6767
catch (Exception ex)
6868
{
69-
ThrowProbeFailedException(
69+
ThrowCsrMetadataRequestException(
7070
"ImdsV2ManagedIdentitySource.GetCsrMetadataAsync failed.",
7171
ex);
7272
}
7373

7474
if (response.StatusCode != HttpStatusCode.OK)
7575
{
76-
ThrowProbeFailedException(
76+
ThrowCsrMetadataRequestException(
7777
$"ImdsV2ManagedIdentitySource.GetCsrMetadataAsync failed due to HTTP error. Status code: {response.StatusCode} Body: {response.Body}",
7878
null,
7979
(int)response.StatusCode);
@@ -87,7 +87,7 @@ public static async Task<CsrMetadata> GetCsrMetadataAsync(RequestContext request
8787
return TryCreateCsrMetadata(response, requestContext.Logger);
8888
}
8989

90-
private static void ThrowProbeFailedException(
90+
private static void ThrowCsrMetadataRequestException(
9191
String errorMessage,
9292
Exception ex = null,
9393
int? statusCode = null)
@@ -111,15 +111,15 @@ private static bool ValidateCsrMetadataResponse(
111111

112112
if (serverHeader == null)
113113
{
114-
ThrowProbeFailedException(
114+
ThrowCsrMetadataRequestException(
115115
$"ImdsV2ManagedIdentitySource.GetCsrMetadataAsync failed because response doesn't have server header. Status code: {response.StatusCode} Body: {response.Body}",
116116
null,
117117
(int)response.StatusCode);
118118
}
119119

120120
if (!serverHeader.Contains("IMDS", StringComparison.OrdinalIgnoreCase))
121121
{
122-
ThrowProbeFailedException(
122+
ThrowCsrMetadataRequestException(
123123
$"ImdsV2ManagedIdentitySource.GetCsrMetadataAsync failed because the 'server' header format is invalid. Extracted server header: {serverHeader}. Status code: {response.StatusCode} Body: {response.Body}",
124124
null,
125125
(int)response.StatusCode);
@@ -135,7 +135,7 @@ private static CsrMetadata TryCreateCsrMetadata(
135135
CsrMetadata csrMetadata = JsonHelper.DeserializeFromJson<CsrMetadata>(response.Body);
136136
if (!CsrMetadata.ValidateCsrMetadata(csrMetadata))
137137
{
138-
ThrowProbeFailedException(
138+
ThrowCsrMetadataRequestException(
139139
$"ImdsV2ManagedIdentitySource.GetCsrMetadataAsync failed because the CsrMetadata response is invalid. Status code: {response.StatusCode} Body: {response.Body}",
140140
null,
141141
(int)response.StatusCode);

tests/Microsoft.Identity.Test.Common/Core/Mocks/MockHelpers.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,8 @@ public static MockHttpMessageHandler MockImdsProbe(
618618
ImdsVersion imdsVersion,
619619
UserAssignedIdentityId userAssignedIdentityId = UserAssignedIdentityId.None,
620620
string userAssignedId = null,
621-
bool success = true)
621+
bool success = true,
622+
bool retry = false)
622623
{
623624
string apiVersionQueryParam;
624625
string imdsApiVersion;
@@ -650,7 +651,14 @@ public static MockHttpMessageHandler MockImdsProbe(
650651
}
651652
else
652653
{
653-
statusCode = HttpStatusCode.NotFound;
654+
if (retry)
655+
{
656+
statusCode = HttpStatusCode.InternalServerError;
657+
}
658+
else
659+
{
660+
statusCode = HttpStatusCode.NotFound;
661+
}
654662
}
655663

656664
IDictionary<string, string> expectedQueryParams = new Dictionary<string, string>();
@@ -687,9 +695,10 @@ public static MockHttpMessageHandler MockImdsProbe(
687695
public static MockHttpMessageHandler MockImdsProbeFailure(
688696
ImdsVersion imdsVersion,
689697
UserAssignedIdentityId userAssignedIdentityId = UserAssignedIdentityId.None,
690-
string userAssignedId = null)
698+
string userAssignedId = null,
699+
bool retry = false)
691700
{
692-
return MockImdsProbe(imdsVersion, userAssignedIdentityId, userAssignedId, success: false);
701+
return MockImdsProbe(imdsVersion, userAssignedIdentityId, userAssignedId, success: false, retry: retry);
693702
}
694703

695704
public static MockHttpMessageHandler MockCsrResponse(

0 commit comments

Comments
 (0)