internal async Task TransientErrorRetryTest(MockMsi.MsiTestType testType) { // To simplify tests, mock as MSI App Services to skip Azure VM IDMS probe request by Environment.SetEnvironmentVariable(Constants.MsiAppServiceEndpointEnv, Constants.MsiEndpoint); Environment.SetEnvironmentVariable(Constants.MsiAppServiceHeaderEnv, Constants.ClientSecret); MockMsi mockMsi = new MockMsi(testType); MsiAccessTokenProvider msiAccessTokenProvider = new MsiAccessTokenProvider(mockMsi); MsiRetryHelper.WaitBeforeRetry = false; // Get token, requests will fail several times before success var authResult = await msiAccessTokenProvider.GetAuthResultAsync(Constants.KeyVaultResourceId, Constants.TenantId).ConfigureAwait(false); Validator.ValidateToken(authResult.AccessToken, msiAccessTokenProvider.PrincipalUsed, Constants.AppType, Constants.TenantId, Constants.TestAppId, expiresOn: authResult.ExpiresOn); // Request for token again, subsequent requests will all fail var exception = await Assert.ThrowsAsync <AzureServiceTokenProviderException>(() => Task.Run(() => msiAccessTokenProvider.GetAuthResultAsync(Constants.KeyVaultResourceId, Constants.TenantId))); Assert.Contains(AzureServiceTokenProviderException.RetryFailure, exception.Message); if (testType == MockMsi.MsiTestType.MsiUnresponsive) { Assert.Contains(AzureServiceTokenProviderException.MsiEndpointNotListening, exception.Message); } else { Assert.Contains(AzureServiceTokenProviderException.GenericErrorMessage, exception.Message); Assert.Contains(testType.ToString(), exception.Message); } }
private async Task TransientErrorTest(MockMsi.MsiTestType testType) { MockMsi mockMsi = new MockMsi(testType); HttpClient httpClient = new HttpClient(mockMsi); MsiAccessTokenProvider msiAccessTokenProvider = new MsiAccessTokenProvider(httpClient); MsiRetryHelper.WaitBeforeRetry = false; // Get token. var authResult = await msiAccessTokenProvider.GetAuthResultAsync(Constants.KeyVaultResourceId, Constants.TenantId).ConfigureAwait(false); // Check if the principalused and type are as expected. Validator.ValidateToken(authResult.AccessToken, msiAccessTokenProvider.PrincipalUsed, Constants.AppType, Constants.TenantId, Constants.TestAppId, expiresOn: authResult.ExpiresOn); }