From 3fdc50b354a19b248e24e5c450877265f258e133 Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Thu, 22 Aug 2019 21:41:59 +0200 Subject: [PATCH] Fixed flaky test by flushing logs and getting them via the api --- src/NzbDrone.Api/Logs/LogFileModuleBase.cs | 5 +++- .../Client/ClientBase.cs | 11 +++++++-- .../Client/LogsClient.cs | 24 +++++++++++++++++++ .../HttpLogFixture.cs | 19 ++++++++------- .../IntegrationTestBase.cs | 2 ++ 5 files changed, 49 insertions(+), 12 deletions(-) create mode 100644 src/NzbDrone.Integration.Test/Client/LogsClient.cs diff --git a/src/NzbDrone.Api/Logs/LogFileModuleBase.cs b/src/NzbDrone.Api/Logs/LogFileModuleBase.cs index 53c8e57a3..8a4be2dcb 100644 --- a/src/NzbDrone.Api/Logs/LogFileModuleBase.cs +++ b/src/NzbDrone.Api/Logs/LogFileModuleBase.cs @@ -1,9 +1,10 @@ using System.Collections.Generic; using System.IO; using System.Linq; -using NzbDrone.Common.Disk; using Nancy; using Nancy.Responses; +using NLog; +using NzbDrone.Common.Disk; using NzbDrone.Core.Configuration; using Sonarr.Http; @@ -54,6 +55,8 @@ namespace NzbDrone.Api.Logs private Response GetLogFileResponse(string filename) { + LogManager.Flush(); + var filePath = GetLogFilePath(filename); if (!_diskProvider.FileExists(filePath)) diff --git a/src/NzbDrone.Integration.Test/Client/ClientBase.cs b/src/NzbDrone.Integration.Test/Client/ClientBase.cs index ca7dd71b7..d242662b4 100644 --- a/src/NzbDrone.Integration.Test/Client/ClientBase.cs +++ b/src/NzbDrone.Integration.Test/Client/ClientBase.cs @@ -40,7 +40,7 @@ namespace NzbDrone.Integration.Test.Client return request; } - public T Execute(IRestRequest request, HttpStatusCode statusCode) where T : class, new() + public string Execute(IRestRequest request, HttpStatusCode statusCode) { _logger.Info("{0}: {1}", request.Method, _restClient.BuildUri(request)); @@ -58,7 +58,14 @@ namespace NzbDrone.Integration.Test.Client response.StatusCode.Should().Be(statusCode); - return Json.Deserialize(response.Content); + return response.Content; + } + + public T Execute(IRestRequest request, HttpStatusCode statusCode) where T : class, new() + { + var content = Execute(request, statusCode); + + return Json.Deserialize(content); } private static void AssertDisableCache(IList headers) diff --git a/src/NzbDrone.Integration.Test/Client/LogsClient.cs b/src/NzbDrone.Integration.Test/Client/LogsClient.cs new file mode 100644 index 000000000..b64ec2971 --- /dev/null +++ b/src/NzbDrone.Integration.Test/Client/LogsClient.cs @@ -0,0 +1,24 @@ +using System; +using RestSharp; + +namespace NzbDrone.Integration.Test.Client +{ + public class LogsClient : ClientBase + { + public LogsClient(IRestClient restClient, string apiKey) + : base(restClient, apiKey, "log/file") + { + } + + public string[] GetLogFileLines(string filename) + { + var request = BuildRequest(filename); + var content = Execute(request, System.Net.HttpStatusCode.OK); + + var lines = content.Split('\n'); + lines = Array.ConvertAll(lines, s => s.TrimEnd('\r')); + Array.Resize(ref lines, lines.Length - 1); + return lines; + } + } +} \ No newline at end of file diff --git a/src/NzbDrone.Integration.Test/HttpLogFixture.cs b/src/NzbDrone.Integration.Test/HttpLogFixture.cs index 7db6a88b7..3b4ee7a88 100644 --- a/src/NzbDrone.Integration.Test/HttpLogFixture.cs +++ b/src/NzbDrone.Integration.Test/HttpLogFixture.cs @@ -1,4 +1,4 @@ -using System.IO; +using System; using System.Linq; using FluentAssertions; using NUnit.Framework; @@ -9,25 +9,26 @@ namespace NzbDrone.Integration.Test public class HttpLogFixture : IntegrationTest { [Test] - [Retry(5)] public void should_log_on_error() { var config = HostConfig.Get(1); config.LogLevel = "Trace"; HostConfig.Put(config); - + var resultGet = Series.All(); - var logFile = Path.Combine(_runner.AppData, "logs", "sonarr.trace.txt"); - var logLines = File.ReadAllLines(logFile); + var logFile = "sonarr.trace.txt"; + var logLines = Logs.GetLogFileLines(logFile); var resultPost = Series.InvalidPost(new Api.Series.SeriesResource()); - logLines = File.ReadAllLines(logFile).Skip(logLines.Length).ToArray(); + // Skip 2 and 1 to ignore the logs endpoint + logLines = Logs.GetLogFileLines(logFile).Skip(logLines.Length + 2).ToArray(); + Array.Resize(ref logLines, logLines.Length - 1); - logLines.Should().Contain(v => v.Contains("|Trace|Http|Req")); - logLines.Should().Contain(v => v.Contains("|Trace|Http|Res")); - logLines.Should().Contain(v => v.Contains("|Debug|Api|")); + logLines.Should().Contain(v => v.Contains("|Trace|Http|Req") && v.Contains("/api/series/")); + logLines.Should().Contain(v => v.Contains("|Trace|Http|Res") && v.Contains("/api/series/: 400.BadRequest")); + logLines.Should().Contain(v => v.Contains("|Debug|Api|") && v.Contains("/api/series/: 400.BadRequest")); } } } diff --git a/src/NzbDrone.Integration.Test/IntegrationTestBase.cs b/src/NzbDrone.Integration.Test/IntegrationTestBase.cs index a6a078ab9..8350d8d2d 100644 --- a/src/NzbDrone.Integration.Test/IntegrationTestBase.cs +++ b/src/NzbDrone.Integration.Test/IntegrationTestBase.cs @@ -45,6 +45,7 @@ namespace NzbDrone.Integration.Test public ClientBase History; public ClientBase HostConfig; public IndexerClient Indexers; + public LogsClient Logs; public ClientBase NamingConfig; public NotificationClient Notifications; public ClientBase Profiles; @@ -106,6 +107,7 @@ namespace NzbDrone.Integration.Test History = new ClientBase(RestClient, ApiKey); HostConfig = new ClientBase(RestClient, ApiKey, "config/host"); Indexers = new IndexerClient(RestClient, ApiKey); + Logs = new LogsClient(RestClient, ApiKey); NamingConfig = new ClientBase(RestClient, ApiKey, "config/naming"); Notifications = new NotificationClient(RestClient, ApiKey); Profiles = new ClientBase(RestClient, ApiKey);