Skip to content

Commit 8085c6d

Browse files
committed
clear blob containers before tests
1 parent 08d7624 commit 8085c6d

File tree

5 files changed

+28
-8
lines changed

5 files changed

+28
-8
lines changed

test/WebJobs.Script.Tests/BashEndToEndTests.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public async Task BlobTriggerToBlobTest()
3131
public class TestFixture : EndToEndTestFixture
3232
{
3333
public TestFixture() : base(@"TestScripts\Bash", "bash")
34-
{
34+
{
3535
}
3636

3737
public string TestBlobContents { get; private set; }
@@ -40,16 +40,15 @@ public TestFixture() : base(@"TestScripts\Bash", "bash")
4040

4141
protected override void CreateTestStorageEntities()
4242
{
43+
// This will ensure the input container is created.
4344
base.CreateTestStorageEntities();
4445

4546
TestBlobContents = "My Test Blob";
4647
TestBlobName = Guid.NewGuid().ToString();
4748

4849
// write the test blob before the host starts, so it gets picked
4950
// up relatively quickly by the blob trigger test
50-
CloudBlobContainer inputContainer = BlobClient.GetContainerReference("test-input-bash");
51-
inputContainer.CreateIfNotExists();
52-
CloudBlockBlob inputBlob = inputContainer.GetBlockBlobReference(TestBlobName);
51+
CloudBlockBlob inputBlob = TestInputContainer.GetBlockBlobReference(TestBlobName);
5352
inputBlob.UploadText(TestBlobContents);
5453
}
5554
}

test/WebJobs.Script.Tests/EndToEndTestFixture.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ protected virtual void CreateTestStorageEntities()
8383

8484
TestInputContainer = BlobClient.GetContainerReference(string.Format("test-input-{0}", _testId));
8585
TestInputContainer.CreateIfNotExists();
86+
// Processing a large number of blobs on startup can take a while,
87+
// so let's start with an empty container.
88+
TestHelpers.ClearContainer(TestInputContainer);
8689

8790
TestOutputContainer = BlobClient.GetContainerReference(string.Format("test-output-{0}", _testId));
8891
TestOutputContainer.CreateIfNotExists();

test/WebJobs.Script.Tests/PowerShellEndToEndTests.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,16 +144,15 @@ public TestFixture() : base(@"TestScripts\PowerShell", "powershell")
144144

145145
protected override void CreateTestStorageEntities()
146146
{
147+
// This will ensure the input container is created.
147148
base.CreateTestStorageEntities();
148149

149150
TestBlobContents = "My Test Blob";
150151
TestBlobName = Guid.NewGuid().ToString();
151152

152153
// write the test blob before the host starts, so it gets picked
153-
// up relatively quickly by the blob trigger test
154-
CloudBlobContainer inputContainer = BlobClient.GetContainerReference("test-input-powershell");
155-
inputContainer.CreateIfNotExists();
156-
CloudBlockBlob inputBlob = inputContainer.GetBlockBlobReference(TestBlobName);
154+
// up relatively quickly by the blob trigger test
155+
CloudBlockBlob inputBlob = TestInputContainer.GetBlockBlobReference(TestBlobName);
157156
inputBlob.UploadText(TestBlobContents);
158157
}
159158
}

test/WebJobs.Script.Tests/SamplesEndToEndTests.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,11 @@ public async Task BlobTriggerBatch_Succeeds()
305305
{
306306
// write input blob
307307
CloudBlobContainer inputContainer = _fixture.BlobClient.GetContainerReference("samples-batch");
308+
await inputContainer.CreateIfNotExistsAsync();
309+
// Processing a large number of blobs on startup can take a while,
310+
// so let's start with an empty container.
311+
TestHelpers.ClearContainer(inputContainer);
312+
308313
string blobName = Guid.NewGuid().ToString();
309314
string testData = "This is a test";
310315
CloudBlockBlob inputBlob = inputContainer.GetBlockBlobReference(blobName);

test/WebJobs.Script.Tests/TestHelpers.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,20 @@ public static async Task<Collection<string>> GetFunctionLogsAsync(string functio
9595
return null;
9696
}
9797

98+
// Deleting and recreating a container can result in a 409 as the container name is not
99+
// immediately available. Instead, use this helper to clear a container.
100+
public static void ClearContainer(CloudBlobContainer container)
101+
{
102+
foreach (IListBlobItem blobItem in container.ListBlobs())
103+
{
104+
CloudBlockBlob blockBlob = blobItem as CloudBlockBlob;
105+
if (blockBlob != null)
106+
{
107+
container.GetBlobReference(blockBlob.Name).DeleteIfExists();
108+
}
109+
}
110+
}
111+
98112
public static DirectoryInfo GetFunctionLogFileDirectory(string functionName)
99113
{
100114
string functionLogsPath = Path.Combine(Path.GetTempPath(), "Functions", "Function", functionName);

0 commit comments

Comments
 (0)