Skip to content

Commit a9e69d2

Browse files
authored
adding regression tests for missing FUNCTION_WORKER_RUNTIME (#9284)
1 parent 4ec73a3 commit a9e69d2

File tree

6 files changed

+151
-4
lines changed

6 files changed

+151
-4
lines changed

src/WebJobs.Script/Config/HostJsonFileConfigurationSource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ internal JObject LoadHostConfig(string configFilePath)
198198
JObject hostConfigObject;
199199
try
200200
{
201-
string json = File.ReadAllText(configFilePath);
201+
string json = FileUtility.Instance.File.ReadAllText(configFilePath);
202202
hostConfigObject = JObject.Parse(json);
203203
}
204204
catch (JsonException ex)

test/CSharpPrecompiledTestProjects/WebJobsStartupTests/Function1.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,17 @@ public void TimerRun([TimerTrigger("%Cron%", RunOnStartup = false)] TimerInfo ti
8989
{
9090
}
9191

92+
[FunctionName("Echo")]
93+
public IActionResult Echo([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req)
94+
{
95+
if (req.Query.TryGetValue("echo", out var value))
96+
{
97+
return new OkObjectResult(value.Single());
98+
}
99+
100+
return new BadRequestResult();
101+
}
102+
92103
private static bool ValidateConfig(IConfiguration _config)
93104
{
94105
if (_config is ConfigurationRoot root)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Net;
3+
using System.Threading.Tasks;
4+
using Microsoft.Azure.WebJobs.Script.Description;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using Xunit;
7+
8+
namespace Microsoft.Azure.WebJobs.Script.Tests.Integration.WebHostEndToEnd
9+
{
10+
public class CSharpPrecompiledEndToEndTests
11+
{
12+
[Theory]
13+
[InlineData(null)]
14+
[InlineData("dotnet")]
15+
public async Task InProc_ChoosesCorrectLanguage(string functionWorkerRuntime)
16+
{
17+
var fixture = new CSharpPrecompiledEndToEndTestFixture("WebJobsStartupTests", functionWorkerRuntime: functionWorkerRuntime);
18+
try
19+
{
20+
await fixture.InitializeAsync();
21+
var client = fixture.Host.HttpClient;
22+
23+
var echo = Guid.NewGuid().ToString();
24+
var response = await client.GetAsync($"api/echo?echo={echo}");
25+
26+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
27+
Assert.Equal(echo, await response.Content.ReadAsStringAsync());
28+
29+
// Previous bug incorrectly chose dotnet-isolated as the Language for C# precompiled functions
30+
// if FUNCTIONS_WORKER_RUNTIME was missing
31+
var metadataManager = fixture.Host.WebHostServices.GetService<IFunctionMetadataManager>();
32+
var metadata = metadataManager.GetFunctionMetadata();
33+
foreach (var functionMetadata in metadata)
34+
{
35+
Assert.Equal(DotNetScriptTypes.DotNetAssembly, functionMetadata.Language);
36+
}
37+
}
38+
finally
39+
{
40+
await fixture.DisposeAsync();
41+
}
42+
}
43+
}
44+
}

test/WebJobs.Script.Tests.Integration/WebHostEndToEnd/CSharpPrecompiledTestFixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ public class CSharpPrecompiledEndToEndTestFixture : EndToEndTestFixture
99
private const string TestPathTemplate = "..\\..\\..\\..\\CSharpPrecompiledTestProjects\\{0}\\bin\\Debug\\netcoreapp3.1";
1010
private readonly IDisposable _dispose;
1111

12-
public CSharpPrecompiledEndToEndTestFixture(string testProjectName, IDictionary<string, string> envVars = null)
13-
: base(string.Format(TestPathTemplate, testProjectName), testProjectName, "dotnet")
12+
public CSharpPrecompiledEndToEndTestFixture(string testProjectName, IDictionary<string, string> envVars = null, string functionWorkerRuntime = "dotnet")
13+
: base(string.Format(TestPathTemplate, testProjectName), testProjectName, functionWorkerRuntime)
1414
{
1515
if (envVars != null)
1616
{

test/WebJobs.Script.Tests/ScriptHostTests.cs

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using System.Collections.ObjectModel;
77
using System.IO;
8+
using System.IO.Abstractions;
89
using System.Linq;
910
using System.Reflection;
1011
using System.Threading;
@@ -17,7 +18,6 @@
1718
using Microsoft.Azure.WebJobs.Script.Description;
1819
using Microsoft.Azure.WebJobs.Script.Diagnostics;
1920
using Microsoft.Azure.WebJobs.Script.Eventing;
20-
using Microsoft.Azure.WebJobs.Script.WebHost;
2121
using Microsoft.Azure.WebJobs.Script.Workers.Rpc;
2222
using Microsoft.Extensions.Configuration;
2323
using Microsoft.Extensions.DependencyInjection;
@@ -1512,6 +1512,97 @@ public async Task Initialize_LogsWarningForExplicitlySetHostId()
15121512
Assert.Single(logger.GetLogMessages(), x => x.FormattedMessage.Contains("Host id explicitly set in configuration."));
15131513
}
15141514

1515+
[Theory]
1516+
[InlineData("main.py", "python", "python")]
1517+
[InlineData("app.dll", "dotnet", DotNetScriptTypes.DotNetAssembly)] // if FUNCTIONS_WORKER_RUNTIME is missing, assume dotnet
1518+
public async Task Initialize_MissingWorkerRuntime_SetsCorrectRuntimeFromFunctionMetadata(string scriptFile, string expectedMetricLanguage, string expectedMetadataLanguage)
1519+
{
1520+
IFileSystem CreateFileSystem(string rootPath)
1521+
{
1522+
var fullFileSystem = new FileSystem();
1523+
var fileSystem = new Mock<IFileSystem>();
1524+
var fileBase = new Mock<FileBase>();
1525+
var dirBase = new Mock<DirectoryBase>();
1526+
1527+
fileSystem.SetupGet(f => f.Path).Returns(fullFileSystem.Path);
1528+
fileSystem.SetupGet(f => f.File).Returns(fileBase.Object);
1529+
1530+
var hostjson = """
1531+
{
1532+
"version": "2.0"
1533+
}
1534+
""";
1535+
fileBase.Setup(f => f.ReadAllText(Path.Combine(rootPath, "host.json"))).Returns(hostjson);
1536+
1537+
fileSystem.SetupGet(f => f.Directory).Returns(dirBase.Object);
1538+
1539+
dirBase.Setup(d => d.Exists(rootPath)).Returns(true);
1540+
dirBase.Setup(d => d.EnumerateDirectories(rootPath))
1541+
.Returns(new[]
1542+
{
1543+
Path.Combine(rootPath, "function1"),
1544+
});
1545+
1546+
var function1 = $$"""
1547+
{
1548+
"scriptFile": "{{scriptFile}}",
1549+
"bindings": [
1550+
{
1551+
"type": "httpTrigger",
1552+
"direction": "in",
1553+
"name": "test"
1554+
}
1555+
]
1556+
}
1557+
""";
1558+
fileBase.Setup(f => f.ReadAllText(Path.Combine(rootPath, @"function1", "function.json"))).Returns(function1);
1559+
1560+
fileBase.Setup(f => f.Exists(Path.Combine(rootPath, @"function1", "function.json"))).Returns(true);
1561+
fileBase.Setup(f => f.Exists(Path.Combine(rootPath, @"function1", scriptFile))).Returns(true);
1562+
1563+
return fileSystem.Object;
1564+
}
1565+
1566+
try
1567+
{
1568+
string rootPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
1569+
var metricsLogger = new TestMetricsLogger();
1570+
var environment = new TestEnvironment();
1571+
1572+
FileUtility.Instance = CreateFileSystem(rootPath);
1573+
1574+
IHost host = new HostBuilder()
1575+
.ConfigureServices(s =>
1576+
{
1577+
s.AddSingleton<IEnvironment>(environment);
1578+
})
1579+
.ConfigureDefaultTestWebScriptHost(null, o => o.ScriptPath = rootPath, false, s =>
1580+
{
1581+
s.AddSingleton<IMetricsLogger>(metricsLogger);
1582+
})
1583+
.Build();
1584+
1585+
var scriptHost = host.GetScriptHost();
1586+
await scriptHost.InitializeAsync();
1587+
1588+
Assert.Contains($"host.startup.runtime.language.{expectedMetricLanguage}", metricsLogger.LoggedEvents);
1589+
1590+
// Previous bug incorrectly chose dotnet-isolated as the Language for C# precompiled functions
1591+
// if FUNCTIONS_WORKER_RUNTIME was missing
1592+
var metadataManager = host.Services.GetService<IFunctionMetadataManager>();
1593+
var metadata = metadataManager.GetFunctionMetadata();
1594+
foreach (var functionMetadata in metadata)
1595+
{
1596+
Assert.Equal(expectedMetadataLanguage, functionMetadata.Language);
1597+
}
1598+
}
1599+
finally
1600+
{
1601+
FileUtility.Instance = null;
1602+
EnvironmentExtensions.BaseDirectory = null;
1603+
}
1604+
}
1605+
15151606
public class AssemblyMock : Assembly
15161607
{
15171608
public override object[] GetCustomAttributes(Type attributeType, bool inherit)

test/WebJobs.Script.Tests/WebJobs.Script.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<IsPackable>false</IsPackable>
77
<AssemblyName>Microsoft.Azure.WebJobs.Script.Tests</AssemblyName>
88
<RootNamespace>Microsoft.Azure.WebJobs.Script.Tests</RootNamespace>
9+
<LangVersion>preview</LangVersion>
910
</PropertyGroup>
1011
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
1112
<DefineConstants>TRACE;DEBUG;NETCOREAPP2_0;SCRIPT_TEST</DefineConstants>

0 commit comments

Comments
 (0)