Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}