diff --git a/NzbDrone.Common.Test/ProcessProviderTests.cs b/NzbDrone.Common.Test/ProcessProviderTests.cs index b3cc05b14..a30cb00a0 100644 --- a/NzbDrone.Common.Test/ProcessProviderTests.cs +++ b/NzbDrone.Common.Test/ProcessProviderTests.cs @@ -1,4 +1,5 @@ -using System.Diagnostics; +// ReSharper disable InconsistentNaming +using System.Diagnostics; using System.Linq; using FluentAssertions; using NUnit.Framework; diff --git a/NzbDrone.Common/ConsoleProvider.cs b/NzbDrone.Common/ConsoleProvider.cs index aaf72a702..1f31d6e1d 100644 --- a/NzbDrone.Common/ConsoleProvider.cs +++ b/NzbDrone.Common/ConsoleProvider.cs @@ -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); - } } } \ No newline at end of file diff --git a/NzbDrone.Common/ProcessProvider.cs b/NzbDrone.Common/ProcessProvider.cs index 94fded59f..8ed58d95c 100644 --- a/NzbDrone.Common/ProcessProvider.cs +++ b/NzbDrone.Common/ProcessProvider.cs @@ -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() { diff --git a/NzbDrone.Update.Test/NzbDrone.Update.Test.csproj b/NzbDrone.Update.Test/NzbDrone.Update.Test.csproj index 1f4961b90..e8de74c36 100644 --- a/NzbDrone.Update.Test/NzbDrone.Update.Test.csproj +++ b/NzbDrone.Update.Test/NzbDrone.Update.Test.csproj @@ -38,6 +38,9 @@ + + ..\packages\NBuilder.3.0.1\lib\FizzWare.NBuilder.dll + ..\packages\FluentAssertions.1.5.0.0\Lib\.NetFramework 4.0\FluentAssertions.dll @@ -75,7 +78,8 @@ - + + diff --git a/NzbDrone.Update.Test/UpdateProviderTest.cs b/NzbDrone.Update.Test/UpdateProviderStartTest.cs similarity index 51% rename from NzbDrone.Update.Test/UpdateProviderTest.cs rename to NzbDrone.Update.Test/UpdateProviderStartTest.cs index 5bb4e84aa..68cb95fe5 100644 --- a/NzbDrone.Update.Test/UpdateProviderTest.cs +++ b/NzbDrone.Update.Test/UpdateProviderStartTest.cs @@ -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() - .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() - .Setup(c => c.FolderExists(sandboxFolder)). - Returns(false); - - mocker.GetMock(MockBehavior.Strict); - - //Act - mocker.Resolve().Start(); - - //Assert - mocker.GetMock().Verify(c => c.UpdateFolderDoestExist(sandboxFolder), Times.Once()); - mocker.VerifyAllMocks(); } [Test] public void should_stop_nzbdrone_service_if_installed() { - mocker.GetMock() - .Setup(c => c.FolderExists(It.IsAny())). - Returns(true); - mocker.GetMock() .Setup(c => c.ServiceExist(ServiceProvider.NzbDroneServiceName)) .Returns(true); - + //Act - mocker.Resolve().Start(); + mocker.Resolve().Start(null); //Assert mocker.GetMock().Verify(c => c.Stop(ServiceProvider.NzbDroneServiceName), Times.Once()); mocker.VerifyAllMocks(); } + + [Test] + public void should_kill_nzbdrone_process_if_running() + { + var proccesses = Builder.CreateListOfSize(2).Build(); + + mocker.GetMock() + .Setup(c => c.GetProcessByName(ProcessProvider.NzbDroneProccessName)) + .Returns(proccesses); + + //Act + mocker.Resolve().Start(null); + + //Assert + mocker.GetMock().Verify(c => c.Kill(proccesses[0].Id), Times.Once()); + mocker.GetMock().Verify(c => c.Kill(proccesses[1].Id), Times.Once()); + mocker.VerifyAllMocks(); + } } } diff --git a/NzbDrone.Update.Test/UpdateProviderVerifyTest.cs b/NzbDrone.Update.Test/UpdateProviderVerifyTest.cs new file mode 100644 index 000000000..0abb6d51a --- /dev/null +++ b/NzbDrone.Update.Test/UpdateProviderVerifyTest.cs @@ -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() + .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(() => mocker.Resolve().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(() => mocker.Resolve().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() + .Setup(c => c.FolderExists(targetFolder)) + .Returns(true); + + mocker.GetMock() + .Setup(c => c.FolderExists(sandboxFolder)) + .Returns(false); + + Assert.Throws(() => mocker.Resolve().Verify(targetFolder)) + .Message.Should().StartWith("Update folder doesn't exist"); + } + } +} diff --git a/NzbDrone.Update.Test/packages.config b/NzbDrone.Update.Test/packages.config index 6617e506c..0132851a6 100644 --- a/NzbDrone.Update.Test/packages.config +++ b/NzbDrone.Update.Test/packages.config @@ -3,6 +3,7 @@ + \ No newline at end of file diff --git a/NzbDrone.Update/Providers/UpdateProvider.cs b/NzbDrone.Update/Providers/UpdateProvider.cs index 6dbec1410..a19a20bc1 100644 --- a/NzbDrone.Update/Providers/UpdateProvider.cs +++ b/NzbDrone.Update/Providers/UpdateProvider.cs @@ -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