BootstrapAdmin/test/UnitTest/Bootstrap.Admin/Api/HealthCheckTest.cs

138 lines
5.0 KiB
C#
Raw Normal View History

using Bootstrap.Admin.HealthChecks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Newtonsoft.Json;
2019-09-02 23:13:57 +08:00
using System.IO;
using System.Net.Http;
2019-09-02 23:13:57 +08:00
using UnitTest;
using Xunit;
2019-08-14 11:05:59 +08:00
namespace Bootstrap.Admin.Api
{
public class HealthCheckTest : IClassFixture<HealthCheckWebHost<HealthCheckStartup>>
2019-08-14 11:05:59 +08:00
{
private HttpClient client;
public HealthCheckTest(HealthCheckWebHost<HealthCheckStartup> factory)
{
client = factory.CreateDefaultClient();
}
2019-08-14 11:05:59 +08:00
[Fact]
public async void Get_Ok()
{
var cates = await client.GetAsJsonAsync<object>("/Healths");
2019-08-14 11:05:59 +08:00
Assert.NotNull(cates);
}
[Fact]
public async void UI_Ok()
{
var cates = await client.GetStringAsync("/Healths");
Assert.Contains("TotalDuration", cates);
// 测试数据库不能加载时健康检查
var config = Longbow.Configuration.ConfigurationManager.AppSettings;
config["DB:0:Enabled"] = "false";
config["DB:4:Enabled"] = "true";
config["DB:4:Widget"] = "Bootstrap.DataAccess.MongoDB1";
cates = await client.GetStringAsync("/Healths");
Assert.Contains("TotalDuration", cates);
// 测试数据库连接字符串未配置
config["DB:0:Enabled"] = "false";
config["DB:2:Enabled"] = "true";
config["DB:2:ConnectionStrings:ba"] = "Server=localhost;Database=UnitTest1;Uid=argozhang123;Pwd=argo@163.com;SslMode=none;";
config["DB:4:Enabled"] = "false";
config["DB:4:Widget"] = "Bootstrap.DataAccess.MongoDB";
cates = await client.GetStringAsync("/Healths");
Assert.Contains("TotalDuration", cates);
config["DB:2:Enabled"] = "false";
config["DB:2:ConnectionStrings:ba"] = "Server=localhost;Database=UnitTest;Uid=argozhang123;Pwd=argo@163.com;SslMode=none;";
config["ConnectionStrings:ba"] = "";
cates = await client.GetStringAsync("/Healths");
Assert.Contains("TotalDuration", cates);
}
}
public class HealthCheckStartup
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="configuration"></param>
public HealthCheckStartup(IConfiguration configuration)
{
Configuration = configuration;
}
/// <summary>
/// 获得 系统配置项 Iconfiguration 实例
/// </summary>
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddCacheManager();
services.AddConfigurationManager();
services.AddDbAdapter();
var builder = services.AddHealthChecks();
builder.AddCheck<DBHealthCheck>("db");
services.AddMvcCore();
2019-08-14 11:05:59 +08:00
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.Use(async (context, next) =>
{
context.User = new System.Security.Claims.ClaimsPrincipal(new System.Security.Principal.GenericIdentity("Argo"));
await next();
});
app.UseHealthChecks("/Healths", new HealthCheckOptions()
{
ResponseWriter = (context, report) =>
{
context.Response.ContentType = "application/json";
return context.Response.WriteAsync(JsonConvert.SerializeObject(new { report.Entries.Keys, Report = report }));
},
ResultStatusCodes =
{
[HealthStatus.Healthy] = StatusCodes.Status200OK,
[HealthStatus.Degraded] = StatusCodes.Status200OK,
[HealthStatus.Unhealthy] = StatusCodes.Status200OK
}
});
app.UseMvcWithDefaultRoute();
}
}
/// <summary>
///
/// </summary>
public class HealthCheckWebHost<TStartup> : WebApplicationFactory<TStartup> where TStartup : class
{
2019-09-02 23:13:57 +08:00
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
base.ConfigureWebHost(builder);
builder.UseContentRoot(TestHelper.RetrievePath($"UnitTest"));
}
/// <summary>
///
/// </summary>
/// <returns></returns>
protected override IWebHostBuilder CreateWebHostBuilder() => WebHost.CreateDefaultBuilder<TStartup>(null);
2019-08-14 11:05:59 +08:00
}
}