Skip to content

Commit faf22ad

Browse files
committed
Refactored the machine suffix addition and added test coverage
1 parent 1427156 commit faf22ad

File tree

7 files changed

+82
-18
lines changed

7 files changed

+82
-18
lines changed

AzureWebFarm.OctopusDeploy.Tests/AzureWebFarm.OctopusDeploy.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
</Reference>
9191
</ItemGroup>
9292
<ItemGroup>
93+
<Compile Include="Infrastructure\AzureEnvironmentTests.cs" />
9394
<Compile Include="Infrastructure\OctopusDeployTests.cs" />
9495
<Compile Include="Infrastructure\RegistryEditorTests.cs" />
9596
<Compile Include="Properties\AssemblyInfo.cs" />
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using AzureWebFarm.OctopusDeploy.Infrastructure;
3+
using Shouldly;
4+
using Xunit;
5+
6+
namespace AzureWebFarm.OctopusDeploy.Tests.Infrastructure
7+
{
8+
public class AzureEnvironmentTests
9+
{
10+
private readonly TestConfigSettings _config = new TestConfigSettings();
11+
12+
[Fact]
13+
public void GivenSuffixIsEmpty_WhenGeneratingFarmName_ThenUseMachineNameAndEnvironment()
14+
{
15+
var machineName = Environment.MachineName;
16+
_config.TentacleMachineNameSuffix = "";
17+
_config.TentacleEnvironment = "Production";
18+
19+
var name = AzureEnvironment.GetMachineName(_config);
20+
21+
name.ShouldBe(machineName + "_" + _config.TentacleEnvironment);
22+
}
23+
24+
[Fact]
25+
public void GivenSuffixIsNotEmpty_WhenGeneratingFarmName_ThenUseMachineNameAndEnvironmentAndSuffix()
26+
{
27+
var machineName = Environment.MachineName;
28+
_config.TentacleMachineNameSuffix = "Suffix";
29+
_config.TentacleEnvironment = "Production";
30+
31+
var name = AzureEnvironment.GetMachineName(_config);
32+
33+
name.ShouldBe(machineName + "_" + _config.TentacleEnvironment + "_" + _config.TentacleMachineNameSuffix);
34+
}
35+
}
36+
37+
public class TestConfigSettings : IConfigSettings
38+
{
39+
public string OctopusServer { get; set; }
40+
public string OctopusApiKey { get; set; }
41+
public string TentacleEnvironment { get; set; }
42+
public string TentacleRole { get; set; }
43+
public string TentacleMachineNameSuffix { get; set; }
44+
public string TentacleDeploymentsPath { get; set; }
45+
public string TentacleInstallPath { get; set; }
46+
}
47+
}

AzureWebFarm.OctopusDeploy/AzureWebFarm.OctopusDeploy.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
</ItemGroup>
9696
<ItemGroup>
9797
<Compile Include="Infrastructure\AzureEnvironment.cs" />
98+
<Compile Include="Infrastructure\AzureRoleEnvironment.cs" />
9899
<Compile Include="Infrastructure\ConfigSettings.cs" />
99100
<Compile Include="Infrastructure\IisEnvironment.cs" />
100101
<Compile Include="Infrastructure\OctopusDeploy.cs" />

AzureWebFarm.OctopusDeploy/Infrastructure/AzureEnvironment.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,16 @@ public static void RequestRecycleIfConfigSettingChanged(ConfigSettings config)
2323
};
2424
}
2525

26-
public static string GetMachineName(ConfigSettings config)
26+
public static string GetMachineName(IConfigSettings config)
2727
{
28-
return RoleEnvironment.IsEmulated
28+
var name = AzureRoleEnvironment.IsEmulated()
2929
? Environment.MachineName
30-
: String.Format("{0}_{1}", RoleEnvironment.CurrentRoleInstance.Id, config.TentacleEnvironment);
30+
: string.Format("{0}_{1}", AzureRoleEnvironment.CurrentRoleInstanceId(), config.TentacleEnvironment);
31+
32+
if (!string.IsNullOrEmpty(config.TentacleMachineNameSuffix))
33+
name = string.Format("{0}_{1}", name, config.TentacleMachineNameSuffix);
34+
35+
return name;
3136
}
3237

3338
public static ConfigSettings GetConfigSettings()
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using Microsoft.WindowsAzure.ServiceRuntime;
3+
4+
namespace AzureWebFarm.OctopusDeploy.Infrastructure
5+
{
6+
internal static class AzureRoleEnvironment
7+
{
8+
public static Func<bool> IsAvailable = () => RoleEnvironment.IsAvailable;
9+
public static Func<string> CurrentRoleInstanceId = () => IsAvailable() ? RoleEnvironment.CurrentRoleInstance.Id : Environment.MachineName;
10+
public static Func<bool> IsEmulated = () => IsAvailable() && RoleEnvironment.IsEmulated;
11+
}
12+
}

AzureWebFarm.OctopusDeploy/Infrastructure/ConfigSettings.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,17 @@
33

44
namespace AzureWebFarm.OctopusDeploy.Infrastructure
55
{
6-
internal class ConfigSettings
6+
internal interface IConfigSettings {
7+
string OctopusServer { get; }
8+
string OctopusApiKey { get; }
9+
string TentacleEnvironment { get; }
10+
string TentacleRole { get; }
11+
string TentacleMachineNameSuffix { get; }
12+
string TentacleDeploymentsPath { get; }
13+
string TentacleInstallPath { get; }
14+
}
15+
16+
internal class ConfigSettings : IConfigSettings
717
{
818
private const string OctopusServerConfigName = "OctopusServer";
919
private const string OctopusApiKeyConfigName = "OctopusApiKey";
@@ -13,7 +23,7 @@ internal class ConfigSettings
1323
private const string TentacleInstallPathConfigName = "Install";
1424
private const string TentacleMachineNameSuffixConfigName = "TentacleMachineNameSuffix";
1525

16-
private static readonly string[] ConfigSettingsNames = new[] { OctopusServerConfigName, OctopusApiKeyConfigName, TentacleEnvironmentConfigName, TentacleRoleConfigName, TentacleMachineNameSuffixConfigName };
26+
private static readonly string[] ConfigSettingsNames = { OctopusServerConfigName, OctopusApiKeyConfigName, TentacleEnvironmentConfigName, TentacleRoleConfigName, TentacleMachineNameSuffixConfigName };
1727

1828
private static string _octopusServer;
1929
private static string _octopusApiKey;

AzureWebFarm.OctopusDeploy/WebFarmRole.cs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public WebFarmRole(string machineName = null)
2222
Log.Logger = AzureEnvironment.GetAzureLogger();
2323
var config = AzureEnvironment.GetConfigSettings();
2424

25-
machineName = machineName ?? GetMachineName(config);
25+
machineName = machineName ?? AzureEnvironment.GetMachineName(config);
2626
var octopusRepository = Infrastructure.OctopusDeploy.GetRepository(config);
2727
var processRunner = new ProcessRunner();
2828
var registryEditor = new RegistryEditor();
@@ -78,17 +78,5 @@ public void OnStop()
7878
_octopusDeploy.DeleteMachine();
7979
IisEnvironment.WaitForAllHttpRequestsToEnd();
8080
}
81-
82-
private static string GetMachineName(ConfigSettings config)
83-
{
84-
var machineName = AzureEnvironment.GetMachineName(config);
85-
86-
if (!string.IsNullOrEmpty(config.TentacleMachineNameSuffix))
87-
{
88-
machineName = machineName + "_" + config.TentacleMachineNameSuffix;
89-
}
90-
91-
return machineName;
92-
}
9381
}
9482
}

0 commit comments

Comments
 (0)