2013-03-29 01:07:09 +03:00
|
|
|
|
2011-10-23 20:32:57 +03:00
|
|
|
|
|
|
|
using System.ServiceProcess;
|
|
|
|
using FluentAssertions;
|
|
|
|
using NUnit.Framework;
|
2011-11-13 21:16:31 +03:00
|
|
|
using NzbDrone.Test.Common;
|
2011-10-23 20:32:57 +03:00
|
|
|
|
|
|
|
namespace NzbDrone.Common.Test
|
|
|
|
{
|
|
|
|
[TestFixture]
|
2011-11-15 05:38:15 +03:00
|
|
|
public class ServiceProviderTests : TestBase
|
2011-10-23 20:32:57 +03:00
|
|
|
{
|
|
|
|
private const string ALWAYS_INSTALLED_SERVICE = "SCardSvr"; //Smart Card
|
2011-11-15 05:38:15 +03:00
|
|
|
private const string TEMP_SERVICE_NAME = "NzbDrone_Nunit";
|
2011-10-23 20:32:57 +03:00
|
|
|
private ServiceProvider serviceProvider;
|
|
|
|
|
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
public void Setup()
|
|
|
|
{
|
2013-02-18 07:13:23 +03:00
|
|
|
if(EnvironmentProvider.IsLinux)
|
|
|
|
{
|
|
|
|
throw new IgnoreException("Windows services aren't available in none-windows environments.");
|
|
|
|
}
|
|
|
|
|
2011-10-23 20:32:57 +03:00
|
|
|
serviceProvider = new ServiceProvider();
|
2011-10-26 20:15:47 +03:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2011-10-23 20:32:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void Exists_should_find_existing_service()
|
|
|
|
{
|
2013-03-29 01:07:09 +03:00
|
|
|
|
2011-10-23 20:32:57 +03:00
|
|
|
var exists = serviceProvider.ServiceExist(ALWAYS_INSTALLED_SERVICE);
|
|
|
|
|
|
|
|
exists.Should().BeTrue();
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void Exists_should_not_find_random_service()
|
|
|
|
{
|
2013-03-29 01:07:09 +03:00
|
|
|
|
2011-10-23 20:32:57 +03:00
|
|
|
var exists = serviceProvider.ServiceExist("random_service_name");
|
|
|
|
|
|
|
|
exists.Should().BeFalse();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void Service_should_be_installed_and_then_uninstalled()
|
|
|
|
{
|
2013-03-29 01:07:09 +03:00
|
|
|
|
2011-10-26 20:15:47 +03:00
|
|
|
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();
|
2012-01-17 10:12:22 +03:00
|
|
|
|
|
|
|
ExceptionVerification.ExpectedWarns(1);
|
2011-10-23 20:32:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
[Explicit]
|
|
|
|
public void UnInstallService()
|
|
|
|
{
|
2013-03-29 01:07:09 +03:00
|
|
|
|
2011-10-26 20:15:47 +03:00
|
|
|
serviceProvider.UnInstall(ServiceProvider.NZBDRONE_SERVICE_NAME);
|
|
|
|
serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME).Should().BeFalse();
|
2011-10-23 20:32:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2011-11-13 21:16:31 +03:00
|
|
|
//[Timeout(10000)]
|
2011-10-23 20:32:57 +03:00
|
|
|
public void Should_be_able_to_start_and_stop_service()
|
|
|
|
{
|
|
|
|
serviceProvider.GetService(ALWAYS_INSTALLED_SERVICE).Status
|
|
|
|
.Should().NotBe(ServiceControllerStatus.Running);
|
|
|
|
|
|
|
|
serviceProvider.Start(ALWAYS_INSTALLED_SERVICE);
|
|
|
|
|
|
|
|
serviceProvider.GetService(ALWAYS_INSTALLED_SERVICE).Status
|
|
|
|
.Should().Be(ServiceControllerStatus.Running);
|
|
|
|
|
|
|
|
serviceProvider.Stop(ALWAYS_INSTALLED_SERVICE);
|
|
|
|
|
|
|
|
serviceProvider.GetService(ALWAYS_INSTALLED_SERVICE).Status
|
|
|
|
.Should().Be(ServiceControllerStatus.Stopped);
|
|
|
|
}
|
2011-11-15 05:38:15 +03:00
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void Should_log_warn_if_on_stop_if_service_is_already_stopped()
|
|
|
|
{
|
|
|
|
serviceProvider.GetService(ALWAYS_INSTALLED_SERVICE).Status
|
|
|
|
.Should().NotBe(ServiceControllerStatus.Running);
|
|
|
|
|
2013-03-29 01:07:09 +03:00
|
|
|
|
2011-11-15 05:38:15 +03:00
|
|
|
serviceProvider.Stop(ALWAYS_INSTALLED_SERVICE);
|
|
|
|
|
2013-03-29 01:07:09 +03:00
|
|
|
|
2011-11-15 05:38:15 +03:00
|
|
|
serviceProvider.GetService(ALWAYS_INSTALLED_SERVICE).Status
|
|
|
|
.Should().Be(ServiceControllerStatus.Stopped);
|
|
|
|
|
2011-12-20 03:58:26 +03:00
|
|
|
ExceptionVerification.ExpectedWarns(1);
|
2011-11-15 05:38:15 +03:00
|
|
|
}
|
2011-10-23 20:32:57 +03:00
|
|
|
}
|
|
|
|
}
|