Exemplo n.º 1
0
        public void ExceptionThrownInHealthCheckImplementationIsReportedAsFailedHealthCheckResult()
        {
            IHealthCheck         firstHealthCheck     = m_Repository.StrictMock <IHealthCheck>();
            HealthCheckException healthCheckException = m_Repository.StrictMock <HealthCheckException>();

            using (m_Repository.Record())
            {
                Expect.Call(firstHealthCheck.Execute()).Throw(healthCheckException);
                Expect.Call(healthCheckException.Message).Return("Sample Exception");
                m_Repository.ReplayAll();

                HealthCheckResultCollection resultCollection =
                    HealthCheckRunner.RunHealthChecks(new HealthChecklistCollection(new[] { firstHealthCheck }));

                Assert.AreEqual(1, resultCollection.Count);

                IHealthCheckResult result = resultCollection[0];

                Assert.IsFalse(result.Passed);
                StringAssert.StartsWith(result.Message,
                                        "An exception was thrown during the execution of the Health Check");
                StringAssert.Contains(result.Message, "Sample Exception");

                m_Repository.VerifyAll();
            }
        }
        public void Constructor2_SetsProperties()
        {
            var healthChecks            = new[] { new Mock <IHealthCheck>().Object, new Mock <IHealthCheck>().Object };
            var name                    = "test-name";
            var description             = "fake";
            var serviceId               = "2.4.6";
            var version                 = "1.2.3";
            var releaseId               = "4.5.6";
            var responseCustomizer      = new Mock <IHealthResponseCustomizer>().Object;
            var contentType             = "application/fake+json";
            var passStatusCode          = 299;
            var warnStatusCode          = 399;
            var failStatusCode          = 599;
            var uncaughtExceptionStatus = HealthStatus.Fail;

            var runner = new HealthCheckRunner(healthChecks, name, description, serviceId, version, releaseId,
                                               responseCustomizer, contentType, passStatusCode, warnStatusCode, failStatusCode, uncaughtExceptionStatus);

            runner.HealthChecks.Should().BeEquivalentTo(healthChecks);
            runner.Name.Should().Be(name);
            runner.Description.Should().Be(description);
            runner.ServiceId.Should().Be(serviceId);
            runner.Version.Should().Be(version);
            runner.ReleaseId.Should().Be(releaseId);
            runner.ResponseCustomizer.Should().BeSameAs(responseCustomizer);
            runner.ContentType.Should().Be(contentType);
            runner.PassStatusCode.Should().Be(passStatusCode);
            runner.WarnStatusCode.Should().Be(warnStatusCode);
            runner.FailStatusCode.Should().Be(failStatusCode);
            runner.UncaughtExceptionStatus.Should().Be(uncaughtExceptionStatus);
        }
        public async Task RunAsync_GivenAResponseCustomizerThatThrows_DoesNotThrow()
        {
            var mockHealthCheck1 = new Mock <IHealthCheck>();
            var result1          = new HealthCheckResult();
            IReadOnlyList <HealthCheckResult> results1 = new[] { result1 };

            mockHealthCheck1.Setup(m => m.CheckAsync(It.IsAny <CancellationToken>())).Returns(Task.FromResult(results1));

            var mockHealthCheck2 = new Mock <IHealthCheck>();
            var result2          = new HealthCheckResult();
            IReadOnlyList <HealthCheckResult> results2 = new[] { result2 };

            mockHealthCheck2.Setup(m => m.CheckAsync(It.IsAny <CancellationToken>())).Returns(Task.FromResult(results2));

            var mockResponseCustomizer = new Mock <IHealthResponseCustomizer>();
            var exception = new Exception();

            mockResponseCustomizer.Setup(m => m.CustomizeResponse(It.IsAny <HealthResponse>())).Throws(exception);

            var healthChecks       = new[] { mockHealthCheck1.Object, mockHealthCheck2.Object };
            var responseCustomizer = mockResponseCustomizer.Object;

            var runner = new HealthCheckRunner(healthChecks, responseCustomizer: responseCustomizer);

            var response = await runner.RunAsync();

            response.GetChecks().Should().BeEquivalentTo(new[] { result1, result2 });

            mockResponseCustomizer.Verify(m => m.CustomizeResponse(It.Is <HealthResponse>(responseParam => ReferenceEquals(response, responseParam))), Times.Once);

            mockHealthCheck1.Verify(m => m.CheckAsync(It.IsAny <CancellationToken>()), Times.Once);
            mockHealthCheck2.Verify(m => m.CheckAsync(It.IsAny <CancellationToken>()), Times.Once);
        }
        public async Task RunAsync_GivenNoResponseCustomizer_DoesNotThrow()
        {
            var mockHealthCheck1 = new Mock <IHealthCheck>();
            var result1          = new HealthCheckResult();
            IReadOnlyList <HealthCheckResult> results1 = new[] { result1 };

            mockHealthCheck1.Setup(m => m.CheckAsync(It.IsAny <CancellationToken>())).Returns(Task.FromResult(results1));

            var mockHealthCheck2 = new Mock <IHealthCheck>();
            var result2          = new HealthCheckResult();
            IReadOnlyList <HealthCheckResult> results2 = new[] { result2 };

            mockHealthCheck2.Setup(m => m.CheckAsync(It.IsAny <CancellationToken>())).Returns(Task.FromResult(results2));

            var healthChecks = new[] { mockHealthCheck1.Object, mockHealthCheck2.Object };

            var runner = new HealthCheckRunner(healthChecks);

            var response = await runner.RunAsync();

            response.GetChecks().Should().BeEquivalentTo(new[] { result1, result2 });

            mockHealthCheck1.Verify(m => m.CheckAsync(It.IsAny <CancellationToken>()), Times.Once);
            mockHealthCheck2.Verify(m => m.CheckAsync(It.IsAny <CancellationToken>()), Times.Once);
        }
        public HttpResponseMessage GetState()
        {
            try
            {
                log.Debug("GetState: Get state of host");

                return(HealthCheckRunner.ServerStatusIsOk()
                    ? ResultHelper.Success(HealthCheckResource.HostWorks)
                    : ResultHelper.Error(HealthCheckResource.HostError));
            }
            catch (Exception ex)
            {
                log.ErrorFormat("Unexpected error on GetState: {0} {1}", ex.ToString(), ex.InnerException != null ? ex.InnerException.Message : string.Empty);
                return(ResultHelper.ServerError());
            }
        }
Exemplo n.º 6
0
 public static void StopService()
 {
     try
     {
         HealthCheckRunner.Stop();
         if (webApiHost != null)
         {
             webApiHost.Dispose();
             log.DebugFormat("StopService: HealthCheckService");
             webApiHost = null;
         }
     }
     catch (Exception ex)
     {
         log.Error("Error while stopping the service", ex);
     }
 }
Exemplo n.º 7
0
        public static void StartService()
        {
            DBResourceManager.PatchAssemblies();
            var url = HealthCheckCfgSectionHandler.Instance.Url;

            log.DebugFormat("StartSevice: RunHealthCheckService. url: {0}", url);
            webApiHost = WebApp.Start <Startup>(url);

            /* Hack for mono because first requests can failed.
             * https://bugzilla.xamarin.com/show_bug.cgi?format=multiple&id=17965
             */
            var warmingUpper = new WarmingUpper();

            warmingUpper.WarmingUp(url);

            HealthCheckRunner.Run();
        }
        public HttpResponseMessage RefreshLicense()
        {
            try
            {
                log.Debug("ChangeLicenseStatus");
                LicenseReader.RefreshLicense();
                if (!ServiceRepository.GetServicesSnapshot().Any())
                {
                    HealthCheckRunner.Run();
                }

                return(ResultHelper.Success());
            }
            catch (Exception ex)
            {
                log.ErrorFormat("Unexpected error on RefreshLicense: {0} {1}", ex.ToString(), ex.InnerException != null ? ex.InnerException.Message : string.Empty);
                return(ResultHelper.ServerError());
            }
        }
Exemplo n.º 9
0
        public void HealthCheckRunnerExecutesListOfHealthChecksAndAllSucceed()
        {
            IHealthCheck firstHealthCheck  = m_Repository.StrictMock <IHealthCheck>();
            IHealthCheck secondHealthCheck = m_Repository.StrictMock <IHealthCheck>();
            IHealthCheck thirdHealthCheck  = m_Repository.StrictMock <IHealthCheck>();

            IHealthCheckResult firstHealthCheckResult  = m_Repository.StrictMock <IHealthCheckResult>();
            IHealthCheckResult secondHealthCheckResult = m_Repository.StrictMock <IHealthCheckResult>();
            IHealthCheckResult thirdHealthCheckResult  = m_Repository.StrictMock <IHealthCheckResult>();

            using (m_Repository.Record())
            {
                Expect.Call(firstHealthCheck.Execute()).Return(firstHealthCheckResult);
                Expect.Call(secondHealthCheck.Execute()).Return(secondHealthCheckResult);
                Expect.Call(thirdHealthCheck.Execute()).Return(thirdHealthCheckResult);

                Expect.Call(firstHealthCheckResult.Passed).Return(true);
                Expect.Call(firstHealthCheckResult.Message).Return("The Health Check was successful.");

                Expect.Call(secondHealthCheckResult.Passed).Return(true);
                Expect.Call(secondHealthCheckResult.Message).Return("The Health Check was successful.");

                Expect.Call(thirdHealthCheckResult.Passed).Return(true);
                Expect.Call(thirdHealthCheckResult.Message).Return("The Health Check was successful.");

                m_Repository.ReplayAll();

                HealthCheckResultCollection resultCollection =
                    HealthCheckRunner.RunHealthChecks(
                        new HealthChecklistCollection(new[] { firstHealthCheck, secondHealthCheck, thirdHealthCheck }));

                Assert.AreEqual(3, resultCollection.Count);
                foreach (IHealthCheckResult healthCheckResult in resultCollection)
                {
                    Assert.IsTrue(healthCheckResult.Passed);
                    StringAssert.Contains(healthCheckResult.Message, "success");
                }

                m_Repository.VerifyAll();
            }
        }