-
Notifications
You must be signed in to change notification settings - Fork 800
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
|
||
namespace HealthChecks.System; | ||
|
||
public class FileHealthCheck : IHealthCheck | ||
{ | ||
private readonly FileHealthCheckOptions _fileOptions; | ||
|
||
public FileHealthCheck(FileHealthCheckOptions fileOptions) | ||
{ | ||
_fileOptions = fileOptions; | ||
} | ||
|
||
public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) | ||
{ | ||
try | ||
{ | ||
List<string> errorList = new(); | ||
foreach (string file in _fileOptions.Files) | ||
{ | ||
if (!string.IsNullOrEmpty(file)) | ||
{ | ||
if (!File.Exists(file)) | ||
{ | ||
errorList.Add($"File {file} does not exist."); | ||
if (!_fileOptions.CheckAllFiles) | ||
{ | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return Task.FromResult(errorList.GetHealthState(context)); | ||
} | ||
catch (Exception ex) | ||
{ | ||
return Task.FromResult(new HealthCheckResult(context.Registration.FailureStatus, exception: ex)); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace HealthChecks.System; | ||
|
||
public class FileHealthCheckOptions | ||
{ | ||
public List<string> Files { get; } = new(); | ||
public bool CheckAllFiles { get; set; } | ||
|
||
public FileHealthCheckOptions AddFile(string file) | ||
{ | ||
Files.Add(file); | ||
return this; | ||
} | ||
|
||
public FileHealthCheckOptions WithCheckAllFiles() | ||
{ | ||
CheckAllFiles = true; | ||
return this; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System.Net; | ||
|
||
namespace HealthChecks.System.Tests.Functional; | ||
|
||
public class file_healthcheck_should | ||
{ | ||
[Fact] | ||
public async Task be_healthy_if_file_is_available() | ||
{ | ||
var webHostBuilder = new WebHostBuilder() | ||
.ConfigureServices(services => | ||
{ | ||
services.AddHealthChecks() | ||
.AddFile(setup => setup.AddFile(Path.Combine(Directory.GetCurrentDirectory(), "HealthChecks.System.Tests.dll")), tags: new string[] { "file" }); | ||
}) | ||
.Configure(app => | ||
{ | ||
app.UseHealthChecks("/health", new HealthCheckOptions | ||
{ | ||
Predicate = r => r.Tags.Contains("file") | ||
}); | ||
}); | ||
|
||
using var server = new TestServer(webHostBuilder); | ||
|
||
using var response = await server.CreateRequest("/health").GetAsync().ConfigureAwait(false); | ||
Check failure on line 26 in test/HealthChecks.System.Tests/Functional/FileHealthCheckTests.cs GitHub Actions / build / build
Check failure on line 26 in test/HealthChecks.System.Tests/Functional/FileHealthCheckTests.cs GitHub Actions / build / build
Check failure on line 26 in test/HealthChecks.System.Tests/Functional/FileHealthCheckTests.cs GitHub Actions / build / build
|
||
|
||
response.StatusCode.ShouldBe(HttpStatusCode.OK); | ||
} | ||
|
||
[Fact] | ||
public async Task be_healthy_if_no_file_provided() | ||
{ | ||
var webHostBuilder = new WebHostBuilder() | ||
.ConfigureServices(services => | ||
{ | ||
services.AddHealthChecks() | ||
.AddFile(setup => | ||
{ | ||
}, tags: new string[] { "file" }); | ||
}) | ||
.Configure(app => | ||
{ | ||
app.UseHealthChecks("/health", new HealthCheckOptions | ||
{ | ||
Predicate = r => r.Tags.Contains("file") | ||
}); | ||
}); | ||
|
||
using var server = new TestServer(webHostBuilder); | ||
|
||
using var response = await server.CreateRequest("/health").GetAsync().ConfigureAwait(false); | ||
Check failure on line 52 in test/HealthChecks.System.Tests/Functional/FileHealthCheckTests.cs GitHub Actions / build / build
Check failure on line 52 in test/HealthChecks.System.Tests/Functional/FileHealthCheckTests.cs GitHub Actions / build / build
Check failure on line 52 in test/HealthChecks.System.Tests/Functional/FileHealthCheckTests.cs GitHub Actions / build / build
|
||
|
||
response.StatusCode.ShouldBe(HttpStatusCode.OK); | ||
} | ||
|
||
[Fact] | ||
public async Task be_unhealthy_if_file_is_not_available() | ||
{ | ||
var webHostBuilder = new WebHostBuilder() | ||
.ConfigureServices(services => | ||
{ | ||
services.AddHealthChecks() | ||
.AddFile(setup => setup.AddFile($"{Directory.GetCurrentDirectory()}/non-existing-file"), tags: new string[] { "file" }); | ||
}) | ||
.Configure(app => | ||
{ | ||
app.UseHealthChecks("/health", new HealthCheckOptions | ||
{ | ||
Predicate = r => r.Tags.Contains("file") | ||
}); | ||
}); | ||
|
||
using var server = new TestServer(webHostBuilder); | ||
|
||
using var response = await server.CreateRequest("/health").GetAsync().ConfigureAwait(false); | ||
Check failure on line 76 in test/HealthChecks.System.Tests/Functional/FileHealthCheckTests.cs GitHub Actions / build / build
Check failure on line 76 in test/HealthChecks.System.Tests/Functional/FileHealthCheckTests.cs GitHub Actions / build / build
Check failure on line 76 in test/HealthChecks.System.Tests/Functional/FileHealthCheckTests.cs GitHub Actions / build / build
|
||
|
||
response.StatusCode.ShouldBe(HttpStatusCode.ServiceUnavailable); | ||
} | ||
} |