Raft round 2 (#182)

* brought in rafty

* moved raft classes into Ocelot and deleted from int project

* started to set up rafty in Ocelot

* RAFTY INSIDE OCELOT...WOOT

* more work adding rafty...just need to get auth working now

* rudimentary authenticated raft requests working

* asyn await stuff

* hacked rafty into the fileconfigurationcontroller...everything seems to be working roughly but I have a lot of refactoring to do

* updated to latest rafty that doesnt need an id

* hacky but all tests passing

* changed admin area set up to use builder not configuration.json, changed admin area auth to use client credentials

* missing code coverage

* ignore raft sectionf for code coverage

* ignore raft sectionf for code coverage

* back to normal filters

* try exclude attr

* missed these

* moved client secret to builder for authentication and updated docs

* lock to try and fix error accessing identity server created temprsa file on build server

* updated postman scripts and changed Ocelot to not always use type handling as this looked crap when manually accessing the configuration endpoint

* added rafty docs

* changes I missed

* added serialisation code we need for rafty to process commands when they proxy to leader

* moved controllers into their feature slices
This commit is contained in:
Tom Pallister
2018-01-01 18:40:39 +00:00
committed by GitHub
parent 194f76cf7f
commit f082f7318a
50 changed files with 1876 additions and 459 deletions

View File

@ -15,6 +15,7 @@ using Xunit;
namespace Ocelot.UnitTests.Configuration
{
using Ocelot.DependencyInjection;
using Ocelot.Errors;
using Ocelot.UnitTests.TestData;
@ -36,6 +37,7 @@ namespace Ocelot.UnitTests.Configuration
private Mock<IRateLimitOptionsCreator> _rateLimitOptions;
private Mock<IRegionCreator> _regionCreator;
private Mock<IHttpHandlerOptionsCreator> _httpHandlerOptionsCreator;
private Mock<IAdministrationPath> _adminPath;
public FileConfigurationCreatorTests()
{
@ -52,13 +54,23 @@ namespace Ocelot.UnitTests.Configuration
_rateLimitOptions = new Mock<IRateLimitOptionsCreator>();
_regionCreator = new Mock<IRegionCreator>();
_httpHandlerOptionsCreator = new Mock<IHttpHandlerOptionsCreator>();
_adminPath = new Mock<IAdministrationPath>();
_ocelotConfigurationCreator = new FileOcelotConfigurationCreator(
_fileConfig.Object, _validator.Object, _logger.Object,
_fileConfig.Object,
_validator.Object,
_logger.Object,
_claimsToThingCreator.Object,
_authOptionsCreator.Object, _upstreamTemplatePatternCreator.Object, _requestIdKeyCreator.Object,
_serviceProviderConfigCreator.Object, _qosOptionsCreator.Object, _fileReRouteOptionsCreator.Object,
_rateLimitOptions.Object, _regionCreator.Object, _httpHandlerOptionsCreator.Object);
_authOptionsCreator.Object,
_upstreamTemplatePatternCreator.Object,
_requestIdKeyCreator.Object,
_serviceProviderConfigCreator.Object,
_qosOptionsCreator.Object,
_fileReRouteOptionsCreator.Object,
_rateLimitOptions.Object,
_regionCreator.Object,
_httpHandlerOptionsCreator.Object,
_adminPath.Object);
}
[Fact]

View File

@ -91,7 +91,6 @@ namespace Ocelot.UnitTests.Configuration
private void ThenTheConfigurationIsStoredAs(FileConfiguration expected)
{
_result.GlobalConfiguration.AdministrationPath.ShouldBe(expected.GlobalConfiguration.AdministrationPath);
_result.GlobalConfiguration.RequestIdKey.ShouldBe(expected.GlobalConfiguration.RequestIdKey);
_result.GlobalConfiguration.ServiceDiscoveryProvider.Host.ShouldBe(expected.GlobalConfiguration.ServiceDiscoveryProvider.Host);
_result.GlobalConfiguration.ServiceDiscoveryProvider.Port.ShouldBe(expected.GlobalConfiguration.ServiceDiscoveryProvider.Port);
@ -126,7 +125,6 @@ namespace Ocelot.UnitTests.Configuration
private void ThenTheFollowingIsReturned(FileConfiguration expected)
{
_result.GlobalConfiguration.AdministrationPath.ShouldBe(expected.GlobalConfiguration.AdministrationPath);
_result.GlobalConfiguration.RequestIdKey.ShouldBe(expected.GlobalConfiguration.RequestIdKey);
_result.GlobalConfiguration.ServiceDiscoveryProvider.Host.ShouldBe(expected.GlobalConfiguration.ServiceDiscoveryProvider.Host);
_result.GlobalConfiguration.ServiceDiscoveryProvider.Port.ShouldBe(expected.GlobalConfiguration.ServiceDiscoveryProvider.Port);
@ -155,7 +153,6 @@ namespace Ocelot.UnitTests.Configuration
var globalConfiguration = new FileGlobalConfiguration
{
AdministrationPath = "asdas",
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider
{
Port = 198,
@ -185,7 +182,6 @@ namespace Ocelot.UnitTests.Configuration
var globalConfiguration = new FileGlobalConfiguration
{
AdministrationPath = "testy",
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider
{
Port = 198,

View File

@ -9,7 +9,7 @@ namespace Ocelot.UnitTests.Configuration
[Fact]
public void happy_path_only_exists_for_test_coverage_even_uncle_bob_probably_wouldnt_test_this()
{
var result = IdentityServerConfigurationCreator.GetIdentityServerConfiguration();
var result = IdentityServerConfigurationCreator.GetIdentityServerConfiguration("secret");
result.ApiName.ShouldBe("admin");
}
}

View File

@ -1,117 +0,0 @@
using Ocelot.Configuration.Authentication;
using Xunit;
using Shouldly;
using TestStack.BDDfy;
using Moq;
using IdentityServer4.Validation;
using Ocelot.Configuration.Provider;
using System.Collections.Generic;
namespace Ocelot.UnitTests.Configuration
{
public class OcelotResourceOwnerPasswordValidatorTests
{
private OcelotResourceOwnerPasswordValidator _validator;
private Mock<IHashMatcher> _matcher;
private string _userName;
private string _password;
private ResourceOwnerPasswordValidationContext _context;
private Mock<IIdentityServerConfiguration> _config;
private User _user;
public OcelotResourceOwnerPasswordValidatorTests()
{
_matcher = new Mock<IHashMatcher>();
_config = new Mock<IIdentityServerConfiguration>();
_validator = new OcelotResourceOwnerPasswordValidator(_matcher.Object, _config.Object);
}
[Fact]
public void should_return_success()
{
this.Given(x => GivenTheUserName("tom"))
.And(x => GivenThePassword("password"))
.And(x => GivenTheUserIs(new User("sub", "tom", "xxx", "xxx")))
.And(x => GivenTheMatcherReturns(true))
.When(x => WhenIValidate())
.Then(x => ThenTheUserIsValidated())
.And(x => ThenTheMatcherIsCalledCorrectly())
.BDDfy();
}
[Fact]
public void should_return_fail_when_no_user()
{
this.Given(x => GivenTheUserName("bob"))
.And(x => GivenTheUserIs(new User("sub", "tom", "xxx", "xxx")))
.And(x => GivenTheMatcherReturns(true))
.When(x => WhenIValidate())
.Then(x => ThenTheUserIsNotValidated())
.BDDfy();
}
[Fact]
public void should_return_fail_when_password_doesnt_match()
{
this.Given(x => GivenTheUserName("tom"))
.And(x => GivenThePassword("password"))
.And(x => GivenTheUserIs(new User("sub", "tom", "xxx", "xxx")))
.And(x => GivenTheMatcherReturns(false))
.When(x => WhenIValidate())
.Then(x => ThenTheUserIsNotValidated())
.And(x => ThenTheMatcherIsCalledCorrectly())
.BDDfy();
}
private void ThenTheMatcherIsCalledCorrectly()
{
_matcher
.Verify(x => x.Match(_password, _user.Salt, _user.Hash), Times.Once);
}
private void GivenThePassword(string password)
{
_password = password;
}
private void GivenTheUserIs(User user)
{
_user = user;
_config
.Setup(x => x.Users)
.Returns(new List<User>{_user});
}
private void GivenTheMatcherReturns(bool expected)
{
_matcher
.Setup(x => x.Match(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns(expected);
}
private void GivenTheUserName(string userName)
{
_userName = userName;
}
private void WhenIValidate()
{
_context = new ResourceOwnerPasswordValidationContext
{
UserName = _userName,
Password = _password
};
_validator.ValidateAsync(_context).Wait();
}
private void ThenTheUserIsValidated()
{
_context.Result.IsError.ShouldBe(false);
}
private void ThenTheUserIsNotValidated()
{
_context.Result.IsError.ShouldBe(true);
}
}
}

View File

@ -1,14 +1,20 @@
using System;
using Microsoft.AspNetCore.Mvc;
using Moq;
using Ocelot.Configuration.File;
using Ocelot.Configuration.Setter;
using Ocelot.Controllers;
using Ocelot.Errors;
using Ocelot.Responses;
using TestStack.BDDfy;
using Xunit;
using Shouldly;
using Ocelot.Configuration.Provider;
using Microsoft.Extensions.DependencyInjection;
using Ocelot.Raft;
using Rafty.Concensus;
using Newtonsoft.Json;
using Rafty.FiniteStateMachine;
using Ocelot.Configuration;
namespace Ocelot.UnitTests.Controllers
{
@ -19,18 +25,21 @@ namespace Ocelot.UnitTests.Controllers
private Mock<IFileConfigurationSetter> _configSetter;
private IActionResult _result;
private FileConfiguration _fileConfiguration;
private Mock<IServiceProvider> _provider;
private Mock<INode> _node;
public FileConfigurationControllerTests()
{
_provider = new Mock<IServiceProvider>();
_configGetter = new Mock<IFileConfigurationProvider>();
_configSetter = new Mock<IFileConfigurationSetter>();
_controller = new FileConfigurationController(_configGetter.Object, _configSetter.Object);
_controller = new FileConfigurationController(_configGetter.Object, _configSetter.Object, _provider.Object);
}
[Fact]
public void should_get_file_configuration()
{
var expected = new OkResponse<FileConfiguration>(new FileConfiguration());
var expected = new Responses.OkResponse<FileConfiguration>(new FileConfiguration());
this.Given(x => x.GivenTheGetConfigurationReturns(expected))
.When(x => x.WhenIGetTheFileConfiguration())
@ -41,7 +50,7 @@ namespace Ocelot.UnitTests.Controllers
[Fact]
public void should_return_error_when_cannot_get_config()
{
var expected = new ErrorResponse<FileConfiguration>(It.IsAny<Error>());
var expected = new Responses.ErrorResponse<FileConfiguration>(It.IsAny<Error>());
this.Given(x => x.GivenTheGetConfigurationReturns(expected))
.When(x => x.WhenIGetTheFileConfiguration())
@ -56,26 +65,81 @@ namespace Ocelot.UnitTests.Controllers
var expected = new FileConfiguration();
this.Given(x => GivenTheFileConfiguration(expected))
.And(x => GivenTheConfigSetterReturnsAnError(new OkResponse()))
.And(x => GivenTheConfigSetterReturns(new OkResponse()))
.When(x => WhenIPostTheFileConfiguration())
.Then(x => x.ThenTheConfigrationSetterIsCalledCorrectly())
.BDDfy();
}
[Fact]
public void should_post_file_configuration_using_raft_node()
{
var expected = new FileConfiguration();
this.Given(x => GivenTheFileConfiguration(expected))
.And(x => GivenARaftNodeIsRegistered())
.And(x => GivenTheNodeReturnsOK())
.And(x => GivenTheConfigSetterReturns(new OkResponse()))
.When(x => WhenIPostTheFileConfiguration())
.Then(x => x.ThenTheNodeIsCalledCorrectly())
.BDDfy();
}
[Fact]
public void should_return_error_when_cannot_set_config_using_raft_node()
{
var expected = new FileConfiguration();
this.Given(x => GivenTheFileConfiguration(expected))
.And(x => GivenARaftNodeIsRegistered())
.And(x => GivenTheNodeReturnsError())
.When(x => WhenIPostTheFileConfiguration())
.Then(x => ThenTheResponseIs<BadRequestObjectResult>())
.BDDfy();
}
[Fact]
public void should_return_error_when_cannot_set_config()
{
var expected = new FileConfiguration();
this.Given(x => GivenTheFileConfiguration(expected))
.And(x => GivenTheConfigSetterReturnsAnError(new ErrorResponse(new FakeError())))
.And(x => GivenTheConfigSetterReturns(new ErrorResponse(new FakeError())))
.When(x => WhenIPostTheFileConfiguration())
.Then(x => x.ThenTheConfigrationSetterIsCalledCorrectly())
.And(x => ThenTheResponseIs<BadRequestObjectResult>())
.BDDfy();
}
private void GivenTheConfigSetterReturnsAnError(Response response)
private void ThenTheNodeIsCalledCorrectly()
{
_node.Verify(x => x.Accept(It.IsAny<UpdateFileConfiguration>()), Times.Once);
}
private void GivenARaftNodeIsRegistered()
{
_node = new Mock<INode>();
_provider
.Setup(x => x.GetService(typeof(INode)))
.Returns(_node.Object);
}
private void GivenTheNodeReturnsOK()
{
_node
.Setup(x => x.Accept(It.IsAny<UpdateFileConfiguration>()))
.Returns(new Rafty.Concensus.OkResponse<UpdateFileConfiguration>(new UpdateFileConfiguration(new FileConfiguration())));
}
private void GivenTheNodeReturnsError()
{
_node
.Setup(x => x.Accept(It.IsAny<UpdateFileConfiguration>()))
.Returns(new Rafty.Concensus.ErrorResponse<UpdateFileConfiguration>("error", new UpdateFileConfiguration(new FileConfiguration())));
}
private void GivenTheConfigSetterReturns(Response response)
{
_configSetter
.Setup(x => x.Set(It.IsAny<FileConfiguration>()))
@ -103,7 +167,7 @@ namespace Ocelot.UnitTests.Controllers
_result.ShouldBeOfType<T>();
}
private void GivenTheGetConfigurationReturns(Response<FileConfiguration> fileConfiguration)
private void GivenTheGetConfigurationReturns(Ocelot.Responses.Response<FileConfiguration> fileConfiguration)
{
_configGetter
.Setup(x => x.Get())
@ -128,4 +192,4 @@ namespace Ocelot.UnitTests.Controllers
}
}
}
}
}

View File

@ -1,10 +1,9 @@
using Xunit;
using Shouldly;
using TestStack.BDDfy;
using Ocelot.Controllers;
using Ocelot.Cache;
using System;
using Moq;
using Ocelot.Cache;
using System.Net.Http;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;

View File

@ -75,6 +75,16 @@ namespace Ocelot.UnitTests.DependencyInjection
.BDDfy();
}
[Fact]
public void should_set_up_rafty()
{
this.Given(x => WhenISetUpOcelotServices())
.When(x => WhenISetUpRafty())
.Then(x => ThenAnExceptionIsntThrown())
.Then(x => ThenTheCorrectAdminPathIsRegitered())
.BDDfy();
}
[Fact]
public void should_use_logger_factory()
{
@ -85,6 +95,13 @@ namespace Ocelot.UnitTests.DependencyInjection
.BDDfy();
}
private void ThenTheCorrectAdminPathIsRegitered()
{
_serviceProvider = _services.BuildServiceProvider();
var path = _serviceProvider.GetService<IAdministrationPath>();
path.Path.ShouldBe("/administration");
}
private void OnlyOneVersionOfEachCacheIsRegistered()
{
var outputCache = _services.Single(x => x.ServiceType == typeof(IOcelotCache<CachedResponse>));
@ -111,6 +128,18 @@ namespace Ocelot.UnitTests.DependencyInjection
}
}
private void WhenISetUpRafty()
{
try
{
_ocelotBuilder.AddAdministration("/administration", "secret").AddRafty();
}
catch (Exception e)
{
_ex = e;
}
}
private void ThenAnOcelotBuilderIsReturned()
{
_ocelotBuilder.ShouldBeOfType<OcelotBuilder>();

View File

@ -0,0 +1,45 @@
using Moq;
using Ocelot.Configuration.Setter;
using Ocelot.Raft;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.UnitTests.Raft
{
public class OcelotFiniteStateMachineTests
{
private UpdateFileConfiguration _command;
private OcelotFiniteStateMachine _fsm;
private Mock<IFileConfigurationSetter> _setter;
public OcelotFiniteStateMachineTests()
{
_setter = new Mock<IFileConfigurationSetter>();
_fsm = new OcelotFiniteStateMachine(_setter.Object);
}
[Fact]
public void should_handle_update_file_configuration_command()
{
this.Given(x => GivenACommand(new UpdateFileConfiguration(new Ocelot.Configuration.File.FileConfiguration())))
.When(x => WhenTheCommandIsHandled())
.Then(x => ThenTheStateIsUpdated())
.BDDfy();
}
private void GivenACommand(UpdateFileConfiguration command)
{
_command = command;
}
private void WhenTheCommandIsHandled()
{
_fsm.Handle(new Rafty.Log.LogEntry(_command, _command.GetType(), 0));
}
private void ThenTheStateIsUpdated()
{
_setter.Verify(x => x.Set(_command.Configuration), Times.Once);
}
}
}