Skip to content

Commit a7a5921

Browse files
authored
Fixing bug in Flex Consumption HostId generation in placeholder mode (#9391)
1 parent ed7e802 commit a7a5921

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

src/WebJobs.Script/Host/ScriptHostIdProvider.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,16 @@ internal static HostIdResult GetDefaultHostId(IEnvironment environment, ScriptAp
5454
{
5555
// in Flex Consumption, we use a Guid based host ID without truncation.
5656
string uniqueSlotName = environment?.GetAzureWebsiteUniqueSlotName();
57-
byte[] hash;
58-
using (MD5 md5 = MD5.Create())
57+
if (!string.IsNullOrEmpty(uniqueSlotName))
5958
{
60-
hash = md5.ComputeHash(Encoding.UTF8.GetBytes(uniqueSlotName));
61-
}
59+
byte[] hash;
60+
using (MD5 md5 = MD5.Create())
61+
{
62+
hash = md5.ComputeHash(Encoding.UTF8.GetBytes(uniqueSlotName));
63+
}
6264

63-
result.HostId = new Guid(hash).ToString().Replace("-", string.Empty).ToLowerInvariant();
65+
result.HostId = new Guid(hash).ToString().Replace("-", string.Empty).ToLowerInvariant();
66+
}
6467

6568
return result;
6669
}

test/WebJobs.Script.Tests/ScriptHostIdProviderTests.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,47 @@ public void GetDefaultHostId_FlexConsumption_ReturnsExpectedResult(string siteNa
117117
Assert.False(result.IsTruncated);
118118
}
119119

120+
[Fact]
121+
public void GetDefaultHostId_PlaceholderMode_ReturnsExpectedResult()
122+
{
123+
var options = new ScriptApplicationHostOptions
124+
{
125+
ScriptPath = @"c:\testscripts"
126+
};
127+
128+
// In placeholder mode, site name and other settings aren't available to compute the
129+
// default HostId, so we expect null.
130+
var environment = new TestEnvironment();
131+
environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteInstanceId, "123123");
132+
133+
Assert.False(environment.IsFlexConsumptionSku());
134+
135+
var result = ScriptHostIdProvider.GetDefaultHostId(environment, options);
136+
137+
Assert.Equal(null, result.HostId);
138+
}
139+
140+
[Fact]
141+
public void GetDefaultHostId_PlaceholderMode_FlexConsumption_ReturnsExpectedResult()
142+
{
143+
var options = new ScriptApplicationHostOptions
144+
{
145+
ScriptPath = @"c:\testscripts"
146+
};
147+
148+
// In placeholder mode, site name and other settings aren't available to compute the
149+
// default HostId, so we expect null.
150+
var environment = new TestEnvironment();
151+
environment.SetEnvironmentVariable(EnvironmentSettingNames.ContainerName, "testContainer");
152+
environment.SetEnvironmentVariable(EnvironmentSettingNames.LegionServiceHost, "legionhost");
153+
154+
Assert.True(environment.IsFlexConsumptionSku());
155+
156+
var result = ScriptHostIdProvider.GetDefaultHostId(environment, options);
157+
158+
Assert.Equal(null, result.HostId);
159+
}
160+
120161
[Fact]
121162
public void GetDefaultHostId_SelfHost_ReturnsExpectedResult()
122163
{

0 commit comments

Comments
 (0)