-
Notifications
You must be signed in to change notification settings - Fork 355
/
MultipleStorageAccountsEndToEndTests.cs
384 lines (320 loc) · 15 KB
/
MultipleStorageAccountsEndToEndTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs.Host.TestCommon;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.Queue;
using Microsoft.WindowsAzure.Storage.Table;
using Newtonsoft.Json.Linq;
using Xunit;
namespace Microsoft.Azure.WebJobs.Host.EndToEndTests
{
public class MultipleStorageAccountsEndToEndTests : IClassFixture<MultipleStorageAccountsEndToEndTests.TestFixture>
{
private const string TestArtifactPrefix = "e2etestmultiaccount";
private const string Input = TestArtifactPrefix + "-input-%rnd%";
private const string Output = TestArtifactPrefix + "-output-%rnd%";
private const string InputTableName = TestArtifactPrefix + "tableinput%rnd%";
private const string OutputTableName = TestArtifactPrefix + "tableinput%rnd%";
private const string TestData = "TestData";
private const string Secondary = "SecondaryStorage";
private static CloudStorageAccount primaryAccountResult;
private static CloudStorageAccount secondaryAccountResult;
private readonly TestFixture _fixture;
public MultipleStorageAccountsEndToEndTests(TestFixture fixture)
{
_fixture = fixture;
}
[Fact]
public async Task BlobToBlob_DifferentAccounts_PrimaryToSecondary_Succeeds()
{
CloudBlockBlob resultBlob = null;
await TestHelpers.Await(async () =>
{
resultBlob = (CloudBlockBlob)(await _fixture.OutputContainer2.ListBlobsSegmentedAsync("blob1", null)).Results.SingleOrDefault();
return resultBlob != null;
});
string data = await resultBlob.DownloadTextAsync();
Assert.Equal("blob1", resultBlob.Name);
Assert.Equal(TestData, data);
}
[Fact]
public async Task BlobToBlob_DifferentAccounts_SecondaryToPrimary_Succeeds()
{
CloudBlockBlob resultBlob = null;
await TestHelpers.Await(async () =>
{
resultBlob = (CloudBlockBlob)(await _fixture.OutputContainer1.ListBlobsSegmentedAsync(null)).Results.SingleOrDefault();
return resultBlob != null;
});
string data = await resultBlob.DownloadTextAsync();
Assert.Equal("blob2", resultBlob.Name);
Assert.Equal(TestData, data);
}
[Fact]
public async Task QueueToQueue_DifferentAccounts_PrimaryToSecondary_Succeeds()
{
CloudQueueMessage resultMessage = null;
await TestHelpers.Await(async () =>
{
resultMessage = await _fixture.OutputQueue2.GetMessageAsync();
return resultMessage != null;
});
Assert.Equal(TestData, resultMessage.AsString);
}
[Theory]
[InlineData("QueueToBlob_DifferentAccounts_PrimaryToSecondary_NameResolver")]
[InlineData("QueueToBlob_DifferentAccounts_PrimaryToSecondary_FullSettingName")]
public async Task QueueToBlob_DifferentAccounts_PrimaryToSecondary_NameResolver_Succeeds(string methodName)
{
var method = typeof(MultipleStorageAccountsEndToEndTests).GetMethod(methodName);
string name = Guid.NewGuid().ToString();
JObject jObject = new JObject
{
{ "Name", name },
{ "Value", TestData }
};
await _fixture.JobHost.CallAsync(method, new { input = jObject.ToString() });
var blobReference = await _fixture.OutputContainer2.GetBlobReferenceFromServerAsync(name);
await TestHelpers.Await(() => blobReference.ExistsAsync());
string data;
using (var memoryStream = new MemoryStream())
{
await blobReference.DownloadToStreamAsync(memoryStream);
memoryStream.Position = 0;
using (var reader = new StreamReader(memoryStream))
{
data = reader.ReadToEnd();
}
}
Assert.Equal(TestData, data);
}
[Fact]
public async Task QueueToQueue_DifferentAccounts_SecondaryToPrimary_Succeeds()
{
CloudQueueMessage resultMessage = null;
await TestHelpers.Await(async () =>
{
resultMessage = await _fixture.OutputQueue1.GetMessageAsync();
return resultMessage != null;
});
Assert.Equal(TestData, resultMessage.AsString);
}
[Fact]
public async Task Table_PrimaryAndSecondary_Succeeds()
{
await _fixture.JobHost.CallAsync(typeof(MultipleStorageAccountsEndToEndTests).GetMethod("Table_PrimaryAndSecondary"));
TestTableEntity entity1 = null;
TestTableEntity entity2 = null;
await TestHelpers.Await(async () =>
{
TableResult result = await _fixture.OutputTable1.ExecuteAsync(TableOperation.Retrieve<TestTableEntity>("test", "test"));
if (result != null)
{
entity1 = (TestTableEntity)result.Result;
}
result = await _fixture.OutputTable2.ExecuteAsync(TableOperation.Retrieve<TestTableEntity>("test", "test"));
if (result != null)
{
entity2 = (TestTableEntity)result.Result;
}
return entity1 != null && entity2 != null;
});
Assert.Equal(TestData, entity1.Text);
Assert.Equal(TestData, entity2.Text);
}
[Fact]
public async Task CloudStorageAccount_PrimaryAndSecondary_Succeeds()
{
await _fixture.JobHost.CallAsync(typeof(MultipleStorageAccountsEndToEndTests).GetMethod(nameof(MultipleStorageAccountsEndToEndTests.BindToCloudStorageAccount)));
Assert.Equal(_fixture.Account1.SdkObject.Credentials.AccountName, primaryAccountResult.Credentials.AccountName);
Assert.Equal(_fixture.Account2.SdkObject.Credentials.AccountName, secondaryAccountResult.Credentials.AccountName);
}
public static void BlobToBlob_DifferentAccounts_PrimaryToSecondary(
[BlobTrigger(Input + "/{name}")] string input,
[Blob(Output + "/{name}", Connection = Secondary)] out string output)
{
output = input;
}
public static void BlobToBlob_DifferentAccounts_SecondaryToPrimary(
[BlobTrigger(Input + "/{name}", Connection = Secondary)] string input,
[Blob(Output + "/{name}")] out string output)
{
output = input;
}
public static void QueueToQueue_DifferentAccounts_PrimaryToSecondary(
[QueueTrigger(Input)] string input,
[Queue(Output, Connection = Secondary)] out string output)
{
output = input;
}
[NoAutomaticTrigger]
public static void QueueToBlob_DifferentAccounts_PrimaryToSecondary_NameResolver(
[QueueTrigger("test")] Message input,
[Blob(Output + "/{Name}", Connection = "%test_account%")] out string output)
{
output = input.Value;
}
[NoAutomaticTrigger]
public static void QueueToBlob_DifferentAccounts_PrimaryToSecondary_FullSettingName(
[QueueTrigger("test")] Message input,
[Blob(Output + "/{Name}", Connection = "AzureWebJobsSecondaryStorage")] out string output)
{
output = input.Value;
}
public static void QueueToQueue_DifferentAccounts_SecondaryToPrimary(
[QueueTrigger(Input, Connection = Secondary)] string input,
[Queue(Output)] out string output)
{
output = input;
}
[NoAutomaticTrigger]
public async static Task Table_PrimaryAndSecondary(
[Table(OutputTableName)] CloudTable primaryOutput,
[Table(OutputTableName, Connection = Secondary)] CloudTable secondaryOutput)
{
TestTableEntity entity = new TestTableEntity
{
PartitionKey = "test",
RowKey = "test",
Text = TestData
};
await primaryOutput.ExecuteAsync(TableOperation.InsertOrReplace(entity));
await secondaryOutput.ExecuteAsync(TableOperation.InsertOrReplace(entity));
}
[NoAutomaticTrigger]
public static void BindToCloudStorageAccount(
CloudStorageAccount primary,
[StorageAccount(Secondary)] CloudStorageAccount secondary)
{
primaryAccountResult = primary;
secondaryAccountResult = secondary;
}
private class TestNameResolver : RandomNameResolver
{
public override string Resolve(string name)
{
if (name == "test_account")
{
return "SecondaryStorage";
}
return base.Resolve(name);
}
}
public class TestFixture : IAsyncLifetime
{
public async Task InitializeAsync()
{
RandomNameResolver nameResolver = new TestNameResolver();
Host = new HostBuilder()
.ConfigureDefaultTestHost<MultipleStorageAccountsEndToEndTests>(b =>
{
b.AddAzureStorage();
})
.ConfigureServices(services =>
{
services.AddSingleton<INameResolver>(nameResolver);
})
.Build();
Account1 = Host.GetStorageAccount();
var config = Host.Services.GetService<IConfiguration>();
string secondaryConnectionString = config[$"AzureWebJobs{Secondary}"];
Account2 = StorageAccount.NewFromConnectionString(secondaryConnectionString);
await CleanContainersAsync();
CloudBlobClient blobClient1 = Account1.CreateCloudBlobClient();
string inputName = nameResolver.ResolveInString(Input);
CloudBlobContainer inputContainer1 = blobClient1.GetContainerReference(inputName);
await inputContainer1.CreateIfNotExistsAsync();
string outputName = nameResolver.ResolveWholeString(Output);
OutputContainer1 = blobClient1.GetContainerReference(outputName);
await OutputContainer1.CreateIfNotExistsAsync();
CloudBlobClient blobClient2 = Account2.CreateCloudBlobClient();
CloudBlobContainer inputContainer2 = blobClient2.GetContainerReference(inputName);
await inputContainer2.CreateIfNotExistsAsync();
OutputContainer2 = blobClient2.GetContainerReference(outputName);
await OutputContainer2.CreateIfNotExistsAsync();
CloudQueueClient queueClient1 = Account1.CreateCloudQueueClient();
CloudQueue inputQueue1 = queueClient1.GetQueueReference(inputName);
await inputQueue1.CreateIfNotExistsAsync();
OutputQueue1 = queueClient1.GetQueueReference(outputName);
await OutputQueue1.CreateIfNotExistsAsync();
CloudQueueClient queueClient2 = Account2.CreateCloudQueueClient();
CloudQueue inputQueue2 = queueClient2.GetQueueReference(inputName);
await inputQueue2.CreateIfNotExistsAsync();
OutputQueue2 = queueClient2.GetQueueReference(outputName);
await OutputQueue2.CreateIfNotExistsAsync();
CloudTableClient tableClient1 = Account1.CreateCloudTableClient();
string outputTableName = nameResolver.ResolveWholeString(OutputTableName);
OutputTable1 = tableClient1.GetTableReference(outputTableName);
OutputTable2 = Account2.CreateCloudTableClient().GetTableReference(outputTableName);
// upload some test blobs to the input containers of both storage accounts
CloudBlockBlob blob = inputContainer1.GetBlockBlobReference("blob1");
await blob.UploadTextAsync(TestData);
blob = inputContainer2.GetBlockBlobReference("blob2");
await blob.UploadTextAsync(TestData);
// upload some test queue messages to the input queues of both storage accounts
await inputQueue1.AddMessageAsync(new CloudQueueMessage(TestData));
await inputQueue2.AddMessageAsync(new CloudQueueMessage(TestData));
Host.Start();
}
public JobHost JobHost => Host.GetJobHost();
public IHost Host
{
get;
private set;
}
public StorageAccount Account1 { get; private set; }
public StorageAccount Account2 { get; private set; }
public CloudBlobContainer OutputContainer1 { get; private set; }
public CloudBlobContainer OutputContainer2 { get; private set; }
public CloudQueue OutputQueue1 { get; private set; }
public CloudQueue OutputQueue2 { get; private set; }
public CloudTable OutputTable1 { get; private set; }
public CloudTable OutputTable2 { get; private set; }
public async Task DisposeAsync()
{
await Host.StopAsync();
await CleanContainersAsync();
}
private async Task CleanContainersAsync()
{
await Clean(Account1);
await Clean(Account2);
}
}
private async static Task Clean(StorageAccount account)
{
CloudBlobClient blobClient = account.CreateCloudBlobClient();
foreach (var testContainer in (await blobClient.ListContainersSegmentedAsync(TestArtifactPrefix, null)).Results)
{
await testContainer.DeleteAsync();
}
CloudTableClient tableClient = account.CreateCloudTableClient();
foreach (var table in await tableClient.ListTablesSegmentedAsync(TestArtifactPrefix, null))
{
await table.DeleteAsync();
}
CloudQueueClient queueClient = account.CreateCloudQueueClient();
foreach (var queue in (await queueClient.ListQueuesSegmentedAsync(TestArtifactPrefix, null)).Results)
{
await queue.DeleteAsync();
}
}
public class TestTableEntity : TableEntity
{
public string Text { get; set; }
}
public class Message
{
public string Name { get; set; }
public string Value { get; set; }
}
}
}