Skip to content
This repository has been archived by the owner on Jul 19, 2024. It is now read-only.

Commit

Permalink
Fixing tests to properly setup configuration instead of name resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiocav committed Sep 18, 2018
1 parent e8c6aa0 commit 60628c6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 25 deletions.
3 changes: 2 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ pull_requests:
do_not_increment_build_number: true
branches:
only:
- v2.x
- dev
- master
image: Visual Studio 2017
install:
- ps: >-
Expand Down
26 changes: 17 additions & 9 deletions test/Extension.tests/Common/TestHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,45 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Azure.WebJobs.Host.Config;
using Microsoft.Extensions.Configuration;
using System.Collections.Generic;

namespace Microsoft.Azure.WebJobs.Extensions.EventGrid.Tests
{
internal static class TestHelpers
{
public static IHost NewHost<T>(EventGridExtensionConfigProvider ext = null, INameResolver nameResolver = null)
public static IHost NewHost<T>(EventGridExtensionConfigProvider ext = null, Dictionary<string, string> configuration = null)
{
IHost host = new HostBuilder()
var builder = new HostBuilder()
.ConfigureServices(services =>
{
services.AddSingleton(nameResolver ?? new DefaultNameResolver());
services.AddSingleton<ITypeLocator>(new FakeTypeLocator<T>());
if (ext != null)
{
services.AddSingleton<IExtensionConfigProvider>(ext);
}
services.AddSingleton<IExtensionConfigProvider>(new TestExtensionConfig());
})
.ConfigureWebJobs(builder =>
.ConfigureWebJobs(webJobsBuilder =>
{
builder.AddEventGrid();
builder.UseHostId(Guid.NewGuid().ToString("n"));
webJobsBuilder.AddEventGrid();
webJobsBuilder.UseHostId(Guid.NewGuid().ToString("n"));
})
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.AddProvider(new TestLoggerProvider());
})
.Build();
});

if (configuration != null)
{
builder.ConfigureAppConfiguration(b =>
{
b.AddInMemoryCollection(configuration);
});
}

return host;
return builder.Build();
}

public static JobHost GetJobHost(this IHost host)
Expand Down
37 changes: 22 additions & 15 deletions test/Extension.tests/JobhostEndToEnd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.Azure.WebJobs.Extensions.EventGrid.Tests.Common;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Azure.WebJobs.Host.Indexers;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Rest.Azure;
using Moq;
Expand Down Expand Up @@ -107,22 +108,26 @@ public async Task OutputBindingInvalidCredentialTests()
var host = TestHelpers.NewHost<OutputBindingParams>();
// appsetting is missing
var indexException = await Assert.ThrowsAsync<FunctionIndexingException>(() => host.StartAsync());
Assert.Equal($"Unable to resolve app setting for property '{nameof(EventGridAttribute)}.{nameof(EventGridAttribute.TopicEndpointUri)}'. Make sure the app setting exists and has a valid value.", indexException.InnerException.Message);
Assert.Equal($"Unable to resolve the value for property '{nameof(EventGridAttribute)}.{nameof(EventGridAttribute.TopicEndpointUri)}'. Make sure the setting exists and has a valid value.", indexException.InnerException.Message);

var nameResolverMock = new Mock<INameResolver>();
// invalid uri
nameResolverMock.Setup(x => x.Resolve("eventgridUri")).Returns("this could be anything...so lets try yolo");
nameResolverMock.Setup(x => x.Resolve("eventgridKey")).Returns("thisismagic");
var configuration = new Dictionary<string, string>
{
{ "eventGridUri" , "this could be anything...so lets try yolo" },
{ "eventgridKey" , "thisismagic" }
};

host = TestHelpers.NewHost<OutputBindingParams>(nameResolver: nameResolverMock.Object);
host = TestHelpers.NewHost<OutputBindingParams>(configuration: configuration);
indexException = await Assert.ThrowsAsync<FunctionIndexingException>(() => host.StartAsync());
Assert.Equal($"The '{nameof(EventGridAttribute.TopicEndpointUri)}' property must be a valid absolute Uri", indexException.InnerException.Message);

nameResolverMock.Setup(x => x.Resolve("eventgridUri")).Returns("https://pccode.westus2-1.eventgrid.azure.net/api/events");
// invalid sas token
nameResolverMock.Setup(x => x.Resolve("eventgridKey")).Returns("");

host = TestHelpers.NewHost<OutputBindingParams>(nameResolver: nameResolverMock.Object);
configuration = new Dictionary<string, string>
{
{ "eventGridUri" , "https://pccode.westus2-1.eventgrid.azure.net/api/events" },
// invalid sas token
{ "eventgridKey" , "" }
};

host = TestHelpers.NewHost<OutputBindingParams>(configuration: configuration);
indexException = await Assert.ThrowsAsync<FunctionIndexingException>(() => host.StartAsync());
Assert.Equal($"The '{nameof(EventGridAttribute.TopicKeySetting)}' property must be the name of an application setting containing the Topic Key", indexException.InnerException.Message);
}
Expand Down Expand Up @@ -161,11 +166,13 @@ public async Task OutputBindingParamsTests(string functionName, string expectedC
// use moq eventgridclient for test extension
var customExtension = new EventGridExtensionConfigProvider(customConverter, loggerFactory);

var nameResolverMock = new Mock<INameResolver>();
nameResolverMock.Setup(x => x.Resolve("eventgridUri")).Returns("https://pccode.westus2-1.eventgrid.azure.net/api/events");
nameResolverMock.Setup(x => x.Resolve("eventgridKey")).Returns("thisismagic");
var configuration = new Dictionary<string, string>
{
{ "eventGridUri" , "https://pccode.westus2-1.eventgrid.azure.net/api/events" },
{ "eventgridKey" , "thisismagic" }
};

var host = TestHelpers.NewHost<OutputBindingParams>(customExtension, nameResolverMock.Object);
var host = TestHelpers.NewHost<OutputBindingParams>(customExtension, configuration: configuration);

await host.GetJobHost().CallAsync($"OutputBindingParams.{functionName}");

Expand Down

0 comments on commit 60628c6

Please sign in to comment.