public async Task IsFunction_ReturnsExpectedResult() { var host = TestHelpers.GetDefaultHost(o => { o.ScriptPath = TestHelpers.FunctionsTestDirectory; o.LogPath = TestHelpers.GetHostLogFileDirectory().FullName; }); await host.StartAsync(); var scriptHost = host.GetScriptHost(); var parameters = new Collection <ParameterDescriptor>(); parameters.Add(new ParameterDescriptor("param1", typeof(string))); var metadata = new FunctionMetadata(); var invoker = new TestInvoker(); var function = new FunctionDescriptor("TestFunction", invoker, metadata, parameters, null, null, null); scriptHost.Functions.Add(function); var errors = new Collection <string>(); errors.Add("A really really bad error!"); scriptHost.FunctionErrors.Add("ErrorFunction", errors); Assert.True(scriptHost.IsFunction("TestFunction")); Assert.True(scriptHost.IsFunction("ErrorFunction")); Assert.False(scriptHost.IsFunction("DoesNotExist")); Assert.False(scriptHost.IsFunction(string.Empty)); Assert.False(scriptHost.IsFunction(null)); }
// TODO: DI (FACAVAL) Use test helpers to create host and inject services private RunDependencies CreateDependencies(Action <IServiceCollection> configureServices = null) { var dependencies = new RunDependencies(); TestLoggerProvider loggerProvider = new TestLoggerProvider(); ILoggerFactory loggerFactory = new LoggerFactory(); loggerFactory.AddProvider(loggerProvider); var eventManager = new ScriptEventManager(); var entrypointResolver = new Mock <IFunctionEntryPointResolver>(); var compilation = new Mock <IDotNetCompilation>(); compilation.Setup(c => c.GetDiagnostics()) .Returns(ImmutableArray <Diagnostic> .Empty); var compilationService = new Mock <ICompilationService <IDotNetCompilation> >(); compilationService.Setup(s => s.SupportedFileTypes) .Returns(() => new[] { ".csx" }); compilationService.Setup(s => s.GetFunctionCompilationAsync(It.IsAny <FunctionMetadata>())) .ReturnsAsync(compilation.Object); var compilationServiceFactory = new Mock <ICompilationServiceFactory <ICompilationService <IDotNetCompilation>, IFunctionMetadataResolver> >(); compilationServiceFactory.Setup(f => f.CreateService(DotNetScriptTypes.CSharp, It.IsAny <IFunctionMetadataResolver>())) .Returns(compilationService.Object); var metricsLogger = new MetricsLogger(); var hostBuilder = new HostBuilder() .ConfigureDefaultTestWebScriptHost(o => { o.ScriptPath = TestHelpers.FunctionsTestDirectory; o.LogPath = TestHelpers.GetHostLogFileDirectory().Parent.FullName; }); if (configureServices != null) { hostBuilder.ConfigureServices(configureServices); } var host = hostBuilder.Build(); var scriptHost = host.GetScriptHost(); return(new RunDependencies { Host = scriptHost, EntrypointResolver = entrypointResolver, Compilation = compilation, CompilationService = compilationService, CompilationServiceFactory = compilationServiceFactory, LoggerProvider = loggerProvider, LoggerFactory = loggerFactory, MetricsLogger = metricsLogger }); }
public static IHost GetDefaultHost(Action <ScriptApplicationHostOptions> configure = null) { if (configure == null) { configure = o => { o.ScriptPath = TestHelpers.FunctionsTestDirectory; o.LogPath = TestHelpers.GetHostLogFileDirectory().FullName; }; } return(new HostBuilder() .ConfigureDefaultTestWebScriptHost(configure) .Build()); }
public async Task InitializeAsync() { Directory.CreateDirectory(TestHelpers.FunctionsTestDirectory); var environment = new Mock <IScriptJobHostEnvironment>(); var eventManager = new Mock <IScriptEventManager>(); Host = new HostBuilder() .ConfigureDefaultTestWebScriptHost(o => { o.ScriptPath = TestHelpers.FunctionsTestDirectory; o.LogPath = TestHelpers.GetHostLogFileDirectory().FullName; }) .Build(); await ScriptHost.InitializeAsync(); }
public FunctionDescriptorProviderTests() { string rootPath = Path.Combine(Environment.CurrentDirectory, @"TestScripts\Node"); _host = new HostBuilder() .ConfigureDefaultTestWebScriptHost(webJobsBuilder => { webJobsBuilder.AddAzureStorage(); }, o => { o.ScriptPath = rootPath; o.LogPath = TestHelpers.GetHostLogFileDirectory().Parent.FullName; }) .Build(); _scriptHost = _host.GetScriptHost(); _scriptHost.InitializeAsync().GetAwaiter().GetResult(); _provider = new TestDescriptorProvider(_scriptHost, _host.Services.GetService <IOptions <ScriptJobHostOptions> >().Value, _host.Services.GetService <ICollection <IScriptBindingProvider> >()); }
public WorkerFunctionInvokerTests() { _applicationLifetime = new Mock <IApplicationLifetime>(); _mockFunctionInvocationDispatcher = new Mock <IFunctionInvocationDispatcher>(); _mockFunctionInvocationDispatcher.Setup(a => a.ErrorEventsThreshold).Returns(0); var hostBuilder = new HostBuilder() .ConfigureDefaultTestWebScriptHost(o => { o.ScriptPath = TestHelpers.FunctionsTestDirectory; o.LogPath = TestHelpers.GetHostLogFileDirectory().Parent.FullName; }); var host = hostBuilder.Build(); var sc = host.GetScriptHost(); FunctionMetadata metaData = new FunctionMetadata(); _testFunctionInvoker = new TestWorkerFunctionInvoker(sc, null, metaData, NullLoggerFactory.Instance, null, new Collection <FunctionBinding>(), _mockFunctionInvocationDispatcher.Object, _applicationLifetime.Object); }
public ProxyFunctionDescriptorProviderTests() { string rootPath = Path.Combine(Environment.CurrentDirectory, @"TestScripts\Proxies"); var hostOptions = new JobHostOptions(); var eventManager = new Mock <IScriptEventManager>(); var contextFactory = new Mock <IJobHostContextFactory>(); _proxyClient = GetMockProxyClient(); _settingsManager = ScriptSettingsManager.Instance; _host = new HostBuilder() .ConfigureDefaultTestWebScriptHost(o => { o.ScriptPath = rootPath; o.LogPath = TestHelpers.GetHostLogFileDirectory().Parent.FullName; }) .Build(); _scriptHost = _host.GetScriptHost(); }
public static FunctionMetadataManager GetFunctionMetadataManager(IOptions <ScriptJobHostOptions> jobHostOptions, Mock <IScriptHostManager> managerMock, IFunctionMetadataProvider functionMetadataProvider, IList <IFunctionProvider> functionProviders, IOptions <HttpWorkerOptions> httpOptions, ILoggerFactory loggerFactory, IOptions <LanguageWorkerOptions> languageWorkerOptions) { managerMock.As <IServiceProvider>().Setup(m => m.GetService(typeof(IEnumerable <IFunctionProvider>))).Returns(functionProviders); managerMock.As <IServiceProvider>().Setup(m => m.GetService(typeof(IOptions <ScriptJobHostOptions>))).Returns(jobHostOptions); managerMock.As <IServiceProvider>().Setup(m => m.GetService(typeof(IOptions <HttpWorkerOptions>))).Returns(httpOptions); managerMock.As <IServiceProvider>().Setup(m => m.GetService(typeof(IOptions <LanguageWorkerOptions>))).Returns(languageWorkerOptions); managerMock.As <IServiceProvider>().Setup(m => m.GetService(typeof(ILoggerFactory))).Returns(loggerFactory); var options = new ScriptApplicationHostOptions() { IsSelfHost = true, ScriptPath = TestHelpers.FunctionsTestDirectory, LogPath = TestHelpers.GetHostLogFileDirectory().FullName }; var factory = new TestOptionsFactory <ScriptApplicationHostOptions>(options); var source = new TestChangeTokenSource <ScriptApplicationHostOptions>(); var changeTokens = new[] { source }; var optionsMonitor = new OptionsMonitor <ScriptApplicationHostOptions>(factory, changeTokens, factory); return(new FunctionMetadataManager(jobHostOptions, functionMetadataProvider, httpOptions, managerMock.Object, loggerFactory, languageWorkerOptions, SystemEnvironment.Instance)); }
public async Task InitializeAsync() { if (!string.IsNullOrEmpty(_functionsWorkerLanguage)) { Environment.SetEnvironmentVariable(RpcWorkerConstants.FunctionWorkerRuntimeSettingName, _functionsWorkerLanguage); } IConfiguration configuration = TestHelpers.GetTestConfiguration(); string connectionString = configuration.GetWebJobsConnectionString(ConnectionStringNames.Storage); CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString); QueueClient = storageAccount.CreateCloudQueueClient(); BlobClient = storageAccount.CreateCloudBlobClient(); TableStorageAccount tableStorageAccount = TableStorageAccount.Parse(connectionString); TableClient = tableStorageAccount.CreateCloudTableClient(); await CreateTestStorageEntities(); // ApiHubTestHelper.SetDefaultConnectionFactory(); //ILoggerProviderFactory loggerProviderFactory = new TestLoggerProviderFactory(LoggerProvider); // Reset the timer logs first, since one of the tests will // be checking them TestHelpers.ClearFunctionLogs("TimerTrigger"); TestHelpers.ClearFunctionLogs("ListenerStartupException"); Host = new HostBuilder() .ConfigureDefaultTestWebScriptHost(webjobsBuilder => { webjobsBuilder.AddAzureStorage(); // This needs to added manually at the ScriptHost level, as although FunctionMetadataManager is available through WebHost, // it needs to change the services during its lifetime. webjobsBuilder.Services.AddSingleton <IFunctionMetadataManager, FunctionMetadataManager>(); }, o => { o.ScriptPath = _rootPath; o.LogPath = TestHelpers.GetHostLogFileDirectory().Parent.FullName; }, runStartupHostedServices: true) .ConfigureServices(services => { services.Configure <ScriptJobHostOptions>(o => { o.FileLoggingMode = FileLoggingMode.Always; if (_functions != null) { o.Functions = _functions; } }); if (_proxyClient != null) { services.AddSingleton <ProxyClientExecutor>(_proxyClient); } // Shared memory data transfer if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { services.AddSingleton <IMemoryMappedFileAccessor, MemoryMappedFileAccessorWindows>(); } else { services.AddSingleton <IMemoryMappedFileAccessor, MemoryMappedFileAccessorUnix>(); } services.AddSingleton <ISharedMemoryManager, SharedMemoryManager>(); ConfigureServices(services); }) .ConfigureLogging(b => { b.AddProvider(LoggerProvider); }) .Build(); JobHost = Host.GetScriptHost(); if (_startHost) { JobHost.HostStarted += (s, e) => _hostStartedEvent.Set(); await Host.StartAsync(); _hostStartedEvent.Wait(TimeSpan.FromSeconds(30)); } }
public async Task InitializeAsync() { if (!string.IsNullOrEmpty(_functionsWorkerLanguage)) { Environment.SetEnvironmentVariable(RpcWorkerConstants.FunctionWorkerRuntimeSettingName, _functionsWorkerLanguage); } IConfiguration configuration = TestHelpers.GetTestConfiguration(); string connectionString = configuration.GetWebJobsConnectionString(ConnectionStringNames.Storage); CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString); QueueClient = storageAccount.CreateCloudQueueClient(); BlobClient = storageAccount.CreateCloudBlobClient(); TableClient = storageAccount.CreateCloudTableClient(); await CreateTestStorageEntities(); // ApiHubTestHelper.SetDefaultConnectionFactory(); //ILoggerProviderFactory loggerProviderFactory = new TestLoggerProviderFactory(LoggerProvider); // Reset the timer logs first, since one of the tests will // be checking them TestHelpers.ClearFunctionLogs("TimerTrigger"); TestHelpers.ClearFunctionLogs("ListenerStartupException"); Host = new HostBuilder() .ConfigureDefaultTestWebScriptHost(webjobsBuilder => { webjobsBuilder.AddAzureStorage(); }, o => { o.ScriptPath = _rootPath; o.LogPath = TestHelpers.GetHostLogFileDirectory().Parent.FullName; }, runStartupHostedServices: true) .ConfigureServices(services => { services.Configure <ScriptJobHostOptions>(o => { o.FileLoggingMode = FileLoggingMode.Always; if (_functions != null) { o.Functions = _functions; } }); if (_proxyClient != null) { services.AddSingleton <ProxyClientExecutor>(_proxyClient); } ConfigureServices(services); }) .ConfigureLogging(b => { b.AddProvider(LoggerProvider); }) .Build(); JobHost = Host.GetScriptHost(); if (_startHost) { JobHost.HostStarted += (s, e) => _hostStartedEvent.Set(); await Host.StartAsync(); _hostStartedEvent.Wait(TimeSpan.FromSeconds(30)); } }