Skip to content

Commit

Permalink
adding unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
soninaren committed Sep 29, 2023
1 parent 226534f commit 6b4abf1
Showing 1 changed file with 82 additions and 9 deletions.
91 changes: 82 additions & 9 deletions test/WebJobs.Script.Tests/ScriptStartupTypeDiscovererTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -392,10 +392,63 @@ public async Task GetExtensionsStartupTypes_LegacyBundles_UsesExtensionBundleBin
}
}

[Fact]
public async Task GetExtensionsStartupTypes_WorkerRuntimeNotSetForNodeApp_LoadsExtensionBundle()
{
var vars = new Dictionary<string, string>();

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<ScriptStartupTypeLocator>();

var mockExtensionBundleManager = new Mock<IExtensionBundleManager>();
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<RpcWorkerConfig>();
tempOptions.WorkerConfigs.Add(nodeWorkerConfig);
tempOptions.WorkerConfigs.Add(dotnetIsolatedWorkerConfig);

var optionsMonitor = new TestOptionsMonitor<LanguageWorkerOptions>(tempOptions);
var mockFunctionMetadataManager = GetTestFunctionMetadataManager(optionsMonitor, hasNodeFunctions: true);

var languageWorkerOptions = new TestOptionsMonitor<LanguageWorkerOptions>(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<string, string>();

Expand All @@ -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))
{
Expand All @@ -424,11 +482,15 @@ public async Task GetExtensionsStartupTypes_DotnetIsolated_ExtensionBundleConfig
var tempOptions = new LanguageWorkerOptions();
tempOptions.WorkerConfigs = new List<RpcWorkerConfig>();
tempOptions.WorkerConfigs.Add(workerConfig);

RpcWorkerConfig nodeWorkerConfig = new RpcWorkerConfig() { Description = TestHelpers.GetTestWorkerDescription("node", "none", false) };
tempOptions.WorkerConfigs.Add(nodeWorkerConfig);

var optionsMonitor = new TestOptionsMonitor<LanguageWorkerOptions>(tempOptions);
var mockFunctionMetadataManager = GetTestFunctionMetadataManager(optionsMonitor);

var mockFunctionMetadataManager = GetTestFunctionMetadataManager(optionsMonitor, hasDotnetIsolatedFunctions: true);

var languageWorkerOptions = new TestOptionsMonitor<LanguageWorkerOptions>(tempOptions);
Environment.SetEnvironmentVariable(EnvironmentSettingNames.FunctionWorkerRuntime, "dotnet-isolated");

var discoverer = new ScriptStartupTypeLocator(directory.Path, testLogger, mockExtensionBundleManager.Object, mockFunctionMetadataManager, testMetricsLogger, languageWorkerOptions);

Expand All @@ -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);
Expand Down Expand Up @@ -678,7 +742,7 @@ void CopyToBin(string path)
}
}

private IFunctionMetadataManager GetTestFunctionMetadataManager(IOptionsMonitor<LanguageWorkerOptions> options, ICollection<FunctionMetadata> metadataColection = null, bool hasPrecompiledFunction = false)
private IFunctionMetadataManager GetTestFunctionMetadataManager(IOptionsMonitor<LanguageWorkerOptions> options, ICollection<FunctionMetadata> metadataColection = null, bool hasPrecompiledFunction = false, bool hasNodeFunctions = false, bool hasDotnetIsolatedFunctions = false)
{
var functionMetdata = new FunctionMetadata();
functionMetdata.Bindings.Add(new BindingMetadata() { Type = "blob" });
Expand All @@ -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<FunctionMetadata>() { functionMetdata };

Expand Down

0 comments on commit 6b4abf1

Please sign in to comment.