Example #1
0
            public TestFixture()
            {
                HostOptions = new ScriptApplicationHostOptions
                {
                    IsSelfHost  = true,
                    ScriptPath  = Path.Combine(Environment.CurrentDirectory, @"..\..\..\TestScripts\Proxies"),
                    LogPath     = Path.Combine(Path.GetTempPath(), @"ProxyTests\Logs"),
                    SecretsPath = Path.Combine(Path.GetTempPath(), @"ProxyTests\Secrets")
                };

                var factory        = new TestOptionsFactory <ScriptApplicationHostOptions>(HostOptions);
                var optionsMonitor = new OptionsMonitor <ScriptApplicationHostOptions>(factory, Array.Empty <IOptionsChangeTokenSource <ScriptApplicationHostOptions> >(), factory);

                _testServer = new TestServer(
                    AspNetCore.WebHost.CreateDefaultBuilder()
                    .UseStartup <Startup>()
                    .ConfigureServices(services =>
                {
                    services.Replace(new ServiceDescriptor(typeof(IOptions <ScriptApplicationHostOptions>), new OptionsWrapper <ScriptApplicationHostOptions>(HostOptions)));
                    services.Replace(new ServiceDescriptor(typeof(ISecretManager), new TestSecretManager()));
                    services.Replace(new ServiceDescriptor(typeof(IOptionsMonitor <ScriptApplicationHostOptions>), optionsMonitor));
                }));

                var scriptConfig = _testServer.Host.Services.GetService <IOptions <ScriptJobHostOptions> >().Value;

                HttpClient             = _testServer.CreateClient();
                HttpClient.BaseAddress = new Uri("https://localhost/");

                TestHelpers.WaitForWebHost(HttpClient);
            }
        public TestFunctionHost(string appRoot,
                                Action <IWebJobsBuilder> configureJobHost,
                                Action <IConfigurationBuilder> configureAppConfiguration = null)
        {
            _appRoot = appRoot;

            _hostOptions = new ScriptApplicationHostOptions
            {
                IsSelfHost     = true,
                ScriptPath     = _appRoot,
                LogPath        = Path.Combine(Path.GetTempPath(), @"Functions"),
                SecretsPath    = Environment.CurrentDirectory, // not used
                HasParentScope = true
            };

            var factory        = new TestOptionsFactory <ScriptApplicationHostOptions>(_hostOptions);
            var optionsMonitor = new OptionsMonitor <ScriptApplicationHostOptions>(factory, Array.Empty <IOptionsChangeTokenSource <ScriptApplicationHostOptions> >(), factory);
            var builder        = new WebHostBuilder()
                                 .ConfigureServices(services =>
            {
                services.Replace(new ServiceDescriptor(typeof(ISecretManagerProvider), new TestSecretManagerProvider(new TestSecretManager())));
                services.Replace(ServiceDescriptor.Singleton <IServiceProviderFactory <IServiceCollection> >(new WebHostServiceProviderFactory()));
                services.Replace(new ServiceDescriptor(typeof(IOptions <ScriptApplicationHostOptions>), new OptionsWrapper <ScriptApplicationHostOptions>(_hostOptions)));
                services.Replace(new ServiceDescriptor(typeof(IOptionsMonitor <ScriptApplicationHostOptions>), optionsMonitor));

                services.AddSingleton <IConfigureBuilder <IConfigurationBuilder> >(_ => new DelegatedConfigureBuilder <IConfigurationBuilder>(configureAppConfiguration));
            })
                                 .AddScriptHostBuilder(webJobsBuilder =>
            {
                var loggingBuilder = new LoggingBuilder(webJobsBuilder.Services);
                loggingBuilder.AddProvider(_loggerProvider);
                loggingBuilder.AddFilter <TestLoggerProvider>(_ => true);

                webJobsBuilder.AddAzureStorage();

                configureJobHost?.Invoke(webJobsBuilder);
            })
                                 .UseStartup <Startup>();

            _testServer = new TestServer(builder);

            HttpClient             = new HttpClient(new UpdateContentLengthHandler(_testServer.CreateHandler()));
            HttpClient.BaseAddress = new Uri("https://localhost/");

            var manager = _testServer.Host.Services.GetService <IScriptHostManager>();

            _hostService = manager as WebJobsScriptHostService;

            StartAsync().GetAwaiter().GetResult();
        }
Example #3
0
        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));
        }
Example #4
0
        public static IOptionsMonitor <T> CreateOptionsMonitor <T>(T options) where T : class, new()
        {
            var factory = new TestOptionsFactory <T>(options);

            return(new OptionsMonitor <T>(factory, Array.Empty <IOptionsChangeTokenSource <T> >(), factory));
        }