Skip to content

Commit cba50f8

Browse files
authored
Migrate E2E Integration tests to Azure.Data.Tables (#10362)
1 parent 0812211 commit cba50f8

File tree

3 files changed

+133
-104
lines changed

3 files changed

+133
-104
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,31 +3,30 @@
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;
18+
using Microsoft.Azure.WebJobs.Script.WebHost.Helpers;
1419
using Microsoft.Azure.WebJobs.Script.Workers.Rpc;
20+
using Microsoft.Azure.WebJobs.Script.Workers.SharedMemoryDataTransfer;
1521
using Microsoft.Extensions.Configuration;
1622
using Microsoft.Extensions.DependencyInjection;
1723
using Microsoft.Extensions.Hosting;
1824
using Microsoft.Extensions.Logging;
1925
using Microsoft.WebJobs.Script.Tests;
20-
using Microsoft.Azure.Storage;
21-
using Microsoft.Azure.Storage.Blob;
22-
using Microsoft.Azure.Storage.Queue;
23-
using Microsoft.Azure.Cosmos.Table;
2426
using Moq;
2527
using Xunit;
2628
using CloudStorageAccount = Microsoft.Azure.Storage.CloudStorageAccount;
27-
using TableStorageAccount = Microsoft.Azure.Cosmos.Table.CloudStorageAccount;
2829
using IApplicationLifetime = Microsoft.AspNetCore.Hosting.IApplicationLifetime;
29-
using Microsoft.Azure.WebJobs.Script.Workers.SharedMemoryDataTransfer;
30-
using System.Runtime.InteropServices;
3130

3231
namespace Microsoft.Azure.WebJobs.Script.Tests
3332
{
@@ -72,15 +71,15 @@ protected ScriptHostEndToEndTestFixture(string rootPath, string testId, string f
7271

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

75-
public CloudTableClient TableClient { get; private set; }
74+
public TableServiceClient TableServiceClient { get; private set; }
7675

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

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

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

83-
public CloudTable TestTable { get; private set; }
82+
public TableClient TestTable { get; private set; }
8483

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

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

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

114112
await CreateTestStorageEntities();
115113

@@ -216,50 +214,56 @@ protected virtual async Task CreateTestStorageEntities()
216214
await TestOutputContainer.CreateIfNotExistsAsync();
217215
await TestHelpers.ClearContainerAsync(TestOutputContainer);
218216

219-
TestTable = TableClient.GetTableReference("test");
217+
TestTable = TableServiceClient.GetTableClient("test");
220218
await TestTable.CreateIfNotExistsAsync();
221219

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

240-
public async Task DeleteEntities(CloudTable table, string partition = null)
242+
public async Task DeleteEntities(TableClient table, TableServiceClient tableServiceClient, string partition = null)
241243
{
242-
if (!await table.ExistsAsync())
244+
if (!await TableStorageHelpers.TableExistAsync(table, tableServiceClient))
243245
{
244246
return;
245247
}
246248

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

253-
var entities = await table.ExecuteQuerySegmentedAsync(query, null);
255+
var entities = table.QueryAsync<TableEntity>(query, null);
256+
257+
258+
var batch = new List<TableTransactionAction>();
259+
await foreach (var entity in entities)
260+
{
261+
batch.Add(new TableTransactionAction(TableTransactionActionType.Delete, entity));
262+
}
254263

255-
if (entities.Any())
264+
if (batch.Count != 0)
256265
{
257-
var batch = new TableBatchOperation();
258-
foreach (var entity in entities)
259-
{
260-
batch.Delete(entity);
261-
}
262-
await table.ExecuteBatchAsync(batch);
266+
await table.SubmitTransactionAsync(batch);
263267
}
264268
}
265269

@@ -279,13 +283,21 @@ public virtual async Task DisposeAsync()
279283
Environment.SetEnvironmentVariable(RpcWorkerConstants.FunctionWorkerRuntimeSettingName, string.Empty);
280284
}
281285

282-
private class TestEntity : TableEntity
286+
private class TestEntity : ITableEntity
283287
{
284288
public string Name { get; set; }
285289

286290
public string Region { get; set; }
287291

288292
public int Status { get; set; }
293+
294+
public string PartitionKey { get; set; }
295+
296+
public string RowKey { get; set; }
297+
298+
public DateTimeOffset? Timestamp { get; set; }
299+
300+
public ETag ETag { get; set; }
289301
}
290302
}
291303
}

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)