Skip to content

Commit 8fd50c6

Browse files
cjaliagajviau
andauthored
Migrate EndToEnd integration tests to Azure.Data.Tables (#10355)
* Migrate E2E Integration tests to Azure.Data.Tables * Fix formatting Co-authored-by: Jacob Viau <[email protected]> --------- Co-authored-by: Jacob Viau <[email protected]>
1 parent c4b8e28 commit 8fd50c6

File tree

3 files changed

+124
-95
lines changed

3 files changed

+124
-95
lines changed

test/WebJobs.Script.Tests.Integration/ScriptHostEndToEnd/ScriptHostEndToEndTestFixture.cs

Lines changed: 54 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,31 @@
33

44
using System;
55
using System.Collections.Generic;
6-
using System.Linq;
6+
using System.Runtime.InteropServices;
77
using System.Threading;
88
using System.Threading.Tasks;
99
using System.Web.Http;
10+
using Azure;
11+
using Azure.Data.Tables;
1012
using Microsoft.AspNetCore.Hosting;
13+
using Microsoft.Azure.Storage.Blob;
14+
using Microsoft.Azure.Storage.Queue;
1115
using Microsoft.Azure.WebJobs.Script.Config;
1216
using Microsoft.Azure.WebJobs.Script.Description;
1317
using Microsoft.Azure.WebJobs.Script.Eventing;
1418
using Microsoft.Azure.WebJobs.Script.Metrics;
19+
using Microsoft.Azure.WebJobs.Script.WebHost.Helpers;
1520
using Microsoft.Azure.WebJobs.Script.Workers.Rpc;
21+
using Microsoft.Azure.WebJobs.Script.Workers.SharedMemoryDataTransfer;
1622
using Microsoft.Extensions.Configuration;
1723
using Microsoft.Extensions.DependencyInjection;
1824
using Microsoft.Extensions.Hosting;
1925
using Microsoft.Extensions.Logging;
2026
using Microsoft.WebJobs.Script.Tests;
21-
using Microsoft.Azure.Storage;
22-
using Microsoft.Azure.Storage.Blob;
23-
using Microsoft.Azure.Storage.Queue;
24-
using Microsoft.Azure.Cosmos.Table;
2527
using Moq;
2628
using Xunit;
2729
using CloudStorageAccount = Microsoft.Azure.Storage.CloudStorageAccount;
28-
using TableStorageAccount = Microsoft.Azure.Cosmos.Table.CloudStorageAccount;
2930
using IApplicationLifetime = Microsoft.AspNetCore.Hosting.IApplicationLifetime;
30-
using Microsoft.Azure.WebJobs.Script.Workers.SharedMemoryDataTransfer;
31-
using System.Runtime.InteropServices;
3231

3332
namespace Microsoft.Azure.WebJobs.Script.Tests
3433
{
@@ -73,15 +72,15 @@ protected ScriptHostEndToEndTestFixture(string rootPath, string testId, string f
7372

7473
public CloudQueueClient QueueClient { get; private set; }
7574

76-
public CloudTableClient TableClient { get; private set; }
75+
public TableServiceClient TableServiceClient { get; private set; }
7776

7877
public CloudBlobClient BlobClient { get; private set; }
7978

8079
public CloudQueue TestQueue { get; private set; }
8180

8281
public CloudQueue MobileTablesQueue { get; private set; }
8382

84-
public CloudTable TestTable { get; private set; }
83+
public TableClient TestTable { get; private set; }
8584

8685
public ScriptHost JobHost { get; private set; }
8786

@@ -109,8 +108,7 @@ public async Task InitializeAsync()
109108
QueueClient = storageAccount.CreateCloudQueueClient();
110109
BlobClient = storageAccount.CreateCloudBlobClient();
111110

112-
TableStorageAccount tableStorageAccount = TableStorageAccount.Parse(connectionString);
113-
TableClient = tableStorageAccount.CreateCloudTableClient();
111+
TableServiceClient = new TableServiceClient(connectionString);
114112

115113
await CreateTestStorageEntities();
116114

@@ -219,50 +217,56 @@ protected virtual async Task CreateTestStorageEntities()
219217
await TestOutputContainer.CreateIfNotExistsAsync();
220218
await TestHelpers.ClearContainerAsync(TestOutputContainer);
221219

222-
TestTable = TableClient.GetTableReference("test");
220+
TestTable = TableServiceClient.GetTableClient("test");
223221
await TestTable.CreateIfNotExistsAsync();
224222

225-
await DeleteEntities(TestTable, "AAA");
226-
await DeleteEntities(TestTable, "BBB");
227-
228-
var batch = new TableBatchOperation();
229-
batch.Insert(new TestEntity { PartitionKey = "AAA", RowKey = "001", Region = "West", Name = "Test Entity 1", Status = 0 });
230-
batch.Insert(new TestEntity { PartitionKey = "AAA", RowKey = "002", Region = "East", Name = "Test Entity 2", Status = 1 });
231-
batch.Insert(new TestEntity { PartitionKey = "AAA", RowKey = "003", Region = "West", Name = "Test Entity 3", Status = 1 });
232-
batch.Insert(new TestEntity { PartitionKey = "AAA", RowKey = "004", Region = "West", Name = "Test Entity 4", Status = 1 });
233-
batch.Insert(new TestEntity { PartitionKey = "AAA", RowKey = "005", Region = "East", Name = "Test Entity 5", Status = 0 });
234-
await TestTable.ExecuteBatchAsync(batch);
235-
236-
batch = new TableBatchOperation();
237-
batch.Insert(new TestEntity { PartitionKey = "BBB", RowKey = "001", Region = "South", Name = "Test Entity 1", Status = 0 });
238-
batch.Insert(new TestEntity { PartitionKey = "BBB", RowKey = "002", Region = "West", Name = "Test Entity 2", Status = 1 });
239-
batch.Insert(new TestEntity { PartitionKey = "BBB", RowKey = "003", Region = "West", Name = "Test Entity 3", Status = 0 });
240-
await TestTable.ExecuteBatchAsync(batch);
223+
await DeleteEntities(TestTable, TableServiceClient, "AAA");
224+
await DeleteEntities(TestTable, TableServiceClient, "BBB");
225+
226+
var batch = new List<TableTransactionAction>
227+
{
228+
new TableTransactionAction(TableTransactionActionType.Add, new TestEntity { PartitionKey = "AAA", RowKey = "001", Region = "West", Name = "Test Entity 1", Status = 0 }),
229+
new TableTransactionAction(TableTransactionActionType.Add, new TestEntity { PartitionKey = "AAA", RowKey = "002", Region = "East", Name = "Test Entity 2", Status = 1 }),
230+
new TableTransactionAction (TableTransactionActionType.Add, new TestEntity { PartitionKey = "AAA", RowKey = "003", Region = "West", Name = "Test Entity 3", Status = 1 }),
231+
new TableTransactionAction (TableTransactionActionType.Add, new TestEntity { PartitionKey = "AAA", RowKey = "004", Region = "West", Name = "Test Entity 4", Status = 1 }),
232+
new TableTransactionAction (TableTransactionActionType.Add, new TestEntity { PartitionKey = "AAA", RowKey = "005", Region = "East", Name = "Test Entity 5", Status = 0 })
233+
};
234+
await TestTable.SubmitTransactionAsync(batch);
235+
236+
batch =
237+
[
238+
new TableTransactionAction(TableTransactionActionType.Add, new TestEntity { PartitionKey = "BBB", RowKey = "001", Region = "South", Name = "Test Entity 1", Status = 0 }),
239+
new TableTransactionAction(TableTransactionActionType.Add, new TestEntity { PartitionKey = "BBB", RowKey = "002", Region = "West", Name = "Test Entity 2", Status = 1 }),
240+
new TableTransactionAction(TableTransactionActionType.Add, new TestEntity { PartitionKey = "BBB", RowKey = "003", Region = "West", Name = "Test Entity 3", Status = 0 }),
241+
];
242+
await TestTable.SubmitTransactionAsync(batch);
241243
}
242244

243-
public async Task DeleteEntities(CloudTable table, string partition = null)
245+
public async Task DeleteEntities(TableClient table, TableServiceClient tableServiceClient, string partition = null)
244246
{
245-
if (!await table.ExistsAsync())
247+
if (!await TableStorageHelpers.TableExistAsync(table, tableServiceClient))
246248
{
247249
return;
248250
}
249251

250-
TableQuery query = new TableQuery();
252+
string query = string.Empty;
251253
if (partition != null)
252254
{
253-
query.FilterString = string.Format("PartitionKey eq '{0}'", partition);
255+
query = TableClient.CreateQueryFilter($"PartitionKey eq {partition}");
254256
}
255257

256-
var entities = await table.ExecuteQuerySegmentedAsync(query, null);
258+
var entities = table.QueryAsync<TableEntity>(query, null);
257259

258-
if (entities.Any())
260+
261+
var batch = new List<TableTransactionAction>();
262+
await foreach (var entity in entities)
259263
{
260-
var batch = new TableBatchOperation();
261-
foreach (var entity in entities)
262-
{
263-
batch.Delete(entity);
264-
}
265-
await table.ExecuteBatchAsync(batch);
264+
batch.Add(new TableTransactionAction(TableTransactionActionType.Delete, entity));
265+
}
266+
267+
if (batch.Count != 0)
268+
{
269+
await table.SubmitTransactionAsync(batch);
266270
}
267271
}
268272

@@ -282,13 +286,21 @@ public virtual async Task DisposeAsync()
282286
Environment.SetEnvironmentVariable(RpcWorkerConstants.FunctionWorkerRuntimeSettingName, string.Empty);
283287
}
284288

285-
private class TestEntity : TableEntity
289+
private class TestEntity : ITableEntity
286290
{
287291
public string Name { get; set; }
288292

289293
public string Region { get; set; }
290294

291295
public int Status { get; set; }
296+
297+
public string PartitionKey { get; set; }
298+
299+
public string RowKey { get; set; }
300+
301+
public DateTimeOffset? Timestamp { get; set; }
302+
303+
public ETag ETag { get; set; }
292304
}
293305
}
294306
}

test/WebJobs.Script.Tests.Integration/WebHostEndToEnd/EndToEndTestFixture.cs

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Collections.Generic;
56
using System.IO;
67
using System.Linq;
78
using System.Net.Http;
89
using System.Text;
910
using System.Threading.Tasks;
10-
using Microsoft.Azure.Cosmos.Table;
11+
using Azure;
12+
using Azure.Data.Tables;
1113
using Microsoft.Azure.Storage.Blob;
1214
using Microsoft.Azure.Storage.Queue;
1315
using Microsoft.Azure.WebJobs.Script.BindingExtensions;
@@ -16,6 +18,7 @@
1618
using Microsoft.Azure.WebJobs.Script.Models;
1719
using Microsoft.Azure.WebJobs.Script.WebHost.Authentication;
1820
using Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics;
21+
using Microsoft.Azure.WebJobs.Script.WebHost.Helpers;
1922
using Microsoft.Azure.WebJobs.Script.WebHost.Management;
2023
using Microsoft.Azure.WebJobs.Script.Workers.Rpc;
2124
using Microsoft.Extensions.Configuration;
@@ -25,7 +28,6 @@
2528
using Moq;
2629
using Xunit;
2730
using CloudStorageAccount = Microsoft.Azure.Storage.CloudStorageAccount;
28-
using TableStorageAccount = Microsoft.Azure.Cosmos.Table.CloudStorageAccount;
2931

3032
namespace Microsoft.Azure.WebJobs.Script.Tests
3133
{
@@ -61,15 +63,15 @@ protected EndToEndTestFixture(
6163

6264
public CloudQueueClient QueueClient { get; private set; }
6365

64-
public CloudTableClient TableClient { get; private set; }
66+
public TableServiceClient TableServiceClient { get; private set; }
6567

6668
public CloudBlobClient BlobClient { get; private set; }
6769

6870
public CloudQueue TestQueue { get; private set; }
6971

7072
public CloudQueue MobileTablesQueue { get; private set; }
7173

72-
public CloudTable TestTable { get; private set; }
74+
public TableClient TestTable { get; private set; }
7375

7476
public TestFunctionHost Host { get; private set; }
7577

@@ -169,9 +171,7 @@ string GetDestPath(int counter)
169171

170172
QueueClient = storageAccount.CreateCloudQueueClient();
171173
BlobClient = storageAccount.CreateCloudBlobClient();
172-
173-
TableStorageAccount tableStorageAccount = TableStorageAccount.Parse(connectionString);
174-
TableClient = tableStorageAccount.CreateCloudTableClient();
174+
TableServiceClient = new TableServiceClient(connectionString);
175175

176176
await CreateTestStorageEntities();
177177
}
@@ -229,50 +229,56 @@ protected virtual async Task CreateTestStorageEntities()
229229
await TestOutputContainer.CreateIfNotExistsAsync();
230230
await TestHelpers.ClearContainerAsync(TestOutputContainer);
231231

232-
TestTable = TableClient.GetTableReference("test");
232+
TestTable = TableServiceClient.GetTableClient("test");
233233
await TestTable.CreateIfNotExistsAsync();
234234

235-
await DeleteEntities(TestTable, "AAA");
236-
await DeleteEntities(TestTable, "BBB");
237-
238-
var batch = new TableBatchOperation();
239-
batch.Insert(new TestEntity { PartitionKey = "AAA", RowKey = "001", Region = "West", Name = "Test Entity 1", Status = 0 });
240-
batch.Insert(new TestEntity { PartitionKey = "AAA", RowKey = "002", Region = "East", Name = "Test Entity 2", Status = 1 });
241-
batch.Insert(new TestEntity { PartitionKey = "AAA", RowKey = "003", Region = "West", Name = "Test Entity 3", Status = 1 });
242-
batch.Insert(new TestEntity { PartitionKey = "AAA", RowKey = "004", Region = "West", Name = "Test Entity 4", Status = 1 });
243-
batch.Insert(new TestEntity { PartitionKey = "AAA", RowKey = "005", Region = "East", Name = "Test Entity 5", Status = 0 });
244-
await TestTable.ExecuteBatchAsync(batch);
245-
246-
batch = new TableBatchOperation();
247-
batch.Insert(new TestEntity { PartitionKey = "BBB", RowKey = "001", Region = "South", Name = "Test Entity 1", Status = 0 });
248-
batch.Insert(new TestEntity { PartitionKey = "BBB", RowKey = "002", Region = "West", Name = "Test Entity 2", Status = 1 });
249-
batch.Insert(new TestEntity { PartitionKey = "BBB", RowKey = "003", Region = "West", Name = "Test Entity 3", Status = 0 });
250-
await TestTable.ExecuteBatchAsync(batch);
235+
await DeleteEntities(TestTable, TableServiceClient, "AAA");
236+
await DeleteEntities(TestTable, TableServiceClient, "BBB");
237+
238+
var batch = new List<TableTransactionAction>
239+
{
240+
new TableTransactionAction(TableTransactionActionType.Add, new TestEntity { PartitionKey = "AAA", RowKey = "001", Region = "West", Name = "Test Entity 1", Status = 0 }),
241+
new TableTransactionAction(TableTransactionActionType.Add, new TestEntity { PartitionKey = "AAA", RowKey = "002", Region = "East", Name = "Test Entity 2", Status = 1 }),
242+
new TableTransactionAction (TableTransactionActionType.Add, new TestEntity { PartitionKey = "AAA", RowKey = "003", Region = "West", Name = "Test Entity 3", Status = 1 }),
243+
new TableTransactionAction (TableTransactionActionType.Add, new TestEntity { PartitionKey = "AAA", RowKey = "004", Region = "West", Name = "Test Entity 4", Status = 1 }),
244+
new TableTransactionAction (TableTransactionActionType.Add, new TestEntity { PartitionKey = "AAA", RowKey = "005", Region = "East", Name = "Test Entity 5", Status = 0 })
245+
};
246+
await TestTable.SubmitTransactionAsync(batch);
247+
248+
batch =
249+
[
250+
new TableTransactionAction(TableTransactionActionType.Add, new TestEntity { PartitionKey = "BBB", RowKey = "001", Region = "South", Name = "Test Entity 1", Status = 0 }),
251+
new TableTransactionAction(TableTransactionActionType.Add, new TestEntity { PartitionKey = "BBB", RowKey = "002", Region = "West", Name = "Test Entity 2", Status = 1 }),
252+
new TableTransactionAction(TableTransactionActionType.Add, new TestEntity { PartitionKey = "BBB", RowKey = "003", Region = "West", Name = "Test Entity 3", Status = 0 }),
253+
];
254+
await TestTable.SubmitTransactionAsync(batch);
251255
}
252256

253-
public async Task DeleteEntities(CloudTable table, string partition = null)
257+
public async Task DeleteEntities(TableClient table, TableServiceClient tableServiceClient, string partition = null)
254258
{
255-
if (!await table.ExistsAsync())
259+
if (!await TableStorageHelpers.TableExistAsync(table, tableServiceClient))
256260
{
257261
return;
258262
}
259263

260-
TableQuery query = new TableQuery();
264+
string query = string.Empty;
261265
if (partition != null)
262266
{
263-
query.FilterString = string.Format("PartitionKey eq '{0}'", partition);
267+
query = TableClient.CreateQueryFilter($"PartitionKey eq {partition}");
264268
}
265269

266-
var entities = await table.ExecuteQuerySegmentedAsync(query, null);
270+
var entities = table.QueryAsync<TableEntity>(query, null);
271+
267272

268-
if (entities.Any())
273+
var batch = new List<TableTransactionAction>();
274+
await foreach (var entity in entities)
269275
{
270-
var batch = new TableBatchOperation();
271-
foreach (var entity in entities)
272-
{
273-
batch.Delete(entity);
274-
}
275-
await table.ExecuteBatchAsync(batch);
276+
batch.Add(new TableTransactionAction(TableTransactionActionType.Delete, entity));
277+
}
278+
279+
if (batch.Count != 0)
280+
{
281+
await table.SubmitTransactionAsync(batch);
276282
}
277283
}
278284

@@ -345,13 +351,21 @@ private class TestExtensionBundleManager : IExtensionBundleManager
345351

346352
}
347353

348-
private class TestEntity : TableEntity
354+
private class TestEntity : ITableEntity
349355
{
350356
public string Name { get; set; }
351357

352358
public string Region { get; set; }
353359

354360
public int Status { get; set; }
361+
362+
public string PartitionKey { get; set; }
363+
364+
public string RowKey { get; set; }
365+
366+
public DateTimeOffset? Timestamp { get; set; }
367+
368+
public ETag ETag { get; set; }
355369
}
356370
}
357371
}

0 commit comments

Comments
 (0)