public async Task GetAccountAsync_CachesAccounts() { var services = CreateServices(); var accountMock = new Mock <IStorageAccount>(); var storageMock = new Mock <IStorageAccount>(); var csProvider = CreateConnectionStringProvider("account", "cs"); var parser = CreateParser(services, "account", "cs", accountMock.Object); // strick mock tests that validate does not occur twice Mock <IStorageCredentialsValidator> validatorMock = new Mock <IStorageCredentialsValidator>(MockBehavior.Strict); validatorMock.Setup(v => v.ValidateCredentialsAsync(accountMock.Object, It.IsAny <CancellationToken>())) .Returns(Task.CompletedTask); validatorMock.Setup(v => v.ValidateCredentialsAsync(storageMock.Object, It.IsAny <CancellationToken>())) .Returns(Task.CompletedTask); DefaultStorageAccountProvider provider = CreateProductUnderTest(services, csProvider, parser, validatorMock.Object); var account = await provider.TryGetAccountAsync("account", CancellationToken.None); var account2 = await provider.TryGetAccountAsync("account", CancellationToken.None); Assert.Equal(account, account2); validatorMock.Verify(v => v.ValidateCredentialsAsync(accountMock.Object, It.IsAny <CancellationToken>()), Times.Once()); }
/// <summary> /// Initializes a new instance of the <see cref="JobHostConfiguration"/> class, using the /// specified connection string for both reading and writing data as well as Dashboard logging. /// </summary> /// <param name="dashboardAndStorageConnectionString">The Azure Storage connection string to use. /// </param> public JobHostConfiguration(string dashboardAndStorageConnectionString) { if (!string.IsNullOrEmpty(dashboardAndStorageConnectionString)) { _storageAccountProvider = new DefaultStorageAccountProvider(this, dashboardAndStorageConnectionString); } else { _storageAccountProvider = new DefaultStorageAccountProvider(this); } Singleton = new SingletonConfiguration(); // add our built in services here IExtensionRegistry extensions = new DefaultExtensionRegistry(); ITypeLocator typeLocator = new DefaultTypeLocator(ConsoleProvider.Out, extensions); AddService <IExtensionRegistry>(extensions); AddService <StorageClientFactory>(new StorageClientFactory()); AddService <INameResolver>(new DefaultNameResolver()); AddService <IJobActivator>(DefaultJobActivator.Instance); AddService <ITypeLocator>(typeLocator); string value = ConfigurationUtility.GetSettingFromConfigOrEnvironment(Constants.EnvironmentSettingName); IsDevelopment = string.Compare(Constants.DevelopmentEnvironmentValue, value, StringComparison.OrdinalIgnoreCase) == 0; }
private JobHostConfiguration(DefaultStorageAccountProvider storageAccountProvider) { _storageAccountProvider = storageAccountProvider; // add our built in services here AddService <IExtensionRegistry>(new DefaultExtensionRegistry()); }
public void ConnectionStringProvider_NoDashboardConnectionString_Throws() { const string DashboardConnectionEnvironmentVariable = "AzureWebJobsDashboard"; string previousConnectionString = Environment.GetEnvironmentVariable(DashboardConnectionEnvironmentVariable); try { Environment.SetEnvironmentVariable(DashboardConnectionEnvironmentVariable, null); Mock <IServiceProvider> mockServices = new Mock <IServiceProvider>(MockBehavior.Strict); IStorageAccountProvider product = new DefaultStorageAccountProvider(mockServices.Object) { StorageConnectionString = new CloudStorageAccount(new StorageCredentials("Test", new byte[0], "key"), true).ToString(exportSecrets: true) }; // Act & Assert ExceptionAssert.ThrowsInvalidOperation(() => product.GetDashboardAccountAsync(CancellationToken.None).GetAwaiter().GetResult(), "Microsoft Azure WebJobs SDK Dashboard connection string is missing or empty. The Microsoft Azure Storage account connection string can be set in the following ways:" + Environment.NewLine + "1. Set the connection string named 'AzureWebJobsDashboard' in the connectionStrings section of the .config file in the following format " + "<add name=\"AzureWebJobsDashboard\" connectionString=\"DefaultEndpointsProtocol=http|https;AccountName=NAME;AccountKey=KEY\" />, or" + Environment.NewLine + "2. Set the environment variable named 'AzureWebJobsDashboard', or" + Environment.NewLine + "3. Set corresponding property of JobHostConfiguration."); } finally { Environment.SetEnvironmentVariable(DashboardConnectionEnvironmentVariable, previousConnectionString); } }
public async Task GetAccountAsync_WhenStorageOverriddenWithNull_Succeeds() { DefaultStorageAccountProvider provider = CreateProductUnderTest(); provider.StorageConnectionString = null; var account = await provider.GetAccountAsync(ConnectionStringNames.Storage, CancellationToken.None); Assert.Null(account); }
/// <summary> /// Initializes a new instance of the <see cref="JobHostConfiguration"/> class, using the /// specified connection string for both reading and writing data as well as Dashboard logging. /// </summary> /// <param name="dashboardAndStorageConnectionString">The Azure Storage connection string to use. /// <param name="configuration">A configuration object that will be used as the source of application settings.</param> /// </param> public JobHostConfiguration(string dashboardAndStorageConnectionString, IConfiguration configuration) { if (configuration != null) { ConfigurationUtility.SetConfigurationFactory(() => configuration); } if (!string.IsNullOrEmpty(dashboardAndStorageConnectionString)) { _storageAccountProvider = new DefaultStorageAccountProvider(this, dashboardAndStorageConnectionString); } else { _storageAccountProvider = new DefaultStorageAccountProvider(this); } var sasBlobContainer = _storageAccountProvider.InternalSasStorage; if (sasBlobContainer != null) { var uri = new Uri(sasBlobContainer); var sdkContainer = new CloudBlobContainer(uri); this.InternalStorageConfiguration = new JobHostInternalStorageConfiguration { InternalContainer = sdkContainer }; } Singleton = new SingletonConfiguration(); Aggregator = new FunctionResultAggregatorConfiguration(); // add our built in services here IExtensionRegistry extensions = new DefaultExtensionRegistry(); ITypeLocator typeLocator = new DefaultTypeLocator(ConsoleProvider.Out, extensions); IConverterManager converterManager = new ConverterManager(); IWebJobsExceptionHandler exceptionHandler = new WebJobsExceptionHandler(); AddService <IQueueConfiguration>(_queueConfiguration); AddService <IConsoleProvider>(ConsoleProvider); AddService <ILoggerFactory>(new LoggerFactory()); AddService <IStorageAccountProvider>(_storageAccountProvider); AddService <IExtensionRegistry>(extensions); AddService <StorageClientFactory>(new StorageClientFactory()); AddService <INameResolver>(new DefaultNameResolver()); AddService <IJobActivator>(DefaultJobActivator.Instance); AddService <ITypeLocator>(typeLocator); AddService <IConverterManager>(converterManager); AddService <IWebJobsExceptionHandler>(exceptionHandler); AddService <IFunctionResultAggregatorFactory>(new FunctionResultAggregatorFactory()); string value = ConfigurationUtility.GetSetting(Host.Constants.EnvironmentSettingName); IsDevelopment = string.Compare(Host.Constants.DevelopmentEnvironmentValue, value, StringComparison.OrdinalIgnoreCase) == 0; }
public void GetAccountAsync_WhenDashboardOverriddenWithNull_ReturnsNull() { DefaultStorageAccountProvider provider = CreateProductUnderTest(); provider.DashboardConnectionString = null; IStorageAccount actualAccount = provider.GetAccountAsync( ConnectionStringNames.Dashboard, CancellationToken.None).GetAwaiter().GetResult(); Assert.Null(actualAccount); }
private JobHostConfiguration(DefaultStorageAccountProvider storageAccountProvider) { _storageAccountProvider = storageAccountProvider; IExtensionRegistry extensions = new DefaultExtensionRegistry(); _typeLocator = new DefaultTypeLocator(ConsoleProvider.Out, extensions); Singleton = new SingletonConfiguration(); // add our built in services here AddService <IExtensionRegistry>(extensions); }
public async Task GetAccountAsync_WhenNoStorage_Succeeds() { DefaultStorageAccountProvider provider = CreateProductUnderTest(); provider.DashboardConnectionString = null; provider.StorageConnectionString = null; var dashboardAccount = await provider.GetAccountAsync(ConnectionStringNames.Dashboard, CancellationToken.None); Assert.Null(dashboardAccount); var storageAccount = await provider.GetAccountAsync(ConnectionStringNames.Storage, CancellationToken.None); Assert.Null(storageAccount); }
public void GetAccountAsync_WhenDashboardOverridden_ReturnsParsedAccount() { IConnectionStringProvider connectionStringProvider = CreateDummyConnectionStringProvider(); string connectionString = "valid-ignore"; IStorageAccount parsedAccount = Mock.Of <IStorageAccount>(); IServiceProvider services = CreateServices(); IStorageAccountParser parser = CreateParser(services, ConnectionStringNames.Dashboard, connectionString, parsedAccount); IStorageCredentialsValidator validator = CreateValidator(parsedAccount); DefaultStorageAccountProvider provider = CreateProductUnderTest(services, connectionStringProvider, parser, validator); provider.DashboardConnectionString = connectionString; IStorageAccount actualAccount = provider.GetAccountAsync( ConnectionStringNames.Dashboard, CancellationToken.None).GetAwaiter().GetResult(); Assert.Same(parsedAccount, actualAccount); }
public void GetAccountAsync_WhenStorageOverriddenWithNull_Throws() { DefaultStorageAccountProvider provider = CreateProductUnderTest(); provider.StorageConnectionString = null; ExceptionAssert.ThrowsInvalidOperation(() => provider.GetAccountAsync( ConnectionStringNames.Storage, CancellationToken.None).GetAwaiter().GetResult(), @"Microsoft Azure WebJobs SDK Storage connection string is missing or empty. The Microsoft Azure Storage account connection string can be set in the following ways:" + Environment.NewLine + @"1. Set the connection string named 'AzureWebJobsStorage' in the connectionStrings section of the .config file in the following format <add name=""AzureWebJobsStorage"" connectionString=""DefaultEndpointsProtocol=http|https;AccountName=NAME;AccountKey=KEY"" />, or" + Environment.NewLine + @"2. Set the environment variable named 'AzureWebJobsStorage', or" + Environment.NewLine + @"3. Set corresponding property of JobHostConfiguration."); }
public async Task GetAccountAsync_WhenWebJobsDashboardAccountNotGeneral_Throws() { string connectionString = "valid-ignore"; var connStringMock = new Mock <IConnectionStringProvider>(); connStringMock.Setup(c => c.GetConnectionString(ConnectionStringNames.Dashboard)).Returns(connectionString); var connectionStringProvider = connStringMock.Object; var accountMock = new Mock <IStorageAccount>(); accountMock.SetupGet(s => s.Type).Returns(StorageAccountType.Premium); accountMock.SetupGet(s => s.Credentials).Returns(new StorageCredentials("name", string.Empty)); var parsedAccount = accountMock.Object; IServiceProvider services = CreateServices(); IStorageAccountParser parser = CreateParser(services, ConnectionStringNames.Dashboard, connectionString, parsedAccount); IStorageCredentialsValidator validator = CreateValidator(parsedAccount); DefaultStorageAccountProvider provider = CreateProductUnderTest(services, connectionStringProvider, parser, validator); var exception = await Assert.ThrowsAsync <InvalidOperationException>(() => provider.GetDashboardAccountAsync(CancellationToken.None)); Assert.Equal("Storage account 'name' is of unsupported type 'Premium'. Supported types are 'General Purpose'", exception.Message); }
public TestFixture() { DefaultStorageAccountProvider accountProvider = new DefaultStorageAccountProvider(); var task = accountProvider.GetStorageAccountAsync(CancellationToken.None); task.Wait(); IStorageQueueClient client = task.Result.CreateQueueClient(); QueueClient = client.SdkObject; string queueName = string.Format("{0}-{1}", TestQueuePrefix, Guid.NewGuid()); Queue = client.GetQueueReference(queueName).SdkObject; Queue.CreateIfNotExistsAsync(CancellationToken.None).Wait(); string poisonQueueName = string.Format("{0}-poison", queueName); PoisonQueue = client.GetQueueReference(poisonQueueName).SdkObject; PoisonQueue.CreateIfNotExistsAsync(CancellationToken.None).Wait(); }
/// <summary> /// Initializes a new instance of the <see cref="JobHostConfiguration"/> class, using the /// specified connection string for both reading and writing data as well as Dashboard logging. /// </summary> /// <param name="dashboardAndStorageConnectionString">The Azure Storage connection string to use. /// </param> public JobHostConfiguration(string dashboardAndStorageConnectionString) { if (!string.IsNullOrEmpty(dashboardAndStorageConnectionString)) { _storageAccountProvider = new DefaultStorageAccountProvider(this, dashboardAndStorageConnectionString); } else { _storageAccountProvider = new DefaultStorageAccountProvider(this); } Singleton = new SingletonConfiguration(); // add our built in services here IExtensionRegistry extensions = new DefaultExtensionRegistry(); ITypeLocator typeLocator = new DefaultTypeLocator(ConsoleProvider.Out, extensions); AddService <IExtensionRegistry>(extensions); AddService <StorageClientFactory>(new StorageClientFactory()); AddService <INameResolver>(new DefaultNameResolver()); AddService <IJobActivator>(DefaultJobActivator.Instance); AddService <ITypeLocator>(typeLocator); }
public TestFixture() { Mock <IServiceProvider> services = new Mock <IServiceProvider>(MockBehavior.Strict); StorageClientFactory clientFactory = new StorageClientFactory(); services.Setup(p => p.GetService(typeof(StorageClientFactory))).Returns(clientFactory); DefaultStorageAccountProvider accountProvider = new DefaultStorageAccountProvider(services.Object); var task = accountProvider.GetStorageAccountAsync(CancellationToken.None); IStorageQueueClient client = task.Result.CreateQueueClient(); QueueClient = client.SdkObject; string queueName = string.Format("{0}-{1}", TestQueuePrefix, Guid.NewGuid()); Queue = client.GetQueueReference(queueName).SdkObject; Queue.CreateIfNotExistsAsync(null, null, CancellationToken.None).Wait(); string poisonQueueName = string.Format("{0}-poison", queueName); PoisonQueue = client.GetQueueReference(poisonQueueName).SdkObject; PoisonQueue.CreateIfNotExistsAsync(null, null, CancellationToken.None).Wait(); }
public void ConnectionStringProvider_NoDashboardConnectionString_Throws() { var configuration = new ConfigurationBuilder() .AddInMemoryCollection(new Dictionary <string, string> { { "Dashboard", null } }) .Build(); IStorageAccountProvider product = new DefaultStorageAccountProvider(new AmbientConnectionStringProvider(configuration), new StorageAccountParser(new StorageClientFactory()), new DefaultStorageCredentialsValidator()) { StorageConnectionString = new CloudStorageAccount(new StorageCredentials("Test", string.Empty, "key"), true).ToString(exportSecrets: true) }; // Act & Assert ExceptionAssert.ThrowsInvalidOperation(() => product.GetDashboardAccountAsync(CancellationToken.None).GetAwaiter().GetResult(), "Microsoft Azure WebJobs SDK 'Dashboard' connection string is missing or empty. The Microsoft Azure Storage account connection string can be set in the following ways:" + Environment.NewLine + "1. Set the connection string named 'AzureWebJobsDashboard' in the connectionStrings section of the .config file in the following format " + "<add name=\"AzureWebJobsDashboard\" connectionString=\"DefaultEndpointsProtocol=http|https;AccountName=NAME;AccountKey=KEY\" />, or" + Environment.NewLine + "2. Set the environment variable named 'AzureWebJobsDashboard', or" + Environment.NewLine + "3. Set corresponding property of JobHostConfiguration."); }
/// <summary> /// Initializes a new instance of the <see cref="JobHostConfiguration"/> class, using the /// specified connection string for both reading and writing data as well as Dashboard logging. /// </summary> /// <param name="dashboardAndStorageConnectionString">The Azure Storage connection string to use. /// </param> public JobHostConfiguration(string dashboardAndStorageConnectionString) { if (!string.IsNullOrEmpty(dashboardAndStorageConnectionString)) { _storageAccountProvider = new DefaultStorageAccountProvider(this, dashboardAndStorageConnectionString); } else { _storageAccountProvider = new DefaultStorageAccountProvider(this); } Singleton = new SingletonConfiguration(); Aggregator = new FunctionResultAggregatorConfiguration(); // add our built in services here _tooling = new JobHostMetadataProvider(this); IExtensionRegistry extensions = new DefaultExtensionRegistry(_tooling); ITypeLocator typeLocator = new DefaultTypeLocator(ConsoleProvider.Out, extensions); IConverterManager converterManager = new ConverterManager(); IWebJobsExceptionHandler exceptionHandler = new WebJobsExceptionHandler(); AddService <IQueueConfiguration>(_queueConfiguration); AddService <IConsoleProvider>(ConsoleProvider); AddService <IStorageAccountProvider>(_storageAccountProvider); AddService <IExtensionRegistry>(extensions); AddService <StorageClientFactory>(new StorageClientFactory()); AddService <INameResolver>(new DefaultNameResolver()); AddService <IJobActivator>(DefaultJobActivator.Instance); AddService <ITypeLocator>(typeLocator); AddService <IConverterManager>(converterManager); AddService <IWebJobsExceptionHandler>(exceptionHandler); AddService <IFunctionResultAggregatorFactory>(new FunctionResultAggregatorFactory()); string value = ConfigurationUtility.GetSettingFromConfigOrEnvironment(Host.Constants.EnvironmentSettingName); IsDevelopment = string.Compare(Host.Constants.DevelopmentEnvironmentValue, value, StringComparison.OrdinalIgnoreCase) == 0; }
private JobHostConfiguration(DefaultStorageAccountProvider storageAccountProvider) { _storageAccountProvider = storageAccountProvider; }