mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 15:50:49 +08:00 
			
		
		
		
	Feature/#295 consul acl (#307)
* removed file * updated package * updated package * updated package * updated package * updated package * updated package * updated package * all packages updated * #295 can add token to service provider config and this will be used by consul clients to get services and configuration * #295 wait longer for this test
This commit is contained in:
		@@ -10,8 +10,7 @@ namespace Ocelot.UnitTests.Configuration
 | 
			
		||||
{
 | 
			
		||||
    public class ServiceProviderCreatorTests
 | 
			
		||||
    {
 | 
			
		||||
        private ServiceProviderConfigurationCreator _creator;
 | 
			
		||||
        private FileReRoute _reRoute;
 | 
			
		||||
        private readonly ServiceProviderConfigurationCreator _creator;
 | 
			
		||||
        private FileGlobalConfiguration _globalConfig;
 | 
			
		||||
        private ServiceProviderConfiguration _result;
 | 
			
		||||
 | 
			
		||||
@@ -23,36 +22,30 @@ namespace Ocelot.UnitTests.Configuration
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_create_service_provider_config()
 | 
			
		||||
        {
 | 
			
		||||
            var reRoute = new FileReRoute();
 | 
			
		||||
            
 | 
			
		||||
            var globalConfig = new FileGlobalConfiguration
 | 
			
		||||
            {
 | 
			
		||||
                ServiceDiscoveryProvider = new FileServiceDiscoveryProvider
 | 
			
		||||
                {
 | 
			
		||||
                    Host = "127.0.0.1",
 | 
			
		||||
                    Port = 1234,
 | 
			
		||||
                    Type = "ServiceFabric"
 | 
			
		||||
                    Type = "ServiceFabric",
 | 
			
		||||
                    Token = "testtoken"
 | 
			
		||||
                }
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            var expected = new ServiceProviderConfigurationBuilder()
 | 
			
		||||
                .WithServiceDiscoveryProviderHost("127.0.0.1")
 | 
			
		||||
                .WithServiceDiscoveryProviderPort(1234)
 | 
			
		||||
                .WithServiceDiscoveryProviderType("ServiceFabric")
 | 
			
		||||
                .WithHost("127.0.0.1")
 | 
			
		||||
                .WithPort(1234)
 | 
			
		||||
                .WithType("ServiceFabric")
 | 
			
		||||
                .WithToken("testtoken")
 | 
			
		||||
                .Build();
 | 
			
		||||
 | 
			
		||||
            this.Given(x => x.GivenTheFollowingReRoute(reRoute))
 | 
			
		||||
                .And(x => x.GivenTheFollowingGlobalConfig(globalConfig))
 | 
			
		||||
            this.Given(x => x.GivenTheFollowingGlobalConfig(globalConfig))
 | 
			
		||||
                .When(x => x.WhenICreate())
 | 
			
		||||
                .Then(x => x.ThenTheConfigIs(expected))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenTheFollowingReRoute(FileReRoute fileReRoute)
 | 
			
		||||
        {
 | 
			
		||||
            _reRoute = fileReRoute;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenTheFollowingGlobalConfig(FileGlobalConfiguration fileGlobalConfig)
 | 
			
		||||
        {
 | 
			
		||||
            _globalConfig = fileGlobalConfig;
 | 
			
		||||
@@ -67,6 +60,8 @@ namespace Ocelot.UnitTests.Configuration
 | 
			
		||||
        {
 | 
			
		||||
            _result.Host.ShouldBe(expected.Host);
 | 
			
		||||
            _result.Port.ShouldBe(expected.Port);
 | 
			
		||||
            _result.Token.ShouldBe(expected.Token);
 | 
			
		||||
            _result.Type.ShouldBe(expected.Type);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -82,9 +82,9 @@ namespace Ocelot.UnitTests.DownstreamUrlCreator
 | 
			
		||||
                .Build();
 | 
			
		||||
 | 
			
		||||
            var config = new ServiceProviderConfigurationBuilder()
 | 
			
		||||
                .WithServiceDiscoveryProviderType("ServiceFabric")
 | 
			
		||||
                .WithServiceDiscoveryProviderHost("localhost")
 | 
			
		||||
                .WithServiceDiscoveryProviderPort(19081)
 | 
			
		||||
                .WithType("ServiceFabric")
 | 
			
		||||
                .WithHost("localhost")
 | 
			
		||||
                .WithPort(19081)
 | 
			
		||||
                .Build();
 | 
			
		||||
 | 
			
		||||
            this.Given(x => x.GivenTheDownStreamRouteIs(
 | 
			
		||||
@@ -118,9 +118,9 @@ namespace Ocelot.UnitTests.DownstreamUrlCreator
 | 
			
		||||
                    .Build());
 | 
			
		||||
 | 
			
		||||
            var config = new ServiceProviderConfigurationBuilder()
 | 
			
		||||
                .WithServiceDiscoveryProviderType("ServiceFabric")
 | 
			
		||||
                .WithServiceDiscoveryProviderHost("localhost")
 | 
			
		||||
                .WithServiceDiscoveryProviderPort(19081)
 | 
			
		||||
                .WithType("ServiceFabric")
 | 
			
		||||
                .WithHost("localhost")
 | 
			
		||||
                .WithPort(19081)
 | 
			
		||||
                .Build();
 | 
			
		||||
 | 
			
		||||
            this.Given(x => x.GivenTheDownStreamRouteIs(downstreamRoute))
 | 
			
		||||
@@ -148,9 +148,9 @@ namespace Ocelot.UnitTests.DownstreamUrlCreator
 | 
			
		||||
                    .Build());
 | 
			
		||||
 | 
			
		||||
            var config = new ServiceProviderConfigurationBuilder()
 | 
			
		||||
                .WithServiceDiscoveryProviderType("ServiceFabric")
 | 
			
		||||
                .WithServiceDiscoveryProviderHost("localhost")
 | 
			
		||||
                .WithServiceDiscoveryProviderPort(19081)
 | 
			
		||||
                .WithType("ServiceFabric")
 | 
			
		||||
                .WithHost("localhost")
 | 
			
		||||
                .WithPort(19081)
 | 
			
		||||
                .Build();
 | 
			
		||||
 | 
			
		||||
            this.Given(x => x.GivenTheDownStreamRouteIs(downstreamRoute))
 | 
			
		||||
@@ -178,9 +178,9 @@ namespace Ocelot.UnitTests.DownstreamUrlCreator
 | 
			
		||||
                    .Build());
 | 
			
		||||
 | 
			
		||||
            var config = new ServiceProviderConfigurationBuilder()
 | 
			
		||||
                .WithServiceDiscoveryProviderType("ServiceFabric")
 | 
			
		||||
                .WithServiceDiscoveryProviderHost("localhost")
 | 
			
		||||
                .WithServiceDiscoveryProviderPort(19081)
 | 
			
		||||
                .WithType("ServiceFabric")
 | 
			
		||||
                .WithHost("localhost")
 | 
			
		||||
                .WithPort(19081)
 | 
			
		||||
                .Build();
 | 
			
		||||
 | 
			
		||||
            this.Given(x => x.GivenTheDownStreamRouteIs(downstreamRoute))
 | 
			
		||||
 
 | 
			
		||||
@@ -25,7 +25,7 @@ namespace Ocelot.UnitTests.LoadBalancer
 | 
			
		||||
        {
 | 
			
		||||
            _factory = new Mock<ILoadBalancerFactory>();
 | 
			
		||||
            _loadBalancerHouse = new LoadBalancerHouse(_factory.Object);
 | 
			
		||||
            _serviceProviderConfig = new ServiceProviderConfiguration("myType","myHost",123);
 | 
			
		||||
            _serviceProviderConfig = new ServiceProviderConfiguration("myType","myHost",123, string.Empty);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
 
 | 
			
		||||
@@ -35,23 +35,23 @@
 | 
			
		||||
  </ItemGroup>
 | 
			
		||||
 | 
			
		||||
  <ItemGroup>
 | 
			
		||||
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
 | 
			
		||||
    <PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.0.0" />
 | 
			
		||||
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0-preview-20171031-01" />
 | 
			
		||||
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.6" />
 | 
			
		||||
    <PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.0.2" />
 | 
			
		||||
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.0" />
 | 
			
		||||
    <PackageReference Include="StyleCop.Analyzers" Version="1.0.2">
 | 
			
		||||
      <PrivateAssets>all</PrivateAssets>
 | 
			
		||||
    </PackageReference>
 | 
			
		||||
    <PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
 | 
			
		||||
    <PackageReference Include="Microsoft.DotNet.InternalAbstractions" Version="1.0.500-preview2-1-003177" />
 | 
			
		||||
    <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.0.0" />
 | 
			
		||||
    <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.0.0" />
 | 
			
		||||
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.0" />
 | 
			
		||||
    <PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0" />
 | 
			
		||||
    <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.0.0" />
 | 
			
		||||
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.0.0" />
 | 
			
		||||
    <PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.0.0" />
 | 
			
		||||
    <PackageReference Include="Moq" Version="4.7.142" />
 | 
			
		||||
    <PackageReference Include="Shouldly" Version="3.0.0-beta0003" />
 | 
			
		||||
    <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.0.1" />
 | 
			
		||||
    <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.0.1" />
 | 
			
		||||
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.1" />
 | 
			
		||||
    <PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.1" />
 | 
			
		||||
    <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.0.1" />
 | 
			
		||||
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.0.1" />
 | 
			
		||||
    <PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.0.1" />
 | 
			
		||||
    <PackageReference Include="Moq" Version="4.8.2" />
 | 
			
		||||
    <PackageReference Include="Shouldly" Version="3.0.0" />
 | 
			
		||||
    <PackageReference Include="TestStack.BDDfy" Version="4.3.2" />
 | 
			
		||||
    <PackageReference Include="xunit" Version="2.3.1" />
 | 
			
		||||
    <PackageReference Include="Butterfly.Client.AspNetCore" Version="0.0.8" />
 | 
			
		||||
 
 | 
			
		||||
@@ -1,14 +1,14 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.IO;
 | 
			
		||||
using System.Text;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using Consul;
 | 
			
		||||
using Microsoft.AspNetCore.Builder;
 | 
			
		||||
using Microsoft.AspNetCore.Hosting;
 | 
			
		||||
using Microsoft.AspNetCore.Http;
 | 
			
		||||
using Moq;
 | 
			
		||||
using Ocelot.Infrastructure.Consul;
 | 
			
		||||
using Ocelot.Logging;
 | 
			
		||||
using Ocelot.ServiceDiscovery;
 | 
			
		||||
using Ocelot.ServiceDiscovery.Configuration;
 | 
			
		||||
using Ocelot.ServiceDiscovery.Providers;
 | 
			
		||||
using Ocelot.Values;
 | 
			
		||||
@@ -22,14 +22,16 @@ namespace Ocelot.UnitTests.ServiceDiscovery
 | 
			
		||||
    {
 | 
			
		||||
        private IWebHost _fakeConsulBuilder;
 | 
			
		||||
        private readonly List<ServiceEntry> _serviceEntries;
 | 
			
		||||
        private readonly ConsulServiceDiscoveryProvider _provider;
 | 
			
		||||
        private ConsulServiceDiscoveryProvider _provider;
 | 
			
		||||
        private readonly string _serviceName;
 | 
			
		||||
        private readonly int _port;
 | 
			
		||||
        private readonly string _consulHost;
 | 
			
		||||
        private readonly string _fakeConsulServiceDiscoveryUrl;
 | 
			
		||||
        private List<Service> _services;
 | 
			
		||||
        private Mock<IOcelotLoggerFactory> _factory;
 | 
			
		||||
        private readonly Mock<IOcelotLoggerFactory> _factory;
 | 
			
		||||
        private readonly Mock<IOcelotLogger> _logger;
 | 
			
		||||
        private string _receivedToken;
 | 
			
		||||
        private IConsulClientFactory _clientFactory;
 | 
			
		||||
 | 
			
		||||
        public ConsulServiceDiscoveryProviderTests()
 | 
			
		||||
        {
 | 
			
		||||
@@ -40,11 +42,12 @@ namespace Ocelot.UnitTests.ServiceDiscovery
 | 
			
		||||
            _serviceEntries = new List<ServiceEntry>();
 | 
			
		||||
 | 
			
		||||
            _factory = new Mock<IOcelotLoggerFactory>();
 | 
			
		||||
            _clientFactory = new ConsulClientFactory();
 | 
			
		||||
            _logger = new Mock<IOcelotLogger>();
 | 
			
		||||
            _factory.Setup(x => x.CreateLogger<ConsulServiceDiscoveryProvider>()).Returns(_logger.Object);
 | 
			
		||||
 | 
			
		||||
            var config = new ConsulRegistryConfiguration(_consulHost, _port, _serviceName);
 | 
			
		||||
            _provider = new ConsulServiceDiscoveryProvider(config, _factory.Object);
 | 
			
		||||
            var config = new ConsulRegistryConfiguration(_consulHost, _port, _serviceName, null);
 | 
			
		||||
            _provider = new ConsulServiceDiscoveryProvider(config, _factory.Object, _clientFactory);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
@@ -69,6 +72,33 @@ namespace Ocelot.UnitTests.ServiceDiscovery
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_use_token()
 | 
			
		||||
        {
 | 
			
		||||
            var token = "test token";
 | 
			
		||||
            var config = new ConsulRegistryConfiguration(_consulHost, _port, _serviceName, token);
 | 
			
		||||
            _provider = new ConsulServiceDiscoveryProvider(config, _factory.Object, _clientFactory);
 | 
			
		||||
 | 
			
		||||
            var serviceEntryOne = new ServiceEntry()
 | 
			
		||||
            {
 | 
			
		||||
                Service = new AgentService()
 | 
			
		||||
                {
 | 
			
		||||
                    Service = _serviceName,
 | 
			
		||||
                    Address = "localhost",
 | 
			
		||||
                    Port = 50881,
 | 
			
		||||
                    ID = Guid.NewGuid().ToString(),
 | 
			
		||||
                    Tags = new string[0]
 | 
			
		||||
                },
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            this.Given(_ => GivenThereIsAFakeConsulServiceDiscoveryProvider(_fakeConsulServiceDiscoveryUrl, _serviceName))
 | 
			
		||||
                .And(_ => GivenTheServicesAreRegisteredWithConsul(serviceEntryOne))
 | 
			
		||||
                .When(_ => WhenIGetTheServices())
 | 
			
		||||
                .Then(_ => ThenTheCountIs(1))
 | 
			
		||||
                .And(_ => _receivedToken.ShouldBe(token))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_not_return_services_with_invalid_address()
 | 
			
		||||
        {
 | 
			
		||||
@@ -197,6 +227,11 @@ namespace Ocelot.UnitTests.ServiceDiscovery
 | 
			
		||||
                    {
 | 
			
		||||
                        if (context.Request.Path.Value == $"/v1/health/service/{serviceName}")
 | 
			
		||||
                        {
 | 
			
		||||
                            if (context.Request.Headers.TryGetValue("X-Consul-Token", out var values))
 | 
			
		||||
                            {
 | 
			
		||||
                                _receivedToken = values.First();
 | 
			
		||||
                            }
 | 
			
		||||
 | 
			
		||||
                            await context.Response.WriteJsonAsync(_serviceEntries);
 | 
			
		||||
                        }
 | 
			
		||||
                    });
 | 
			
		||||
 
 | 
			
		||||
@@ -3,6 +3,7 @@ using System.Collections.Generic;
 | 
			
		||||
using Moq;
 | 
			
		||||
using Ocelot.Configuration;
 | 
			
		||||
using Ocelot.Configuration.Builder;
 | 
			
		||||
using Ocelot.Infrastructure.Consul;
 | 
			
		||||
using Ocelot.Logging;
 | 
			
		||||
using Ocelot.ServiceDiscovery;
 | 
			
		||||
using Ocelot.ServiceDiscovery.Providers;
 | 
			
		||||
@@ -19,11 +20,13 @@ namespace Ocelot.UnitTests.ServiceDiscovery
 | 
			
		||||
        private readonly ServiceDiscoveryProviderFactory _factory;
 | 
			
		||||
        private DownstreamReRoute _reRoute;
 | 
			
		||||
        private Mock<IOcelotLoggerFactory> _loggerFactory;
 | 
			
		||||
        private IConsulClientFactory _clientFactory;
 | 
			
		||||
 | 
			
		||||
        public ServiceProviderFactoryTests()
 | 
			
		||||
        {
 | 
			
		||||
            _loggerFactory = new Mock<IOcelotLoggerFactory>();
 | 
			
		||||
            _factory = new ServiceDiscoveryProviderFactory(_loggerFactory.Object);
 | 
			
		||||
            _clientFactory = new ConsulClientFactory();
 | 
			
		||||
            _factory = new ServiceDiscoveryProviderFactory(_loggerFactory.Object, _clientFactory);
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        [Fact]
 | 
			
		||||
@@ -87,7 +90,7 @@ namespace Ocelot.UnitTests.ServiceDiscovery
 | 
			
		||||
                .Build();
 | 
			
		||||
 | 
			
		||||
            var serviceConfig = new ServiceProviderConfigurationBuilder()
 | 
			
		||||
                .WithServiceDiscoveryProviderType("ServiceFabric")
 | 
			
		||||
                .WithType("ServiceFabric")
 | 
			
		||||
                .Build();
 | 
			
		||||
 | 
			
		||||
            this.Given(x => x.GivenTheReRoute(serviceConfig, reRoute))
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user