This repository has been archived by the owner on Apr 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add MySQL liveness * Add MySQL service to appveyor
- Loading branch information
1 parent
0d5e2ac
commit 02f95a7
Showing
12 changed files
with
179 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>$(NetStandardTargetVersion)</TargetFramework> | ||
<PackageLicenseUrl>$(PackageLicenseUrl)</PackageLicenseUrl> | ||
<PackageProjectUrl>$(PackageProjectUrl)</PackageProjectUrl> | ||
<Description>BeatPulse.MySql is the health check package for MySQL on BeatPulse </Description> | ||
<PackageTags>BeatPulse;HealthCheck;Beat;Health;MySQL</PackageTags> | ||
<Version>$(Version)</Version> | ||
<RepositoryUrl>$(RepositoryUrl)</RepositoryUrl> | ||
<Company>$(Company)</Company> | ||
<Authors>$(Authors)</Authors> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> | ||
<LangVersion>latest</LangVersion> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="MySql.Data" Version="$(MySqlDataVersion)" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\BeatPulse\BeatPulse.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using BeatPulse.Core; | ||
using BeatPulse.MySql; | ||
|
||
namespace BeatPulse | ||
{ | ||
public static class BeatPulseContextExtensions | ||
{ | ||
public static BeatPulseContext AddMySql(this BeatPulseContext context, string connectionString) | ||
{ | ||
context.Add(new MySqlLiveness(connectionString)); | ||
return context; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using BeatPulse.Core; | ||
using Microsoft.AspNetCore.Http; | ||
using MySql.Data.MySqlClient; | ||
using System; | ||
using System.Threading; | ||
|
||
namespace BeatPulse.MySql | ||
{ | ||
public class MySqlLiveness : IBeatPulseLiveness | ||
{ | ||
private readonly string connectionString; | ||
|
||
public string Name => nameof(MySqlLiveness); | ||
|
||
public string DefaultPath => "mysql"; | ||
|
||
public MySqlLiveness(string connectionString) | ||
{ | ||
this.connectionString = connectionString; | ||
} | ||
|
||
public async System.Threading.Tasks.Task<(string, bool)> IsHealthy(HttpContext context, bool isDevelopment, CancellationToken cancellationToken = default) | ||
{ | ||
try | ||
{ | ||
using (var connection = new MySqlConnection(connectionString)) | ||
{ | ||
await connection.OpenAsync(cancellationToken); | ||
return (BeatPulseKeys.BEATPULSE_HEALTHCHECK_DEFAULT_OK_MESSAGE, true); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
var message = !isDevelopment ? string.Format(BeatPulseKeys.BEATPULSE_HEALTHCHECK_DEFAULT_ERROR_MESSAGE, Name) | ||
: $"Exception {ex.GetType().Name} with message ('{ex.Message}')"; | ||
|
||
return (message, false); | ||
} | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
tests/FunctionalTests/BeatPulse.MySql/MySqlLivenessTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using FunctionalTests.Base; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
using BeatPulse; | ||
using Microsoft.AspNetCore.TestHost; | ||
using FluentAssertions; | ||
using System.Net; | ||
|
||
namespace FunctionalTests.BeatPulse.MySql | ||
{ | ||
[Collection("execution")] | ||
public class mysql_liveness_should | ||
{ | ||
private readonly ExecutionFixture _fixture; | ||
|
||
public mysql_liveness_should(ExecutionFixture fixture) | ||
{ | ||
_fixture = fixture ?? throw new ArgumentException(nameof(fixture)); | ||
} | ||
|
||
[Fact] | ||
public async Task be_healthy_when_mysql_server_is_available() | ||
{ | ||
var connectionString = "server=localhost;port=3306;database=information_schema;uid=root;password=Password12!"; | ||
|
||
var webHostBuilder = new WebHostBuilder() | ||
.UseStartup<DefaultStartup>() | ||
.UseBeatPulse() | ||
.ConfigureServices(services => | ||
{ | ||
services.AddBeatPulse(context => | ||
{ | ||
context.AddMySql(connectionString); | ||
}); | ||
}); | ||
|
||
var server = new TestServer(webHostBuilder); | ||
|
||
var response = await server.CreateRequest(BeatPulseKeys.BEATPULSE_DEFAULT_PATH) | ||
.GetAsync(); | ||
|
||
response.EnsureSuccessStatusCode(); | ||
} | ||
|
||
[Fact] | ||
public async Task be_unhealthy_when_mysql_server_is_unavailable() | ||
{ | ||
var connectionString = "server=255.255.255.255;port=3306;database=information_schema;uid=root;password=Password12!"; | ||
|
||
var webHostBuilder = new WebHostBuilder() | ||
.UseStartup<DefaultStartup>() | ||
.UseBeatPulse() | ||
.ConfigureServices(services => | ||
{ | ||
services.AddBeatPulse(context => | ||
{ | ||
context.AddMySql(connectionString); | ||
}); | ||
}); | ||
|
||
var server = new TestServer(webHostBuilder); | ||
|
||
var response = await server.CreateRequest(BeatPulseKeys.BEATPULSE_DEFAULT_PATH) | ||
.GetAsync(); | ||
|
||
response.StatusCode.Should().Be(HttpStatusCode.ServiceUnavailable); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters