Fixed flaky test by flushing logs and getting them via the api

This commit is contained in:
Taloth Saldono 2019-08-22 21:41:59 +02:00
parent 7eeff32185
commit 3fdc50b354
5 changed files with 49 additions and 12 deletions

View File

@ -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))

View File

@ -40,7 +40,7 @@ namespace NzbDrone.Integration.Test.Client
return request;
}
public T Execute<T>(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<T>(response.Content);
return response.Content;
}
public T Execute<T>(IRestRequest request, HttpStatusCode statusCode) where T : class, new()
{
var content = Execute(request, statusCode);
return Json.Deserialize<T>(content);
}
private static void AssertDisableCache(IList<Parameter> headers)

View File

@ -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;
}
}
}

View File

@ -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"));
}
}
}

View File

@ -45,6 +45,7 @@ namespace NzbDrone.Integration.Test
public ClientBase<HistoryResource> History;
public ClientBase<HostConfigResource> HostConfig;
public IndexerClient Indexers;
public LogsClient Logs;
public ClientBase<NamingConfigResource> NamingConfig;
public NotificationClient Notifications;
public ClientBase<ProfileResource> Profiles;
@ -106,6 +107,7 @@ namespace NzbDrone.Integration.Test
History = new ClientBase<HistoryResource>(RestClient, ApiKey);
HostConfig = new ClientBase<HostConfigResource>(RestClient, ApiKey, "config/host");
Indexers = new IndexerClient(RestClient, ApiKey);
Logs = new LogsClient(RestClient, ApiKey);
NamingConfig = new ClientBase<NamingConfigResource>(RestClient, ApiKey, "config/naming");
Notifications = new NotificationClient(RestClient, ApiKey);
Profiles = new ClientBase<ProfileResource>(RestClient, ApiKey);