|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT license. |
| 3 | +// |
| 4 | +using Azure; |
| 5 | +using Azure.Data.AppConfiguration; |
| 6 | +using Microsoft.Extensions.Configuration; |
| 7 | +using Microsoft.Extensions.Configuration.AzureAppConfiguration; |
| 8 | +using Microsoft.Extensions.Diagnostics.HealthChecks; |
| 9 | +using Moq; |
| 10 | +using System.Threading; |
| 11 | +using System.Collections.Generic; |
| 12 | +using System.Threading.Tasks; |
| 13 | +using Xunit; |
| 14 | +using System; |
| 15 | +using System.Linq; |
| 16 | +using Microsoft.Extensions.DependencyInjection; |
| 17 | + |
| 18 | +namespace Tests.AzureAppConfiguration |
| 19 | +{ |
| 20 | + public class HealthCheckTest |
| 21 | + { |
| 22 | + readonly List<ConfigurationSetting> kvCollection = new List<ConfigurationSetting> |
| 23 | + { |
| 24 | + ConfigurationModelFactory.ConfigurationSetting("TestKey1", "TestValue1", "label", |
| 25 | + eTag: new ETag("0a76e3d7-7ec1-4e37-883c-9ea6d0d89e63"), |
| 26 | + contentType:"text"), |
| 27 | + ConfigurationModelFactory.ConfigurationSetting("TestKey2", "TestValue2", "label", |
| 28 | + eTag: new ETag("31c38369-831f-4bf1-b9ad-79db56c8b989"), |
| 29 | + contentType: "text"), |
| 30 | + ConfigurationModelFactory.ConfigurationSetting("TestKey3", "TestValue3", "label", |
| 31 | + |
| 32 | + eTag: new ETag("bb203f2b-c113-44fc-995d-b933c2143339"), |
| 33 | + contentType: "text"), |
| 34 | + ConfigurationModelFactory.ConfigurationSetting("TestKey4", "TestValue4", "label", |
| 35 | + eTag: new ETag("3ca43b3e-d544-4b0c-b3a2-e7a7284217a2"), |
| 36 | + contentType: "text"), |
| 37 | + }; |
| 38 | + |
| 39 | + [Fact] |
| 40 | + public async Task HealthCheckTests_ReturnsHealthyWhenInitialLoadIsCompleted() |
| 41 | + { |
| 42 | + var mockResponse = new Mock<Response>(); |
| 43 | + var mockClient = new Mock<ConfigurationClient>(MockBehavior.Strict); |
| 44 | + |
| 45 | + mockClient.Setup(c => c.GetConfigurationSettingsAsync(It.IsAny<SettingSelector>(), It.IsAny<CancellationToken>())) |
| 46 | + .Returns(new MockAsyncPageable(kvCollection)); |
| 47 | + |
| 48 | + var config = new ConfigurationBuilder() |
| 49 | + .AddAzureAppConfiguration(options => |
| 50 | + { |
| 51 | + options.ClientManager = TestHelpers.CreateMockedConfigurationClientManager(mockClient.Object); |
| 52 | + }) |
| 53 | + .Build(); |
| 54 | + |
| 55 | + IHealthCheck healthCheck = new AzureAppConfigurationHealthCheck(config); |
| 56 | + |
| 57 | + Assert.True(config["TestKey1"] == "TestValue1"); |
| 58 | + var result = await healthCheck.CheckHealthAsync(new HealthCheckContext()); |
| 59 | + Assert.Equal(HealthStatus.Healthy, result.Status); |
| 60 | + } |
| 61 | + |
| 62 | + [Fact] |
| 63 | + public async Task HealthCheckTests_ReturnsUnhealthyWhenRefreshFailed() |
| 64 | + { |
| 65 | + IConfigurationRefresher refresher = null; |
| 66 | + var mockResponse = new Mock<Response>(); |
| 67 | + var mockClient = new Mock<ConfigurationClient>(MockBehavior.Strict); |
| 68 | + |
| 69 | + mockClient.SetupSequence(c => c.GetConfigurationSettingsAsync(It.IsAny<SettingSelector>(), It.IsAny<CancellationToken>())) |
| 70 | + .Returns(new MockAsyncPageable(kvCollection)) |
| 71 | + .Throws(new RequestFailedException(503, "Request failed.")) |
| 72 | + .Returns(new MockAsyncPageable(Enumerable.Empty<ConfigurationSetting>().ToList())) |
| 73 | + .Returns(new MockAsyncPageable(Enumerable.Empty<ConfigurationSetting>().ToList())); |
| 74 | + |
| 75 | + var config = new ConfigurationBuilder() |
| 76 | + .AddAzureAppConfiguration(options => |
| 77 | + { |
| 78 | + options.ClientManager = TestHelpers.CreateMockedConfigurationClientManager(mockClient.Object); |
| 79 | + options.MinBackoffDuration = TimeSpan.FromSeconds(2); |
| 80 | + options.ConfigurationSettingPageIterator = new MockConfigurationSettingPageIterator(); |
| 81 | + options.ConfigureRefresh(refreshOptions => |
| 82 | + { |
| 83 | + refreshOptions.RegisterAll() |
| 84 | + .SetRefreshInterval(TimeSpan.FromSeconds(1)); |
| 85 | + }); |
| 86 | + refresher = options.GetRefresher(); |
| 87 | + }) |
| 88 | + .Build(); |
| 89 | + |
| 90 | + IHealthCheck healthCheck = new AzureAppConfigurationHealthCheck(config); |
| 91 | + |
| 92 | + var result = await healthCheck.CheckHealthAsync(new HealthCheckContext()); |
| 93 | + Assert.Equal(HealthStatus.Healthy, result.Status); |
| 94 | + |
| 95 | + // Wait for the refresh interval to expire |
| 96 | + Thread.Sleep(1000); |
| 97 | + |
| 98 | + await refresher.TryRefreshAsync(); |
| 99 | + result = await healthCheck.CheckHealthAsync(new HealthCheckContext()); |
| 100 | + Assert.Equal(HealthStatus.Unhealthy, result.Status); |
| 101 | + |
| 102 | + // Wait for client backoff to end |
| 103 | + Thread.Sleep(3000); |
| 104 | + |
| 105 | + await refresher.RefreshAsync(); |
| 106 | + result = await healthCheck.CheckHealthAsync(new HealthCheckContext()); |
| 107 | + Assert.Equal(HealthStatus.Healthy, result.Status); |
| 108 | + } |
| 109 | + |
| 110 | + [Fact] |
| 111 | + public async Task HealthCheckTests_RegisterAzureAppConfigurationHealthCheck() |
| 112 | + { |
| 113 | + var mockResponse = new Mock<Response>(); |
| 114 | + var mockClient = new Mock<ConfigurationClient>(MockBehavior.Strict); |
| 115 | + |
| 116 | + mockClient.Setup(c => c.GetConfigurationSettingsAsync(It.IsAny<SettingSelector>(), It.IsAny<CancellationToken>())) |
| 117 | + .Returns(new MockAsyncPageable(kvCollection)); |
| 118 | + |
| 119 | + var config = new ConfigurationBuilder() |
| 120 | + .AddAzureAppConfiguration(options => |
| 121 | + { |
| 122 | + options.ClientManager = TestHelpers.CreateMockedConfigurationClientManager(mockClient.Object); |
| 123 | + }) |
| 124 | + .Build(); |
| 125 | + |
| 126 | + var services = new ServiceCollection(); |
| 127 | + services.AddSingleton<IConfiguration>(config); |
| 128 | + services.AddLogging(); // add logging for health check service |
| 129 | + services.AddHealthChecks() |
| 130 | + .AddAzureAppConfiguration(); |
| 131 | + var provider = services.BuildServiceProvider(); |
| 132 | + var healthCheckService = provider.GetRequiredService<HealthCheckService>(); |
| 133 | + |
| 134 | + var result = await healthCheckService.CheckHealthAsync(); |
| 135 | + Assert.Equal(HealthStatus.Healthy, result.Status); |
| 136 | + Assert.Contains(HealthCheckConstants.HealthCheckRegistrationName, result.Entries.Keys); |
| 137 | + Assert.Equal(HealthStatus.Healthy, result.Entries[HealthCheckConstants.HealthCheckRegistrationName].Status); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments