Skip to content

Commit

Permalink
[Storage][Webjobs] reduce number of times azurite is created. (#15541)
Browse files Browse the repository at this point in the history
In this PR:
- changes in tests to assure isolation through storage resources naming
- AzuriteFixture creates just one account
- Changed collection behavior to per class (more parallelism - faster run)
- azurite is created once per test assembly instead of once per test class (from 34 to 6 in local run)

Note: lot of binding tests rely on attributes that can only take constant strings, so the options for isolating through resource names are somewhat limited.
  • Loading branch information
kasobol-msft authored Sep 30, 2020
1 parent ba2a40c commit 442e922
Show file tree
Hide file tree
Showing 26 changed files with 163 additions and 274 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
<PackageReference Include="xunit.runner.visualstudio" />
</ItemGroup>

<ItemGroup>
<Compile Include="$(AzureStorageWebjobsExtensionSharedTestSources)\**\*.cs" Link="Shared\%(RecursiveDir)\%(Filename)%(Extension)" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Azure.Storage.Webjobs.Extensions.Common\tests\Azure.WebJobs.Extensions.Storage.Common.Tests.csproj" />
<ProjectReference Include="..\..\Microsoft.Azure.WebJobs.Host.TestCommon\Azure.WebJobs.Host.TestCommon.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,27 @@

namespace Microsoft.Azure.WebJobs.Host.FunctionalTests
{
public class BlobTests : IClassFixture<AzuriteFixture>
[Collection(AzuriteCollection.Name)]
public class BlobTests
{
private const string TriggerQueueName = "input";
private const string ContainerName = "container";
private const string TriggerQueueName = "input-blobtests";
private const string ContainerName = "container-blobtests";
private const string BlobName = "blob";
private const string BlobPath = ContainerName + "/" + BlobName;

private readonly AzuriteFixture azuriteFixture;
private readonly StorageAccount account;

public BlobTests(AzuriteFixture azuriteFixture)
{
this.azuriteFixture = azuriteFixture;
account = azuriteFixture.GetAccount();
account.CreateBlobServiceClient().GetBlobContainerClient(ContainerName).DeleteIfExists();
account.CreateQueueServiceClient().GetQueueClient(TriggerQueueName).DeleteIfExists();
}

[Fact]
public async Task Blob_IfBoundToCloudBlockBlob_BindsAndCreatesContainerButNotBlob()
{
// Act
var account = azuriteFixture.GetAccount();

var prog = new BindToCloudBlockBlobProgram();
IHost host = new HostBuilder()
.ConfigureDefaultTestHost<BindToCloudBlockBlobProgram>(prog, builder =>
Expand Down Expand Up @@ -65,7 +66,6 @@ public async Task Blob_IfBoundToTextWriter_CreatesBlob()
{
// Arrange
const string expectedContent = "message";
var account = azuriteFixture.GetAccount();
QueueClient triggerQueue = CreateQueue(account, TriggerQueueName);
await triggerQueue.SendMessageAsync(expectedContent);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,26 @@

namespace Microsoft.Azure.WebJobs.Host.FunctionalTests
{
public class BlobTriggerTests : IClassFixture<AzuriteFixture>
[Collection(AzuriteCollection.Name)]
public class BlobTriggerTests
{
private const string ContainerName = "container";
private const string ContainerName = "container-blobtriggertests";
private const string BlobName = "blob";
private const string BlobPath = ContainerName + "/" + BlobName;
private readonly AzuriteFixture azuriteFixture;
private readonly StorageAccount account;

public BlobTriggerTests(AzuriteFixture azuriteFixture)
{
this.azuriteFixture = azuriteFixture;
account = azuriteFixture.GetAccount();
account.CreateBlobServiceClient().GetBlobContainerClient(ContainerName).DeleteIfExists();
// make sure our system containers are present
CreateContainer(account, "azure-webjobs-hosts");
}

[Fact]
public async Task BlobTrigger_IfBoundToCloudBlob_Binds()
{
// Arrange
var account = CreateFakeStorageAccount();
var container = CreateContainer(account, ContainerName);
var blob = container.GetBlockBlobClient(BlobName);

Expand Down Expand Up @@ -60,7 +63,6 @@ public async Task BlobTrigger_Binding_Metadata()
{
var app = new BindToCloudBlob2Program();
var activator = new FakeActivator(app);
var account = CreateFakeStorageAccount();
var provider = new FakeStorageAccountProvider(account);
var host = new HostBuilder()
.ConfigureDefaultTestHost<BindToCloudBlob2Program>(b =>
Expand Down Expand Up @@ -106,16 +108,6 @@ private static BlobContainerClient CreateContainer(StorageAccount account, strin
return container;
}

private StorageAccount CreateFakeStorageAccount()
{
var account = azuriteFixture.GetAccount();

// make sure our system containers are present
var container = CreateContainer(account, "azure-webjobs-hosts");

return account;
}

private static async Task<TResult> RunTriggerAsync<TResult>(StorageAccount account, Type programType,
Action<TaskCompletionSource<TResult>> setTaskSource)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,21 @@ namespace Microsoft.Azure.WebJobs.Host.FunctionalTests
{
// Some tests in this class aren't as targeted as most other tests in this project.
// (Look elsewhere for better examples to use as templates for new tests.)
public class HostCallTests : IClassFixture<AzuriteFixture>
[Collection(AzuriteCollection.Name)]
public class HostCallTests
{
private const string ContainerName = "container";
private const string ContainerName = "container-hostcalltests";
private const string BlobName = "blob";
private const string BlobPath = ContainerName + "/" + BlobName;
private const string OutputBlobName = "blob.out";
private const string OutputBlobPath = ContainerName + "/" + OutputBlobName;
private const int TestValue = Int32.MinValue;
private readonly AzuriteFixture azuriteFixture;
private readonly StorageAccount account;

public HostCallTests(AzuriteFixture azuriteFixture)
{
this.azuriteFixture = azuriteFixture;
account = azuriteFixture.GetAccount();
account.CreateBlobServiceClient().GetBlobContainerClient(ContainerName).DeleteIfExists();
}

[Theory]
Expand All @@ -49,7 +51,6 @@ public HostCallTests(AzuriteFixture azuriteFixture)
public async Task Blob_IfBoundToTypeAndBlobIsMissing_DoesNotCreate(string methodName)
{
// Arrange
var account = CreateFakeStorageAccount();
var client = account.CreateBlobServiceClient();
var container = client.GetBlobContainerClient(ContainerName);
var blob = container.GetBlockBlobClient(BlobName);
Expand All @@ -71,7 +72,6 @@ public async Task Blob_IfBoundToTypeAndBlobIsMissing_DoesNotCreate(string method
public async Task Blob_IfBoundToTypeAndBlobIsMissing_Creates(string methodName)
{
// Arrange
StorageAccount account = CreateFakeStorageAccount();
var client = account.CreateBlobServiceClient();
var container = client.GetBlobContainerClient(ContainerName);
var blob = container.GetBlockBlobClient(BlobName);
Expand All @@ -87,7 +87,6 @@ public async Task Blob_IfBoundToTypeAndBlobIsMissing_Creates(string methodName)
public async Task BlobTrigger_IfHasUnboundParameter_CanCall()
{
// Arrange
StorageAccount account = CreateFakeStorageAccount();
var client = account.CreateBlobServiceClient();
var container = client.GetBlobContainerClient(ContainerName);
const string inputBlobName = "note-monday.csv";
Expand Down Expand Up @@ -119,7 +118,6 @@ public async Task BlobTrigger_IfHasUnboundParameter_CanCall()
public async Task Blob_IfBoundToCloudBlockBlob_CanCall()
{
// Arrange
var account = CreateFakeStorageAccount();
var client = account.CreateBlobServiceClient();
var container = client.GetBlobContainerClient(ContainerName);
var inputBlob = container.GetBlockBlobClient(BlobName);
Expand All @@ -134,7 +132,6 @@ public async Task Blob_IfBoundToCloudBlockBlob_CanCall()
public async Task Blob_IfBoundToString_CanCall()
{
// Arrange
var account = CreateFakeStorageAccount();
var client = account.CreateBlobServiceClient();
var container = client.GetBlobContainerClient(ContainerName);
var inputBlob = container.GetBlockBlobClient(BlobName);
Expand All @@ -148,7 +145,6 @@ public async Task Blob_IfBoundToString_CanCall()
public async Task Blob_IfCopiedViaString_CanCall()
{
// Arrange
var account = CreateFakeStorageAccount();
var client = account.CreateBlobServiceClient();
var container = client.GetBlobContainerClient(ContainerName);
var inputBlob = container.GetBlockBlobClient(BlobName);
Expand All @@ -169,7 +165,6 @@ public async Task Blob_IfCopiedViaString_CanCall()
public async Task BlobTrigger_IfCopiedViaTextReaderTextWriter_CanCall()
{
// Arrange
StorageAccount account = CreateFakeStorageAccount();
var client = account.CreateBlobServiceClient();
var container = client.GetBlobContainerClient(ContainerName);
var inputBlob = container.GetBlockBlobClient(BlobName);
Expand All @@ -196,7 +191,6 @@ public async Task BlobTrigger_IfCopiedViaTextReaderTextWriter_CanCall()
public async Task BlobTrigger_IfBoundToICloudBlob_CanCallWithBlockBlob()
{
// Arrange
StorageAccount account = CreateFakeStorageAccount();
var client = account.CreateBlobServiceClient();
var container = client.GetBlobContainerClient(ContainerName);
var blob = container.GetBlockBlobClient(BlobName);
Expand All @@ -222,7 +216,6 @@ public async Task BlobTrigger_IfBoundToICloudBlob_CanCallWithBlockBlob()
public async Task BlobTrigger_IfBoundToICloudBlob_CanCallWithPageBlob()
{
// Arrange
StorageAccount account = CreateFakeStorageAccount();
var client = account.CreateBlobServiceClient();
var container = client.GetBlobContainerClient(ContainerName);
var blob = container.GetPageBlobClient(BlobName);
Expand All @@ -247,9 +240,6 @@ public async Task BlobTrigger_IfBoundToICloudBlob_CanCallWithPageBlob()
[Fact]
public async Task BlobTrigger_IfBoundToICloudBlobAndTriggerArgumentIsMissing_CallThrows()
{
// Arrange
StorageAccount account = CreateFakeStorageAccount();

// Act
Exception exception = await CallFailureAsync(account, typeof(BlobTriggerBindToICloudBlobProgram), "Call");

Expand All @@ -262,7 +252,6 @@ public async Task BlobTrigger_IfBoundToICloudBlobAndTriggerArgumentIsMissing_Cal
public async Task BlobTrigger_IfBoundToCloudBlockBlob_CanCall()
{
// Arrange
StorageAccount account = CreateFakeStorageAccount();
var client = account.CreateBlobServiceClient();
var container = client.GetBlobContainerClient(ContainerName);
var blob = container.GetBlockBlobClient(BlobName);
Expand All @@ -286,9 +275,6 @@ public async Task BlobTrigger_IfBoundToCloudBlockBlob_CanCall()
[Fact]
public async Task BlobTrigger_IfBoundToCloudBLockBlobAndTriggerArgumentIsMissing_CallThrows()
{
// Arrange
StorageAccount account = CreateFakeStorageAccount();

// Act
Exception exception = await CallFailureAsync(account, typeof(BlobTriggerBindToCloudBlockBlobProgram), "Call");

Expand All @@ -311,7 +297,6 @@ public static void Call([BlobTrigger(BlobPath)] BlockBlobClient blob)
public async Task BlobTrigger_IfBoundToCloudPageBlob_CanCall()
{
// Arrange
StorageAccount account = CreateFakeStorageAccount();
var client = account.CreateBlobServiceClient();
var container = client.GetBlobContainerClient(ContainerName);
var blob = container.GetPageBlobClient(BlobName);
Expand All @@ -335,9 +320,6 @@ public async Task BlobTrigger_IfBoundToCloudPageBlob_CanCall()
[Fact]
public async Task BlobTrigger_IfBoundToCloudPageBlobAndTriggerArgumentIsMissing_CallThrows()
{
// Arrange
var account = CreateFakeStorageAccount();

// Act
Exception exception = await CallFailureAsync(account, typeof(BlobTriggerBindToCloudPageBlobProgram), "Call");

Expand All @@ -360,7 +342,6 @@ public static void Call([BlobTrigger(BlobPath)] PageBlobClient blob)
public async Task BlobTrigger_IfBoundToCloudAppendBlob_CanCall()
{
// Arrange
var account = CreateFakeStorageAccount();
var client = account.CreateBlobServiceClient();
var container = client.GetBlobContainerClient(ContainerName);
var blob = container.GetAppendBlobClient(BlobName);
Expand All @@ -384,9 +365,6 @@ public async Task BlobTrigger_IfBoundToCloudAppendBlob_CanCall()
[Fact]
public async Task BlobTrigger_IfBoundToCloudAppendBlobAndTriggerArgumentIsMissing_CallThrows()
{
// Arrange
var account = CreateFakeStorageAccount();

// Act
Exception exception = await CallFailureAsync(account, typeof(BlobTriggerBindToCloudAppendBlobProgram), "Call");

Expand All @@ -408,8 +386,6 @@ public static void Call([BlobTrigger(BlobPath)] AppendBlobClient blob)
[Fact]
public async Task Int32Argument_CanCallViaStringParse()
{
// Arrange
var account = CreateFakeStorageAccount();
IDictionary<string, object> arguments = new Dictionary<string, object>
{
{ "value", "15" }
Expand All @@ -436,9 +412,6 @@ public static void Call(int value)
[Fact]
public async Task Binder_IfBindingBlobToTextWriter_CanCall()
{
// Arrange
StorageAccount account = CreateFakeStorageAccount();

// Act
await CallAsync(account, typeof(BindToBinderBlobTextWriterProgram), "Call");

Expand All @@ -465,7 +438,6 @@ public static void Call(IBinder binder)
public async Task BlobTrigger_IfCopiedViaPoco_CanCall()
{
// Arrange
StorageAccount account = CreateFakeStorageAccount();
var client = account.CreateBlobServiceClient();
var container = client.GetBlobContainerClient(ContainerName);
var inputBlob = container.GetBlockBlobClient(BlobName);
Expand Down Expand Up @@ -523,11 +495,6 @@ private static async Task<Exception> CallFailureAsync(StorageAccount account, Ty
return await FunctionalTest.CallFailureAsync(account, programType, programType.GetMethod(methodName), null);
}

private StorageAccount CreateFakeStorageAccount()
{
return azuriteFixture.GetAccount();
}

private struct CustomDataValue
{
public int ValueId { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,28 @@

namespace Microsoft.Azure.WebJobs.Host.UnitTests.Blobs.Listeners
{
public class BlobQueueTriggerExecutorTests : IClassFixture<AzuriteFixture>
[Collection(AzuriteCollection.Name)]
public class BlobQueueTriggerExecutorTests
{
private const string TestBlobName = "TestBlobName";
private const string TestContainerName = "container";
private const string TestContainerName = "container-blobqueuetriggerexecutortests";
private const string TestQueueMessageId = "abc123";

private readonly TestLoggerProvider _loggerProvider = new TestLoggerProvider();
private readonly ILogger<BlobListener> _logger;
private readonly BlobServiceClient blobServiceClient;
private readonly BlobContainerClient blobContainer;

private readonly AzuriteFixture azuriteFixture;

public BlobQueueTriggerExecutorTests(AzuriteFixture azuriteFixture)
{
var loggerFactory = new LoggerFactory();
loggerFactory.AddProvider(_loggerProvider);
_logger = loggerFactory.CreateLogger<BlobListener>();

this.azuriteFixture = azuriteFixture;
blobServiceClient = azuriteFixture.GetAccount().CreateBlobServiceClient();
var account = azuriteFixture.GetAccount();
blobServiceClient = account.CreateBlobServiceClient();
blobContainer = blobServiceClient.GetBlobContainerClient(TestContainerName);
blobContainer.DeleteIfExists();
blobContainer.CreateIfNotExists();
}

Expand Down
Loading

0 comments on commit 442e922

Please sign in to comment.