From c1cf29757f3bf78a40d2b6e4c21126007773cfa1 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Wed, 26 Oct 2011 10:15:47 -0700 Subject: [PATCH] Unit test should now run in a machine with NzbDrone installed as a service with no side effects. --- NzbDrone.App.Test/NzbDrone.App.Test.csproj | 2 - NzbDrone.App.Test/ProcessProviderTests.cs | 57 ----------------- NzbDrone.App.Test/RouterTest.cs | 16 ++--- NzbDrone.App.Test/ServiceControllerTests.cs | 62 ------------------- .../ServiceControllerTests.cs | 29 ++++++--- NzbDrone.Common/ConsoleProvider.cs | 8 +-- NzbDrone.Common/ServiceProvider.cs | 18 +++--- .../UpdateProviderStartTest.cs | 4 +- NzbDrone.Update/Providers/UpdateProvider.cs | 4 +- NzbDrone/Router.cs | 8 +-- 10 files changed, 51 insertions(+), 157 deletions(-) delete mode 100644 NzbDrone.App.Test/ProcessProviderTests.cs delete mode 100644 NzbDrone.App.Test/ServiceControllerTests.cs diff --git a/NzbDrone.App.Test/NzbDrone.App.Test.csproj b/NzbDrone.App.Test/NzbDrone.App.Test.csproj index 1fdbe99ca..6a2d1a06b 100644 --- a/NzbDrone.App.Test/NzbDrone.App.Test.csproj +++ b/NzbDrone.App.Test/NzbDrone.App.Test.csproj @@ -69,8 +69,6 @@ - - diff --git a/NzbDrone.App.Test/ProcessProviderTests.cs b/NzbDrone.App.Test/ProcessProviderTests.cs deleted file mode 100644 index d22a86bf9..000000000 --- a/NzbDrone.App.Test/ProcessProviderTests.cs +++ /dev/null @@ -1,57 +0,0 @@ -using System.Diagnostics; -using FluentAssertions; -using NUnit.Framework; -using NzbDrone.Common; - -namespace NzbDrone.App.Test -{ - [TestFixture] - public class ProcessProviderTests - { - ProcessProvider _processProvider; - - - - [SetUp] - public void Setup() - { - _processProvider = new ProcessProvider(); - } - - [TestCase(0)] - [TestCase(123332324)] - public void Kill_should_not_fail_on_invalid_process_is(int processId) - { - _processProvider.Kill(processId); - } - - [Test] - public void GetById_should_return_null_if_process_doesnt_exist() - { - _processProvider.GetProcessById(1234567).Should().BeNull(); - } - - [Test] - public void Should_be_able_to_kill_procces() - { - var dummyProcess = StartDummyProcess(); - _processProvider.Kill(dummyProcess.Id); - dummyProcess.HasExited.Should().BeTrue(); - } - - [TestCase(0)] - [TestCase(-1)] - [TestCase(9999)] - public void GetProcessById_should_return_null_for_invalid_process(int processId) - { - _processProvider.GetProcessById(processId).Should().BeNull(); - } - - - public Process StartDummyProcess() - { - return Process.Start("NzbDrone.Test.Dummy.exe"); - } - - } -} diff --git a/NzbDrone.App.Test/RouterTest.cs b/NzbDrone.App.Test/RouterTest.cs index f48082ff9..f528e9494 100644 --- a/NzbDrone.App.Test/RouterTest.cs +++ b/NzbDrone.App.Test/RouterTest.cs @@ -47,13 +47,13 @@ namespace NzbDrone.App.Test { var mocker = new AutoMoqer(MockBehavior.Strict); var serviceProviderMock = mocker.GetMock(); - serviceProviderMock.Setup(c => c.Install()); - serviceProviderMock.Setup(c => c.ServiceExist(ServiceProvider.NzbDroneServiceName)).Returns(false); + serviceProviderMock.Setup(c => c.Install(ServiceProvider.NZBDRONE_SERVICE_NAME)); + serviceProviderMock.Setup(c => c.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME)).Returns(false); mocker.GetMock().SetupGet(c => c.IsUserInteractive).Returns(true); mocker.Resolve().Route(ApplicationMode.InstallService); - serviceProviderMock.Verify(c => c.Install(), Times.Once()); + serviceProviderMock.Verify(c => c.Install(ServiceProvider.NZBDRONE_SERVICE_NAME), Times.Once()); } @@ -62,13 +62,13 @@ namespace NzbDrone.App.Test { var mocker = new AutoMoqer(MockBehavior.Strict); var serviceProviderMock = mocker.GetMock(); - serviceProviderMock.Setup(c => c.UnInstall()); + serviceProviderMock.Setup(c => c.UnInstall(ServiceProvider.NZBDRONE_SERVICE_NAME)); mocker.GetMock().SetupGet(c => c.IsUserInteractive).Returns(true); - serviceProviderMock.Setup(c => c.ServiceExist(ServiceProvider.NzbDroneServiceName)).Returns(true); + serviceProviderMock.Setup(c => c.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME)).Returns(true); mocker.Resolve().Route(ApplicationMode.UninstallService); - serviceProviderMock.Verify(c => c.UnInstall(), Times.Once()); + serviceProviderMock.Verify(c => c.UnInstall(ServiceProvider.NZBDRONE_SERVICE_NAME), Times.Once()); } [Test] @@ -116,7 +116,7 @@ namespace NzbDrone.App.Test mocker.GetMock().SetupGet(c => c.IsUserInteractive).Returns(true); consoleMock.Setup(c => c.PrintServiceAlreadyExist()); - serviceMock.Setup(c => c.ServiceExist(ServiceProvider.NzbDroneServiceName)).Returns(true); + serviceMock.Setup(c => c.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME)).Returns(true); mocker.Resolve().Route(ApplicationMode.InstallService); @@ -132,7 +132,7 @@ namespace NzbDrone.App.Test mocker.GetMock().SetupGet(c => c.IsUserInteractive).Returns(true); consoleMock.Setup(c => c.PrintServiceDoestExist()); - serviceMock.Setup(c => c.ServiceExist(ServiceProvider.NzbDroneServiceName)).Returns(false); + serviceMock.Setup(c => c.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME)).Returns(false); mocker.Resolve().Route(ApplicationMode.UninstallService); diff --git a/NzbDrone.App.Test/ServiceControllerTests.cs b/NzbDrone.App.Test/ServiceControllerTests.cs deleted file mode 100644 index 73f24cb35..000000000 --- a/NzbDrone.App.Test/ServiceControllerTests.cs +++ /dev/null @@ -1,62 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using FluentAssertions; -using NUnit.Framework; -using NzbDrone.Common; -using NzbDrone.Providers; - -namespace NzbDrone.App.Test -{ - [TestFixture] - public class ServiceControllerTests - { - [Test] - public void Exists_should_find_spooler_service() - { - var serviceController = new ServiceProvider(); - - //Act - var exists = serviceController.ServiceExist("spooler"); - - exists.Should().BeTrue(); - } - - [Test] - public void Exists_should_not_find_random_service() - { - var serviceController = new ServiceProvider(); - - //Act - var exists = serviceController.ServiceExist("random_service_name"); - - exists.Should().BeFalse(); - } - - - [Test] - public void Service_should_be_installed_and_then_uninstalled() - { - var serviceController = new ServiceProvider(); - - //Act - serviceController.ServiceExist(ServiceProvider.NzbDroneServiceName).Should().BeFalse("Service already installed"); - serviceController.Install(); - serviceController.ServiceExist(ServiceProvider.NzbDroneServiceName).Should().BeTrue(); - serviceController.UnInstall(); - serviceController.ServiceExist(ServiceProvider.NzbDroneServiceName).Should().BeFalse(); - } - - [Test] - [Explicit] - public void UnInstallService() - { - var serviceController = new ServiceProvider(); - - //Act - serviceController.UnInstall(); - serviceController.ServiceExist(ServiceProvider.NzbDroneServiceName).Should().BeFalse(); - } - } -} diff --git a/NzbDrone.Common.Test/ServiceControllerTests.cs b/NzbDrone.Common.Test/ServiceControllerTests.cs index 90595b837..20e64fa61 100644 --- a/NzbDrone.Common.Test/ServiceControllerTests.cs +++ b/NzbDrone.Common.Test/ServiceControllerTests.cs @@ -10,6 +10,7 @@ namespace NzbDrone.Common.Test public class ServiceControllerTests { private const string ALWAYS_INSTALLED_SERVICE = "SCardSvr"; //Smart Card + private const string TEMP_SERVICE_NAME = "NzbDrone_Nunit"; //Smart Card private ServiceProvider serviceProvider; @@ -17,6 +18,20 @@ namespace NzbDrone.Common.Test public void Setup() { serviceProvider = new ServiceProvider(); + + if (serviceProvider.ServiceExist(TEMP_SERVICE_NAME)) + { + serviceProvider.UnInstall(TEMP_SERVICE_NAME); + } + } + + [TearDown] + public void TearDown() + { + if (serviceProvider.ServiceExist(TEMP_SERVICE_NAME)) + { + serviceProvider.UnInstall(TEMP_SERVICE_NAME); + } } [Test] @@ -42,11 +57,11 @@ namespace NzbDrone.Common.Test public void Service_should_be_installed_and_then_uninstalled() { //Act - serviceProvider.ServiceExist(ServiceProvider.NzbDroneServiceName).Should().BeFalse("Service already installed"); - serviceProvider.Install(); - serviceProvider.ServiceExist(ServiceProvider.NzbDroneServiceName).Should().BeTrue(); - serviceProvider.UnInstall(); - serviceProvider.ServiceExist(ServiceProvider.NzbDroneServiceName).Should().BeFalse(); + serviceProvider.ServiceExist(TEMP_SERVICE_NAME).Should().BeFalse("Service already installed"); + serviceProvider.Install(TEMP_SERVICE_NAME); + serviceProvider.ServiceExist(TEMP_SERVICE_NAME).Should().BeTrue(); + serviceProvider.UnInstall(TEMP_SERVICE_NAME); + serviceProvider.ServiceExist(TEMP_SERVICE_NAME).Should().BeFalse(); } [Test] @@ -54,8 +69,8 @@ namespace NzbDrone.Common.Test public void UnInstallService() { //Act - serviceProvider.UnInstall(); - serviceProvider.ServiceExist(ServiceProvider.NzbDroneServiceName).Should().BeFalse(); + serviceProvider.UnInstall(ServiceProvider.NZBDRONE_SERVICE_NAME); + serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME).Should().BeFalse(); } [Test] diff --git a/NzbDrone.Common/ConsoleProvider.cs b/NzbDrone.Common/ConsoleProvider.cs index 1f31d6e1d..b942e612c 100644 --- a/NzbDrone.Common/ConsoleProvider.cs +++ b/NzbDrone.Common/ConsoleProvider.cs @@ -18,19 +18,19 @@ namespace NzbDrone.Common Console.WriteLine(); Console.WriteLine(" Usage: {0} ", Process.GetCurrentProcess().MainModule.ModuleName); Console.WriteLine(" Commands:"); - Console.WriteLine(" /i Install the application as a Windows Service ({0}).", ServiceProvider.NzbDroneServiceName); - Console.WriteLine(" /u Uninstall already installed Windows Service ({0}).", ServiceProvider.NzbDroneServiceName); + Console.WriteLine(" /i Install the application as a Windows Service ({0}).", ServiceProvider.NZBDRONE_SERVICE_NAME); + Console.WriteLine(" /u Uninstall already installed Windows Service ({0}).", ServiceProvider.NZBDRONE_SERVICE_NAME); Console.WriteLine(" Run application in console mode."); } public virtual void PrintServiceAlreadyExist() { - Console.WriteLine("A service with the same name ({0}) already exists. Aborting installation", ServiceProvider.NzbDroneServiceName); + Console.WriteLine("A service with the same name ({0}) already exists. Aborting installation", ServiceProvider.NZBDRONE_SERVICE_NAME); } public virtual void PrintServiceDoestExist() { - Console.WriteLine("Can't find service ({0})", ServiceProvider.NzbDroneServiceName); + Console.WriteLine("Can't find service ({0})", ServiceProvider.NZBDRONE_SERVICE_NAME); } } } \ No newline at end of file diff --git a/NzbDrone.Common/ServiceProvider.cs b/NzbDrone.Common/ServiceProvider.cs index f1076e6e6..bf22ca256 100644 --- a/NzbDrone.Common/ServiceProvider.cs +++ b/NzbDrone.Common/ServiceProvider.cs @@ -10,7 +10,7 @@ namespace NzbDrone.Common { public class ServiceProvider { - public const string NzbDroneServiceName = "NzbDrone"; + public const string NZBDRONE_SERVICE_NAME = "NzbDrone"; private static readonly Logger Logger = LogManager.GetLogger("Host.ServiceManager"); @@ -23,9 +23,9 @@ namespace NzbDrone.Common } - public virtual void Install() + public virtual void Install(string serviceName) { - Logger.Info("Installing service '{0}'", NzbDroneServiceName); + Logger.Info("Installing service '{0}'", serviceName); var installer = new ServiceProcessInstaller @@ -40,8 +40,8 @@ namespace NzbDrone.Common var context = new InstallContext("service_install.log", cmdline); serviceInstaller.Context = context; - serviceInstaller.DisplayName = NzbDroneServiceName; - serviceInstaller.ServiceName = NzbDroneServiceName; + serviceInstaller.DisplayName = serviceName; + serviceInstaller.ServiceName = serviceName; serviceInstaller.Description = "NzbDrone Application Server"; serviceInstaller.StartType = ServiceStartMode.Automatic; @@ -52,17 +52,17 @@ namespace NzbDrone.Common Logger.Info("Service Has installed successfully."); } - public virtual void UnInstall() + public virtual void UnInstall(string serviceName) { - Logger.Info("Uninstalling NzbDrone service"); + Logger.Info("Uninstalling {0} service", serviceName); var serviceInstaller = new ServiceInstaller(); var context = new InstallContext("service_uninstall.log", null); serviceInstaller.Context = context; - serviceInstaller.ServiceName = NzbDroneServiceName; + serviceInstaller.ServiceName = serviceName; serviceInstaller.Uninstall(null); - Logger.Info("NzbDrone successfully uninstalled"); + Logger.Info("{0} successfully uninstalled", serviceName); } diff --git a/NzbDrone.Update.Test/UpdateProviderStartTest.cs b/NzbDrone.Update.Test/UpdateProviderStartTest.cs index 46c37a111..920f89a2f 100644 --- a/NzbDrone.Update.Test/UpdateProviderStartTest.cs +++ b/NzbDrone.Update.Test/UpdateProviderStartTest.cs @@ -23,14 +23,14 @@ namespace NzbDrone.Update.Test public void should_stop_nzbdrone_service_if_installed() { mocker.GetMock() - .Setup(c => c.ServiceExist(ServiceProvider.NzbDroneServiceName)) + .Setup(c => c.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME)) .Returns(true); //Act mocker.Resolve().Start(null); //Assert - mocker.GetMock().Verify(c => c.Stop(ServiceProvider.NzbDroneServiceName), Times.Once()); + mocker.GetMock().Verify(c => c.Stop(ServiceProvider.NZBDRONE_SERVICE_NAME), Times.Once()); mocker.VerifyAllMocks(); } diff --git a/NzbDrone.Update/Providers/UpdateProvider.cs b/NzbDrone.Update/Providers/UpdateProvider.cs index 41cfae5e0..1cc962ce9 100644 --- a/NzbDrone.Update/Providers/UpdateProvider.cs +++ b/NzbDrone.Update/Providers/UpdateProvider.cs @@ -44,9 +44,9 @@ namespace NzbDrone.Update.Providers public void Start(string installationFolder) { Logger.Info("Stopping all running services"); - if (_serviceProvider.ServiceExist(ServiceProvider.NzbDroneServiceName)) + if (_serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME)) { - _serviceProvider.Stop(ServiceProvider.NzbDroneServiceName); + _serviceProvider.Stop(ServiceProvider.NZBDRONE_SERVICE_NAME); } Logger.Info("Killing all running processes"); diff --git a/NzbDrone/Router.cs b/NzbDrone/Router.cs index a61b818e4..1b8902670 100644 --- a/NzbDrone/Router.cs +++ b/NzbDrone/Router.cs @@ -50,25 +50,25 @@ namespace NzbDrone } case ApplicationMode.InstallService: { - if (_serviceProvider.ServiceExist(ServiceProvider.NzbDroneServiceName)) + if (_serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME)) { _consoleProvider.PrintServiceAlreadyExist(); } else { - _serviceProvider.Install(); + _serviceProvider.Install(ServiceProvider.NZBDRONE_SERVICE_NAME); } break; } case ApplicationMode.UninstallService: { - if (!_serviceProvider.ServiceExist(ServiceProvider.NzbDroneServiceName)) + if (!_serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME)) { _consoleProvider.PrintServiceDoestExist(); } else { - _serviceProvider.UnInstall(); + _serviceProvider.UnInstall(ServiceProvider.NZBDRONE_SERVICE_NAME); } break;