public EndToEndTestsBase(TTestFixture fixture) { _configuration = TestHelpers.GetTestConfiguration(); _nameResolver = new DefaultNameResolver(_configuration); Fixture = fixture; }
public Fixture() { TestSiteName = "TestSiteName"; var configuration = TestHelpers.GetTestConfiguration(); BlobConnectionString = configuration.GetWebJobsConnectionString(ConnectionStringNames.Storage); BlobContainer = CloudStorageAccount.Parse(BlobConnectionString).CreateCloudBlobClient().GetContainerReference("azure-webjobs-secrets"); }
public Fixture() { TestSiteName = "Test_test"; var configuration = TestHelpers.GetTestConfiguration(); BlobConnectionString = configuration.GetWebJobsConnectionString(ConnectionStringNames.Storage); KeyVaultConnectionString = configuration.GetWebJobsConnectionString(EnvironmentSettingNames.AzureWebJobsSecretStorageKeyVaultConnectionString); KeyVaultName = configuration.GetWebJobsConnectionString(EnvironmentSettingNames.AzureWebJobsSecretStorageKeyVaultName); AzureServiceTokenProvider azureServiceTokenProvider = new AzureServiceTokenProvider(KeyVaultConnectionString); KeyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback)); }
public Fixture() { TestSiteName = "Test_test"; var configuration = TestHelpers.GetTestConfiguration(); BlobConnectionString = configuration.GetWebJobsConnectionString(ConnectionStringNames.Storage); BlobContainer = CloudStorageAccount.Parse(BlobConnectionString).CreateCloudBlobClient().GetContainerReference("azure-webjobs-secrets"); KeyVaultConnectionString = configuration.GetWebJobsConnectionString(EnvironmentSettingNames.AzureWebJobsSecretStorageKeyVaultConnectionString); KeyVaultName = configuration.GetWebJobsConnectionString(EnvironmentSettingNames.AzureWebJobsSecretStorageKeyVaultName); AzureServiceTokenProvider azureServiceTokenProvider = new AzureServiceTokenProvider(KeyVaultConnectionString); KeyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback)); }
public async Task Specialization_CustomStartupAddsWebJobsStorage() { var storageValue = TestHelpers.GetTestConfiguration().GetWebJobsConnectionString("AzureWebJobsStorage"); // No AzureWebJobsStorage set in environment variables (App Settings from portal) using (new TestScopedEnvironmentVariable("AzureWebJobsStorage", "")) { var builder = CreateStandbyHostBuilder("FunctionExecutionContext") .ConfigureScriptHostWebJobsBuilder(s => { if (!_environment.IsPlaceholderModeEnabled()) { // Add an extension that calls GetUrl(), which can cause secrets to be loaded // before the host is initialized. s.Services.AddSingleton <IExtensionConfigProvider, TestWebHookExtension>(); } }) .ConfigureScriptHostAppConfiguration(c => { if (!_environment.IsPlaceholderModeEnabled()) { var inMemoryCollection = new Dictionary <string, string>() { { "AzureWebJobsStorage", storageValue } }; c.AddInMemoryCollection(inMemoryCollection); } }); // This is required to force secrets to load. _environment.SetEnvironmentVariable("WEBSITE_HOSTNAME", "test"); using (var testServer = new TestServer(builder)) { var client = testServer.CreateClient(); var response = await client.GetAsync("api/warmup"); response.EnsureSuccessStatusCode(); _environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteContainerReady, "1"); _environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsitePlaceholderMode, "0"); // Now that we're specialized, set up the expected env var, which will be loaded internally // when the config is refreshed during specialization. // This request will force specialization. response = await client.GetAsync("api/functionexecutioncontext"); response.EnsureSuccessStatusCode(); } } }
public async Task Specialization_CustomStartupRemovesAzureWebJobsStorage() { var storageValue = TestHelpers.GetTestConfiguration().GetWebJobsConnectionString("AzureWebJobsStorage"); // We can't assume the placeholder has any environment variables specified by the customer. // Add environment variables expected throughout the specialization (similar to how DWAS updates the environment) using (new TestScopedEnvironmentVariable("AzureWebJobsStorage", "")) { var builder = CreateStandbyHostBuilder("FunctionExecutionContext") .ConfigureScriptHostWebJobsBuilder(s => { if (!_environment.IsPlaceholderModeEnabled()) { // Add an extension that calls GetUrl(), which can cause secrets to be loaded // before the host is initialized. s.Services.AddSingleton <IExtensionConfigProvider, TestWebHookExtension>(); } }) .ConfigureScriptHostServices(s => { // Override the IConfiguration of the ScriptHost to empty configuration s.AddSingleton <IConfiguration>(new ConfigurationBuilder().Build()); }); // This is required to force secrets to load. _environment.SetEnvironmentVariable("WEBSITE_HOSTNAME", "test"); using (var testServer = new TestServer(builder)) { var client = testServer.CreateClient(); var response = await client.GetAsync("api/warmup"); response.EnsureSuccessStatusCode(); _environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteContainerReady, "1"); _environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsitePlaceholderMode, "0"); // This value is available now using (new TestScopedEnvironmentVariable("AzureWebJobsStorage", storageValue)) { // Now that we're specialized, set up the expected env var, which will be loaded internally // when the config is refreshed during specialization. // This request will force specialization. response = await client.GetAsync("api/functionexecutioncontext"); response.EnsureSuccessStatusCode(); } } } }
public Fixture() { TestSiteName = "Test_test"; Environment = new TestEnvironment(); var configuration = TestHelpers.GetTestConfiguration(); BlobConnectionString = configuration.GetWebJobsConnectionString(ConnectionStringNames.Storage); KeyVaultUri = configuration.GetWebJobsConnectionString(EnvironmentSettingNames.AzureWebJobsSecretStorageKeyVaultUri); KeyVaultClientId = configuration.GetWebJobsConnectionString(EnvironmentSettingNames.AzureWebJobsSecretStorageKeyVaultClientId); KeyVaultClientSecret = configuration.GetWebJobsConnectionString(EnvironmentSettingNames.AzureWebJobsSecretStorageKeyVaultClientSecret); KeyVaultTenantId = configuration.GetWebJobsConnectionString(EnvironmentSettingNames.AzureWebJobsSecretStorageKeyVaultTenantId); var credential = new ClientSecretCredential(KeyVaultTenantId, KeyVaultClientId, KeyVaultClientSecret); SecretClient = new SecretClient(new Uri(KeyVaultUri), credential); AzureBlobStorageProvider = TestHelpers.GetAzureBlobStorageProvider(configuration); }
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 Specialization_JobHostInternalStorageOptionsUpdatesWithActiveHost() { var storageValue = TestHelpers.GetTestConfiguration().GetWebJobsConnectionString("AzureWebJobsStorage"); var blobServiceClient = new BlobServiceClient(storageValue); var containerClient = blobServiceClient.GetBlobContainerClient("test-sas-container"); await containerClient.CreateIfNotExistsAsync(); // this will throw if storageValue is bad; var fakeSasUri = containerClient.GenerateSasUri(BlobContainerSasPermissions.Read | BlobContainerSasPermissions.Write | BlobContainerSasPermissions.List, DateTime.UtcNow.AddDays(10)); // We can't assume the placeholder has any environment variables specified by the customer. // Add environment variables expected throughout the specialization (similar to how DWAS updates the environment) using (new TestScopedEnvironmentVariable("AzureFunctionsJobHost__InternalSasBlobContainer", "")) using (new TestScopedEnvironmentVariable("AzureWebJobsStorage", "")) { var builder = CreateStandbyHostBuilder("FunctionExecutionContext") .ConfigureScriptHostWebJobsBuilder(s => { if (!_environment.IsPlaceholderModeEnabled()) { // Add an extension that calls GetUrl(), which can cause secrets to be loaded // before the host is initialized. s.Services.AddSingleton <IExtensionConfigProvider, TestWebHookExtension>(); } }); // This is required to force secrets to load. _environment.SetEnvironmentVariable("WEBSITE_HOSTNAME", "test"); using (var testServer = new TestServer(builder)) { var client = testServer.CreateClient(); var response = await client.GetAsync("api/warmup"); response.EnsureSuccessStatusCode(); // Should not be able to get the Hosting BlobContainerClient before specialization since // customer provided storage-related configuration is not in the Environment var blobStorageProvider = testServer.Services.GetService <IAzureBlobStorageProvider>(); Assert.False(blobStorageProvider.TryCreateHostingBlobContainerClient(out _)); _environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteContainerReady, "1"); _environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsitePlaceholderMode, "0"); // This value is available now using (new TestScopedEnvironmentVariable("AzureFunctionsJobHost__InternalSasBlobContainer", fakeSasUri.ToString())) using (new TestScopedEnvironmentVariable("AzureWebJobsStorage", storageValue)) { // Now that we're specialized, set up the expected env var, which will be loaded internally // when the config is refreshed during specialization. // This request will force specialization. response = await client.GetAsync("api/functionexecutioncontext"); response.EnsureSuccessStatusCode(); // The HostingBlobContainerClient should be the sas container specified. blobStorageProvider = testServer.Services.GetService <IAzureBlobStorageProvider>(); Assert.True(blobStorageProvider.TryCreateHostingBlobContainerClient(out var blobContainerClient)); Assert.Equal("test-sas-container", blobContainerClient.Name); } } await containerClient.DeleteAsync(); } }
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)); } }
public async Task Generate_EndToEnd() { // construct our TimerTrigger attribute ([TimerTrigger("00:00:02", RunOnStartup = true)]) Collection <ParameterDescriptor> parameters = new Collection <ParameterDescriptor>(); ParameterDescriptor parameter = new ParameterDescriptor("timerInfo", typeof(TimerInfo)); ConstructorInfo ctorInfo = typeof(TimerTriggerAttribute).GetConstructor(new Type[] { typeof(string) }); PropertyInfo runOnStartupProperty = typeof(TimerTriggerAttribute).GetProperty("RunOnStartup"); CustomAttributeBuilder attributeBuilder = new CustomAttributeBuilder( ctorInfo, new object[] { "00:00:02" }, new PropertyInfo[] { runOnStartupProperty }, new object[] { true }); parameter.CustomAttributes.Add(attributeBuilder); parameters.Add(parameter); // create the FunctionDefinition FunctionMetadata metadata = new FunctionMetadata(); TestInvoker invoker = new TestInvoker(); FunctionDescriptor function = new FunctionDescriptor("TimerFunction", invoker, metadata, parameters, null, null, null); Collection <FunctionDescriptor> functions = new Collection <FunctionDescriptor>(); functions.Add(function); // Get the Type Attributes (in this case, a TimeoutAttribute) ScriptJobHostOptions scriptConfig = new ScriptJobHostOptions(); scriptConfig.FunctionTimeout = TimeSpan.FromMinutes(5); Collection <CustomAttributeBuilder> typeAttributes = new Collection <CustomAttributeBuilder>(); // generate the Type Type functionType = FunctionGenerator.Generate("TestScriptHost", "TestFunctions", typeAttributes, functions); // verify the generated function MethodInfo method = functionType.GetMethod("TimerFunction"); ParameterInfo triggerParameter = method.GetParameters()[0]; TimerTriggerAttribute triggerAttribute = triggerParameter.GetCustomAttribute <TimerTriggerAttribute>(); Assert.NotNull(triggerAttribute); // start the JobHost which will start running the timer function var builder = new HostBuilder() .ConfigureWebJobs(b => { b.AddTimers() .AddAzureStorageCoreServices(); }) .ConfigureServices(s => { s.AddSingleton <ITypeLocator>(new TestTypeLocator(functionType)); s.AddSingleton <ILoggerFactory>(new LoggerFactory()); TestHelpers.AddTestAzureBlobStorageProvider(s, TestHelpers.GetTestConfiguration()); TestHostBuilderExtensions.AddMockedSingleton <IScriptHostManager>(s); }); using (var host = builder.Build()) { await host.StartAsync(); await Task.Delay(3000); await host.StopAsync(); } // verify our custom invoker was called Assert.True(invoker.InvokeCount >= 2); }