Skip to content

Commit

Permalink
Add more blob e2e binding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
liliankasem committed Feb 16, 2023
1 parent f7ec69a commit 3aec7d9
Show file tree
Hide file tree
Showing 8 changed files with 441 additions and 130 deletions.
122 changes: 122 additions & 0 deletions test/E2ETests/E2EApps/E2EApp/Blob/BlobInputBindingFunctions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// 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.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Azure.Storage.Blobs;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;

namespace Microsoft.Azure.Functions.Worker.E2EApp.Blob
{
public class BlobInputBindingFunctions
{
private readonly ILogger<BlobInputBindingFunctions> _logger;

public BlobInputBindingFunctions(ILogger<BlobInputBindingFunctions> logger)
{
_logger = logger;
}

[Function(nameof(BlobInputClientTest))]
public async Task<HttpResponseData> BlobInputClientTest(
[HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req,
[BlobInput("test-input-dotnet-isolated/testFile")] BlobClient client)
{
var response = req.CreateResponse(HttpStatusCode.OK);
var downloadResult = await client.DownloadContentAsync();
await response.Body.WriteAsync(downloadResult.Value.Content);
return response;
}

[Function(nameof(BlobInputStreamTest))]
public async Task<HttpResponseData> BlobInputStreamTest(
[HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req,
[BlobInput("test-input-dotnet-isolated/testFile")] Stream stream)
{
var response = req.CreateResponse(HttpStatusCode.OK);
using var blobStreamReader = new StreamReader(stream);
await response.WriteStringAsync(blobStreamReader.ReadToEnd());
return response;
}

[Function(nameof(BlobInputByteTest))]
public async Task<HttpResponseData> BlobInputByteTest(
[HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req,
[BlobInput("test-input-dotnet-isolated/testFile")] Byte[] data)
{
var response = req.CreateResponse(HttpStatusCode.OK);
await response.WriteStringAsync(Encoding.Default.GetString(data));
return response;
}

[Function(nameof(BlobInputStringTest))]
public async Task<HttpResponseData> BlobInputStringTest(
[HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req,
[BlobInput("test-input-dotnet-isolated/testFile")] string data)
{
var response = req.CreateResponse(HttpStatusCode.OK);
await response.WriteStringAsync(data);
return response;
}

[Function(nameof(BlobInputPocoTest))]
public async Task<HttpResponseData> BlobInputPocoTest(
[HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req,
[BlobInput("test-input-dotnet-isolated/testFile")] Book data)
{
var response = req.CreateResponse(HttpStatusCode.OK);
await response.WriteStringAsync(data.Name);
return response;
}

[Function(nameof(BlobInputCollectionTest))]
public async Task<HttpResponseData> BlobInputCollectionTest(
[HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req,
[BlobInput("test-input-dotnet-isolated", IsBatched = true)] IEnumerable<BlobClient> blobs)
{
List<string> blobList = new();

foreach (BlobClient blob in blobs)
{
_logger.LogInformation("Blob name: {blobName}, Container name: {containerName}", blob.Name, blob.BlobContainerName);
blobList.Add(blob.Name);
}

var response = req.CreateResponse(HttpStatusCode.OK);
await response.WriteStringAsync(blobList.ToString());
return response;
}

[Function(nameof(BlobInputStringArrayTest))]
public async Task<HttpResponseData> BlobInputStringArrayTest(
[HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req,
[BlobInput("test-input-dotnet-isolated", IsBatched = true)] string[] blobContent)
{
var response = req.CreateResponse(HttpStatusCode.OK);
await response.WriteStringAsync(blobContent.ToString());
return response;
}

[Function(nameof(BlobInputPocoArrayTest))]
public async Task<HttpResponseData> BlobInputPocoArrayTest(
[HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req,
[BlobInput("test-input-dotnet-isolated", IsBatched = true)] Book[] books)
{
List<string> bookNames = new();

foreach (var item in books)
{
bookNames.Add(item.Name);
}

var response = req.CreateResponse(HttpStatusCode.OK);
await response.WriteStringAsync(bookNames.ToString());
return response;
}
}
}
57 changes: 0 additions & 57 deletions test/E2ETests/E2EApps/E2EApp/Blob/BlobTestFunctions.cs

This file was deleted.

94 changes: 94 additions & 0 deletions test/E2ETests/E2EApps/E2EApp/Blob/BlobTriggerBindingFunctions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System.IO;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Azure.Storage.Blobs;
using Microsoft.Extensions.Logging;

namespace Microsoft.Azure.Functions.Worker.E2EApp.Blob
{
public class BlobTriggerBindingFunctions
{
private readonly ILogger<BlobTriggerBindingFunctions> _logger;

public BlobTriggerBindingFunctions(ILogger<BlobTriggerBindingFunctions> logger)
{
_logger = logger;
}

[Function(nameof(BlobTriggerToBlobTest))]
[BlobOutput("test-output-dotnet-isolated/{name}")]
public byte[] BlobTriggerToBlobTest(
[BlobTrigger("test-trigger-dotnet-isolated/{name}")] byte[] triggerBlob, string name,
[BlobInput("test-input-dotnet-isolated/{name}")] byte[] inputBlob,
FunctionContext context)
{
_logger.LogInformation("Trigger:\n Name: " + name + "\n Size: " + triggerBlob.Length + " Bytes");
_logger.LogInformation("Input:\n Name: " + name + "\n Size: " + inputBlob.Length + " Bytes");
return inputBlob;
}

[Function(nameof(BlobTriggerPocoTest))]
[BlobOutput("test-output-poco-dotnet-isolated/{name}")]
public TestBlobData BlobTriggerPocoTest(
[BlobTrigger("test-trigger-poco-dotnet-isolated/{name}")] TestBlobData triggerBlob, string name,
FunctionContext context)
{
_logger.LogInformation(".NET Blob trigger function processed a blob.\n Name: " + name + "\n Content: " + triggerBlob.BlobText);
return triggerBlob;
}

[Function(nameof(BlobTriggerStringTest))]
[BlobOutput("test-output-string-dotnet-isolated/{name}")]
public string BlobTriggerStringTest(
[BlobTrigger("test-trigger-string-dotnet-isolated/{name}")] string triggerBlobText, string name,
FunctionContext context)
{
_logger.LogInformation(".NET Blob trigger function processed a blob.\n Name: " + name + "\n Content: " + triggerBlobText);
return triggerBlobText;
}

[Function(nameof(BlobTriggerStreamTest))]
[BlobOutput("test-output-stream-dotnet-isolated/{name}")]
public async Task<Stream> BlobTriggerStreamTest(
[BlobTrigger("test-trigger-stream-dotnet-isolated/{name}")] Stream stream, string name)
{
using var blobStreamReader = new StreamReader(stream);
var content = await blobStreamReader.ReadToEndAsync();
_logger.LogInformation(".NET Blob trigger function processed a blob.\n Name: " + name + "\n Content: " + content);
return stream;

}

[Function(nameof(BlobTriggerBlobClientTest))]
[BlobOutput("test-output-blobclient-dotnet-isolated/{name}")]
public async Task<string> BlobTriggerBlobClientTest(
[BlobTrigger("test-trigger-blobclient-dotnet-isolated/{name}")] BlobClient client, string name)
{
var downloadResult = await client.DownloadContentAsync();
var content = downloadResult.Value.Content.ToString();
_logger.LogInformation(".NET Blob trigger function processed a blob.\n Name: " + name + "\n Content: " + content);
return content;
}

[Function(nameof(BlobTriggerBlobContainerClientTest))]
[BlobOutput("test-output-containerclient-dotnet-isolated/{name}")]
public async Task<string> BlobTriggerBlobContainerClientTest(
[BlobTrigger("test-trigger-containerclient-dotnet-isolated/{name}")] BlobContainerClient client, string name)
{
var blobClient = client.GetBlobClient(name);
var downloadResult = await blobClient.DownloadContentAsync();
var content = downloadResult.Value.Content.ToString();
_logger.LogInformation(".NET Blob trigger function processed a blob.\n Name: " + name + "\n Content: " + content);
return content;
}

public class TestBlobData
{
[JsonPropertyName("text")]
public string BlobText { get; set; }
}
}
}
11 changes: 11 additions & 0 deletions test/E2ETests/E2EApps/E2EApp/Blob/Book.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

namespace Microsoft.Azure.Functions.Worker.E2EApp.Blob
{
public class Book
{
public string Id { get; set; }
public string Name { get; set; }
}
}
18 changes: 11 additions & 7 deletions test/E2ETests/E2ETests/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,19 @@ public static class Queue
//Blob tests
public static class Blob
{
public const string TriggerInputBindingContainer = "test-triggerinput-dotnet-isolated";
public const string TriggerInputBindingContainer = "test-trigger-dotnet-isolated";
public const string InputBindingContainer = "test-input-dotnet-isolated";
public const string OutputBindingContainer = "test-output-dotnet-isolated";

public const string TriggerPocoContainer = "test-triggerinputpoco-dotnet-isolated";
public const string OutputPocoContainer = "test-outputpoco-dotnet-isolated";

public const string TriggerStringContainer = "test-triggerinputstring-dotnet-isolated";
public const string OutputStringContainer = "test-outputstring-dotnet-isolated";
public const string TriggerPocoContainer = "test-trigger-poco-dotnet-isolated";
public const string OutputPocoContainer = "test-output-poco-dotnet-isolated";
public const string TriggerStringContainer = "test-trigger-string-dotnet-isolated";
public const string OutputStringContainer = "test-output-string-dotnet-isolated";
public const string TriggerStreamContainer = "test-trigger-stream-dotnet-isolated";
public const string OutputStreamContainer = "test-output-stream-dotnet-isolated";
public const string TriggerBlobClientContainer = "test-trigger-blobclient-dotnet-isolated";
public const string OutputBlobClientContainer = "test-output-blobclient-dotnet-isolated";
public const string TriggerBlobContainerClientContainer = "test-trigger-containerclient-dotnet-isolated";
public const string OutputBlobContainerClientContainer = "test-output-containerclient-dotnet-isolated";
}

// CosmosDB tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using Xunit;
using Xunit.Abstractions;

namespace Microsoft.Azure.Functions.Tests.E2ETests
namespace Microsoft.Azure.Functions.Tests.E2ETests.Cosmos
{
[Collection(Constants.FunctionAppCollectionName)]
public class CosmosDBEndToEndTests : IDisposable
Expand All @@ -26,7 +26,7 @@ public async Task CosmosDBTriggerAndOutput_Succeeds()
string expectedDocId = Guid.NewGuid().ToString();
try
{
//Trigger
//Trigger
await CosmosDBHelpers.CreateDocument(expectedDocId);

//Read
Expand Down
Loading

0 comments on commit 3aec7d9

Please sign in to comment.