From f7d13872d3d6882508a914ca7e9c3ab1245a8d15 Mon Sep 17 00:00:00 2001 From: andystaples <77818326+andystaples@users.noreply.github.com> Date: Wed, 18 Oct 2023 14:45:33 -0700 Subject: [PATCH] Replace deprecated packages in E2E (#1022) * Replace Microsoft.Azure.EventHubs with latest stable Azure.Messaging.EventHubs * Replace Microsoft.Azure.DocumentDB.Core with latest stable Microsoft.Azure.Cosmos --- ...zure.Functions.PowerShellWorker.E2E.csproj | 4 +- .../Constants.cs | 4 +- .../Helpers/CosmosDBHelpers.cs | 100 ++++++++++-------- .../Helpers/EventHubsHelpers.cs | 13 +-- .../CosmosDBTriggerAndOutput/function.json | 4 +- 5 files changed, 65 insertions(+), 60 deletions(-) diff --git a/test/E2E/Azure.Functions.PowerShellWorker.E2E/Azure.Functions.PowerShellWorker.E2E/Azure.Functions.PowerShellWorker.E2E.csproj b/test/E2E/Azure.Functions.PowerShellWorker.E2E/Azure.Functions.PowerShellWorker.E2E/Azure.Functions.PowerShellWorker.E2E.csproj index f4e83f61..1d53e976 100644 --- a/test/E2E/Azure.Functions.PowerShellWorker.E2E/Azure.Functions.PowerShellWorker.E2E/Azure.Functions.PowerShellWorker.E2E.csproj +++ b/test/E2E/Azure.Functions.PowerShellWorker.E2E/Azure.Functions.PowerShellWorker.E2E/Azure.Functions.PowerShellWorker.E2E.csproj @@ -7,8 +7,8 @@ - - + + diff --git a/test/E2E/Azure.Functions.PowerShellWorker.E2E/Azure.Functions.PowerShellWorker.E2E/Constants.cs b/test/E2E/Azure.Functions.PowerShellWorker.E2E/Azure.Functions.PowerShellWorker.E2E/Constants.cs index ea02fb5f..aaaa84d2 100644 --- a/test/E2E/Azure.Functions.PowerShellWorker.E2E/Azure.Functions.PowerShellWorker.E2E/Constants.cs +++ b/test/E2E/Azure.Functions.PowerShellWorker.E2E/Azure.Functions.PowerShellWorker.E2E/Constants.cs @@ -23,8 +23,8 @@ public static class Queue { public static class CosmosDB { public static string CosmosDBConnectionStringSetting = Environment.GetEnvironmentVariable("AzureWebJobsCosmosDBConnectionString"); public static string DbName = "ItemDb"; - public static string InputCollectionName = "ItemCollectionIn"; - public static string OutputCollectionName = "ItemCollectionOut"; + public static string InputCollectionName = "PartitionedItemCollectionIn"; + public static string OutputCollectionName = "PartitionedItemCollectionOut"; public static string LeaseCollectionName = "leases"; } diff --git a/test/E2E/Azure.Functions.PowerShellWorker.E2E/Azure.Functions.PowerShellWorker.E2E/Helpers/CosmosDBHelpers.cs b/test/E2E/Azure.Functions.PowerShellWorker.E2E/Azure.Functions.PowerShellWorker.E2E/Helpers/CosmosDBHelpers.cs index a52c3c83..ea217a8d 100644 --- a/test/E2E/Azure.Functions.PowerShellWorker.E2E/Azure.Functions.PowerShellWorker.E2E/Helpers/CosmosDBHelpers.cs +++ b/test/E2E/Azure.Functions.PowerShellWorker.E2E/Azure.Functions.PowerShellWorker.E2E/Helpers/CosmosDBHelpers.cs @@ -3,30 +3,27 @@ using System; using System.Threading.Tasks; -using Microsoft.Azure.Documents; -using Microsoft.Azure.Documents.Client; +using Microsoft.Azure.Cosmos; namespace Azure.Functions.PowerShell.Tests.E2E { - public class TestDocument - { - public string id { get; set; } - public string name { get; set; } - } public static class CosmosDBHelpers { - private static DocumentClient _docDbClient; - private static Uri inputCollectionsUri = UriFactory.CreateDocumentCollectionUri(Constants.CosmosDB.DbName, Constants.CosmosDB.InputCollectionName); - private static Uri outputCollectionsUri = UriFactory.CreateDocumentCollectionUri(Constants.CosmosDB.DbName, Constants.CosmosDB.OutputCollectionName); - private static Uri leasesCollectionsUri = UriFactory.CreateDocumentCollectionUri(Constants.CosmosDB.DbName, Constants.CosmosDB.LeaseCollectionName); + private static CosmosClient _cosmosDbClient; + + private class Document + { + // lower-case because Cosmos expects a field with this name + public string id { get; set; } + } static CosmosDBHelpers() { var builder = new System.Data.Common.DbConnectionStringBuilder(); builder.ConnectionString = Constants.CosmosDB.CosmosDBConnectionStringSetting; - var serviceUri = new Uri(builder["AccountEndpoint"].ToString()); - _docDbClient = new DocumentClient(serviceUri, builder["AccountKey"].ToString()); + var serviceUri = builder["AccountEndpoint"].ToString(); + _cosmosDbClient = new CosmosClient(serviceUri, builder["AccountKey"].ToString()); } // keep @@ -34,51 +31,48 @@ public async static Task CreateDocument(string docId) { Document documentToTest = new Document() { - Id = docId + id = docId }; - Document insertedDoc = await _docDbClient.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(Constants.CosmosDB.DbName, Constants.CosmosDB.InputCollectionName), documentToTest); - } + Container _inputContainer = _cosmosDbClient.GetContainer(Constants.CosmosDB.DbName, Constants.CosmosDB.InputCollectionName); - public async static Task CreateDocument(TestDocument testDocument) - { - Document insertedDoc = await _docDbClient.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(Constants.CosmosDB.DbName, Constants.CosmosDB.InputCollectionName), testDocument); + Document insertedDoc = await _inputContainer.CreateItemAsync(documentToTest, new PartitionKey(documentToTest.id)); } // keep public async static Task ReadDocument(string docId) { - var docUri = UriFactory.CreateDocumentUri(Constants.CosmosDB.DbName, Constants.CosmosDB.OutputCollectionName, docId); Document retrievedDocument = null; await Utilities.RetryAsync(async () => { try { - retrievedDocument = await _docDbClient.ReadDocumentAsync(docUri); + Container container = _cosmosDbClient.GetContainer(Constants.CosmosDB.DbName, Constants.CosmosDB.OutputCollectionName); + + retrievedDocument = await container.ReadItemAsync(docId, new PartitionKey(docId)); return true; } - catch (DocumentClientException ex) when (ex.Error.Code == "NotFound" || ex.Error.Code == "Not Found") + catch (CosmosException ex) when (ex.StatusCode == System.Net.HttpStatusCode.NotFound) { return false; } }, 120000, 4000); - return retrievedDocument.Id; + return retrievedDocument.id; } // keep public async static Task DeleteTestDocuments(string docId) { - var inputDocUri = UriFactory.CreateDocumentUri(Constants.CosmosDB.DbName, Constants.CosmosDB.InputCollectionName, docId); - await DeleteDocument(inputDocUri); - var outputDocUri = UriFactory.CreateDocumentUri(Constants.CosmosDB.DbName, Constants.CosmosDB.OutputCollectionName, docId); - await DeleteDocument(outputDocUri); + await DeleteDocument(Constants.CosmosDB.DbName, Constants.CosmosDB.InputCollectionName, docId); + await DeleteDocument(Constants.CosmosDB.DbName, Constants.CosmosDB.OutputCollectionName, docId); } - private async static Task DeleteDocument(Uri docUri) + private async static Task DeleteDocument(string dbName, string collectionName, string docId) { try { - await _docDbClient.DeleteDocumentAsync(docUri); + Container container = _cosmosDbClient.GetContainer(dbName, collectionName); + await container.DeleteItemAsync(docId, new PartitionKey(docId)); } catch (Exception) { @@ -89,26 +83,28 @@ private async static Task DeleteDocument(Uri docUri) // keep public async static Task CreateDocumentCollections() { - Database db = await _docDbClient.CreateDatabaseIfNotExistsAsync(new Database { Id = Constants.CosmosDB.DbName }); - Uri dbUri = UriFactory.CreateDatabaseUri(db.Id); - - await CreateCollection(dbUri, Constants.CosmosDB.InputCollectionName); - await CreateCollection(dbUri, Constants.CosmosDB.OutputCollectionName); - await CreateCollection(dbUri, Constants.CosmosDB.LeaseCollectionName); + Database db = await _cosmosDbClient.CreateDatabaseIfNotExistsAsync(Constants.CosmosDB.DbName); + await CreateCollection(Constants.CosmosDB.DbName, Constants.CosmosDB.InputCollectionName, "/id"); + await CreateCollection(Constants.CosmosDB.DbName, Constants.CosmosDB.OutputCollectionName, "/id"); + // While using extensions v2-3, the leases may not have a partition key, but the new SDK requires + // one to manually create a collection. This comment may be removed and this line uncommented when + // extension bundles for tests are updated. + //await CreateCollection(Constants.CosmosDB.DbName, Constants.CosmosDB.LeaseCollectionName, "/id"); } public async static Task DeleteDocumentCollections() { - await DeleteCollection(inputCollectionsUri); - await DeleteCollection(outputCollectionsUri); - await DeleteCollection(leasesCollectionsUri); + await DeleteCollection(Constants.CosmosDB.DbName, Constants.CosmosDB.InputCollectionName); + await DeleteCollection(Constants.CosmosDB.DbName, Constants.CosmosDB.OutputCollectionName); + await DeleteCollection(Constants.CosmosDB.DbName, Constants.CosmosDB.LeaseCollectionName); } - private async static Task DeleteCollection(Uri collectionUri) + private async static Task DeleteCollection(string dbName, string collectionName) { try { - await _docDbClient.DeleteDocumentCollectionAsync(collectionUri); + Database database = _cosmosDbClient.GetDatabase(dbName); + await database.GetContainer(collectionName).DeleteContainerAsync(); } catch (Exception) { @@ -116,14 +112,26 @@ private async static Task DeleteCollection(Uri collectionUri) } } - private async static Task CreateCollection(Uri dbUri, string collectioName) + private async static Task CreateCollection(string dbName, string collectionName, string partitionKey) { - DocumentCollection collection = new DocumentCollection() { Id = collectioName }; - await _docDbClient.CreateDocumentCollectionIfNotExistsAsync(dbUri, collection, - new RequestOptions() + Database database = _cosmosDbClient.GetDatabase(dbName); + IndexingPolicy indexingPolicy = new IndexingPolicy + { + IndexingMode = IndexingMode.Consistent, + Automatic = true, + IncludedPaths = { - OfferThroughput = 400 - }); + new IncludedPath + { + Path = "/*" + } + } + }; + var containerProperties = new ContainerProperties(collectionName, partitionKey) + { + IndexingPolicy = indexingPolicy + }; + await database.CreateContainerIfNotExistsAsync(containerProperties, 400); } } } diff --git a/test/E2E/Azure.Functions.PowerShellWorker.E2E/Azure.Functions.PowerShellWorker.E2E/Helpers/EventHubsHelpers.cs b/test/E2E/Azure.Functions.PowerShellWorker.E2E/Azure.Functions.PowerShellWorker.E2E/Helpers/EventHubsHelpers.cs index 0eacde7d..ba70e63d 100644 --- a/test/E2E/Azure.Functions.PowerShellWorker.E2E/Azure.Functions.PowerShellWorker.E2E/Helpers/EventHubsHelpers.cs +++ b/test/E2E/Azure.Functions.PowerShellWorker.E2E/Azure.Functions.PowerShellWorker.E2E/Helpers/EventHubsHelpers.cs @@ -1,7 +1,8 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT License. See License.txt in the project root for license information. -using Microsoft.Azure.EventHubs; +using Azure.Messaging.EventHubs; +using Azure.Messaging.EventHubs.Producer; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System.Collections.Generic; @@ -29,13 +30,11 @@ public static async Task SendJSONMessagesAsync(string eventId, string eventHubNa events.Add(evt); } - EventHubsConnectionStringBuilder builder = new EventHubsConnectionStringBuilder(Constants.EventHubs.EventHubsConnectionStringSetting); - builder.EntityPath = eventHubName; - EventHubClient eventHubClient = EventHubClient.CreateFromConnectionString(builder.ToString()); + EventHubProducerClient eventHubClient = new EventHubProducerClient(Constants.EventHubs.EventHubsConnectionStringSetting, eventHubName); await eventHubClient.SendAsync(events); } - public static async Task SendMessagesAsync(string eventId, string evenHubName) + public static async Task SendMessagesAsync(string eventId, string eventHubName) { // write 3 events List events = new List(); @@ -48,9 +47,7 @@ public static async Task SendMessagesAsync(string eventId, string evenHubName) events.Add(evt); } - EventHubsConnectionStringBuilder builder = new EventHubsConnectionStringBuilder(Constants.EventHubs.EventHubsConnectionStringSetting); - builder.EntityPath = evenHubName; - EventHubClient eventHubClient = EventHubClient.CreateFromConnectionString(builder.ToString()); + EventHubProducerClient eventHubClient = new EventHubProducerClient(Constants.EventHubs.EventHubsConnectionStringSetting, eventHubName); await eventHubClient.SendAsync(events); } } diff --git a/test/E2E/TestFunctionApp/CosmosDBTriggerAndOutput/function.json b/test/E2E/TestFunctionApp/CosmosDBTriggerAndOutput/function.json index de9191f2..8304d215 100644 --- a/test/E2E/TestFunctionApp/CosmosDBTriggerAndOutput/function.json +++ b/test/E2E/TestFunctionApp/CosmosDBTriggerAndOutput/function.json @@ -7,7 +7,7 @@ "leaseCollectionName": "leases", "connectionStringSetting": "AzureWebJobsCosmosDBConnectionString", "databaseName": "ItemDb", - "collectionName": "ItemCollectionIn", + "collectionName": "PartitionedItemCollectionIn", "createLeaseCollectionIfNotExists": true }, { @@ -17,7 +17,7 @@ "leaseCollectionName": "leases", "connectionStringSetting": "AzureWebJobsCosmosDBConnectionString", "databaseName": "ItemDb", - "collectionName": "ItemCollectionOut" + "collectionName": "PartitionedItemCollectionOut" } ] }