private IHost CreateTestJobHost <TProg>(int hostId, Action <IHostBuilder> extraConfig = null)
        {
            TestJobActivator activator = new TestJobActivator(hostId);

            var hostBuilder = new HostBuilder()
                              .ConfigureDefaultTestHost <TProg>(b =>
            {
                b.UseHostId(TestHostId)
                .AddAzureStorage()
                .AddExtension <TestTriggerAttributeBindingProvider>();
                RuntimeStorageWebJobsBuilderExtensions.AddAzureStorageCoreServices(b);
            })
                              .ConfigureTestLogger()
                              .ConfigureServices(services =>
            {
                services.AddSingleton <IJobActivator>(activator);
                services.AddSingleton <INameResolver>(_resolver);
                services.Configure((QueuesOptions o) => o.MaxPollingInterval = TimeSpan.FromSeconds(2));
                services.Configure((SingletonOptions o) =>
                {
                    o.LockAcquisitionTimeout         = TimeSpan.FromSeconds(10);
                    o.LockAcquisitionPollingInterval = TimeSpan.FromMilliseconds(500);
                });
            });

            extraConfig?.Invoke(hostBuilder); // test hook gets final say to replace.

            IHost host = hostBuilder.Build();

            return(host);
        }
        public async Task Test()
        {
            var containerName = "test-internal1";

            var acs = Environment.GetEnvironmentVariable("AzureWebJobsStorage");

            if (acs == null)
            {
                Assert.False(true, "Missing AzureWebJobsStorage setting");
            }

            // Create a real Blob Container Sas URI
            var account1  = CloudStorageAccount.Parse(acs);
            var client    = account1.CreateCloudBlobClient();
            var container = client.GetContainerReference(containerName);
            await container.CreateIfNotExistsAsync(); // this will throw if acs is bad

            var now = DateTime.UtcNow;
            var sig = container.GetSharedAccessSignature(new SharedAccessBlobPolicy
            {
                Permissions            = SharedAccessBlobPermissions.Read | SharedAccessBlobPermissions.Write | SharedAccessBlobPermissions.List,
                SharedAccessStartTime  = now.AddDays(-10),
                SharedAccessExpiryTime = now.AddDays(10)
            });

            var fakeSasUri = container.Uri + sig;
            var prog       = new BasicProg();

            IHost host = new HostBuilder()
                         .ConfigureDefaultTestHost(prog, b =>
            {
                b.AddAzureStorage();
                RuntimeStorageWebJobsBuilderExtensions.AddAzureStorageCoreServices(b);
            })
                         .ConfigureAppConfiguration(config =>
            {
                // Set env to the SAS container and clear out all other storage.
                config.AddInMemoryCollection(new Dictionary <string, string>()
                {
                    { "AzureWebJobs:InternalSasBlobContainer", fakeSasUri },
                    { "AzureWebJobsStorage", null },
                    { "AzureWebJobsDashboard", null }
                });
            })
                         .Build();

            var internalOptions = host.Services.GetService <DistributedLockManagerContainerProvider>();

            Assert.NotNull(internalOptions);
            Assert.Equal(container.Name, internalOptions.InternalContainer.Name);

            await host.GetJobHost().CallAsync(nameof(BasicProg.Foo));

            Assert.Equal(1, prog._count); // Verify successfully called.
        }
            public async Task InitializeAsync()
            {
                RandomNameResolver nameResolver = new RandomNameResolver();

                Host = new HostBuilder()
                       .ConfigureDefaultTestHost <BlobBindingEndToEndTests>(b =>
                {
                    b.AddAzureStorage();
                    RuntimeStorageWebJobsBuilderExtensions.AddAzureStorageCoreServices(b);
                })
                       .ConfigureServices(services =>
                {
                    services.AddSingleton <INameResolver>(nameResolver);
                })
                       .Build();


                JobHost = Host.GetJobHost();

                var provider = Host.Services.GetService <StorageAccountProvider>();

                StorageAccount = provider.GetHost().SdkObject;
                CloudBlobClient blobClient = StorageAccount.CreateCloudBlobClient();

                BlobContainer = blobClient.GetContainerReference(nameResolver.ResolveInString(ContainerName));
                Assert.False(await BlobContainer.ExistsAsync());
                await BlobContainer.CreateAsync();

                OutputBlobContainer = blobClient.GetContainerReference(nameResolver.ResolveInString(OutputContainerName));

                CloudBlobContainer pageBlobContainer = blobClient.GetContainerReference(nameResolver.ResolveInString(PageBlobContainerName));

                Assert.False(await pageBlobContainer.ExistsAsync());
                await pageBlobContainer.CreateAsync();

                CloudBlobContainer hierarchicalBlobContainer = blobClient.GetContainerReference(nameResolver.ResolveInString(HierarchicalBlobContainerName));

                Assert.False(await hierarchicalBlobContainer.ExistsAsync());
                await hierarchicalBlobContainer.CreateAsync();

                CloudBlobContainer appendBlobContainer = blobClient.GetContainerReference(nameResolver.ResolveInString(AppendBlobContainerName));

                Assert.False(await appendBlobContainer.ExistsAsync());
                await appendBlobContainer.CreateAsync();

                await Host.StartAsync();

                // upload some test blobs
                CloudBlockBlob blob = BlobContainer.GetBlockBlobReference("blob1");
                await blob.UploadTextAsync(TestData);

                blob = BlobContainer.GetBlockBlobReference("blob2");
                await blob.UploadTextAsync(TestData);

                blob = BlobContainer.GetBlockBlobReference("blob3");
                await blob.UploadTextAsync(TestData);

                blob = BlobContainer.GetBlockBlobReference("file1");
                await blob.UploadTextAsync(TestData);

                blob = BlobContainer.GetBlockBlobReference("file2");
                await blob.UploadTextAsync(TestData);

                blob = BlobContainer.GetBlockBlobReference("overwrite");
                await blob.UploadTextAsync(TestData);

                // add a couple hierarchical blob paths
                blob = hierarchicalBlobContainer.GetBlockBlobReference("sub/blob1");
                await blob.UploadTextAsync(TestData);

                blob = hierarchicalBlobContainer.GetBlockBlobReference("sub/blob2");
                await blob.UploadTextAsync(TestData);

                blob = hierarchicalBlobContainer.GetBlockBlobReference("sub/sub/blob3");
                await blob.UploadTextAsync(TestData);

                blob = hierarchicalBlobContainer.GetBlockBlobReference("blob4");
                await blob.UploadTextAsync(TestData);

                byte[] bytes     = new byte[512];
                byte[] testBytes = Encoding.UTF8.GetBytes(TestData);
                for (int i = 0; i < testBytes.Length; i++)
                {
                    bytes[i] = testBytes[i];
                }
                CloudPageBlob pageBlob = pageBlobContainer.GetPageBlobReference("blob1");
                await pageBlob.UploadFromByteArrayAsync(bytes, 0, bytes.Length);

                pageBlob = pageBlobContainer.GetPageBlobReference("blob2");
                await pageBlob.UploadFromByteArrayAsync(bytes, 0, bytes.Length);

                CloudAppendBlob appendBlob = appendBlobContainer.GetAppendBlobReference("blob1");
                await appendBlob.UploadTextAsync(TestData);

                appendBlob = appendBlobContainer.GetAppendBlobReference("blob2");
                await appendBlob.UploadTextAsync(TestData);

                appendBlob = appendBlobContainer.GetAppendBlobReference("blob3");
                await appendBlob.UploadTextAsync(TestData);
            }