From 28247add3c8f75af2c9daa2e5c55854d94ed11b6 Mon Sep 17 00:00:00 2001 From: AndyButland Date: Mon, 5 Jun 2017 14:37:50 +0200 Subject: [PATCH] Created unit tests for health check results --- src/Umbraco.Tests/Umbraco.Tests.csproj | 1 + .../HealthChecks/HealthCheckResultsTests.cs | 121 ++++++++++++++++++ .../HealthCheck/HealthCheckResults.cs | 21 ++- 3 files changed, 131 insertions(+), 12 deletions(-) create mode 100644 src/Umbraco.Tests/Web/HealthChecks/HealthCheckResultsTests.cs diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index 3b5cd3d6eb..b0e6d55454 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -210,6 +210,7 @@ + diff --git a/src/Umbraco.Tests/Web/HealthChecks/HealthCheckResultsTests.cs b/src/Umbraco.Tests/Web/HealthChecks/HealthCheckResultsTests.cs new file mode 100644 index 0000000000..17a3c86baf --- /dev/null +++ b/src/Umbraco.Tests/Web/HealthChecks/HealthCheckResultsTests.cs @@ -0,0 +1,121 @@ +using System; +using System.Collections.Generic; +using Moq; +using NUnit.Framework; +using Umbraco.Core; +using Umbraco.Web.HealthCheck; + +namespace Umbraco.Tests.Web.HealthChecks +{ + [TestFixture] + public class HealthCheckResultsTests + { + [HealthCheck("CFD6FC34-59C9-4402-B55F-C8BC96B628A1", "Stub check")] + public abstract class StubHealthCheck : HealthCheck + { + private readonly string _message; + private readonly StatusResultType _resultType; + + public StubHealthCheck(StatusResultType resultType, string message) : base(null) + { + _resultType = resultType; + _message = message; + } + + public override HealthCheckStatus ExecuteAction(HealthCheckAction action) + { + throw new NotImplementedException(); + } + + public override IEnumerable GetStatus() + { + return new List + { + new HealthCheckStatus(_message) + { + ResultType = _resultType + } + }; + } + } + + [HealthCheck("CFD6FC34-59C9-4402-B55F-C8BC96B628A1", "Stub check 1")] + public class StubHealthCheck1 : StubHealthCheck + { + public StubHealthCheck1(StatusResultType resultType, string message) : base(resultType, message) + { + } + } + + [HealthCheck("CFD6FC34-59C9-4402-B55F-C8BC96B628A2", "Stub check 2")] + public class StubHealthCheck2 : StubHealthCheck + { + public StubHealthCheck2(StatusResultType resultType, string message) : base(resultType, message) + { + } + } + + [HealthCheck("CFD6FC34-59C9-4402-B55F-C8BC96B628A3", "Stub check 3")] + public class StubHealthCheck3 : StubHealthCheck + { + public StubHealthCheck3(StatusResultType resultType, string message) : base(resultType, message) + { + } + + public override IEnumerable GetStatus() + { + throw new Exception("Check threw exception"); + } + } + + [Test] + public void HealthCheckResults_WithSuccessfulChecks_ReturnsCorrectResultDescription() + { + var checks = new List + { + new StubHealthCheck1(StatusResultType.Success, "First check was successful"), + new StubHealthCheck2(StatusResultType.Success, "Second check was successful"), + }; + var results = new HealthCheckResults(checks); + Assert.IsTrue(results.AllChecksSuccessful); + + var resultAsMarkdown = results.ResultsAsMarkDown(); + Assert.IsTrue(resultAsMarkdown.IndexOf("Checks for 'Stub check 1' all completed succesfully.") > -1); + Assert.IsTrue(resultAsMarkdown.IndexOf("Checks for 'Stub check 2' all completed succesfully.") > -1); + } + + [Test] + public void HealthCheckResults_WithFailingChecks_ReturnsCorrectResultDescription() + { + var checks = new List + { + new StubHealthCheck1(StatusResultType.Success, "First check was successful"), + new StubHealthCheck2(StatusResultType.Error, "Second check was not successful"), + }; + var results = new HealthCheckResults(checks); + Assert.IsFalse(results.AllChecksSuccessful); + + var resultAsMarkdown = results.ResultsAsMarkDown(); + Assert.IsTrue(resultAsMarkdown.IndexOf("Checks for 'Stub check 1' all completed succesfully.") > -1); + Assert.IsTrue(resultAsMarkdown.IndexOf("Checks for 'Stub check 2' completed with errors.") > -1); + } + + [Test] + public void HealthCheckResults_WithErroringCheck_ReturnsCorrectResultDescription() + { + var checks = new List + { + new StubHealthCheck1(StatusResultType.Success, "First check was successful"), + new StubHealthCheck3(StatusResultType.Error, "Third check was not successful"), + new StubHealthCheck2(StatusResultType.Error, "Second check was not successful"), + }; + var results = new HealthCheckResults(checks); + Assert.IsFalse(results.AllChecksSuccessful); + + var resultAsMarkdown = results.ResultsAsMarkDown(); + Assert.IsTrue(resultAsMarkdown.IndexOf("Checks for 'Stub check 1' all completed succesfully.") > -1); + Assert.IsTrue(resultAsMarkdown.IndexOf("Checks for 'Stub check 2' completed with errors.") > -1); + Assert.IsTrue(resultAsMarkdown.IndexOf("Checks for 'Stub check 3' completed with errors.") > -1); + } + } +} diff --git a/src/Umbraco.Web/HealthCheck/HealthCheckResults.cs b/src/Umbraco.Web/HealthCheck/HealthCheckResults.cs index 0df0ba5fe8..b16d12d0fb 100644 --- a/src/Umbraco.Web/HealthCheck/HealthCheckResults.cs +++ b/src/Umbraco.Web/HealthCheck/HealthCheckResults.cs @@ -27,8 +27,6 @@ namespace Umbraco.Web.HealthCheck var message = string.Format("Health check failed with exception: {0}. See logs for details.", ex.Message); return new List { - - new HealthCheckStatus(message) { ResultType = StatusResultType.Error @@ -37,18 +35,17 @@ namespace Umbraco.Web.HealthCheck } }); - // find out if all checks pass or not - AllChecksSuccessful = true; - foreach (var result in _results) + // find out if all checks pass or not + AllChecksSuccessful = true; + foreach (var result in _results) + { + var checkIsSuccess = result.Value.All(x => x.ResultType == StatusResultType.Success); + if (checkIsSuccess == false) { - var checkIsSuccess = result.Value.All(x => x.ResultType == StatusResultType.Success); - if (checkIsSuccess == false) - { - AllChecksSuccessful = false; - break; - } + AllChecksSuccessful = false; + break; } - + } } internal void LogResults()