Skip to content

[Please ignore] CosmosDb integration test update. #10857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 14 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.Storage.Queue;
using Microsoft.Azure.Cosmos;
using Microsoft.Azure.WebJobs.Script.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json.Linq;
using Xunit;

namespace Microsoft.Azure.WebJobs.Script.Tests.CosmosDB
Expand All @@ -29,19 +28,15 @@ protected async Task CosmosDBTriggerToBlobTest()
// Waiting for the Processor to acquire leases
await Task.Delay(10000);

await Fixture.InitializeDocumentClient();
Fixture.InitializeCosmosClient();

bool collectionsCreated = await Fixture.CreateDocumentCollections();
var resultBlob = Fixture.TestOutputContainer.GetBlockBlobReference("cosmosdbtriggere2e-completed");
var resultBlob = Fixture.TestOutputContainer.GetBlobClient("cosmosdbtriggere2e-completed");
await resultBlob.DeleteIfExistsAsync();

string id = Guid.NewGuid().ToString();

Document documentToTest = new Document()
{
Id = id
};

await Fixture.DocumentClient.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri("ItemDb", "ItemCollection"), documentToTest);
var container = Fixture.CosmosClient.GetContainer("ItemDb", "ItemCollection");
await container.CreateItemAsync(new { id });

// now wait for function to be invoked
string result = await TestHelpers.WaitForBlobAndGetStringAsync(resultBlob,
Expand All @@ -63,21 +58,21 @@ protected async Task CosmosDBTest()
string id = Guid.NewGuid().ToString();

await Fixture.Host.BeginFunctionAsync("CosmosDBOut", id);

ItemResponse<JObject> itemResponse = await WaitForDocumentAsync(id);

Document doc = await WaitForDocumentAsync(id);

Assert.Equal(doc.Id, id);
Assert.Equal(id, itemResponse.Resource["id"]?.ToString());

// Now add that Id to a Queue, in an object to test binding
var queue = await Fixture.GetNewQueue("documentdb-input");
string messageContent = string.Format("{{ \"documentId\": \"{0}\" }}", id);
await queue.AddMessageAsync(new CloudQueueMessage(messageContent));
await queue.SendMessageAsync(messageContent);

// And wait for the text to be updated
Document updatedDoc = await WaitForDocumentAsync(id, "This was updated!");
ItemResponse<JObject> updatedItemResponse = await WaitForDocumentAsync(id, "This was updated!");

Assert.Equal(updatedDoc.Id, doc.Id);
Assert.NotEqual(doc.ETag, updatedDoc.ETag);
Assert.Equal(id, updatedItemResponse.Resource["id"]?.ToString());
Assert.NotEqual(itemResponse.ETag, updatedItemResponse.ETag);
}
}

Expand All @@ -87,8 +82,8 @@ protected CosmosDBTestFixture(string rootPath, string testId, string language) :
base(rootPath, testId, language)
{
}

public DocumentClient DocumentClient { get; private set; }
public CosmosClient CosmosClient { get; private set; }

protected override ExtensionPackageReference[] GetExtensionsToInstall()
{
Expand All @@ -115,59 +110,45 @@ public override void ConfigureScriptHost(IWebJobsBuilder webJobsBuilder)
});
}

public async Task InitializeDocumentClient()
public void InitializeCosmosClient()
{
if (DocumentClient == null)
if (CosmosClient == null)
{
var builder = new System.Data.Common.DbConnectionStringBuilder
{
ConnectionString = TestHelpers.GetTestConfiguration().GetConnectionString("CosmosDB")
};

var serviceUri = new Uri(builder["AccountEndpoint"].ToString());

DocumentClient = new DocumentClient(serviceUri, builder["AccountKey"].ToString());
await DocumentClient.OpenAsync();
var connectionString = TestHelpers.GetTestConfiguration().GetConnectionString("CosmosDB");
CosmosClient = new CosmosClient(connectionString);
}
}

public async Task<bool> CreateDocumentCollections()
{
bool willCreateCollection = false;
Database db = new Database() { Id = "ItemDb" };
await DocumentClient.CreateDatabaseIfNotExistsAsync(db);
Uri dbUri = UriFactory.CreateDatabaseUri(db.Id);

DocumentCollection collection = new DocumentCollection() { Id = "ItemCollection" };
willCreateCollection = !DocumentClient.CreateDocumentCollectionQuery(dbUri).Where(x => x.Id == collection.Id).ToList().Any();
await DocumentClient.CreateDocumentCollectionIfNotExistsAsync(dbUri, collection,
new RequestOptions()
{
OfferThroughput = 400
});
Database database = await CosmosClient.CreateDatabaseIfNotExistsAsync("ItemDb");

Documents.DocumentCollection leasesCollection = new Documents.DocumentCollection() { Id = "leases" };
await DocumentClient.CreateDocumentCollectionIfNotExistsAsync(dbUri, leasesCollection,
new RequestOptions()
{
OfferThroughput = 400
});
ContainerProperties itemCollectionProperties = new ContainerProperties("ItemCollection", "/_partitionKey");
ContainerResponse itemCollectionResponse = await database.CreateContainerIfNotExistsAsync(
itemCollectionProperties,
throughput: 400);
willCreateCollection = itemCollectionResponse.StatusCode == System.Net.HttpStatusCode.Created;

ContainerProperties leasesCollectionProperties = new ContainerProperties("leases", "/_partitionKey");
await database.CreateContainerIfNotExistsAsync(
leasesCollectionProperties,
throughput: 400);

return willCreateCollection;
}

public async Task DeleteDocumentCollections()
{
Uri collectionsUri = UriFactory.CreateDocumentCollectionUri("ItemDb", "ItemCollection");
Uri leasesCollectionsUri = UriFactory.CreateDocumentCollectionUri("ItemDb", "leases");
await DocumentClient.DeleteDocumentCollectionAsync(collectionsUri);
await DocumentClient.DeleteDocumentCollectionAsync(leasesCollectionsUri);
Database database = CosmosClient.GetDatabase("ItemDb");
await database.GetContainer("ItemCollection").DeleteContainerAsync();
await database.GetContainer("leases").DeleteContainerAsync();
}

public override async Task DisposeAsync()
{
await base.DisposeAsync();
DocumentClient?.Dispose();
CosmosClient?.Dispose();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public async Task EventHub()

// Second, there's an EventHub trigger listener on the events which will write a blob.
// Once the blob is written, we know both sender & listener are working.
var resultBlob = _fixture.TestOutputContainer.GetBlockBlobReference(testData);
var resultBlob = _fixture.TestOutputContainer.GetBlobClient(testData);
string result = await TestHelpers.WaitForBlobAndGetStringAsync(resultBlob,
userMessageCallback: _fixture.Host.GetLog);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
// 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.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Azure;
using Azure.Identity;
using Azure.Security.KeyVault.Secrets;
using Microsoft.Azure.Storage;
using Microsoft.Azure.Storage.Blob;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using Microsoft.Azure.WebJobs.Host.Storage;
using Microsoft.Azure.WebJobs.Script.WebHost;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.WebJobs.Script.Tests;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using WebJobs.Script.Tests;
using Xunit;
using Xunit.Abstractions;
Expand Down Expand Up @@ -557,7 +557,7 @@ public Fixture()

public Uri BlobSasConnectionUri { get; private set; }

public CloudBlobContainer BlobContainer { get; private set; }
public BlobContainerClient BlobContainer { get; private set; }

public SecretClient SecretClient { get; private set; }

Expand All @@ -581,11 +581,12 @@ public async Task TestInitialize(SecretsRepositoryType repositoryType, string se
if (RepositoryType == SecretsRepositoryType.BlobStorageSas)
{
BlobSasConnectionUri = await TestHelpers.CreateBlobContainerSas(BlobConnectionString, "azure-webjobs-secrets-sas");
BlobContainer = new CloudBlobContainer(BlobSasConnectionUri);
BlobContainer = new BlobContainerClient(BlobSasConnectionUri);
}
else
{
BlobContainer = CloudStorageAccount.Parse(BlobConnectionString).CreateCloudBlobClient().GetContainerReference("azure-webjobs-secrets");
BlobServiceClient blobServiceClient = new BlobServiceClient(BlobConnectionString);
BlobContainer = blobServiceClient.GetBlobContainerClient("azure-webjobs-secrets");
}

await ClearAllBlobSecrets();
Expand Down Expand Up @@ -667,9 +668,9 @@ private void WriteSecretsToFile(string functionNameOrHost, string fileText)
private async Task WriteSecretsBlobAndUpdateSentinelFile(string functionNameOrHost, string fileText, bool createSentinelFile = true)
{
string blobPath = RelativeBlobPath(functionNameOrHost);
CloudBlockBlob secretBlob = BlobContainer.GetBlockBlobReference(blobPath);
BlobClient secretBlob = BlobContainer.GetBlobClient(blobPath);

using (StreamWriter writer = new StreamWriter(await secretBlob.OpenWriteAsync()))
using (StreamWriter writer = new StreamWriter(await secretBlob.OpenWriteAsync(true)))
{
writer.Write(fileText);
}
Expand Down Expand Up @@ -716,9 +717,13 @@ private async Task<ScriptSecrets> GetSecretBlobText(string functionNameOrHost, S
{
string blobText = null;
string blobPath = RelativeBlobPath(functionNameOrHost);
if (await BlobContainer.GetBlockBlobReference(blobPath).ExistsAsync())
if (await BlobContainer.GetBlobClient(blobPath).ExistsAsync())
{
blobText = await BlobContainer.GetBlockBlobReference(blobPath).DownloadTextAsync();
BlobDownloadInfo download = await BlobContainer.GetBlobClient(blobPath).DownloadAsync();
using (var reader = new StreamReader(download.Content))
{
blobText = await reader.ReadToEndAsync();
}
}
return ScriptSecretSerializer.DeserializeSecrets(type, blobText);
}
Expand Down Expand Up @@ -793,11 +798,13 @@ private async Task ClearAllBlobSecrets()
await BlobContainer.CreateIfNotExistsAsync();
}

var blobs = await BlobContainer.ListBlobsSegmentedAsync(prefix: TestSiteName.ToLowerInvariant(), useFlatBlobListing: true,
blobListingDetails: BlobListingDetails.None, maxResults: 100, currentToken: null, options: null, operationContext: null);
foreach (IListBlobItem blob in blobs.Results)
await foreach (var blobItem in BlobContainer.GetBlobsByHierarchyAsync(prefix: TestSiteName.ToLowerInvariant(), delimiter: "/"))
{
await BlobContainer.GetBlockBlobReference(((CloudBlockBlob)blob).Name).DeleteIfExistsAsync();
// Get the BlobClient for each blob
BlobClient blobClient = BlobContainer.GetBlobClient(blobItem.Blob.Name);

// Delete the blob if it exists
await blobClient.DeleteIfExistsAsync();
}
}

Expand Down
Loading