diff --git a/source/Calamari.Tests/Fixtures/Deployment/DeployWindowsServiceAbstractFixture.cs b/source/Calamari.Tests/Fixtures/Deployment/DeployWindowsServiceAbstractFixture.cs index 8721b905a9..e77438ed04 100644 --- a/source/Calamari.Tests/Fixtures/Deployment/DeployWindowsServiceAbstractFixture.cs +++ b/source/Calamari.Tests/Fixtures/Deployment/DeployWindowsServiceAbstractFixture.cs @@ -6,6 +6,7 @@ using Calamari.Common.Plumbing.FileSystem; using Calamari.Common.Plumbing.Variables; using Calamari.Tests.Fixtures.Deployment.Packages; +using FluentAssertions; using NUnit.Framework; namespace Calamari.Tests.Fixtures.Deployment @@ -43,20 +44,38 @@ private void DeleteExistingService() } } + protected void RunDeploymentAndAssertRunningState(string startMode, string desiredStatus, ServiceControllerStatus serviceStatus) + { + SetupVariables(startMode, desiredStatus); + DeployAndAssert(serviceStatus, null); + } + protected void RunDeployment(Action extraAsserts = null) + { + SetupVariables("auto", null); + DeployAndAssert(ServiceControllerStatus.Running, extraAsserts); + } + + void SetupVariables(string startMode, string desiredStatus) { if (string.IsNullOrEmpty(Variables[KnownVariables.Package.EnabledFeatures])) Variables[KnownVariables.Package.EnabledFeatures] = "Octopus.Features.WindowsService"; Variables["Octopus.Action.WindowsService.CreateOrUpdateService"] = "True"; Variables["Octopus.Action.WindowsService.ServiceAccount"] = "_CUSTOM"; - Variables["Octopus.Action.WindowsService.StartMode"] = "auto"; + Variables["Octopus.Action.WindowsService.StartMode"] = startMode; + if (desiredStatus != null) + Variables["Octopus.Action.WindowsService.DesiredStatus"] = desiredStatus; + Variables["Octopus.Action.WindowsService.ServiceName"] = ServiceName; if (Variables["Octopus.Action.WindowsService.DisplayName"] == null) { Variables["Octopus.Action.WindowsService.DisplayName"] = ServiceName; } Variables["Octopus.Action.WindowsService.ExecutablePath"] = $"{PackageName}.exe"; + } + void DeployAndAssert(ServiceControllerStatus serviceStatus, Action extraAsserts) + { using (var file = new TemporaryFile(PackageBuilder.BuildSamplePackage(PackageName, "1.0.0"))) { var result = DeployPackage(file.FilePath); @@ -68,8 +87,8 @@ protected void RunDeployment(Action extraAsserts = null) using (var installedService = GetInstalledService()) { - Assert.NotNull(installedService, "Service is installed"); - Assert.AreEqual(ServiceControllerStatus.Running, installedService.Status); + installedService.Should().NotBeNull("Service {0} should be installed", ServiceName); + installedService.Status.Should().Be(serviceStatus); } extraAsserts?.Invoke(); diff --git a/source/Calamari.Tests/Fixtures/Deployment/DeployWindowsServiceRunningStateFixture.cs b/source/Calamari.Tests/Fixtures/Deployment/DeployWindowsServiceRunningStateFixture.cs new file mode 100644 index 0000000000..efdd0c0962 --- /dev/null +++ b/source/Calamari.Tests/Fixtures/Deployment/DeployWindowsServiceRunningStateFixture.cs @@ -0,0 +1,72 @@ +using System; +using System.IO; +using System.ServiceProcess; +using Calamari.Common.Plumbing.Variables; +using Calamari.Deployment; +using Calamari.Testing.Helpers; +using Calamari.Tests.Fixtures.Util; +using NUnit.Framework; + +namespace Calamari.Tests.Fixtures.Deployment +{ + [TestFixture] + [Category(TestCategory.CompatibleOS.OnlyWindows)] + public class DeployWindowsServiceRunningStateFixture : DeployWindowsServiceAbstractFixture + { + protected override string ServiceName => "RunningStateFixture"; + + [Test] + public void ShouldBeStoppedWhenStartModeIsUnchanged() + { + RunDeploymentAndAssertRunningState("unchanged", null, ServiceControllerStatus.Stopped); + } + + [Test] + public void ShouldBeRunningWhenStartModeIsAutoAndNoDesiredStatus() + { + RunDeploymentAndAssertRunningState("auto", null, ServiceControllerStatus.Running); + } + + [Test] + public void ShouldBeStoppedWhenStartModeIsDemandAndNoDesiredStatus() + { + RunDeploymentAndAssertRunningState("demand", null, ServiceControllerStatus.Stopped); + } + + [Test] + public void ShouldBeRunningWhenStartModeIsDemandAndDesiredStatusIsStarted() + { + //Setup service in stopped state + RunDeploymentAndAssertRunningState("demand", null, ServiceControllerStatus.Stopped); + + RunDeploymentAndAssertRunningState("demand", "Started", ServiceControllerStatus.Running); + } + + [Test] + public void ShouldBeStoppedWhenStartModeIsDemandAndDesiredStatusIsStopped() + { + //Setup service in running state + RunDeploymentAndAssertRunningState("demand", "Started", ServiceControllerStatus.Running); + + RunDeploymentAndAssertRunningState("demand", "Stopped", ServiceControllerStatus.Stopped); + } + + [Test] + public void ShouldBeStoppedWhenStartModeIsDemandAndDesiredStatusIsUnchangedAndServiceAlreadyStopped() + { + //Setup service in stopped state + RunDeploymentAndAssertRunningState("demand", "Stopped", ServiceControllerStatus.Stopped); + + RunDeploymentAndAssertRunningState("demand", "Unchanged", ServiceControllerStatus.Stopped); + } + + [Test] + public void ShouldBeRunningWhenStartModeIsDemandAndDesiredStatusIsUnchangedAndServiceAlreadyRunning() + { + //Setup service in stopped state + RunDeploymentAndAssertRunningState("demand", "Started", ServiceControllerStatus.Running); + + RunDeploymentAndAssertRunningState("demand", "Unchanged", ServiceControllerStatus.Running); + } + } +} \ No newline at end of file