From 6b4abf16dd2184b7abb8f23b021c2ff44ad50597 Mon Sep 17 00:00:00 2001 From: soninaren Date: Thu, 28 Sep 2023 15:39:40 -0700 Subject: [PATCH] adding unit tests --- .../ScriptStartupTypeDiscovererTests.cs | 91 +++++++++++++++++-- 1 file changed, 82 insertions(+), 9 deletions(-) diff --git a/test/WebJobs.Script.Tests/ScriptStartupTypeDiscovererTests.cs b/test/WebJobs.Script.Tests/ScriptStartupTypeDiscovererTests.cs index 149fa929ad..4fe516f9ba 100644 --- a/test/WebJobs.Script.Tests/ScriptStartupTypeDiscovererTests.cs +++ b/test/WebJobs.Script.Tests/ScriptStartupTypeDiscovererTests.cs @@ -392,10 +392,63 @@ public async Task GetExtensionsStartupTypes_LegacyBundles_UsesExtensionBundleBin } } + [Fact] + public async Task GetExtensionsStartupTypes_WorkerRuntimeNotSetForNodeApp_LoadsExtensionBundle() + { + var vars = new Dictionary(); + + using (var directory = GetTempDirectory()) + using (var env = new TestScopedEnvironmentVariable(vars)) + { + var binPath = Path.Combine(directory.Path, "bin"); + TestMetricsLogger testMetricsLogger = new TestMetricsLogger(); + TestLoggerProvider testLoggerProvider = new TestLoggerProvider(); + LoggerFactory factory = new LoggerFactory(); + factory.AddProvider(testLoggerProvider); + var testLogger = factory.CreateLogger(); + + var mockExtensionBundleManager = new Mock(); + mockExtensionBundleManager.Setup(e => e.IsExtensionBundleConfigured()).Returns(true); + mockExtensionBundleManager.Setup(e => e.GetExtensionBundleBinPathAsync()).Returns(Task.FromResult(binPath)); + mockExtensionBundleManager.Setup(e => e.IsLegacyExtensionBundle()).Returns(false); + mockExtensionBundleManager.Setup(e => e.GetExtensionBundleDetails()).Returns(Task.FromResult(GetV2BundleDetails())); + + RpcWorkerConfig nodeWorkerConfig = new RpcWorkerConfig() { Description = TestHelpers.GetTestWorkerDescription("node", "none", false) }; + RpcWorkerConfig dotnetIsolatedWorkerConfig = new RpcWorkerConfig() { Description = TestHelpers.GetTestWorkerDescription("dotnet-isolated", "none", false) }; + + var tempOptions = new LanguageWorkerOptions(); + tempOptions.WorkerConfigs = new List(); + tempOptions.WorkerConfigs.Add(nodeWorkerConfig); + tempOptions.WorkerConfigs.Add(dotnetIsolatedWorkerConfig); + + var optionsMonitor = new TestOptionsMonitor(tempOptions); + var mockFunctionMetadataManager = GetTestFunctionMetadataManager(optionsMonitor, hasNodeFunctions: true); + + var languageWorkerOptions = new TestOptionsMonitor(tempOptions); + + var discoverer = new ScriptStartupTypeLocator(directory.Path, testLogger, mockExtensionBundleManager.Object, mockFunctionMetadataManager, testMetricsLogger, languageWorkerOptions); + + // Act + var types = await discoverer.GetExtensionsStartupTypesAsync(); + + //Assert + var traces = testLoggerProvider.GetAllLogMessages(); + var traceMessage = traces.FirstOrDefault(val => val.EventId.Name.Equals("ScriptStartNotLoadingExtensionBundle")); + bool loadingExtensionBundle = traceMessage == null; + + Assert.True(loadingExtensionBundle); + AreExpectedMetricsGenerated(testMetricsLogger); + Assert.Single(types); + Assert.Equal(typeof(AzureStorageWebJobsStartup).FullName, types.Single().FullName); + } + } + [Theory] - [InlineData(true)] - [InlineData(false)] - public async Task GetExtensionsStartupTypes_DotnetIsolated_ExtensionBundleConfigured(bool isLogicApp) + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task GetExtensionsStartupTypes_DotnetIsolated_ExtensionBundleConfigured(bool isLogicApp, bool workerRuntimeSet) { var vars = new Dictionary(); @@ -404,6 +457,11 @@ public async Task GetExtensionsStartupTypes_DotnetIsolated_ExtensionBundleConfig vars.Add(EnvironmentSettingNames.AppKind, ScriptConstants.WorkFlowAppKind); } + if (workerRuntimeSet) + { + vars.Add(EnvironmentSettingNames.FunctionWorkerRuntime, "dotnet-isolated"); + } + using (var directory = GetTempDirectory()) using (var env = new TestScopedEnvironmentVariable(vars)) { @@ -424,11 +482,15 @@ public async Task GetExtensionsStartupTypes_DotnetIsolated_ExtensionBundleConfig var tempOptions = new LanguageWorkerOptions(); tempOptions.WorkerConfigs = new List(); tempOptions.WorkerConfigs.Add(workerConfig); + + RpcWorkerConfig nodeWorkerConfig = new RpcWorkerConfig() { Description = TestHelpers.GetTestWorkerDescription("node", "none", false) }; + tempOptions.WorkerConfigs.Add(nodeWorkerConfig); + var optionsMonitor = new TestOptionsMonitor(tempOptions); - var mockFunctionMetadataManager = GetTestFunctionMetadataManager(optionsMonitor); + + var mockFunctionMetadataManager = GetTestFunctionMetadataManager(optionsMonitor, hasDotnetIsolatedFunctions: true); var languageWorkerOptions = new TestOptionsMonitor(tempOptions); - Environment.SetEnvironmentVariable(EnvironmentSettingNames.FunctionWorkerRuntime, "dotnet-isolated"); var discoverer = new ScriptStartupTypeLocator(directory.Path, testLogger, mockExtensionBundleManager.Object, mockFunctionMetadataManager, testMetricsLogger, languageWorkerOptions); @@ -437,15 +499,17 @@ public async Task GetExtensionsStartupTypes_DotnetIsolated_ExtensionBundleConfig //Assert var traces = testLoggerProvider.GetAllLogMessages(); - var expectedTrace = traces.FirstOrDefault(val => val.EventId.Name.Equals("ScriptStartNotLoadingExtensionBundle")); + var traceMessage = traces.FirstOrDefault(val => val.EventId.Name.Equals("ScriptStartNotLoadingExtensionBundle")); + + bool loadingExtensionBundle = traceMessage == null; if (isLogicApp) { - Assert.Null(expectedTrace); + Assert.True(loadingExtensionBundle); } else { - Assert.NotNull(expectedTrace); + Assert.False(loadingExtensionBundle); } AreExpectedMetricsGenerated(testMetricsLogger); @@ -678,7 +742,7 @@ void CopyToBin(string path) } } - private IFunctionMetadataManager GetTestFunctionMetadataManager(IOptionsMonitor options, ICollection metadataColection = null, bool hasPrecompiledFunction = false) + private IFunctionMetadataManager GetTestFunctionMetadataManager(IOptionsMonitor options, ICollection metadataColection = null, bool hasPrecompiledFunction = false, bool hasNodeFunctions = false, bool hasDotnetIsolatedFunctions = false) { var functionMetdata = new FunctionMetadata(); functionMetdata.Bindings.Add(new BindingMetadata() { Type = "blob" }); @@ -687,6 +751,15 @@ private IFunctionMetadataManager GetTestFunctionMetadataManager(IOptionsMonitor< { functionMetdata.Language = DotNetScriptTypes.DotNetAssembly; } + if (hasNodeFunctions) + { + functionMetdata.Language = RpcWorkerConstants.NodeLanguageWorkerName; + } + + if (hasDotnetIsolatedFunctions) + { + functionMetdata.Language = RpcWorkerConstants.DotNetIsolatedLanguageWorkerName; + } var functionMetadataCollection = metadataColection ?? new List() { functionMetdata };