1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2026-04-26 22:56:23 -04:00

Fixed several tests and test infrastructure issues

This commit is contained in:
Taloth Saldono
2019-08-16 22:04:34 +02:00
parent ef6a648189
commit de31dfb11e
10 changed files with 120 additions and 58 deletions
+34 -30
View File
@@ -9,6 +9,7 @@ using NLog;
using NUnit.Framework;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Processes;
using NzbDrone.Core.Configuration;
using RestSharp;
namespace NzbDrone.Test.Common
@@ -30,8 +31,11 @@ namespace NzbDrone.Test.Common
public void Start()
{
AppData = Path.Combine(TestContext.CurrentContext.TestDirectory, "_intg_" + DateTime.Now.Ticks);
AppData = Path.Combine(TestContext.CurrentContext.TestDirectory, "_intg_" + TestBase.GetUID());
Directory.CreateDirectory(AppData);
GenerateApiKey();
var sonarrConsoleExe = OsInfo.IsWindows ? "Sonarr.Console.exe" : "Sonarr.exe";
if (BuildInfo.IsDebug)
@@ -52,8 +56,6 @@ namespace NzbDrone.Test.Common
Assert.Fail("Process has exited");
}
SetApiKey();
var request = new RestRequest("system/status");
request.AddHeader("Authorization", ApiKey);
request.AddHeader("X-Api-Key", ApiKey);
@@ -74,13 +76,23 @@ namespace NzbDrone.Test.Common
public void KillAll()
{
if (_nzbDroneProcess != null)
try
{
_processProvider.Kill(_nzbDroneProcess.Id);
if (_nzbDroneProcess != null)
{
_processProvider.Kill(_nzbDroneProcess.Id);
}
_processProvider.KillAll(ProcessProvider.SONARR_CONSOLE_PROCESS_NAME);
_processProvider.KillAll(ProcessProvider.SONARR_PROCESS_NAME);
}
catch (InvalidOperationException)
{
// May happen if the process closes while being closed
}
_processProvider.KillAll(ProcessProvider.SONARR_CONSOLE_PROCESS_NAME);
_processProvider.KillAll(ProcessProvider.SONARR_PROCESS_NAME);
TestBase.DeleteTempFolder(AppData);
}
private void Start(string outputNzbdroneConsoleExe)
@@ -100,33 +112,25 @@ namespace NzbDrone.Test.Common
}
}
private void SetApiKey()
private void GenerateApiKey()
{
var configFile = Path.Combine(AppData, "config.xml");
var attempts = 0;
while (ApiKey == null && attempts < 50)
{
try
{
if (File.Exists(configFile))
{
var apiKeyElement = XDocument.Load(configFile)
.XPathSelectElement("Config/ApiKey");
if (apiKeyElement != null)
{
ApiKey = apiKeyElement.Value;
}
}
}
catch (XmlException ex)
{
Console.WriteLine("Error getting API Key from XML file: " + ex.Message, ex);
}
// Generate and set the api key so we don't have to poll the config file
var apiKey = Guid.NewGuid().ToString().Replace("-", "");
attempts++;
Thread.Sleep(1000);
}
var xDoc = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XElement(ConfigFileProvider.CONFIG_ELEMENT_NAME,
new XElement(nameof(ConfigFileProvider.ApiKey), apiKey)
)
);
var data = xDoc.ToString();
File.WriteAllText(configFile, data);
ApiKey = apiKey;
}
}
}
+35 -6
View File
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using FluentAssertions;
@@ -43,8 +44,8 @@ namespace NzbDrone.Test.Common
public abstract class TestBase : LoggingTest
{
private static readonly Random _random = new Random();
private static int _nextUid;
private AutoMoqer _mocker;
protected AutoMoqer Mocker
@@ -84,7 +85,21 @@ namespace NzbDrone.Test.Common
}
}
protected string TempFolder { get; private set; }
private string _tempFolder;
protected string TempFolder
{
get
{
if (_tempFolder == null)
{
_tempFolder = Path.Combine(TestContext.CurrentContext.TestDirectory, "_temp_" + GetUID());
Directory.CreateDirectory(_tempFolder);
}
return _tempFolder;
}
}
[SetUp]
public void TestBaseSetup()
@@ -93,9 +108,7 @@ namespace NzbDrone.Test.Common
LogManager.ReconfigExistingLoggers();
TempFolder = Path.Combine(TestContext.CurrentContext.TestDirectory, "_temp_" + DateTime.Now.Ticks);
Directory.CreateDirectory(TempFolder);
_tempFolder = null;
}
[TearDown]
@@ -103,9 +116,25 @@ namespace NzbDrone.Test.Common
{
_mocker = null;
DeleteTempFolder(_tempFolder);
}
public static string GetUID()
{
return Process.GetCurrentProcess().Id + "_" + DateTime.Now.Ticks + "_" + Interlocked.Increment(ref _nextUid);
}
public static void DeleteTempFolder(string folder)
{
if (folder == null)
{
return;
}
try
{
var tempFolder = new DirectoryInfo(TempFolder);
var tempFolder = new DirectoryInfo(folder);
if (tempFolder.Exists)
{
foreach (var file in tempFolder.GetFiles("*", SearchOption.AllDirectories))