Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
liliankasem committed Feb 17, 2023
1 parent 3aec7d9 commit 799e0fe
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 54 deletions.
26 changes: 19 additions & 7 deletions test/E2ETests/E2EApps/E2EApp/Blob/BlobInputBindingFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,41 @@ public BlobInputBindingFunctions(ILogger<BlobInputBindingFunctions> logger)
[Function(nameof(BlobInputClientTest))]
public async Task<HttpResponseData> BlobInputClientTest(
[HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req,
[BlobInput("test-input-dotnet-isolated/testFile")] BlobClient client)
[BlobInput("test-input-dotnet-isolated/testFile.txt")] BlobClient client)
{
var response = req.CreateResponse(HttpStatusCode.OK);
var downloadResult = await client.DownloadContentAsync();
var response = req.CreateResponse(HttpStatusCode.OK);
await response.Body.WriteAsync(downloadResult.Value.Content);
return response;
}

[Function(nameof(BlobInputContainerClientTest))]
public async Task<HttpResponseData> BlobInputContainerClientTest(
[HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req,
[BlobInput("test-input-dotnet-isolated/testFile.txt")] BlobContainerClient client)
{
var blobClient = client.GetBlobClient("testFile.txt");
var downloadResult = await blobClient.DownloadContentAsync();
var response = req.CreateResponse(HttpStatusCode.OK);
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)
[BlobInput("test-input-dotnet-isolated/testFile.txt")] Stream stream)
{
var response = req.CreateResponse(HttpStatusCode.OK);
using var blobStreamReader = new StreamReader(stream);
var response = req.CreateResponse(HttpStatusCode.OK);
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)
[BlobInput("test-input-dotnet-isolated/testFile.txt")] Byte[] data)
{
var response = req.CreateResponse(HttpStatusCode.OK);
await response.WriteStringAsync(Encoding.Default.GetString(data));
Expand All @@ -57,7 +69,7 @@ public async Task<HttpResponseData> BlobInputByteTest(
[Function(nameof(BlobInputStringTest))]
public async Task<HttpResponseData> BlobInputStringTest(
[HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req,
[BlobInput("test-input-dotnet-isolated/testFile")] string data)
[BlobInput("test-input-dotnet-isolated/testFile.txt")] string data)
{
var response = req.CreateResponse(HttpStatusCode.OK);
await response.WriteStringAsync(data);
Expand All @@ -67,7 +79,7 @@ public async Task<HttpResponseData> BlobInputStringTest(
[Function(nameof(BlobInputPocoTest))]
public async Task<HttpResponseData> BlobInputPocoTest(
[HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req,
[BlobInput("test-input-dotnet-isolated/testFile")] Book data)
[BlobInput("test-input-dotnet-isolated/testFile.txt")] Book data)
{
var response = req.CreateResponse(HttpStatusCode.OK);
await response.WriteStringAsync(data.Name);
Expand Down
34 changes: 15 additions & 19 deletions test/E2ETests/E2EApps/E2EApp/Blob/BlobTriggerBindingFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,38 +51,34 @@ public string BlobTriggerStringTest(
}

[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)
public async Task BlobTriggerStreamTest(
[BlobTrigger("test-trigger-stream-dotnet-isolated/{name}")] Stream stream, string name,
FunctionContext context)
{
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;

string content = await blobStreamReader.ReadToEndAsync();
_logger.LogInformation("StreamTriggerOutput: {c}", content);
}

[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)
public async Task BlobTriggerBlobClientTest(
[BlobTrigger("test-trigger-blobclient-dotnet-isolated/{name}")] BlobClient client, string name,
FunctionContext context)
{
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;
string content = downloadResult.Value.Content.ToString();
_logger.LogInformation("BlobClientTriggerOutput: {c}", 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)
public async Task BlobTriggerBlobContainerClientTest(
[BlobTrigger("test-trigger-containerclient-dotnet-isolated/{name}")] BlobContainerClient client, string name,
FunctionContext context)
{
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;
string content = downloadResult.Value.Content.ToString();
_logger.LogInformation("BlobContainerTriggerOutput: {c}", content);
}

public class TestBlobData
Expand Down
4 changes: 2 additions & 2 deletions test/E2ETests/E2EApps/E2EApp/E2EApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="2.2.0" />
<PackageReference Condition="$(TestBuild) == 'true'" Include="Microsoft.Azure.Functions.Worker" Version="1.10.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.7.0" />
<PackageReference Condition="$(TestBuild) == 'true'" Include="Microsoft.Azure.Functions.Worker" Version="1.12.1-preview1" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.9.0-preview1" />

<PackageReference Condition="$(FunctionsRuntimeVersion) != '3'" Include="System.Text.Json" Version="6.0.5" />
<PackageReference Condition="$(FunctionsRuntimeVersion) == '3'" Include="System.Text.Json" Version="5.0.2" />
Expand Down
6 changes: 6 additions & 0 deletions test/E2ETests/E2ETests/Helpers/StorageHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ public async static Task ClearBlobContainers()
await ClearBlobContainer(Constants.Blob.OutputPocoContainer);
await ClearBlobContainer(Constants.Blob.TriggerStringContainer);
await ClearBlobContainer(Constants.Blob.OutputStringContainer);
await ClearBlobContainer(Constants.Blob.TriggerStreamContainer);
await ClearBlobContainer(Constants.Blob.OutputStreamContainer);
await ClearBlobContainer(Constants.Blob.TriggerBlobClientContainer);
await ClearBlobContainer(Constants.Blob.OutputBlobClientContainer);
await ClearBlobContainer(Constants.Blob.TriggerBlobContainerClientContainer);
await ClearBlobContainer(Constants.Blob.OutputBlobContainerClientContainer);
}

public static Task UploadFileToContainer(string containerName, string fileName)
Expand Down
147 changes: 121 additions & 26 deletions test/E2ETests/E2ETests/Storage/BlobEndToEndTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text.Json;
Expand Down Expand Up @@ -40,82 +42,175 @@ public async Task BlobTriggerToBlob_Succeeds()
Assert.Equal("Hello World", result);
}

[Theory]
[InlineData(Constants.Blob.TriggerBlobContainerClientContainer, Constants.Blob.OutputBlobContainerClientContainer)]
[InlineData(Constants.Blob.TriggerBlobContainerClientContainer, Constants.Blob.OutputBlobContainerClientContainer)]
[InlineData(Constants.Blob.TriggerBlobClientContainer, Constants.Blob.OutputBlobClientContainer)]
[InlineData(Constants.Blob.TriggerStreamContainer, Constants.Blob.OutputStreamContainer)]
[InlineData(Constants.Blob.TriggerStringContainer, Constants.Blob.OutputStringContainer)]
[InlineData(Constants.Blob.TriggerPocoContainer, Constants.Blob.OutputPocoContainer)]
[InlineData()]
public async Task BlobTrigger_Succeeds(string triggerContainer, string outputContainer)
[Fact]
public async Task BlobTrigger_Poco_Succeeds()
{
string fileName = Guid.NewGuid().ToString();

//Cleanup
await StorageHelpers.ClearBlobContainers();

//Trigger
var json = JsonSerializer.Serialize(new { text = "Hello World" });
await StorageHelpers.UploadFileToContainer(Constants.Blob.TriggerPocoContainer, fileName, json);

//Verify
string result = await StorageHelpers.DownloadFileFromContainer(Constants.Blob.OutputPocoContainer, fileName);

Assert.Equal(json, result);
}

[Fact]
public async Task BlobTrigger_String_Succeeds()
{
string fileName = Guid.NewGuid().ToString();

//Cleanup
await StorageHelpers.ClearBlobContainers();

//Trigger
await StorageHelpers.UploadFileToContainer(Constants.Blob.TriggerStringContainer, fileName);

//Verify
string result = await StorageHelpers.DownloadFileFromContainer(Constants.Blob.OutputStringContainer, fileName);

Assert.Equal("Hello World", result);
}

[Fact]
public async Task BlobTrigger_Stream_Succeeds()
{
string key = "StreamTriggerOutput: ";
string fileName = Guid.NewGuid().ToString();

IEnumerable<string> logs = null;
await TestUtility.RetryAsync(() =>
{
logs = _fixture.TestLogs.CoreToolsLogs.Where(p => p.Contains(key));
// The "RunOnStartup" log should show, and then a true invocation.
return Task.FromResult(logs.Count() >= 2);
});

//Cleanup
await StorageHelpers.ClearBlobContainers();

//Trigger
await StorageHelpers.UploadFileToContainer(Constants.Blob.TriggerStreamContainer, fileName);

//Verify
var lastLog = logs.Last();
int subStringStart = lastLog.LastIndexOf(key) + key.Length;
var result = lastLog[subStringStart..];

Assert.Equal("Hello World", result);
}

[Fact]
public async Task BlobTrigger_BlobClient_Succeeds()
{
string key = "BlobClientTriggerOutput: ";
string fileName = Guid.NewGuid().ToString();

IEnumerable<string> logs = null;
await TestUtility.RetryAsync(() =>
{
logs = _fixture.TestLogs.CoreToolsLogs.Where(p => p.Contains(key));
// The "RunOnStartup" log should show, and then a true invocation.
return Task.FromResult(logs.Count() >= 2);
});

//Cleanup
await StorageHelpers.ClearBlobContainers();

//Trigger
await StorageHelpers.UploadFileToContainer(Constants.Blob.TriggerBlobClientContainer, fileName);

//Verify
var lastLog = logs.Last();
int subStringStart = lastLog.LastIndexOf(key) + key.Length;
var result = lastLog[subStringStart..];

Assert.Equal("Hello World", result);
}

[Fact]
public async Task BlobTrigger_BlobContainerClient_Succeeds()
{
string key = "BlobContainerTriggerOutput: ";
string fileName = Guid.NewGuid().ToString();

IEnumerable<string> logs = null;
await TestUtility.RetryAsync(() =>
{
logs = _fixture.TestLogs.CoreToolsLogs.Where(p => p.Contains(key));
// The "RunOnStartup" log should show, and then a true invocation.
return Task.FromResult(logs.Count() >= 2);
});

//Cleanup
await StorageHelpers.ClearBlobContainers();

//Trigger
await StorageHelpers.UploadFileToContainer(triggerContainer, fileName);
await StorageHelpers.UploadFileToContainer(Constants.Blob.TriggerBlobContainerClientContainer, fileName);

//Verify
string result = await StorageHelpers.DownloadFileFromContainer(outputContainer, fileName);
var lastLog = logs.Last();
int subStringStart = lastLog.LastIndexOf(key) + key.Length;
var result = lastLog[subStringStart..];

Assert.Equal("Hello World", result);
}

[Theory]
[InlineData("BlobInputClientTest")]
[InlineData("BlobInputContainerClientTest")]
[InlineData("BlobInputStreamTest")]
[InlineData("BlobInputByteTest")]
[InlineData("BlobInputStringTest")]
[InlineData("BlobInputBookArrayTest")]
[InlineData("BlobInputCollectionTest")]
[InlineData("BlobInputStringArrayTest")]
public async Task BlobInput_Succeeds(string functionName)
public async Task BlobInput_SingleCardinality_Succeeds(string functionName)
{
string fileName = "testFile";
string expectedMessage = "Hello World";
HttpStatusCode expectedStatusCode = HttpStatusCode.OK;

//Cleanup
await StorageHelpers.ClearBlobContainers();

//Setup
await StorageHelpers.UploadFileToContainer(Constants.Blob.InputBindingContainer, fileName, expectedMessage);
await StorageHelpers.UploadFileToContainer(Constants.Blob.InputBindingContainer, "testFile", expectedMessage);

//Trigger
HttpResponseMessage response = await HttpHelpers.InvokeHttpTrigger(functionName);
string actualMessage = await response.Content.ReadAsStringAsync();

//Verify
HttpStatusCode expectedStatusCode = HttpStatusCode.OK;

Assert.Equal(expectedStatusCode, response.StatusCode);
Assert.Contains(expectedMessage, actualMessage);
}

[Fact]
public async Task BlobInput_Poco_Succeeds()
{
string fileContent = $@"{{ ""id"": ""1"", ""name"": ""To Kill a Mockingbird""}}";
string expectedMessage = "To Kill a Mockingbird";
HttpStatusCode expectedStatusCode = HttpStatusCode.OK;

//Cleanup
await StorageHelpers.ClearBlobContainers();

//Setup
await StorageHelpers.UploadFileToContainer(Constants.Blob.InputBindingContainer, "testFile", fileContent);
var json = JsonSerializer.Serialize(new { id = "1", name = "To Kill a Mockingbird" });
await StorageHelpers.UploadFileToContainer(Constants.Blob.InputBindingContainer, "testFile", json);

//Trigger
HttpResponseMessage response = await HttpHelpers.InvokeHttpTrigger("BlobInputPocoTest");
string actualMessage = await response.Content.ReadAsStringAsync();

//Verify
string expectedMessage = "To Kill a Mockingbird";
HttpStatusCode expectedStatusCode = HttpStatusCode.OK;

Assert.Equal(expectedStatusCode, response.StatusCode);
Assert.Contains(expectedMessage, actualMessage);
}

[Fact]
[Fact(Skip = "Collection support released in host version 4.16+")]
public async Task BlobInput_BlobClientCollection_Succeeds()
{
string expectedMessage = "testFile1, testFile2, testFile3";
Expand All @@ -138,7 +233,7 @@ public async Task BlobInput_BlobClientCollection_Succeeds()
Assert.Contains(expectedMessage, actualMessage);
}

[Fact]
[Fact(Skip = "Collection support released in host version 4.16+")]
public async Task BlobInput_StringCollection_Succeeds()
{
string expectedMessage = "ABC, DEF, GHI";
Expand All @@ -161,7 +256,7 @@ public async Task BlobInput_StringCollection_Succeeds()
Assert.Contains(expectedMessage, actualMessage);
}

[Fact]
[Fact(Skip = "Collection support released in host version 4.16+")]
public async Task BlobInput_PocoCollection_Succeeds()
{
string book1 = $@"{{ ""id"": ""1"", ""name"": ""To Kill a Mockingbird""}}";
Expand Down

0 comments on commit 799e0fe

Please sign in to comment.