|
5 | 5 | using System.Collections.Generic;
|
6 | 6 | using System.Collections.ObjectModel;
|
7 | 7 | using System.IO;
|
| 8 | +using System.IO.Abstractions; |
8 | 9 | using System.Linq;
|
9 | 10 | using System.Reflection;
|
10 | 11 | using System.Threading;
|
|
17 | 18 | using Microsoft.Azure.WebJobs.Script.Description;
|
18 | 19 | using Microsoft.Azure.WebJobs.Script.Diagnostics;
|
19 | 20 | using Microsoft.Azure.WebJobs.Script.Eventing;
|
20 |
| -using Microsoft.Azure.WebJobs.Script.WebHost; |
21 | 21 | using Microsoft.Azure.WebJobs.Script.Workers.Rpc;
|
22 | 22 | using Microsoft.Extensions.Configuration;
|
23 | 23 | using Microsoft.Extensions.DependencyInjection;
|
@@ -1512,6 +1512,97 @@ public async Task Initialize_LogsWarningForExplicitlySetHostId()
|
1512 | 1512 | Assert.Single(logger.GetLogMessages(), x => x.FormattedMessage.Contains("Host id explicitly set in configuration."));
|
1513 | 1513 | }
|
1514 | 1514 |
|
| 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 | + |
1515 | 1606 | public class AssemblyMock : Assembly
|
1516 | 1607 | {
|
1517 | 1608 | public override object[] GetCustomAttributes(Type attributeType, bool inherit)
|
|
0 commit comments