Fixed several tests and test infrastructure issues

pull/3268/head
Taloth Saldono 5 years ago
parent ef6a648189
commit de31dfb11e

@ -248,12 +248,14 @@ namespace NzbDrone.Common.Test.DiskTests
} }
[Test] [Test]
[Ignore("No longer behaving this way in a Windows 10 Feature Update")]
public void should_not_be_able_to_rename_open_hardlinks_with_fileshare_none() public void should_not_be_able_to_rename_open_hardlinks_with_fileshare_none()
{ {
Assert.Throws<IOException>(() => DoHardLinkRename(FileShare.None)); Assert.Throws<IOException>(() => DoHardLinkRename(FileShare.None));
} }
[Test] [Test]
[Ignore("No longer behaving this way in a Windows 10 Feature Update")]
public void should_not_be_able_to_rename_open_hardlinks_with_fileshare_write() public void should_not_be_able_to_rename_open_hardlinks_with_fileshare_write()
{ {
Assert.Throws<IOException>(() => DoHardLinkRename(FileShare.Read)); Assert.Throws<IOException>(() => DoHardLinkRename(FileShare.Read));

@ -66,7 +66,7 @@ namespace NzbDrone.Common.Test
[Test] [Test]
public void Should_be_able_to_start_process() public void Should_be_able_to_start_process()
{ {
var process = Subject.Start(Path.Combine(Directory.GetCurrentDirectory(), DummyApp.DUMMY_PROCCESS_NAME + ".exe")); var process = StartDummyProcess();
Subject.Exists(DummyApp.DUMMY_PROCCESS_NAME).Should() Subject.Exists(DummyApp.DUMMY_PROCCESS_NAME).Should()
.BeTrue("excepted one dummy process to be already running"); .BeTrue("excepted one dummy process to be already running");
@ -141,7 +141,8 @@ namespace NzbDrone.Common.Test
private Process StartDummyProcess() private Process StartDummyProcess()
{ {
return Subject.Start(DummyApp.DUMMY_PROCCESS_NAME + ".exe"); var path = Path.Combine(TestContext.CurrentContext.TestDirectory, DummyApp.DUMMY_PROCCESS_NAME + ".exe");
return Subject.Start(path);
} }
[Test] [Test]

@ -1,4 +1,5 @@
using System; using System;
using System.Security.Principal;
using System.ServiceProcess; using System.ServiceProcess;
using FluentAssertions; using FluentAssertions;
using NUnit.Framework; using NUnit.Framework;
@ -61,6 +62,10 @@ namespace NzbDrone.Common.Test
[Test] [Test]
public void Service_should_be_installed_and_then_uninstalled() public void Service_should_be_installed_and_then_uninstalled()
{ {
if (!IsAnAdministrator())
{
Assert.Inconclusive("Can't run test without Administrator rights");
}
Subject.ServiceExist(TEMP_SERVICE_NAME).Should().BeFalse("Service already installed"); Subject.ServiceExist(TEMP_SERVICE_NAME).Should().BeFalse("Service already installed");
Subject.Install(TEMP_SERVICE_NAME); Subject.Install(TEMP_SERVICE_NAME);
@ -100,8 +105,13 @@ namespace NzbDrone.Common.Test
} }
[Test] [Test]
public void should_throw_if_starting_a_running_serivce() public void should_throw_if_starting_a_running_service()
{ {
if (!IsAnAdministrator())
{
Assert.Inconclusive("Can't run test without Administrator rights");
}
Subject.GetService(ALWAYS_INSTALLED_SERVICE).Status Subject.GetService(ALWAYS_INSTALLED_SERVICE).Status
.Should().NotBe(ServiceControllerStatus.Running); .Should().NotBe(ServiceControllerStatus.Running);
@ -127,5 +137,10 @@ namespace NzbDrone.Common.Test
ExceptionVerification.ExpectedWarns(1); ExceptionVerification.ExpectedWarns(1);
} }
private static bool IsAnAdministrator()
{
var principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
} }
} }

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data.SQLite;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using FluentMigrator.Runner; using FluentMigrator.Runner;
@ -115,21 +116,14 @@ namespace NzbDrone.Core.Test.Framework
[TearDown] [TearDown]
public void TearDown() public void TearDown()
{ {
if (TestFolderInfo != null && Directory.Exists(TestFolderInfo.AppDataFolder)) // Make sure there are no lingering connections. (When this happens it means we haven't disposed something properly)
GC.Collect();
GC.WaitForPendingFinalizers();
SQLiteConnection.ClearAllPools();
if (TestFolderInfo != null)
{ {
var files = Directory.GetFiles(TestFolderInfo.AppDataFolder); DeleteTempFolder(TestFolderInfo.AppDataFolder);
foreach (var file in files)
{
try
{
File.Delete(file);
}
catch (Exception)
{
}
}
} }
} }
} }

@ -15,6 +15,9 @@
<Link>Files\1024.png</Link> <Link>Files\1024.png</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>
<Reference Include="System.Data.SQLite">
<HintPath>..\Libraries\Sqlite\System.Data.SQLite.dll</HintPath>
</Reference>
<None Update="Files\**\*.*"> <None Update="Files\**\*.*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>

@ -50,6 +50,8 @@ namespace NzbDrone.Core.Datastore.Migration.Framework
{ {
runner.MigrateUp(true); runner.MigrateUp(true);
} }
processor.Dispose();
} }
catch (SQLiteException) catch (SQLiteException)
{ {
@ -57,7 +59,6 @@ namespace NzbDrone.Core.Datastore.Migration.Framework
SQLiteConnection.ClearAllPools(); SQLiteConnection.ClearAllPools();
throw; throw;
} }
sw.Stop(); sw.Stop();

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
@ -126,7 +127,7 @@ namespace NzbDrone.Integration.Test
[SetUp] [SetUp]
public void IntegrationSetUp() public void IntegrationSetUp()
{ {
TempDirectory = Path.Combine(TestContext.CurrentContext.TestDirectory, "_test_" + DateTime.UtcNow.Ticks); TempDirectory = Path.Combine(TestContext.CurrentContext.TestDirectory, "_test_" + Process.GetCurrentProcess().Id + "_" + DateTime.UtcNow.Ticks);
// Wait for things to get quiet, otherwise the previous test might influence the current one. // Wait for things to get quiet, otherwise the previous test might influence the current one.
Commands.WaitAll(); Commands.WaitAll();
@ -150,6 +151,17 @@ namespace NzbDrone.Integration.Test
_signalrConnection = null; _signalrConnection = null;
_signalRReceived = new List<SignalRMessage>(); _signalRReceived = new List<SignalRMessage>();
} }
if (Directory.Exists(TempDirectory))
{
try
{
Directory.Delete(TempDirectory, true);
}
catch
{
}
}
} }
public string GetTempDirectory(params string[] args) public string GetTempDirectory(params string[] args)

@ -9,6 +9,7 @@ using NLog;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Processes; using NzbDrone.Common.Processes;
using NzbDrone.Core.Configuration;
using RestSharp; using RestSharp;
namespace NzbDrone.Test.Common namespace NzbDrone.Test.Common
@ -30,8 +31,11 @@ namespace NzbDrone.Test.Common
public void Start() 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"; var sonarrConsoleExe = OsInfo.IsWindows ? "Sonarr.Console.exe" : "Sonarr.exe";
if (BuildInfo.IsDebug) if (BuildInfo.IsDebug)
@ -52,8 +56,6 @@ namespace NzbDrone.Test.Common
Assert.Fail("Process has exited"); Assert.Fail("Process has exited");
} }
SetApiKey();
var request = new RestRequest("system/status"); var request = new RestRequest("system/status");
request.AddHeader("Authorization", ApiKey); request.AddHeader("Authorization", ApiKey);
request.AddHeader("X-Api-Key", ApiKey); request.AddHeader("X-Api-Key", ApiKey);
@ -74,13 +76,23 @@ namespace NzbDrone.Test.Common
public void KillAll() public void KillAll()
{ {
if (_nzbDroneProcess != null) try
{
if (_nzbDroneProcess != null)
{
_processProvider.Kill(_nzbDroneProcess.Id);
}
_processProvider.KillAll(ProcessProvider.SONARR_CONSOLE_PROCESS_NAME);
_processProvider.KillAll(ProcessProvider.SONARR_PROCESS_NAME);
}
catch (InvalidOperationException)
{ {
_processProvider.Kill(_nzbDroneProcess.Id); // 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) 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 configFile = Path.Combine(AppData, "config.xml");
var attempts = 0;
while (ApiKey == null && attempts < 50) // Generate and set the api key so we don't have to poll the config file
{ var apiKey = Guid.NewGuid().ToString().Replace("-", "");
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);
}
attempts++; var xDoc = new XDocument(
Thread.Sleep(1000); 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;
} }
} }
} }

@ -1,4 +1,5 @@
using System; using System;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Threading; using System.Threading;
using FluentAssertions; using FluentAssertions;
@ -43,8 +44,8 @@ namespace NzbDrone.Test.Common
public abstract class TestBase : LoggingTest public abstract class TestBase : LoggingTest
{ {
private static readonly Random _random = new Random(); private static readonly Random _random = new Random();
private static int _nextUid;
private AutoMoqer _mocker; private AutoMoqer _mocker;
protected 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] [SetUp]
public void TestBaseSetup() public void TestBaseSetup()
@ -93,9 +108,7 @@ namespace NzbDrone.Test.Common
LogManager.ReconfigExistingLoggers(); LogManager.ReconfigExistingLoggers();
TempFolder = Path.Combine(TestContext.CurrentContext.TestDirectory, "_temp_" + DateTime.Now.Ticks); _tempFolder = null;
Directory.CreateDirectory(TempFolder);
} }
[TearDown] [TearDown]
@ -103,9 +116,25 @@ namespace NzbDrone.Test.Common
{ {
_mocker = null; _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 try
{ {
var tempFolder = new DirectoryInfo(TempFolder); var tempFolder = new DirectoryInfo(folder);
if (tempFolder.Exists) if (tempFolder.Exists)
{ {
foreach (var file in tempFolder.GetFiles("*", SearchOption.AllDirectories)) foreach (var file in tempFolder.GetFiles("*", SearchOption.AllDirectories))

@ -16,7 +16,7 @@ namespace NzbDrone.Update.Test
[Test] [Test]
public void should_start_service_if_app_type_was_serivce() public void should_start_service_if_app_type_was_serivce()
{ {
const string targetFolder = "c:\\Sonarr\\"; string targetFolder = "c:\\Sonarr\\".AsOsAgnostic();
Subject.Start(AppType.Service, targetFolder); Subject.Start(AppType.Service, targetFolder);
@ -26,13 +26,14 @@ namespace NzbDrone.Update.Test
[Test] [Test]
public void should_start_console_if_app_type_was_service_but_start_failed_because_of_permissions() public void should_start_console_if_app_type_was_service_but_start_failed_because_of_permissions()
{ {
const string targetFolder = "c:\\Sonarr\\"; string targetFolder = "c:\\Sonarr\\".AsOsAgnostic();
string targetProcess = "c:\\Sonarr\\Sonarr.Console.exe".AsOsAgnostic();
Mocker.GetMock<IServiceProvider>().Setup(c => c.Start(ServiceProvider.SERVICE_NAME)).Throws(new InvalidOperationException()); Mocker.GetMock<IServiceProvider>().Setup(c => c.Start(ServiceProvider.SERVICE_NAME)).Throws(new InvalidOperationException());
Subject.Start(AppType.Service, targetFolder); Subject.Start(AppType.Service, targetFolder);
Mocker.GetMock<IProcessProvider>().Verify(c => c.SpawnNewProcess("c:\\Sonarr\\Sonarr.Console.exe", "/" + StartupContext.NO_BROWSER, null, false), Times.Once()); Mocker.GetMock<IProcessProvider>().Verify(c => c.SpawnNewProcess(targetProcess, "/" + StartupContext.NO_BROWSER, null, false), Times.Once());
ExceptionVerification.ExpectedWarns(1); ExceptionVerification.ExpectedWarns(1);
} }

Loading…
Cancel
Save