More work on NzbDrone.Update

This commit is contained in:
kay.one 2011-10-23 11:31:17 -07:00
parent f9a316f632
commit aa418a444b
8 changed files with 123 additions and 46 deletions

View File

@ -1,4 +1,5 @@
using System.Diagnostics;
// ReSharper disable InconsistentNaming
using System.Diagnostics;
using System.Linq;
using FluentAssertions;
using NUnit.Framework;

View File

@ -32,10 +32,5 @@ namespace NzbDrone.Common
{
Console.WriteLine("Can't find service ({0})", ServiceProvider.NzbDroneServiceName);
}
public virtual void UpdateFolderDoestExist(string path)
{
Console.WriteLine("Unable to find update package in '{0}'", path);
}
}
}

View File

@ -10,6 +10,7 @@ namespace NzbDrone.Common
{
private static readonly Logger Logger = LogManager.GetLogger("Host.ProcessProvider");
public static readonly string NzbDroneProccessName = "NzbDrone";
public virtual ProcessInfo GetCurrentProcess()
{

View File

@ -38,6 +38,9 @@
<StartupObject />
</PropertyGroup>
<ItemGroup>
<Reference Include="FizzWare.NBuilder">
<HintPath>..\packages\NBuilder.3.0.1\lib\FizzWare.NBuilder.dll</HintPath>
</Reference>
<Reference Include="FluentAssertions">
<HintPath>..\packages\FluentAssertions.1.5.0.0\Lib\.NetFramework 4.0\FluentAssertions.dll</HintPath>
</Reference>
@ -75,7 +78,8 @@
<Compile Include="AutoMoq\AutoMoqerTest.cs" />
<Compile Include="AutoMoq\Unity\AutoMockingBuilderStrategy.cs" />
<Compile Include="AutoMoq\Unity\AutoMockingContainerExtension.cs" />
<Compile Include="UpdateProviderTest.cs" />
<Compile Include="UpdateProviderVerifyTest.cs" />
<Compile Include="UpdateProviderStartTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>

View File

@ -3,15 +3,17 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMoq;
using FizzWare.NBuilder;
using Moq;
using NUnit.Framework;
using NzbDrone.Common;
using NzbDrone.Common.Model;
using NzbDrone.Update.Providers;
namespace NzbDrone.Update.Test
{
[TestFixture]
class UpdateProviderTest
class UpdateProviderStartTest
{
AutoMoqer mocker = new AutoMoqer();
@ -19,47 +21,39 @@ namespace NzbDrone.Update.Test
public void Setup()
{
mocker = new AutoMoqer();
mocker.GetMock<EnviromentProvider>()
.Setup(c => c.StartUpPath).Returns(@"C:\Temp\NzbDrone_update\");
}
[Test]
public void update_should_abort_with_message_if_update_package_isnt_in_current_folder()
{
string sandboxFolder = @"C:\Temp\NzbDrone_update\nzbdrone_update";
mocker.GetMock<DiskProvider>()
.Setup(c => c.FolderExists(sandboxFolder)).
Returns(false);
mocker.GetMock<ServiceProvider>(MockBehavior.Strict);
//Act
mocker.Resolve<UpdateProvider>().Start();
//Assert
mocker.GetMock<ConsoleProvider>().Verify(c => c.UpdateFolderDoestExist(sandboxFolder), Times.Once());
mocker.VerifyAllMocks();
}
[Test]
public void should_stop_nzbdrone_service_if_installed()
{
mocker.GetMock<DiskProvider>()
.Setup(c => c.FolderExists(It.IsAny<string>())).
Returns(true);
mocker.GetMock<ServiceProvider>()
.Setup(c => c.ServiceExist(ServiceProvider.NzbDroneServiceName))
.Returns(true);
//Act
mocker.Resolve<UpdateProvider>().Start();
mocker.Resolve<UpdateProvider>().Start(null);
//Assert
mocker.GetMock<ServiceProvider>().Verify(c => c.Stop(ServiceProvider.NzbDroneServiceName), Times.Once());
mocker.VerifyAllMocks();
}
[Test]
public void should_kill_nzbdrone_process_if_running()
{
var proccesses = Builder<ProcessInfo>.CreateListOfSize(2).Build();
mocker.GetMock<ProcessProvider>()
.Setup(c => c.GetProcessByName(ProcessProvider.NzbDroneProccessName))
.Returns(proccesses);
//Act
mocker.Resolve<UpdateProvider>().Start(null);
//Assert
mocker.GetMock<ProcessProvider>().Verify(c => c.Kill(proccesses[0].Id), Times.Once());
mocker.GetMock<ProcessProvider>().Verify(c => c.Kill(proccesses[1].Id), Times.Once());
mocker.VerifyAllMocks();
}
}
}

View File

@ -0,0 +1,63 @@
// ReSharper disable InconsistentNaming
using System;
using System.IO;
using AutoMoq;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Common;
using NzbDrone.Update.Providers;
namespace NzbDrone.Update.Test
{
[TestFixture]
class UpdateProviderVerifyTest
{
AutoMoqer mocker = new AutoMoqer();
[SetUp]
public void Setup()
{
mocker = new AutoMoqer();
mocker.GetMock<EnviromentProvider>()
.Setup(c => c.StartUpPath).Returns(@"C:\Temp\NzbDrone_update\");
}
[TestCase(null)]
[TestCase("")]
[TestCase(" ")]
public void verify_should_throw_target_folder_is_blank(string target)
{
Assert.Throws<ArgumentException>(() => mocker.Resolve<UpdateProvider>().Verify(target))
.Message.Should().StartWith("Target folder can not be null or empty");
}
[Test]
public void verify_should_throw_if_target_folder_doesnt_exist()
{
string targetFolder = "c:\\NzbDrone\\";
Assert.Throws<DirectoryNotFoundException>(() => mocker.Resolve<UpdateProvider>().Verify(targetFolder))
.Message.Should().StartWith("Target folder doesn't exist");
}
[Test]
public void verify_should_throw_if_update_folder_doesnt_exist()
{
const string sandboxFolder = @"C:\Temp\NzbDrone_update\nzbdrone_update";
const string targetFolder = "c:\\NzbDrone\\";
mocker.GetMock<DiskProvider>()
.Setup(c => c.FolderExists(targetFolder))
.Returns(true);
mocker.GetMock<DiskProvider>()
.Setup(c => c.FolderExists(sandboxFolder))
.Returns(false);
Assert.Throws<DirectoryNotFoundException>(() => mocker.Resolve<UpdateProvider>().Verify(targetFolder))
.Message.Should().StartWith("Update folder doesn't exist");
}
}
}

View File

@ -3,6 +3,7 @@
<package id="CommonServiceLocator" version="1.0" />
<package id="FluentAssertions" version="1.5.0.0" />
<package id="Moq" version="4.0.10827" />
<package id="NBuilder" version="3.0.1" />
<package id="NUnit" version="2.5.10.11092" />
<package id="Unity" version="2.1.505.0" />
</packages>

View File

@ -1,4 +1,6 @@
using System.IO;
using System;
using System.IO;
using System.Linq;
using NLog;
using NzbDrone.Common;
@ -10,36 +12,52 @@ namespace NzbDrone.Update.Providers
private readonly EnviromentProvider _enviromentProvider;
private readonly ConsoleProvider _consoleProvider;
private readonly ServiceProvider _serviceProvider;
private readonly ProcessProvider _processProvider;
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
public UpdateProvider(DiskProvider diskProvider, EnviromentProvider enviromentProvider, ConsoleProvider consoleProvider,
ServiceProvider serviceProvider)
ServiceProvider serviceProvider, ProcessProvider processProvider)
{
_diskProvider = diskProvider;
_enviromentProvider = enviromentProvider;
_consoleProvider = consoleProvider;
_serviceProvider = serviceProvider;
_processProvider = processProvider;
}
public void Start()
public void Verify(string targetFolder)
{
Logger.Info("Verifying requirements before update...");
if (String.IsNullOrWhiteSpace(targetFolder))
throw new ArgumentException("Target folder can not be null or empty");
if (!_diskProvider.FolderExists(targetFolder))
throw new DirectoryNotFoundException("Target folder doesn't exist" + targetFolder);
var sandboxFolder = Path.Combine(_enviromentProvider.StartUpPath, "nzbdrone_update");
Logger.Info("Looking for update package at {0}", sandboxFolder);
Logger.Info("Verifying Update Folder");
if (!_diskProvider.FolderExists(sandboxFolder))
{
Logger.Error("Update folder doesn't exist {0}", sandboxFolder);
_consoleProvider.UpdateFolderDoestExist(sandboxFolder);
return;
throw new DirectoryNotFoundException("Update folder doesn't exist" + sandboxFolder);
}
public void Start(string installationFolder)
{
Logger.Info("Stopping all running services");
if (_serviceProvider.ServiceExist(ServiceProvider.NzbDroneServiceName))
{
_serviceProvider.Stop(ServiceProvider.NzbDroneServiceName);
}
//Stop NzbDrone Process
Logger.Info("Killing all running processes");
var processes = _processProvider.GetProcessByName(ProcessProvider.NzbDroneProccessName);
foreach (var processInfo in processes)
{
_processProvider.Kill(processInfo.Id);
}
//Create backup of current folder