Co-Authored-By: Mark McDowall <markus101@users.noreply.github.com>pull/2/head
parent
f39824cb9c
commit
3766e0eba9
@ -0,0 +1,49 @@
|
|||||||
|
using System;
|
||||||
|
using System.Text;
|
||||||
|
using Moq;
|
||||||
|
using NUnit.Framework;
|
||||||
|
using NzbDrone.Common.Cloud;
|
||||||
|
using NzbDrone.Common.Http;
|
||||||
|
using NzbDrone.Common.Serializer;
|
||||||
|
using NzbDrone.Core.HealthCheck.Checks;
|
||||||
|
using NzbDrone.Core.Test.Framework;
|
||||||
|
using NzbDrone.Test.Common;
|
||||||
|
|
||||||
|
namespace NzbDrone.Core.Test.HealthCheck.Checks
|
||||||
|
{
|
||||||
|
[TestFixture]
|
||||||
|
public class SystemTimeCheckFixture : CoreTest<SystemTimeCheck>
|
||||||
|
{
|
||||||
|
[SetUp]
|
||||||
|
public void Setup()
|
||||||
|
{
|
||||||
|
Mocker.SetConstant<IRadarrCloudRequestBuilder>(new RadarrCloudRequestBuilder());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenServerTime(DateTime dateTime)
|
||||||
|
{
|
||||||
|
var json = new ServiceTimeResponse { DateTimeUtc = dateTime }.ToJson();
|
||||||
|
|
||||||
|
Mocker.GetMock<IHttpClient>()
|
||||||
|
.Setup(s => s.Execute(It.IsAny<HttpRequest>()))
|
||||||
|
.Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), Encoding.ASCII.GetBytes(json)));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void should_not_return_error_when_system_time_is_close_to_server_time()
|
||||||
|
{
|
||||||
|
GivenServerTime(DateTime.UtcNow);
|
||||||
|
|
||||||
|
Subject.Check().ShouldBeOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void should_return_error_when_system_time_is_more_than_one_day_from_server_time()
|
||||||
|
{
|
||||||
|
GivenServerTime(DateTime.UtcNow.AddDays(2));
|
||||||
|
|
||||||
|
Subject.Check().ShouldBeError();
|
||||||
|
ExceptionVerification.ExpectedErrors(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,49 @@
|
|||||||
|
using System;
|
||||||
|
using NLog;
|
||||||
|
using NzbDrone.Common.Cloud;
|
||||||
|
using NzbDrone.Common.Http;
|
||||||
|
using NzbDrone.Common.Serializer;
|
||||||
|
using NzbDrone.Core.Localization;
|
||||||
|
|
||||||
|
namespace NzbDrone.Core.HealthCheck.Checks
|
||||||
|
{
|
||||||
|
public class SystemTimeCheck : HealthCheckBase
|
||||||
|
{
|
||||||
|
private readonly IHttpClient _client;
|
||||||
|
private readonly IHttpRequestBuilderFactory _cloudRequestBuilder;
|
||||||
|
private readonly Logger _logger;
|
||||||
|
|
||||||
|
public SystemTimeCheck(IHttpClient client, IRadarrCloudRequestBuilder cloudRequestBuilder, ILocalizationService localizationService, Logger logger)
|
||||||
|
: base(localizationService)
|
||||||
|
{
|
||||||
|
_client = client;
|
||||||
|
_cloudRequestBuilder = cloudRequestBuilder.Services;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override HealthCheck Check()
|
||||||
|
{
|
||||||
|
var request = _cloudRequestBuilder.Create()
|
||||||
|
.Resource("/time")
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
var response = _client.Execute(request);
|
||||||
|
var result = Json.Deserialize<ServiceTimeResponse>(response.Content);
|
||||||
|
var systemTime = DateTime.UtcNow;
|
||||||
|
|
||||||
|
// +/- more than 1 day
|
||||||
|
if (Math.Abs(result.DateTimeUtc.Subtract(systemTime).TotalDays) >= 1)
|
||||||
|
{
|
||||||
|
_logger.Error("System time mismatch. SystemTime: {0} Expected Time: {1}. Update system time", systemTime, result.DateTimeUtc);
|
||||||
|
return new HealthCheck(GetType(), HealthCheckResult.Error, _localizationService.GetLocalizedString("SystemTimeCheckMessage"));
|
||||||
|
}
|
||||||
|
|
||||||
|
return new HealthCheck(GetType());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ServiceTimeResponse
|
||||||
|
{
|
||||||
|
public DateTime DateTimeUtc { get; set; }
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue