More work on NzbDrone.Update

pull/6/head
kay.one 13 years ago
parent f9a316f632
commit aa418a444b

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

@ -32,10 +32,5 @@ namespace NzbDrone.Common
{ {
Console.WriteLine("Can't find service ({0})", ServiceProvider.NzbDroneServiceName); 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);
}
} }
} }

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

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

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

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

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

@ -1,4 +1,6 @@
using System.IO; using System;
using System.IO;
using System.Linq;
using NLog; using NLog;
using NzbDrone.Common; using NzbDrone.Common;
@ -10,36 +12,52 @@ namespace NzbDrone.Update.Providers
private readonly EnviromentProvider _enviromentProvider; private readonly EnviromentProvider _enviromentProvider;
private readonly ConsoleProvider _consoleProvider; private readonly ConsoleProvider _consoleProvider;
private readonly ServiceProvider _serviceProvider; private readonly ServiceProvider _serviceProvider;
private readonly ProcessProvider _processProvider;
private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
public UpdateProvider(DiskProvider diskProvider, EnviromentProvider enviromentProvider, ConsoleProvider consoleProvider, public UpdateProvider(DiskProvider diskProvider, EnviromentProvider enviromentProvider, ConsoleProvider consoleProvider,
ServiceProvider serviceProvider) ServiceProvider serviceProvider, ProcessProvider processProvider)
{ {
_diskProvider = diskProvider; _diskProvider = diskProvider;
_enviromentProvider = enviromentProvider; _enviromentProvider = enviromentProvider;
_consoleProvider = consoleProvider; _consoleProvider = consoleProvider;
_serviceProvider = serviceProvider; _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"); var sandboxFolder = Path.Combine(_enviromentProvider.StartUpPath, "nzbdrone_update");
Logger.Info("Looking for update package at {0}", sandboxFolder);
Logger.Info("Verifying Update Folder"); Logger.Info("Verifying Update Folder");
if (!_diskProvider.FolderExists(sandboxFolder)) if (!_diskProvider.FolderExists(sandboxFolder))
{ throw new DirectoryNotFoundException("Update folder doesn't exist" + sandboxFolder);
Logger.Error("Update folder doesn't exist {0}", sandboxFolder);
_consoleProvider.UpdateFolderDoestExist(sandboxFolder);
return;
}
}
public void Start(string installationFolder)
{
Logger.Info("Stopping all running services");
if (_serviceProvider.ServiceExist(ServiceProvider.NzbDroneServiceName)) if (_serviceProvider.ServiceExist(ServiceProvider.NzbDroneServiceName))
{ {
_serviceProvider.Stop(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 //Create backup of current folder

Loading…
Cancel
Save