public KeysControllerTests()
        {
            _settingsManager    = ScriptSettingsManager.Instance;
            _testFunctions      = new Collection <FunctionDescriptor>();
            _testFunctionErrors = new Dictionary <string, Collection <string> >();

            string rootScriptPath = @"c:\test\functions";
            var    environment    = new NullScriptHostEnvironment();
            var    eventManager   = new Mock <IScriptEventManager>();
            var    mockRouter     = new Mock <IWebJobsRouter>();

            var settings = new ScriptApplicationHostOptions()
            {
                ScriptPath  = rootScriptPath,
                SecretsPath = _secretsDirectory.Path
            };

            _secretsManagerMock = new Mock <ISecretManager>(MockBehavior.Strict);

            var fileSystem = new Mock <IFileSystem>();
            var fileBase   = new Mock <FileBase>();
            var dirBase    = new Mock <DirectoryBase>();

            fileSystem.SetupGet(f => f.File).Returns(fileBase.Object);
            fileBase.Setup(f => f.ReadAllText(Path.Combine(rootScriptPath, "TestFunction1", ScriptConstants.FunctionMetadataFileName))).Returns("{}");
            fileBase.Setup(f => f.ReadAllText(Path.Combine(rootScriptPath, "TestFunction2", ScriptConstants.FunctionMetadataFileName))).Returns("{}");
            fileBase.Setup(f => f.ReadAllText(Path.Combine(rootScriptPath, "DNE", ScriptConstants.FunctionMetadataFileName))).Throws(new DirectoryNotFoundException());

            _functionsSyncManagerMock = new Mock <IFunctionsSyncManager>(MockBehavior.Strict);
            _functionsSyncManagerMock.Setup(p => p.TrySyncTriggersAsync(false)).ReturnsAsync(new SyncTriggersResult {
                Success = true
            });
            _testController = new KeysController(new OptionsWrapper <ScriptApplicationHostOptions>(settings), new TestSecretManagerProvider(_secretsManagerMock.Object), new LoggerFactory(), fileSystem.Object, _functionsSyncManagerMock.Object);

            var keys = new Dictionary <string, string>
            {
                { "key1", "secret1" }
            };

            _secretsManagerMock.Setup(p => p.GetFunctionSecretsAsync("TestFunction1", false)).ReturnsAsync(keys);

            keys = new Dictionary <string, string>
            {
                { "key1", "secret1" }
            };
            _secretsManagerMock.Setup(p => p.GetFunctionSecretsAsync("TestFunction2", false)).ReturnsAsync(keys);

            _secretsManagerMock.Setup(p => p.GetFunctionSecretsAsync("DNE", false)).ReturnsAsync((IDictionary <string, string>)null);

            SetHttpContext();
        }
        public static ExtensionBundleOptions GetExtensionBundleOptions(ScriptApplicationHostOptions hostOptions = null)
        {
            hostOptions = hostOptions ?? SelfHostWebHostSettingsFactory.Create(Environment.CurrentDirectory);
            IConfigurationBuilder builder = new ConfigurationBuilder();

            builder.Add(new HostJsonFileConfigurationSource(hostOptions, SystemEnvironment.Instance, loggerFactory: NullLoggerFactory.Instance));
            var configuration = builder.Build();

            var configurationHelper = new ExtensionBundleConfigurationHelper(configuration, SystemEnvironment.Instance);
            var options             = new ExtensionBundleOptions();

            configurationHelper.Configure(options);
            return(options);
        }
Exemplo n.º 3
0
            public TestFixture()
            {
                // copy test files to temp directory, since accessing the metadata APIs will result
                // in file creations (for test data files)
                var scriptSource = Path.Combine(Environment.CurrentDirectory, @"..\..\..\TestScripts\Proxies");

                _testHome = Path.Combine(Path.GetTempPath(), @"ProxyTests");
                var scriptRoot = Path.Combine(_testHome, @"site\wwwroot");

                FileUtility.CopyDirectory(scriptSource, scriptRoot);

                HostOptions = new ScriptApplicationHostOptions
                {
                    IsSelfHost   = true,
                    ScriptPath   = scriptRoot,
                    LogPath      = Path.Combine(_testHome, @"LogFiles\Application\Functions"),
                    SecretsPath  = Path.Combine(_testHome, @"data\Functions\Secrets"),
                    TestDataPath = Path.Combine(_testHome, @"data\Functions\SampleData")
                };

                FileUtility.EnsureDirectoryExists(HostOptions.TestDataPath);

                var optionsMonitor = TestHelpers.CreateOptionsMonitor(HostOptions);

                var workerOptions = new LanguageWorkerOptions
                {
                    WorkerConfigs = TestHelpers.GetTestWorkerConfigs()
                };

                var provider = new FunctionMetadataProvider(optionsMonitor, new OptionsWrapper <LanguageWorkerOptions>(workerOptions), NullLogger <FunctionMetadataProvider> .Instance);

                var builder = AspNetCore.WebHost.CreateDefaultBuilder()
                              .UseStartup <Startup>()
                              .ConfigureServices(services =>
                {
                    services.Replace(new ServiceDescriptor(typeof(IOptions <ScriptApplicationHostOptions>), new OptionsWrapper <ScriptApplicationHostOptions>(HostOptions)));
                    services.Replace(new ServiceDescriptor(typeof(ISecretManagerProvider), new TestSecretManagerProvider(new TestSecretManager())));
                    services.Replace(new ServiceDescriptor(typeof(IOptionsMonitor <ScriptApplicationHostOptions>), optionsMonitor));
                    services.Replace(new ServiceDescriptor(typeof(IFunctionMetadataProvider), provider));
                });

                _testServer = new TestServer(builder);
                HostOptions.RootServiceProvider = _testServer.Host.Services;
                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();
        }
        public static IHostBuilder ConfigureDefaultTestWebScriptHost(this IHostBuilder builder, Action <IWebJobsBuilder> configureWebJobs,
                                                                     Action <ScriptApplicationHostOptions> configure = null, bool runStartupHostedServices = false, Action <IServiceCollection> configureRootServices = null)
        {
            var webHostOptions = new ScriptApplicationHostOptions()
            {
                IsSelfHost = true,
                ScriptPath = TestHelpers.FunctionsTestDirectory,
                LogPath    = TestHelpers.GetHostLogFileDirectory().FullName
            };
            TestMetricsLogger metricsLogger = new TestMetricsLogger();

            configure?.Invoke(webHostOptions);

            // Register root services
            var services = new ServiceCollection();

            AddMockedSingleton <IDebugStateProvider>(services);
            AddMockedSingleton <IScriptHostManager>(services);
            AddMockedSingleton <IEnvironment>(services);
            AddMockedSingleton <IScriptWebHostEnvironment>(services);
            AddMockedSingleton <IEventGenerator>(services);
            AddMockedSingleton <IFunctionInvocationDispatcherFactory>(services);
            AddMockedSingleton <IHttpWorkerService>(services);
            AddMockedSingleton <IApplicationLifetime>(services);
            AddMockedSingleton <IDependencyValidator>(services);
            services.AddSingleton <HostNameProvider>();
            services.AddSingleton <IMetricsLogger>(metricsLogger);
            services.AddWebJobsScriptHostRouting();
            services.AddLogging();
            services.AddFunctionMetadataManager(webHostOptions, metricsLogger);
            configureRootServices?.Invoke(services);

            var rootProvider = new WebHostServiceProvider(services);

            builder
            .AddWebScriptHost(rootProvider, rootProvider, webHostOptions, configureWebJobs)
            .ConfigureAppConfiguration(c =>
            {
                c.AddTestSettings();
            });

            if (!runStartupHostedServices)
            {
                builder.ConfigureServices(s => s.RemoveAll <IHostedService>());
            }

            webHostOptions.RootServiceProvider = rootProvider;
            return(builder);
        }
Exemplo n.º 6
0
        private void InitializeTestHost(string testDirName)
        {
            var httpConfig         = new HttpConfiguration();
            var uniqueTestRootPath = Path.Combine(_testRootPath, testDirName, Guid.NewGuid().ToString());

            _loggerProvider = new TestLoggerProvider();

            _webHostOptions = new ScriptApplicationHostOptions
            {
                IsSelfHost  = true,
                LogPath     = Path.Combine(uniqueTestRootPath, "Logs"),
                SecretsPath = Path.Combine(uniqueTestRootPath, "Secrets"),
                ScriptPath  = Path.Combine(uniqueTestRootPath, "WWWRoot")
            };

            if (_settingsManager.IsAppServiceEnvironment)
            {
                // if the test is mocking App Service environment, we need
                // to also set the HOME variable
                Environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteHomePath, uniqueTestRootPath);
            }

            var loggerFactory = new LoggerFactory();

            loggerFactory.AddProvider(_loggerProvider);

            var webHostBuilder = Program.CreateWebHostBuilder()
                                 .ConfigureServices(c =>
            {
                c.AddSingleton(_webHostOptions)
                .AddSingleton <ILoggerProvider>(_loggerProvider)
                .AddSingleton <ILoggerFactory>(loggerFactory);
            });

            _httpServer             = new TestServer(webHostBuilder);
            _httpClient             = _httpServer.CreateClient();
            _httpClient.BaseAddress = new Uri("https://localhost/");

            TestHelpers.WaitForWebHost(_httpClient);

            var traces = _loggerProvider.GetAllLogMessages().ToArray();

            Assert.NotNull(traces.Single(p => p.FormattedMessage.StartsWith("Starting Host (HostId=placeholder-host")));
            Assert.NotNull(traces.Single(p => p.FormattedMessage.StartsWith("Host is in standby mode")));

            var hostConfig = _webHostOptions.ToScriptHostConfiguration(true);
            // TODO: DI (FACAVAL) Review
            // _expectedHostId = hostConfig.HostConfig.HostId;
        }
        public FunctionsSyncManagerTests()
        {
            _testRootScriptPath     = Path.GetTempPath();
            _testHostConfigFilePath = Path.Combine(_testRootScriptPath, ScriptConstants.HostMetadataFileName);
            FileUtility.DeleteFileSafe(_testHostConfigFilePath);

            _hostOptions = new ScriptApplicationHostOptions
            {
                ScriptPath   = @"x:\root",
                IsSelfHost   = false,
                LogPath      = @"x:\tmp\log",
                SecretsPath  = @"x:\secrets",
                TestDataPath = @"x:\test"
            };

            _vars = new Dictionary <string, string>
            {
                { EnvironmentSettingNames.WebSiteAuthEncryptionKey, TestHelpers.GenerateKeyHexString() },
                { EnvironmentSettingNames.AzureWebsiteHostName, "appName.azurewebsites.net" }
            };

            ResetMockFileSystem();

            _loggerProvider = new TestLoggerProvider();
            var loggerFactory = new LoggerFactory();

            loggerFactory.AddProvider(_loggerProvider);
            _contentBuilder  = new StringBuilder();
            _mockHttpHandler = new MockHttpHandler(_contentBuilder);
            var httpClient                = CreateHttpClient(_mockHttpHandler);
            var factory                   = new TestOptionsFactory <ScriptApplicationHostOptions>(_hostOptions);
            var tokenSource               = new TestChangeTokenSource();
            var changeTokens              = new[] { tokenSource };
            var optionsMonitor            = new OptionsMonitor <ScriptApplicationHostOptions>(factory, changeTokens, factory);
            var secretManagerProviderMock = new Mock <ISecretManagerProvider>(MockBehavior.Strict);
            var secretManagerMock         = new Mock <ISecretManager>(MockBehavior.Strict);

            secretManagerProviderMock.SetupGet(p => p.Current).Returns(secretManagerMock.Object);

            var configuration      = ScriptSettingsManager.BuildDefaultConfiguration();
            var hostIdProviderMock = new Mock <IHostIdProvider>(MockBehavior.Strict);

            hostIdProviderMock.Setup(p => p.GetHostIdAsync(CancellationToken.None)).ReturnsAsync("testhostid123");
            _functionsSyncManager = new FunctionsSyncManager(configuration, hostIdProviderMock.Object, optionsMonitor, new OptionsWrapper <LanguageWorkerOptions>(CreateLanguageWorkerConfigSettings()), loggerFactory, httpClient, secretManagerProviderMock.Object);

            _expectedSyncTriggersPayload = "[{\"authLevel\":\"anonymous\",\"type\":\"httpTrigger\",\"direction\":\"in\",\"name\":\"req\",\"functionName\":\"function1\"}," +
                                           "{\"name\":\"myQueueItem\",\"type\":\"orchestrationTrigger\",\"direction\":\"in\",\"queueName\":\"myqueue-items\",\"connection\":\"DurableStorage\",\"functionName\":\"function2\",\"taskHubName\":\"TestHubValue\"}," +
                                           "{\"name\":\"myQueueItem\",\"type\":\"activityTrigger\",\"direction\":\"in\",\"queueName\":\"myqueue-items\",\"connection\":\"DurableStorage\",\"functionName\":\"function3\",\"taskHubName\":\"TestHubValue\"}]";
        }
Exemplo n.º 8
0
        public RpcInitializationServiceTests()
        {
            _mockLanguageWorkerChannelManager = new Mock <ILanguageWorkerChannelManager>();
            _loggerFactory = new LoggerFactory();

            var applicationHostOptions = new ScriptApplicationHostOptions
            {
                IsSelfHost = true,
                ScriptPath = Path.GetTempPath()
            };

            _optionsMonitor = TestHelpers.CreateOptionsMonitor(applicationHostOptions);
            _mockLanguageWorkerChannelManager.Setup(m => m.InitializeChannelAsync(It.IsAny <string>()))
            .Returns(Task.CompletedTask);
        }
        public WebFunctionsManagerTests()
        {
            _testRootScriptPath     = Path.GetTempPath();
            _testHostConfigFilePath = Path.Combine(_testRootScriptPath, ScriptConstants.HostMetadataFileName);
            FileUtility.DeleteFileSafe(_testHostConfigFilePath);

            _hostOptions = new ScriptApplicationHostOptions
            {
                ScriptPath   = @"x:\root",
                IsSelfHost   = false,
                LogPath      = @"x:\tmp\log",
                SecretsPath  = @"x:\secrets",
                TestDataPath = @"x:\test"
            };
        }
        public DebugStateProviderTests()
        {
            _options = new ScriptApplicationHostOptions
            {
                LogPath = Path.Combine(Directory.GetCurrentDirectory(), "DebugPath1")
            };

            var factory = new TestOptionsFactory <ScriptApplicationHostOptions>(_options);

            _tokenSource = new TestChangeTokenSource <ScriptApplicationHostOptions>();
            var changeTokens   = new[] { _tokenSource };
            var optionsMonitor = new OptionsMonitor <ScriptApplicationHostOptions>(factory, changeTokens, factory);

            _provider = new DebugStateProvider(optionsMonitor, new ScriptEventManager());
        }
        public void Configure(ScriptJobHostOptions options)
        {
            // Add the standard built in watched directories set to any the user may have specified
            options.WatchDirectories.Add("node_modules");

            // Add the default files we need to watch
            options.WatchFiles.Add(ScriptConstants.HostMetadataFileName);
            options.WatchFiles.Add(ScriptConstants.FunctionMetadataFileName);
            options.WatchFiles.Add(ScriptConstants.ProxyMetadataFileName);

            // Set default logging mode
            options.FileLoggingMode = FileLoggingMode.DebugOnly;

            // Bind to all configuration properties
            IConfigurationSection jobHostSection = _configuration.GetSection(ConfigurationSectionNames.JobHost);

            if (jobHostSection != null)
            {
                jobHostSection.Bind(options);

                var fileLoggingMode = jobHostSection.GetSection(ConfigurationSectionNames.Logging)
                                      ?.GetValue <FileLoggingMode?>("fileLoggingMode");
                if (fileLoggingMode != null)
                {
                    options.FileLoggingMode = fileLoggingMode.Value;
                }
                Utility.ValidateRetryOptions(options.Retry);
            }

            // FunctionTimeout
            ConfigureFunctionTimeout(options);

            // If we have a read only file system, override any configuration and
            // disable file watching
            if (_applicationHostOptions.Value.IsFileSystemReadOnly)
            {
                options.FileWatchingEnabled = false;
            }

            // Set the root script path to the value the runtime was initialized with:
            ScriptApplicationHostOptions webHostOptions = _applicationHostOptions.Value;

            options.RootScriptPath       = webHostOptions.ScriptPath;
            options.RootLogPath          = webHostOptions.LogPath;
            options.IsSelfHost           = webHostOptions.IsSelfHost;
            options.TestDataPath         = webHostOptions.TestDataPath;
            options.IsFileSystemReadOnly = webHostOptions.IsFileSystemReadOnly;
        }
        public void GetDefaultHostId_AzureHost_ReturnsExpectedResult(string siteName, string expected)
        {
            var options = new ScriptApplicationHostOptions
            {
                ScriptPath = @"c:\testscripts"
            };

            var environment = new TestEnvironment();

            environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteInstanceId, "123123");
            environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteName, siteName);

            string hostId = ScriptHostIdProvider.GetDefaultHostId(environment, options);

            Assert.Equal(expected, hostId);
        }
Exemplo n.º 13
0
        public WebFunctionsManagerTests()
        {
            _testRootScriptPath = Path.GetTempPath();
            _testHostConfigFilePath = Path.Combine(_testRootScriptPath, ScriptConstants.HostMetadataFileName);
            FileUtility.DeleteFileSafe(_testHostConfigFilePath);

            _hostOptions = new ScriptApplicationHostOptions
            {
                ScriptPath = @"x:\root",
                IsSelfHost = false,
                LogPath = @"x:\tmp\log",
                SecretsPath = @"x:\secrets",
                TestDataPath = @"x:\test"
            };

            string functionsPath = Path.Combine(Environment.CurrentDirectory, @"..\..\..\..\..\sample");

            var fileSystem = CreateFileSystem(_hostOptions);
            var loggerFactory = MockNullLoggerFactory.CreateLoggerFactory();
            var contentBuilder = new StringBuilder();
            var httpClient = CreateHttpClient(contentBuilder);
            var factory = new TestOptionsFactory<ScriptApplicationHostOptions>(_hostOptions);
            var tokenSource = new TestChangeTokenSource();
            var changeTokens = new[] { tokenSource };
            var optionsMonitor = new OptionsMonitor<ScriptApplicationHostOptions>(factory, changeTokens, factory);
            var secretManagerProviderMock = new Mock<ISecretManagerProvider>(MockBehavior.Strict);
            var secretManagerMock = new Mock<ISecretManager>(MockBehavior.Strict);
            secretManagerProviderMock.SetupGet(p => p.Current).Returns(secretManagerMock.Object);
            var hostSecretsInfo = new HostSecretsInfo();
            secretManagerMock.Setup(p => p.GetHostSecretsAsync()).ReturnsAsync(hostSecretsInfo);
            Dictionary<string, string> functionSecrets = new Dictionary<string, string>();
            secretManagerMock.Setup(p => p.GetFunctionSecretsAsync("httptrigger", false)).ReturnsAsync(functionSecrets);

            var configurationMock = new Mock<IConfiguration>(MockBehavior.Strict);
            var hostIdProviderMock = new Mock<IHostIdProvider>(MockBehavior.Strict);
            var mockWebHostEnvironment = new Mock<IScriptWebHostEnvironment>(MockBehavior.Strict);
            mockWebHostEnvironment.SetupGet(p => p.InStandbyMode).Returns(false);
            _mockEnvironment = new Mock<IEnvironment>(MockBehavior.Strict);
            _mockEnvironment.Setup(p => p.GetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteContainerReady)).Returns("1");
            _mockEnvironment.Setup(p => p.GetEnvironmentVariable(EnvironmentSettingNames.CoreToolsEnvironment)).Returns((string)null);
            _mockEnvironment.Setup(p => p.GetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteHostName)).Returns(TestHostName);
            var hostNameProvider = new HostNameProvider(_mockEnvironment.Object, loggerFactory.CreateLogger<HostNameProvider>());
            var functionsSyncManager = new FunctionsSyncManager(configurationMock.Object, hostIdProviderMock.Object, optionsMonitor, new OptionsWrapper<LanguageWorkerOptions>(CreateLanguageWorkerConfigSettings()), loggerFactory.CreateLogger<FunctionsSyncManager>(), httpClient, secretManagerProviderMock.Object, mockWebHostEnvironment.Object, _mockEnvironment.Object, hostNameProvider);
            _webFunctionsManager = new WebFunctionsManager(optionsMonitor, new OptionsWrapper<LanguageWorkerOptions>(CreateLanguageWorkerConfigSettings()), loggerFactory, httpClient, secretManagerProviderMock.Object, functionsSyncManager, hostNameProvider);

            FileUtility.Instance = fileSystem;
        }
Exemplo n.º 14
0
        private void ValidateAndBuildHostJsonConfigurationIfFileExists(ScriptApplicationHostOptions hostOptions)
        {
            bool IsPreCompiledApp = IsPreCompiledFunctionApp();
            var  hostJsonPath     = Path.Combine(Environment.CurrentDirectory, Constants.HostJsonFileName);

            if (IsPreCompiledApp && !File.Exists(hostJsonPath))
            {
                throw new CliException($"Host.json file in missing. Please make sure host.json file is present at {Environment.CurrentDirectory}");
            }

            //BuildHostJsonConfigutation only if host.json file exists.
            _hostJsonConfig = Utilities.BuildHostJsonConfigutation(hostOptions);
            if (IsPreCompiledApp && Utilities.JobHostConfigSectionExists(_hostJsonConfig, ConfigurationSectionNames.ExtensionBundle))
            {
                throw new CliException($"Extension bundle configuration should not be present for the function app with pre-compiled functions. Please remove extension bundle configuration from host.json: {Path.Combine(Environment.CurrentDirectory, "host.json")}");
            }
        }
Exemplo n.º 15
0
        private async Task <IWebHost> BuildWebHost(ScriptApplicationHostOptions hostOptions, Uri listenAddress, Uri baseAddress, X509Certificate2 certificate)
        {
            IDictionary <string, string> settings = await GetConfigurationSettings(hostOptions.ScriptPath, baseAddress);

            settings.AddRange(LanguageWorkerHelper.GetWorkerConfiguration(LanguageWorkerSetting));
            UpdateEnvironmentVariables(settings);

            LoggingFilterHelper loggingFilterHelper = new LoggingFilterHelper(_hostJsonConfig, VerboseLogging);

            var defaultBuilder = Microsoft.AspNetCore.WebHost.CreateDefaultBuilder(Array.Empty <string>());

            if (UseHttps)
            {
                defaultBuilder
                .UseKestrel(options =>
                {
                    options.Listen(IPAddress.Any, listenAddress.Port, listenOptins =>
                    {
                        listenOptins.UseHttps(certificate);
                    });
                });
            }
            return(defaultBuilder
                   .UseSetting(WebHostDefaults.ApplicationKey, typeof(Startup).Assembly.GetName().Name)
                   .UseUrls(listenAddress.ToString())
                   .ConfigureAppConfiguration(configBuilder =>
            {
                configBuilder.AddEnvironmentVariables();
            })
                   .ConfigureLogging(loggingBuilder =>
            {
                loggingBuilder.ClearProviders();
                loggingBuilder.Services.AddSingleton <ILoggerProvider>(p =>
                {
                    //Cache LoggerFilterOptions to be used by the logger to filter logs based on content
                    var filterOptions = p.GetService <IOptions <LoggerFilterOptions> >().Value;
                    // Set min level to SystemLogDefaultLogLevel.
                    filterOptions.MinLevel = loggingFilterHelper.SystemLogDefaultLogLevel;
                    return new ColoredConsoleLoggerProvider(loggingFilterHelper, filterOptions);
                });
                // This is needed to filter system logs only for known categories
                loggingBuilder.AddDefaultWebJobsFilters <ColoredConsoleLoggerProvider>(LogLevel.Trace);
            })
                   .ConfigureServices((context, services) => services.AddSingleton <IStartup>(new Startup(context, hostOptions, CorsOrigins, CorsCredentials, EnableAuth, loggingFilterHelper)))
                   .Build());
        }
Exemplo n.º 16
0
        private void ConfiguredOptions(ScriptApplicationHostOptions options, bool inStandbyMode, IEnvironment environment = null, bool blobExists = false)
        {
            var builder       = new ConfigurationBuilder();
            var configuration = builder.Build();

            var standbyOptions = new TestOptionsMonitor <StandbyOptions>(new StandbyOptions {
                InStandbyMode = inStandbyMode
            });
            var mockServiceProvider = new Mock <IServiceProvider>();
            var mockEnvironment     = environment ?? new TestEnvironment();
            var setup = new TestScriptApplicationHostOptionsSetup(configuration, standbyOptions, mockServiceProvider.Object, mockEnvironment)
            {
                BlobExistsReturnValue = blobExists
            };

            setup.Configure(options);
        }
Exemplo n.º 17
0
        public InstanceManager(IOptions <ScriptApplicationHostOptions> webHostSettings, ILoggerFactory loggerFactory, HttpClient client, IScriptWebHostEnvironment webHostEnvironment)
        {
            if (webHostSettings == null)
            {
                throw new ArgumentNullException(nameof(webHostSettings));
            }

            if (loggerFactory == null)
            {
                throw new ArgumentNullException(nameof(loggerFactory));
            }

            _client             = client ?? throw new ArgumentNullException(nameof(client));
            _webHostEnvironment = webHostEnvironment ?? throw new ArgumentNullException(nameof(webHostEnvironment));
            _webHostSettings    = webHostSettings.Value;
            _logger             = loggerFactory.CreateLogger(LogCategories.Startup);
        }
        public async Task GetHostIdAsync_WithConfigurationHostId_ReturnsConfigurationHostId()
        {
            var options     = new ScriptApplicationHostOptions();
            var environment = new TestEnvironment();

            var config = new ConfigurationBuilder()
                         .AddInMemoryCollection(new Dictionary <string, string>
            {
                { ConfigurationPath.Combine(ConfigurationSectionNames.JobHost, "id"), "test-host-id" }
            })
                         .Build();

            var provider = new ScriptHostIdProvider(config, environment, new TestOptionsMonitor <ScriptApplicationHostOptions>(options));

            string hostId = await provider.GetHostIdAsync(CancellationToken.None);

            Assert.Equal("test-host-id", hostId);
        }
Exemplo n.º 19
0
        private ScriptApplicationHostOptions CreateConfiguredOptions(bool inStandbyMode, IEnvironment environment = null)
        {
            var builder       = new ConfigurationBuilder();
            var configuration = builder.Build();

            var standbyOptions = new TestOptionsMonitor <StandbyOptions>(new StandbyOptions {
                InStandbyMode = inStandbyMode
            });
            var mockCache           = new Mock <IOptionsMonitorCache <ScriptApplicationHostOptions> >();
            var mockServiceProvider = new Mock <IServiceProvider>();
            var mockEnvironment     = environment ?? new TestEnvironment();
            var setup = new ScriptApplicationHostOptionsSetup(configuration, standbyOptions, mockCache.Object, mockServiceProvider.Object, mockEnvironment);

            var options = new ScriptApplicationHostOptions();

            setup.Configure(options);
            return(options);
        }
Exemplo n.º 20
0
        private Task <ScriptApplicationHostOptions> GetWebHostSettings()
        {
            var home     = _settingsManager.GetSetting(EnvironmentSettingNames.AzureWebsiteHomePath);
            var settings = new ScriptApplicationHostOptions
            {
                IsSelfHost  = true,
                ScriptPath  = Path.Combine(home, @"site\wwwroot"),
                LogPath     = Path.Combine(home, @"LogFiles\Application\Functions"),
                SecretsPath = Path.Combine(home, @"data\Functions\secrets")
            };

            Directory.CreateDirectory(settings.ScriptPath);
            Directory.CreateDirectory(settings.LogPath);
            Directory.CreateDirectory(settings.SecretsPath);

            return(Task.Delay(200)
                   .ContinueWith(t => settings));
        }
        public void ReadFunctionMetadata_Succeeds()
        {
            string functionsPath = Path.Combine(Environment.CurrentDirectory, @"..\..\..\..\..\sample\node");
            var    scriptApplicationHostOptions = new ScriptApplicationHostOptions()
            {
                ScriptPath = functionsPath
            };

            var optionsMonitor = TestHelpers.CreateOptionsMonitor(scriptApplicationHostOptions);
            var workerOptions  = new LanguageWorkerOptions
            {
                WorkerConfigs = TestHelpers.GetTestWorkerConfigs()
            };

            var metadataProvider = new FunctionMetadataProvider(optionsMonitor, new OptionsWrapper <LanguageWorkerOptions>(workerOptions), NullLogger <FunctionMetadataProvider> .Instance);

            Assert.Equal(17, metadataProvider.GetFunctionMetadata(false).Length);
        }
Exemplo n.º 22
0
        public void Configure_InStandbyMode_ReturnsExpectedConfiguration()
        {
            var settings = new Dictionary <string, string>
            {
                { EnvironmentSettingNames.AzureWebsitePlaceholderMode, "1" }
            };

            ScriptApplicationHostOptionsSetup setup = CreateSetupWithConfiguration(new TestEnvironment(settings));

            var options = new ScriptApplicationHostOptions();

            setup.Configure(options);

            Assert.EndsWith(@"functions\standby\logs", options.LogPath);
            Assert.EndsWith(@"functions\standby\wwwroot", options.ScriptPath);
            Assert.EndsWith(@"functions\standby\secrets", options.SecretsPath);
            Assert.False(options.IsSelfHost);
        }
        public RpcInitializationServiceTests()
        {
            _mockLanguageWorkerChannelManager = new Mock <IWebHostLanguageWorkerChannelManager>();
            _loggerFactory = new LoggerFactory();
            _logger        = _loggerFactory.CreateLogger <RpcInitializationService>();

            var applicationHostOptions = new ScriptApplicationHostOptions
            {
                IsSelfHost = true,
                ScriptPath = Path.GetTempPath()
            };

            _optionsMonitor = TestHelpers.CreateOptionsMonitor(applicationHostOptions);
            ILanguageWorkerChannel testLanguageWorkerChannel = new TestLanguageWorkerChannel(Guid.NewGuid().ToString(), LanguageWorkerConstants.NodeLanguageWorkerName);

            _mockLanguageWorkerChannelManager.Setup(m => m.InitializeChannelAsync(It.IsAny <string>()))
            .Returns(Task.FromResult <ILanguageWorkerChannel>(testLanguageWorkerChannel));
        }
Exemplo n.º 24
0
        public void Configure(ScriptJobHostOptions options)
        {
            // Add the standard built in watched directories set to any the user may have specified
            options.WatchDirectories.Add("node_modules");

            // Set default logging mode
            options.FileLoggingMode = FileLoggingMode.DebugOnly;

            // Bind to all configuration properties
            IConfigurationSection jobHostSection = _configuration.GetSection(ConfigurationSectionNames.JobHost);

            if (jobHostSection != null)
            {
                jobHostSection.Bind(options);

                var fileLoggingMode = jobHostSection.GetSection(ConfigurationSectionNames.Logging)
                                      ?.GetValue <FileLoggingMode?>("fileLoggingMode");
                if (fileLoggingMode != null)
                {
                    options.FileLoggingMode = fileLoggingMode.Value;
                }
            }

            // FunctionTimeout
            ConfigureFunctionTimeout(jobHostSection, options);

            // Worker configuration
            ConfigureLanguageWorkers(jobHostSection, options);

            // If we have a read only file system, override any configuration and
            // disable file watching
            if (_environment.FileSystemIsReadOnly())
            {
                options.FileWatchingEnabled = false;
            }

            // Set the root script path to the value the runtime was initialized with:
            ScriptApplicationHostOptions webHostOptions = _applicationHostOptions.Value;

            options.RootScriptPath = webHostOptions.ScriptPath;
            options.RootLogPath    = webHostOptions.LogPath;
            options.IsSelfHost     = webHostOptions.IsSelfHost;
            options.TestDataPath   = webHostOptions.TestDataPath;
        }
        public virtual async Task InitializeAsync()
        {
            _config          = new HttpConfiguration();
            _settingsManager = ScriptSettingsManager.Instance;

            HostOptions = new ScriptApplicationHostOptions
            {
                IsSelfHost     = true,
                ScriptPath     = Path.Combine(Environment.CurrentDirectory, @"..\..\..\..\..\sample"),
                LogPath        = Path.Combine(Path.GetTempPath(), @"Functions"),
                SecretsPath    = Path.Combine(Path.GetTempPath(), @"FunctionsTests\Secrets"),
                HasParentScope = true
            };

            var factory        = new TestOptionsFactory <ScriptApplicationHostOptions>(HostOptions);
            var optionsMonitor = new OptionsMonitor <ScriptApplicationHostOptions>(factory, Array.Empty <IOptionsChangeTokenSource <ScriptApplicationHostOptions> >(), factory);
            var webHostBuilder = new WebHostBuilder()
                                 .ConfigureServices(services =>
            {
                services.Replace(ServiceDescriptor.Singleton <IServiceProviderFactory <IServiceCollection> >(new WebHostServiceProviderFactory()));
                services.AddSingleton <IOptions <ScriptApplicationHostOptions> >(new OptionsWrapper <ScriptApplicationHostOptions>(HostOptions));
                services.Replace(new ServiceDescriptor(typeof(IOptionsMonitor <ScriptApplicationHostOptions>), optionsMonitor));
            })
                                 .ConfigureAppConfiguration((builderContext, config) =>
            {
                config.Add(new WebScriptHostConfigurationSource
                {
                    IsAppServiceEnvironment     = SystemEnvironment.Instance.IsAppServiceEnvironment(),
                    IsLinuxContainerEnvironment = SystemEnvironment.Instance.IsLinuxContainerEnvironment()
                });
            })
                                 .UseStartup <Startup>()
                                 .ConfigureAppConfiguration(c => c.AddEnvironmentVariables());

            ConfigureWebHostBuilder(webHostBuilder);

            HttpServer = new TestServer(webHostBuilder);

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

            var manager = HttpServer.Host.Services.GetService <IScriptHostManager>();
            await manager.DelayUntilHostReady();
        }
Exemplo n.º 26
0
        public override async Task RunAsync()
        {
            await PreRunConditions();

            var isVerbose = VerboseLogging.HasValue && VerboseLogging.Value;

            if (isVerbose || EnvironmentHelper.GetEnvironmentVariableAsBool(Constants.DisplayLogo))
            {
                Utilities.PrintLogo();
            }

            // Suppress AspNetCoreSupressStatusMessages
            EnvironmentHelper.SetEnvironmentVariableAsBoolIfNotExists(Constants.AspNetCoreSupressStatusMessages);

            Utilities.PrintVersion();

            ScriptApplicationHostOptions hostOptions = SelfHostWebHostSettingsFactory.Create(Environment.CurrentDirectory);

            ValidateAndBuildHostJsonConfigurationIfFileExists(hostOptions);

            (var listenUri, var baseUri, var certificate) = await Setup();

            IWebHost host = await BuildWebHost(hostOptions, listenUri, baseUri, certificate);

            var runTask = host.RunAsync();

            var hostService = host.Services.GetRequiredService <WebJobsScriptHostService>();

            await hostService.DelayUntilHostReady();

            var scriptHost  = hostService.Services.GetRequiredService <IScriptJobHost>();
            var httpOptions = hostService.Services.GetRequiredService <IOptions <HttpOptions> >();

            if (scriptHost != null && scriptHost.Functions.Any())
            {
                DisplayFunctionsInfoUtilities.DisplayFunctionsInfo(scriptHost.Functions, httpOptions.Value, baseUri);
            }
            if (VerboseLogging == null || !VerboseLogging.Value)
            {
                ColoredConsole.WriteLine(AdditionalInfoColor("For detailed output, run func with --verbose flag."));
            }
            await runTask;
        }
        public async Task HostInitialization_OnInitializationException_MaintainsErrorInformation()
        {
            var options = new ScriptApplicationHostOptions
            {
                ScriptPath = @"c:\tests",
                LogPath    = @"c:\tests\logs",
            };

            var monitor = new ScriptApplicationHostOptionsMonitor(options);

            var services = new ServiceCollection()
                           .AddLogging()
                           .BuildServiceProvider();

            var host = new Mock <IHost>();

            host.Setup(h => h.Services)
            .Returns(services);
            host.SetupSequence(h => h.StartAsync(It.IsAny <CancellationToken>()))
            .Throws(new HostInitializationException("boom"))
            .Returns(Task.CompletedTask);

            var hostBuilder = new Mock <IScriptHostBuilder>();

            hostBuilder.Setup(b => b.BuildHost(It.IsAny <bool>(), It.IsAny <bool>()))
            .Returns(host.Object);

            var mockRootServiceProvider      = new Mock <IServiceProvider>();
            var mockRootScopeFactory         = new Mock <IServiceScopeFactory>();
            var mockScriptWebHostEnvironment = new Mock <IScriptWebHostEnvironment>();
            var mockEnvironment        = new Mock <IEnvironment>();
            var healthMonitorOptions   = new OptionsWrapper <HostHealthMonitorOptions>(new HostHealthMonitorOptions());
            var hostPerformanceManager = new HostPerformanceManager(mockEnvironment.Object, healthMonitorOptions);

            var hostService = new WebJobsScriptHostService(
                monitor, hostBuilder.Object, NullLoggerFactory.Instance, mockRootServiceProvider.Object, mockRootScopeFactory.Object,
                mockScriptWebHostEnvironment.Object, mockEnvironment.Object, hostPerformanceManager, healthMonitorOptions);

            await hostService.StartAsync(CancellationToken.None);

            Assert.Equal(ScriptHostState.Error, hostService.State);
            Assert.IsType <HostInitializationException>(hostService.LastError);
        }
        public void GetDefaultHostId_AzureHost_ReturnsExpectedResult(string siteName, string azureWebsiteInstanceId, string expected, bool expectedTruncation)
        {
            var options = new ScriptApplicationHostOptions
            {
                ScriptPath = @"c:\testscripts"
            };

            var environment = new TestEnvironment();

            environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteInstanceId, azureWebsiteInstanceId);
            environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteName, siteName);
            environment.SetEnvironmentVariable(EnvironmentSettingNames.ContainerName, "testContainer");
            environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteHostName, "testsite.azurewebsites.net");
            var result = ScriptHostIdProvider.GetDefaultHostId(environment, options);

            Assert.Equal(expected, result.HostId);
            Assert.Equal(expectedTruncation, result.IsTruncated);
            Assert.False(result.IsLocal);
        }
Exemplo n.º 29
0
        public static async Task Main(string[] args)
        {
            if (args == null)
            {
                throw new ArgumentNullException("args");
            }

            string rootPath = Environment.CurrentDirectory;

            if (args.Length > 0)
            {
                rootPath = (string)args[0];
            }

            var options = new ScriptApplicationHostOptions
            {
                ScriptPath = rootPath,
                LogPath    = Path.Combine(Path.GetTempPath(), "functionshost"),
                IsSelfHost = true
            };

            var host = new HostBuilder()
                       .SetAzureFunctionsEnvironment()
                       .ConfigureLogging(b =>
            {
                b.SetMinimumLevel(LogLevel.Information);
                b.AddConsole();
            })
                       .AddScriptHost(options, webJobsBuilder =>
            {
                webJobsBuilder.AddAzureStorageCoreServices();
            })
                       .UseConsoleLifetime()
                       .Build();

            Console.WriteLine("Starting host");

            using (host)
            {
                await host.RunAsync();
            }
        }
Exemplo n.º 30
0
        public GrpcWorkerChannelTests()
        {
            _logger = new TestLogger("FunctionDispatcherTests");
            _testFunctionRpcService = new TestFunctionRpcService(_eventManager, _workerId, _logger, _expectedLogMsg);
            _testWorkerConfig       = TestHelpers.GetTestWorkerConfigs().FirstOrDefault();
            _mockrpcWorkerProcess.Setup(m => m.StartProcessAsync()).Returns(Task.CompletedTask);
            _testEnvironment = new TestEnvironment();
            ILogger <MemoryMappedFileAccessor> mmapAccessorLogger = NullLogger <MemoryMappedFileAccessor> .Instance;

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                _mapAccessor = new MemoryMappedFileAccessorWindows(mmapAccessorLogger);
            }
            else
            {
                _mapAccessor = new MemoryMappedFileAccessorUnix(mmapAccessorLogger, _testEnvironment);
            }
            _sharedMemoryManager = new SharedMemoryManager(_loggerFactory, _mapAccessor);

            var hostOptions = new ScriptApplicationHostOptions
            {
                IsSelfHost     = true,
                ScriptPath     = _scriptRootPath,
                LogPath        = Environment.CurrentDirectory, // not tested
                SecretsPath    = Environment.CurrentDirectory, // not tested
                HasParentScope = true
            };

            _hostOptionsMonitor = TestHelpers.CreateOptionsMonitor(hostOptions);

            _workerChannel = new GrpcWorkerChannel(
                _workerId,
                _eventManager,
                _testWorkerConfig,
                _mockrpcWorkerProcess.Object,
                _logger,
                _metricsLogger,
                0,
                _testEnvironment,
                _hostOptionsMonitor,
                _sharedMemoryManager);
        }