Feature/should not start if specified using service discovery but no provider registered (#584)

* #580 added failing test

* #580 added failing test for dynamic reroutes

* #580 added failing test for validator

* #580 validation tests passing

* #580 acceptance tests passing

* #580 got rid of the list in sdp factory

* +semver: breaking #580 service discovery provider returned by delegate must be the same as name in config, this is a breaking change because you have to specify consul now

* #580 removed use servide discovery property from file config, we dont need it, just use the service name as indicator the user wants to use service discovery for the given reroute
This commit is contained in:
Tom Pallister
2018-08-31 18:28:43 +01:00
committed by GitHub
parent 0a7d81038e
commit 55277cac45
26 changed files with 821 additions and 255 deletions

View File

@ -15,6 +15,79 @@ namespace Ocelot.AcceptanceTests
_steps = new Steps();
}
[Fact]
public void should_throw_exception_if_cannot_start_because_service_discovery_provider_specified_in_config_but_no_service_discovery_provider_registered_with_dynamic_re_routes()
{
var invalidConfig = new FileConfiguration
{
GlobalConfiguration = new FileGlobalConfiguration
{
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider
{
Host = "localhost",
Type = "consul",
Port = 8500
}
}
};
Exception exception = null;
_steps.GivenThereIsAConfiguration(invalidConfig);
try
{
_steps.GivenOcelotIsRunning();
}
catch (Exception ex)
{
exception = ex;
}
exception.ShouldNotBeNull();
exception.Message.ShouldBe("One or more errors occurred. (Unable to start Ocelot, errors are: Unable to start Ocelot, errors are: Unable to start Ocelot because either a ReRoute or GlobalConfiguration are using ServiceDiscoveryOptions but no ServiceDiscoveryFinderDelegate has been registered in dependency injection container. Are you missing a package like Ocelot.Provider.Consul and services.AddConsul() or Ocelot.Provider.Eureka and services.AddEureka()?)");
}
[Fact]
public void should_throw_exception_if_cannot_start_because_service_discovery_provider_specified_in_config_but_no_service_discovery_provider_registered()
{
var invalidConfig = new FileConfiguration
{
ReRoutes = new List<FileReRoute>
{
new FileReRoute
{
DownstreamPathTemplate = "/",
DownstreamScheme = "http",
UpstreamPathTemplate = "/laura",
UpstreamHttpMethod = new List<string> { "Get" },
ServiceName = "test"
}
},
GlobalConfiguration = new FileGlobalConfiguration
{
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider
{
Host = "localhost",
Type = "consul",
Port = 8500
}
}
};
Exception exception = null;
_steps.GivenThereIsAConfiguration(invalidConfig);
try
{
_steps.GivenOcelotIsRunning();
}
catch (Exception ex)
{
exception = ex;
}
exception.ShouldNotBeNull();
exception.Message.ShouldBe("One or more errors occurred. (Unable to start Ocelot, errors are: Unable to start Ocelot, errors are: Unable to start Ocelot because either a ReRoute or GlobalConfiguration are using ServiceDiscoveryOptions but no ServiceDiscoveryFinderDelegate has been registered in dependency injection container. Are you missing a package like Ocelot.Provider.Consul and services.AddConsul() or Ocelot.Provider.Eureka and services.AddEureka()?,Unable to start Ocelot, errors are: Unable to start Ocelot because either a ReRoute or GlobalConfiguration are using ServiceDiscoveryOptions but no ServiceDiscoveryFinderDelegate has been registered in dependency injection container. Are you missing a package like Ocelot.Provider.Consul and services.AddConsul() or Ocelot.Provider.Eureka and services.AddEureka()?)");
}
[Fact]
public void should_throw_exception_if_cannot_start_because_no_qos_delegate_registered_globally()
{

View File

@ -336,7 +336,6 @@ namespace Ocelot.AcceptanceTests
},
UpstreamPathTemplate = "/vacancy/",
UpstreamHttpMethod = new List<string> { "Options", "Put", "Get", "Post", "Delete" },
ServiceName = "botCore",
LoadBalancerOptions = new FileLoadBalancerOptions { Type = "LeastConnection" }
},
new FileReRoute
@ -353,7 +352,6 @@ namespace Ocelot.AcceptanceTests
},
UpstreamPathTemplate = "/vacancy/{vacancyId}",
UpstreamHttpMethod = new List<string> { "Options", "Put", "Get", "Post", "Delete" },
ServiceName = "botCore",
LoadBalancerOptions = new FileLoadBalancerOptions { Type = "LeastConnection" }
}
}
@ -828,7 +826,6 @@ namespace Ocelot.AcceptanceTests
},
UpstreamPathTemplate = "/vacancy/",
UpstreamHttpMethod = new List<string> { "Options", "Put", "Get", "Post", "Delete" },
ServiceName = "botCore",
LoadBalancerOptions = new FileLoadBalancerOptions { Type = "LeastConnection" }
},
new FileReRoute
@ -845,7 +842,6 @@ namespace Ocelot.AcceptanceTests
},
UpstreamPathTemplate = "/vacancy/{vacancyId}",
UpstreamHttpMethod = new List<string> { "Options", "Put", "Get", "Post", "Delete" },
ServiceName = "botCore",
LoadBalancerOptions = new FileLoadBalancerOptions { Type = "LeastConnection" }
}
}

View File

@ -33,7 +33,6 @@ namespace Ocelot.AcceptanceTests
DownstreamScheme = "http",
UpstreamPathTemplate = "/{everything}",
UpstreamHttpMethod = new List<string> { "Get" },
UseServiceDiscovery = true,
ServiceName = "OcelotServiceApplication/OcelotApplicationService"
}
},
@ -70,7 +69,6 @@ namespace Ocelot.AcceptanceTests
DownstreamScheme = "http",
UpstreamPathTemplate = "/EquipmentInterfaces",
UpstreamHttpMethod = new List<string> { "Get" },
UseServiceDiscovery = true,
ServiceName = "OcelotServiceApplication/OcelotApplicationService"
}
},
@ -107,7 +105,6 @@ namespace Ocelot.AcceptanceTests
DownstreamScheme = "http",
UpstreamPathTemplate = "/EquipmentInterfaces",
UpstreamHttpMethod = new List<string> { "Get" },
UseServiceDiscovery = true,
ServiceName = "OcelotServiceApplication/OcelotApplicationService"
}
},

View File

@ -17,6 +17,9 @@
using Microsoft.Extensions.DependencyInjection;
using Ocelot.Requester;
using Requester;
using Ocelot.ServiceDiscovery.Providers;
using Ocelot.Values;
using Ocelot.ServiceDiscovery;
public class FileConfigurationFluentValidatorTests
{
@ -33,6 +36,191 @@
_configurationValidator = new FileConfigurationFluentValidator(_authProvider.Object, provider);
}
[Fact]
public void configuration_is_valid_if_service_discovery_options_specified_and_has_service_fabric_as_option()
{
var configuration = new FileConfiguration
{
ReRoutes = new List<FileReRoute>
{
new FileReRoute
{
DownstreamPathTemplate = "/",
DownstreamScheme = "http",
UpstreamPathTemplate = "/laura",
UpstreamHttpMethod = new List<string> { "Get" },
ServiceName = "test"
}
},
GlobalConfiguration = new FileGlobalConfiguration
{
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider
{
Host = "localhost",
Type = "ServiceFabric",
Port = 8500
}
}
};
this.Given(x => x.GivenAConfiguration(configuration))
.When(x => x.WhenIValidateTheConfiguration())
.Then(x => x.ThenTheResultIsValid())
.BDDfy();
}
[Fact]
public void configuration_is_valid_if_service_discovery_options_specified_and_has_service_discovery_handler()
{
var configuration = new FileConfiguration
{
ReRoutes = new List<FileReRoute>
{
new FileReRoute
{
DownstreamPathTemplate = "/",
DownstreamScheme = "http",
UpstreamPathTemplate = "/laura",
UpstreamHttpMethod = new List<string> { "Get" },
ServiceName = "test"
}
},
GlobalConfiguration = new FileGlobalConfiguration
{
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider
{
Host = "localhost",
Type = "FakeServiceDiscoveryProvider",
Port = 8500
}
}
};
this.Given(x => x.GivenAConfiguration(configuration))
.And(x => x.GivenAServiceDiscoveryHandler())
.When(x => x.WhenIValidateTheConfiguration())
.Then(x => x.ThenTheResultIsValid())
.BDDfy();
}
[Fact]
public void configuration_is_valid_if_service_discovery_options_specified_dynamically_and_has_service_discovery_handler()
{
var configuration = new FileConfiguration
{
GlobalConfiguration = new FileGlobalConfiguration
{
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider
{
Host = "localhost",
Type = "FakeServiceDiscoveryProvider",
Port = 8500
}
}
};
this.Given(x => x.GivenAConfiguration(configuration))
.And(x => x.GivenAServiceDiscoveryHandler())
.When(x => x.WhenIValidateTheConfiguration())
.Then(x => x.ThenTheResultIsValid())
.BDDfy();
}
[Fact]
public void configuration_is_invalid_if_service_discovery_options_specified_but_no_service_discovery_handler()
{
var configuration = new FileConfiguration
{
ReRoutes = new List<FileReRoute>
{
new FileReRoute
{
DownstreamPathTemplate = "/",
DownstreamScheme = "http",
UpstreamPathTemplate = "/laura",
UpstreamHttpMethod = new List<string> { "Get" },
ServiceName = "test"
}
},
GlobalConfiguration = new FileGlobalConfiguration
{
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider
{
Host = "localhost",
Type = "FakeServiceDiscoveryProvider",
Port = 8500
}
}
};
this.Given(x => x.GivenAConfiguration(configuration))
.When(x => x.WhenIValidateTheConfiguration())
.Then(x => x.ThenTheResultIsNotValid())
.And(x => x.ThenTheErrorIs<FileValidationFailedError>())
.And(x => x.ThenTheErrorMessageAtPositionIs(0, "Unable to start Ocelot, errors are: Unable to start Ocelot because either a ReRoute or GlobalConfiguration are using ServiceDiscoveryOptions but no ServiceDiscoveryFinderDelegate has been registered in dependency injection container. Are you missing a package like Ocelot.Provider.Consul and services.AddConsul() or Ocelot.Provider.Eureka and services.AddEureka()?"))
.BDDfy();
}
[Fact]
public void configuration_is_invalid_if_service_discovery_options_specified_dynamically_but_service_discovery_handler()
{
var configuration = new FileConfiguration
{
GlobalConfiguration = new FileGlobalConfiguration
{
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider
{
Host = "localhost",
Type = "FakeServiceDiscoveryProvider",
Port = 8500
}
}
};
this.Given(x => x.GivenAConfiguration(configuration))
.When(x => x.WhenIValidateTheConfiguration())
.Then(x => x.ThenTheResultIsNotValid())
.And(x => x.ThenTheErrorIs<FileValidationFailedError>())
.And(x => x.ThenTheErrorMessageAtPositionIs(0, "Unable to start Ocelot, errors are: Unable to start Ocelot because either a ReRoute or GlobalConfiguration are using ServiceDiscoveryOptions but no ServiceDiscoveryFinderDelegate has been registered in dependency injection container. Are you missing a package like Ocelot.Provider.Consul and services.AddConsul() or Ocelot.Provider.Eureka and services.AddEureka()?"))
.BDDfy();
}
[Fact]
public void configuration_is_invalid_if_service_discovery_options_specified_but_no_service_discovery_handler_with_matching_name()
{
var configuration = new FileConfiguration
{
ReRoutes = new List<FileReRoute>
{
new FileReRoute
{
DownstreamPathTemplate = "/",
DownstreamScheme = "http",
UpstreamPathTemplate = "/laura",
UpstreamHttpMethod = new List<string> { "Get" },
ServiceName = "test"
}
},
GlobalConfiguration = new FileGlobalConfiguration
{
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider
{
Host = "localhost",
Type = "consul",
Port = 8500
}
}
};
this.Given(x => x.GivenAConfiguration(configuration))
.When(x => x.WhenIValidateTheConfiguration())
.And(x => x.GivenAServiceDiscoveryHandler())
.Then(x => x.ThenTheResultIsNotValid())
.And(x => x.ThenTheErrorIs<FileValidationFailedError>())
.And(x => x.ThenTheErrorMessageAtPositionIs(0, "Unable to start Ocelot, errors are: Unable to start Ocelot because either a ReRoute or GlobalConfiguration are using ServiceDiscoveryOptions but no ServiceDiscoveryFinderDelegate has been registered in dependency injection container. Are you missing a package like Ocelot.Provider.Consul and services.AddConsul() or Ocelot.Provider.Eureka and services.AddEureka()?"))
.BDDfy();
}
[Fact]
public void configuration_is_valid_if_qos_options_specified_and_has_qos_handler()
{
@ -151,7 +339,7 @@
.BDDfy();
}
[Fact]
[Fact]
public void configuration_is_invalid_if_qos_options_specified_globally_but_no_qos_handler()
{
var configuration = new FileConfiguration
@ -988,31 +1176,6 @@
.BDDfy();
}
[Theory]
[InlineData(null)]
[InlineData("")]
public void configuration_is_invalid_with_using_service_discovery_and_no_service_name(string serviceName)
{
this.Given(x => x.GivenAConfiguration(new FileConfiguration
{
ReRoutes = new List<FileReRoute>
{
new FileReRoute
{
DownstreamPathTemplate = "/api/products/",
UpstreamPathTemplate = "/asdf/",
UpstreamHttpMethod = new List<string> {"Get"},
UseServiceDiscovery = true,
ServiceName = serviceName
}
}
}))
.When(x => x.WhenIValidateTheConfiguration())
.Then(x => x.ThenTheResultIsNotValid())
.And(x => x.ThenTheErrorMessageAtPositionIs(0, "ServiceName cannot be empty or null when using service discovery or Ocelot cannot look up your service!"))
.BDDfy();
}
[Fact]
public void configuration_is_valid_with_using_service_discovery_and_service_name()
{
@ -1025,9 +1188,17 @@
DownstreamPathTemplate = "/api/products/",
UpstreamPathTemplate = "/asdf/",
UpstreamHttpMethod = new List<string> {"Get"},
UseServiceDiscovery = true,
ServiceName = "Test"
}
},
GlobalConfiguration = new FileGlobalConfiguration
{
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider
{
Type = "servicefabric",
Host = "localhost",
Port = 1234
}
}
}))
.When(x => x.WhenIValidateTheConfiguration())
@ -1049,7 +1220,6 @@
DownstreamPathTemplate = "/api/products/",
UpstreamPathTemplate = "/asdf/",
UpstreamHttpMethod = new List<string> {"Get"},
UseServiceDiscovery = false,
DownstreamHostAndPorts = new List<FileHostAndPort>
{
new FileHostAndPort
@ -1078,7 +1248,6 @@
DownstreamPathTemplate = "/api/products/",
UpstreamPathTemplate = "/asdf/",
UpstreamHttpMethod = new List<string> {"Get"},
UseServiceDiscovery = false,
DownstreamHostAndPorts = new List<FileHostAndPort>
{
new FileHostAndPort
@ -1106,7 +1275,6 @@
DownstreamPathTemplate = "/api/products/",
UpstreamPathTemplate = "/asdf/",
UpstreamHttpMethod = new List<string> {"Get"},
UseServiceDiscovery = false,
DownstreamHostAndPorts = new List<FileHostAndPort>
{
new FileHostAndPort
@ -1134,7 +1302,6 @@
DownstreamPathTemplate = "/api/products/",
UpstreamPathTemplate = "/asdf/",
UpstreamHttpMethod = new List<string> {"Get"},
UseServiceDiscovery = false,
DownstreamHostAndPorts = new List<FileHostAndPort>
{
}
@ -1159,7 +1326,6 @@
DownstreamPathTemplate = "/api/products/",
UpstreamPathTemplate = "/asdf/",
UpstreamHttpMethod = new List<string> {"Get"},
UseServiceDiscovery = false,
DownstreamHostAndPorts = new List<FileHostAndPort>
{
new FileHostAndPort()
@ -1220,6 +1386,23 @@
_configurationValidator = new FileConfigurationFluentValidator(_authProvider.Object, provider);
}
private void GivenAServiceDiscoveryHandler()
{
var collection = new ServiceCollection();
ServiceDiscoveryFinderDelegate del = (a,b,c) => new FakeServiceDiscoveryProvider();
collection.AddSingleton<ServiceDiscoveryFinderDelegate>(del);
var provider = collection.BuildServiceProvider();
_configurationValidator = new FileConfigurationFluentValidator(_authProvider.Object, provider);
}
private class FakeServiceDiscoveryProvider : IServiceDiscoveryProvider
{
public Task<List<Service>> Get()
{
throw new System.NotImplementedException();
}
}
private class TestOptions : AuthenticationSchemeOptions
{
}

View File

@ -8,6 +8,7 @@ using System.Collections.Generic;
using Ocelot.ServiceDiscovery.Providers;
using TestStack.BDDfy;
using Xunit;
using Ocelot.Responses;
namespace Ocelot.UnitTests.LoadBalancer
{
@ -15,7 +16,7 @@ namespace Ocelot.UnitTests.LoadBalancer
{
private DownstreamReRoute _reRoute;
private readonly LoadBalancerFactory _factory;
private ILoadBalancer _result;
private Response<ILoadBalancer> _result;
private readonly Mock<IServiceDiscoveryProviderFactory> _serviceProviderFactory;
private readonly Mock<IServiceDiscoveryProvider> _serviceProvider;
private ServiceProviderConfiguration _serviceProviderConfig;
@ -115,7 +116,7 @@ namespace Ocelot.UnitTests.LoadBalancer
{
_serviceProviderFactory
.Setup(x => x.Get(It.IsAny<ServiceProviderConfiguration>(), It.IsAny<DownstreamReRoute>()))
.Returns(_serviceProvider.Object);
.Returns(new OkResponse<IServiceDiscoveryProvider>(_serviceProvider.Object));
}
private void ThenTheServiceProviderIsCalledCorrectly()
@ -136,7 +137,7 @@ namespace Ocelot.UnitTests.LoadBalancer
private void ThenTheLoadBalancerIsReturned<T>()
{
_result.ShouldBeOfType<T>();
_result.Data.ShouldBeOfType<T>();
}
}
}

View File

@ -111,7 +111,7 @@ namespace Ocelot.UnitTests.LoadBalancer
private void WhenIGetTheReRouteWithTheSameKeyButDifferentLoadBalancer(DownstreamReRoute reRoute)
{
_reRoute = reRoute;
_factory.Setup(x => x.Get(_reRoute, _serviceProviderConfig)).ReturnsAsync(new LeastConnection(null, null));
_factory.Setup(x => x.Get(_reRoute, _serviceProviderConfig)).ReturnsAsync(new OkResponse<ILoadBalancer>(new LeastConnection(null, null)));
_getResult = _loadBalancerHouse.Get(_reRoute, _serviceProviderConfig).Result;
}
@ -138,7 +138,7 @@ namespace Ocelot.UnitTests.LoadBalancer
{
_reRoute = reRoute;
_loadBalancer = loadBalancer;
_factory.Setup(x => x.Get(_reRoute, _serviceProviderConfig)).ReturnsAsync(loadBalancer);
_factory.Setup(x => x.Get(_reRoute, _serviceProviderConfig)).ReturnsAsync(new OkResponse<ILoadBalancer>(loadBalancer));
_getResult = _loadBalancerHouse.Get(reRoute, _serviceProviderConfig).Result;
}

View File

@ -14,11 +14,12 @@ namespace Ocelot.UnitTests.ServiceDiscovery
using Shouldly;
using TestStack.BDDfy;
using Xunit;
using Ocelot.Responses;
public class ServiceProviderFactoryTests
public class ServiceDiscoveryProviderFactoryTests
{
private ServiceProviderConfiguration _serviceConfig;
private IServiceDiscoveryProvider _result;
private Response<IServiceDiscoveryProvider> _result;
private ServiceDiscoveryProviderFactory _factory;
private DownstreamReRoute _reRoute;
private readonly Mock<IOcelotLoggerFactory> _loggerFactory;
@ -26,7 +27,7 @@ namespace Ocelot.UnitTests.ServiceDiscovery
private IServiceProvider _provider;
private readonly IServiceCollection _collection;
public ServiceProviderFactoryTests()
public ServiceDiscoveryProviderFactoryTests()
{
_loggerFactory = new Mock<IOcelotLoggerFactory>();
_logger = new Mock<IOcelotLogger>();
@ -71,7 +72,7 @@ namespace Ocelot.UnitTests.ServiceDiscovery
}
[Fact]
public void should_call_delegate()
public void should_return_provider_because_type_matches_reflected_type_from_delegate()
{
var reRoute = new DownstreamReRouteBuilder()
.WithServiceName("product")
@ -79,6 +80,7 @@ namespace Ocelot.UnitTests.ServiceDiscovery
.Build();
var serviceConfig = new ServiceProviderConfigurationBuilder()
.WithType(nameof(Fake))
.Build();
this.Given(x => x.GivenTheReRoute(serviceConfig, reRoute))
@ -88,6 +90,25 @@ namespace Ocelot.UnitTests.ServiceDiscovery
.BDDfy();
}
[Fact]
public void should_not_return_provider_because_type_doesnt_match_reflected_type_from_delegate()
{
var reRoute = new DownstreamReRouteBuilder()
.WithServiceName("product")
.WithUseServiceDiscovery(true)
.Build();
var serviceConfig = new ServiceProviderConfigurationBuilder()
.WithType("Wookie")
.Build();
this.Given(x => x.GivenTheReRoute(serviceConfig, reRoute))
.And(x => GivenAFakeDelegate())
.When(x => x.WhenIGetTheServiceProvider())
.Then(x => x.ThenTheResultIsError())
.BDDfy();
}
[Fact]
public void should_return_service_fabric_provider()
{
@ -124,12 +145,17 @@ namespace Ocelot.UnitTests.ServiceDiscovery
private void ThenTheDelegateIsCalled()
{
_result.GetType().Name.ShouldBe("Fake");
_result.Data.GetType().Name.ShouldBe("Fake");
}
private void ThenTheResultIsError()
{
_result.IsError.ShouldBeTrue();
}
private void ThenTheFollowingServicesAreReturned(List<DownstreamHostAndPort> downstreamAddresses)
{
var result = (ConfigurationServiceProvider)_result;
var result = (ConfigurationServiceProvider)_result.Data;
var services = result.Get().Result;
for (int i = 0; i < services.Count; i++)
@ -155,7 +181,7 @@ namespace Ocelot.UnitTests.ServiceDiscovery
private void ThenTheServiceProviderIs<T>()
{
_result.ShouldBeOfType<T>();
_result.Data.ShouldBeOfType<T>();
}
}
}