Lidarr/src/NzbDrone.Common.Test/ServiceProviderFixture.cs

148 lines
4.4 KiB
C#
Raw Normal View History

using System;
2019-08-22 20:15:25 +00:00
using System.Security.Principal;
using System.ServiceProcess;
2020-07-14 19:36:46 +00:00
using System.Threading;
using FluentAssertions;
using NUnit.Framework;
2016-04-02 02:19:32 +00:00
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Processes;
2011-11-13 18:16:31 +00:00
using NzbDrone.Test.Common;
2016-04-02 02:19:32 +00:00
using NzbDrone.Test.Common.Categories;
namespace NzbDrone.Common.Test
{
[TestFixture]
public class ServiceProviderFixture : TestBase<ServiceProvider>
{
private const string ALWAYS_INSTALLED_SERVICE = "SCardSvr"; // Smart Card
private const string TEMP_SERVICE_NAME = "NzbDrone_Nunit";
[SetUp]
public void Setup()
{
2013-04-30 03:09:50 +00:00
WindowsOnly();
Mocker.SetConstant<IProcessProvider>(Mocker.Resolve<ProcessProvider>());
CleanupService();
}
[TearDown]
public void TearDown()
{
2016-04-02 02:19:32 +00:00
if (OsInfo.IsWindows)
{
CleanupService();
}
}
private void CleanupService()
{
2013-04-30 03:09:50 +00:00
if (Subject.ServiceExist(TEMP_SERVICE_NAME))
{
Subject.Uninstall(TEMP_SERVICE_NAME);
}
if (Subject.IsServiceRunning(ALWAYS_INSTALLED_SERVICE))
{
Subject.Stop(ALWAYS_INSTALLED_SERVICE);
}
}
[Test]
public void Exists_should_find_existing_service()
{
2013-04-30 03:09:50 +00:00
Subject.ServiceExist(ALWAYS_INSTALLED_SERVICE).Should().BeTrue();
}
[Test]
public void Exists_should_not_find_random_service()
{
2013-04-30 03:09:50 +00:00
Subject.ServiceExist("random_service_name").Should().BeFalse();
}
[Test]
public void Service_should_be_installed_and_then_uninstalled()
{
2019-08-22 20:15:25 +00:00
if (!IsAnAdministrator())
{
Assert.Inconclusive("Can't run test without Administrator rights");
}
2013-04-30 03:09:50 +00:00
Subject.ServiceExist(TEMP_SERVICE_NAME).Should().BeFalse("Service already installed");
Subject.Install(TEMP_SERVICE_NAME);
Subject.ServiceExist(TEMP_SERVICE_NAME).Should().BeTrue();
Subject.Uninstall(TEMP_SERVICE_NAME);
2020-07-14 19:36:46 +00:00
Thread.Sleep(2000);
2013-04-30 03:09:50 +00:00
Subject.ServiceExist(TEMP_SERVICE_NAME).Should().BeFalse();
ExceptionVerification.ExpectedWarns(1);
}
[Test]
[Explicit]
2016-04-02 02:19:32 +00:00
[ManualTest]
public void UnInstallService()
{
Subject.Uninstall(ServiceProvider.SERVICE_NAME);
2017-09-27 02:06:05 +00:00
Subject.ServiceExist(ServiceProvider.SERVICE_NAME).Should().BeFalse();
}
[Test]
[Explicit]
2016-04-02 02:19:32 +00:00
[ManualTest]
public void Should_be_able_to_start_and_stop_service()
{
2013-04-30 03:09:50 +00:00
Subject.GetService(ALWAYS_INSTALLED_SERVICE).Status
.Should().NotBe(ServiceControllerStatus.Running);
2013-04-30 03:09:50 +00:00
Subject.Start(ALWAYS_INSTALLED_SERVICE);
2013-04-30 03:09:50 +00:00
Subject.GetService(ALWAYS_INSTALLED_SERVICE).Status
.Should().Be(ServiceControllerStatus.Running);
2013-04-30 03:09:50 +00:00
Subject.Stop(ALWAYS_INSTALLED_SERVICE);
2013-04-30 03:09:50 +00:00
Subject.GetService(ALWAYS_INSTALLED_SERVICE).Status
.Should().Be(ServiceControllerStatus.Stopped);
}
[Test]
2019-08-22 20:15:25 +00:00
public void should_throw_if_starting_a_running_service()
{
2019-08-22 20:15:25 +00:00
if (!IsAnAdministrator())
{
Assert.Inconclusive("Can't run test without Administrator rights");
}
Subject.GetService(ALWAYS_INSTALLED_SERVICE).Status
.Should().NotBe(ServiceControllerStatus.Running);
Subject.Start(ALWAYS_INSTALLED_SERVICE);
Assert.Throws<InvalidOperationException>(() => Subject.Start(ALWAYS_INSTALLED_SERVICE));
ExceptionVerification.ExpectedWarns(1);
}
[Test]
public void Should_log_warn_if_on_stop_if_service_is_already_stopped()
{
2013-04-30 03:09:50 +00:00
Subject.GetService(ALWAYS_INSTALLED_SERVICE).Status
.Should().NotBe(ServiceControllerStatus.Running);
2013-04-30 03:09:50 +00:00
Subject.Stop(ALWAYS_INSTALLED_SERVICE);
Subject.GetService(ALWAYS_INSTALLED_SERVICE).Status
.Should().Be(ServiceControllerStatus.Stopped);
ExceptionVerification.ExpectedWarns(1);
}
2019-08-22 20:15:25 +00:00
private static bool IsAnAdministrator()
{
var principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
}
}