#245 hacked these tests around and they are now passing 5 runs in a row on my mac, lets see on build server

This commit is contained in:
Tom Gardham-Pallister 2018-05-01 22:07:56 +01:00
parent 6793278597
commit 003fff8b24
3 changed files with 636 additions and 590 deletions

View File

@ -1,69 +1,76 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Ocelot.Configuration.File; using Ocelot.Configuration.File;
using Ocelot.Configuration.Setter; using Ocelot.Configuration.Setter;
using Ocelot.Raft; using Ocelot.Raft;
using Rafty.Concensus; using Rafty.Concensus;
namespace Ocelot.Configuration namespace Ocelot.Configuration
{ {
using Repository; using Repository;
[Authorize] [Authorize]
[Route("configuration")] [Route("configuration")]
public class FileConfigurationController : Controller public class FileConfigurationController : Controller
{ {
private readonly IFileConfigurationRepository _repo; private readonly IFileConfigurationRepository _repo;
private readonly IFileConfigurationSetter _setter; private readonly IFileConfigurationSetter _setter;
private readonly IServiceProvider _provider; private readonly IServiceProvider _provider;
public FileConfigurationController(IFileConfigurationRepository repo, IFileConfigurationSetter setter, IServiceProvider provider) public FileConfigurationController(IFileConfigurationRepository repo, IFileConfigurationSetter setter, IServiceProvider provider)
{ {
_repo = repo; _repo = repo;
_setter = setter; _setter = setter;
_provider = provider; _provider = provider;
} }
[HttpGet] [HttpGet]
public async Task<IActionResult> Get() public async Task<IActionResult> Get()
{ {
var response = await _repo.Get(); var response = await _repo.Get();
if(response.IsError) if(response.IsError)
{ {
return new BadRequestObjectResult(response.Errors); return new BadRequestObjectResult(response.Errors);
} }
return new OkObjectResult(response.Data); return new OkObjectResult(response.Data);
} }
[HttpPost] [HttpPost]
public async Task<IActionResult> Post([FromBody]FileConfiguration fileConfiguration) public async Task<IActionResult> Post([FromBody]FileConfiguration fileConfiguration)
{ {
//todo - this code is a bit shit sort it out.. try
var test = _provider.GetService(typeof(INode)); {
if (test != null) //todo - this code is a bit shit sort it out..
{ var test = _provider.GetService(typeof(INode));
var node = (INode)test; if (test != null)
var result = node.Accept(new UpdateFileConfiguration(fileConfiguration)); {
if (result.GetType() == typeof(Rafty.Concensus.ErrorResponse<UpdateFileConfiguration>)) var node = (INode)test;
{ var result = node.Accept(new UpdateFileConfiguration(fileConfiguration));
return new BadRequestObjectResult("There was a problem. This error message sucks raise an issue in GitHub."); if (result.GetType() == typeof(Rafty.Concensus.ErrorResponse<UpdateFileConfiguration>))
} {
return new BadRequestObjectResult("There was a problem. This error message sucks raise an issue in GitHub.");
return new OkObjectResult(result.Command.Configuration); }
}
return new OkObjectResult(result.Command.Configuration);
var response = await _setter.Set(fileConfiguration); }
if (response.IsError) var response = await _setter.Set(fileConfiguration);
{
return new BadRequestObjectResult(response.Errors); if (response.IsError)
} {
return new BadRequestObjectResult(response.Errors);
return new OkObjectResult(fileConfiguration); }
}
} return new OkObjectResult(fileConfiguration);
} }
catch(Exception e)
{
return new BadRequestObjectResult($"{e.Message}:{e.StackTrace}");
}
}
}
}

View File

@ -1,128 +1,129 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net.Http; using System.Net.Http;
using Newtonsoft.Json; using Newtonsoft.Json;
using Ocelot.Configuration; using Ocelot.Configuration;
using Ocelot.Middleware; using Ocelot.Middleware;
using Rafty.Concensus; using Rafty.Concensus;
using Rafty.FiniteStateMachine; using Rafty.FiniteStateMachine;
namespace Ocelot.Raft namespace Ocelot.Raft
{ {
[ExcludeFromCoverage] [ExcludeFromCoverage]
public class HttpPeer : IPeer public class HttpPeer : IPeer
{ {
private readonly string _hostAndPort; private readonly string _hostAndPort;
private readonly HttpClient _httpClient; private readonly HttpClient _httpClient;
private readonly JsonSerializerSettings _jsonSerializerSettings; private readonly JsonSerializerSettings _jsonSerializerSettings;
private readonly string _baseSchemeUrlAndPort; private readonly string _baseSchemeUrlAndPort;
private BearerToken _token; private BearerToken _token;
private readonly IInternalConfiguration _config; private readonly IInternalConfiguration _config;
private readonly IIdentityServerConfiguration _identityServerConfiguration; private readonly IIdentityServerConfiguration _identityServerConfiguration;
public HttpPeer(string hostAndPort, HttpClient httpClient, IBaseUrlFinder finder, IInternalConfiguration config, IIdentityServerConfiguration identityServerConfiguration) public HttpPeer(string hostAndPort, HttpClient httpClient, IBaseUrlFinder finder, IInternalConfiguration config, IIdentityServerConfiguration identityServerConfiguration)
{ {
_identityServerConfiguration = identityServerConfiguration; _identityServerConfiguration = identityServerConfiguration;
_config = config; _config = config;
Id = hostAndPort; Id = hostAndPort;
_hostAndPort = hostAndPort; _hostAndPort = hostAndPort;
_httpClient = httpClient; _httpClient = httpClient;
_jsonSerializerSettings = new JsonSerializerSettings() { _jsonSerializerSettings = new JsonSerializerSettings() {
TypeNameHandling = TypeNameHandling.All TypeNameHandling = TypeNameHandling.All
}; };
_baseSchemeUrlAndPort = finder.Find(); _baseSchemeUrlAndPort = finder.Find();
} }
public string Id {get; private set;} public string Id {get; private set;}
public RequestVoteResponse Request(RequestVote requestVote) public RequestVoteResponse Request(RequestVote requestVote)
{ {
if(_token == null) if(_token == null)
{ {
SetToken(); SetToken();
} }
var json = JsonConvert.SerializeObject(requestVote, _jsonSerializerSettings); var json = JsonConvert.SerializeObject(requestVote, _jsonSerializerSettings);
var content = new StringContent(json); var content = new StringContent(json);
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
var response = _httpClient.PostAsync($"{_hostAndPort}/administration/raft/requestvote", content).GetAwaiter().GetResult(); var response = _httpClient.PostAsync($"{_hostAndPort}/administration/raft/requestvote", content).GetAwaiter().GetResult();
if(response.IsSuccessStatusCode) if(response.IsSuccessStatusCode)
{ {
return JsonConvert.DeserializeObject<RequestVoteResponse>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult(), _jsonSerializerSettings); return JsonConvert.DeserializeObject<RequestVoteResponse>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult(), _jsonSerializerSettings);
} }
else else
{ {
return new RequestVoteResponse(false, requestVote.Term); return new RequestVoteResponse(false, requestVote.Term);
} }
} }
public AppendEntriesResponse Request(AppendEntries appendEntries) public AppendEntriesResponse Request(AppendEntries appendEntries)
{ {
try try
{ {
if(_token == null) if(_token == null)
{ {
SetToken(); SetToken();
} }
var json = JsonConvert.SerializeObject(appendEntries, _jsonSerializerSettings); var json = JsonConvert.SerializeObject(appendEntries, _jsonSerializerSettings);
var content = new StringContent(json); var content = new StringContent(json);
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
var response = _httpClient.PostAsync($"{_hostAndPort}/administration/raft/appendEntries", content).GetAwaiter().GetResult(); var response = _httpClient.PostAsync($"{_hostAndPort}/administration/raft/appendEntries", content).GetAwaiter().GetResult();
if(response.IsSuccessStatusCode) if(response.IsSuccessStatusCode)
{ {
return JsonConvert.DeserializeObject<AppendEntriesResponse>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult(),_jsonSerializerSettings); return JsonConvert.DeserializeObject<AppendEntriesResponse>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult(),_jsonSerializerSettings);
} }
else else
{ {
return new AppendEntriesResponse(appendEntries.Term, false); return new AppendEntriesResponse(appendEntries.Term, false);
} }
} }
catch(Exception ex) catch(Exception ex)
{ {
Console.WriteLine(ex); Console.WriteLine(ex);
return new AppendEntriesResponse(appendEntries.Term, false); return new AppendEntriesResponse(appendEntries.Term, false);
} }
} }
public Response<T> Request<T>(T command) public Response<T> Request<T>(T command)
where T : ICommand where T : ICommand
{ {
if(_token == null) if(_token == null)
{ {
SetToken(); SetToken();
} }
var json = JsonConvert.SerializeObject(command, _jsonSerializerSettings); var json = JsonConvert.SerializeObject(command, _jsonSerializerSettings);
var content = new StringContent(json); var content = new StringContent(json);
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
var response = _httpClient.PostAsync($"{_hostAndPort}/administration/raft/command", content).GetAwaiter().GetResult(); var response = _httpClient.PostAsync($"{_hostAndPort}/administration/raft/command", content).GetAwaiter().GetResult();
if(response.IsSuccessStatusCode) if(response.IsSuccessStatusCode)
{ {
return JsonConvert.DeserializeObject<OkResponse<T>>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult(), _jsonSerializerSettings); var okResponse = JsonConvert.DeserializeObject<OkResponse<ICommand>>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult(), _jsonSerializerSettings);
} return new OkResponse<T>((T)okResponse.Command);
else }
{ else
return new ErrorResponse<T>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult(), command); {
} return new ErrorResponse<T>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult(), command);
} }
}
private void SetToken()
{ private void SetToken()
var tokenUrl = $"{_baseSchemeUrlAndPort}{_config.AdministrationPath}/connect/token"; {
var formData = new List<KeyValuePair<string, string>> var tokenUrl = $"{_baseSchemeUrlAndPort}{_config.AdministrationPath}/connect/token";
{ var formData = new List<KeyValuePair<string, string>>
new KeyValuePair<string, string>("client_id", _identityServerConfiguration.ApiName), {
new KeyValuePair<string, string>("client_secret", _identityServerConfiguration.ApiSecret), new KeyValuePair<string, string>("client_id", _identityServerConfiguration.ApiName),
new KeyValuePair<string, string>("scope", _identityServerConfiguration.ApiName), new KeyValuePair<string, string>("client_secret", _identityServerConfiguration.ApiSecret),
new KeyValuePair<string, string>("grant_type", "client_credentials") new KeyValuePair<string, string>("scope", _identityServerConfiguration.ApiName),
}; new KeyValuePair<string, string>("grant_type", "client_credentials")
var content = new FormUrlEncodedContent(formData); };
var response = _httpClient.PostAsync(tokenUrl, content).GetAwaiter().GetResult(); var content = new FormUrlEncodedContent(formData);
var responseContent = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); var response = _httpClient.PostAsync(tokenUrl, content).GetAwaiter().GetResult();
response.EnsureSuccessStatusCode(); var responseContent = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
_token = JsonConvert.DeserializeObject<BearerToken>(responseContent); response.EnsureSuccessStatusCode();
_httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(_token.TokenType, _token.AccessToken); _token = JsonConvert.DeserializeObject<BearerToken>(responseContent);
} _httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(_token.TokenType, _token.AccessToken);
} }
} }
}

View File

@ -1,393 +1,431 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Threading; using System.Threading;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json; using Newtonsoft.Json;
using Ocelot.Configuration.File; using Ocelot.Configuration.File;
using Ocelot.Raft; using Ocelot.Raft;
using Rafty.Concensus; using Rafty.Concensus;
using Rafty.Infrastructure; using Rafty.Infrastructure;
using Shouldly; using Shouldly;
using Xunit; using Xunit;
using static Rafty.Infrastructure.Wait; using static Rafty.Infrastructure.Wait;
using Microsoft.Data.Sqlite; using Microsoft.Data.Sqlite;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Ocelot.DependencyInjection; using Ocelot.DependencyInjection;
using Ocelot.Middleware; using Ocelot.Middleware;
namespace Ocelot.IntegrationTests namespace Ocelot.IntegrationTests
{ {
public class RaftTests : IDisposable public class RaftTests : IDisposable
{ {
private readonly List<IWebHost> _builders; private readonly List<IWebHost> _builders;
private readonly List<IWebHostBuilder> _webHostBuilders; private readonly List<IWebHostBuilder> _webHostBuilders;
private readonly List<Thread> _threads; private readonly List<Thread> _threads;
private FilePeers _peers; private FilePeers _peers;
private readonly HttpClient _httpClient; private readonly HttpClient _httpClient;
private readonly HttpClient _httpClientForAssertions; private readonly HttpClient _httpClientForAssertions;
private BearerToken _token; private BearerToken _token;
private HttpResponseMessage _response; private HttpResponseMessage _response;
private static readonly object _lock = new object(); private static readonly object _lock = new object();
public RaftTests() public RaftTests()
{ {
_httpClientForAssertions = new HttpClient(); _httpClientForAssertions = new HttpClient();
_httpClient = new HttpClient(); _httpClient = new HttpClient();
var ocelotBaseUrl = "http://localhost:5000"; var ocelotBaseUrl = "http://localhost:5000";
_httpClient.BaseAddress = new Uri(ocelotBaseUrl); _httpClient.BaseAddress = new Uri(ocelotBaseUrl);
_webHostBuilders = new List<IWebHostBuilder>(); _webHostBuilders = new List<IWebHostBuilder>();
_builders = new List<IWebHost>(); _builders = new List<IWebHost>();
_threads = new List<Thread>(); _threads = new List<Thread>();
} }
public void Dispose() public void Dispose()
{ {
foreach (var builder in _builders) foreach (var builder in _builders)
{ {
builder?.Dispose(); builder?.Dispose();
} }
foreach (var peer in _peers.Peers) foreach (var peer in _peers.Peers)
{ {
File.Delete(peer.HostAndPort.Replace("/","").Replace(":","")); File.Delete(peer.HostAndPort.Replace("/","").Replace(":",""));
File.Delete($"{peer.HostAndPort.Replace("/","").Replace(":","")}.db"); File.Delete($"{peer.HostAndPort.Replace("/","").Replace(":","")}.db");
} }
} }
[Fact(Skip = "This tests is flakey at the moment so ignoring will be fixed long term see https://github.com/TomPallister/Ocelot/issues/245")] [Fact]
public void should_persist_command_to_five_servers() public void should_persist_command_to_five_servers()
{ {
var configuration = new FileConfiguration var configuration = new FileConfiguration
{ {
GlobalConfiguration = new FileGlobalConfiguration GlobalConfiguration = new FileGlobalConfiguration
{ {
} }
}; };
var updatedConfiguration = new FileConfiguration var updatedConfiguration = new FileConfiguration
{ {
GlobalConfiguration = new FileGlobalConfiguration GlobalConfiguration = new FileGlobalConfiguration
{ {
}, },
ReRoutes = new List<FileReRoute>() ReRoutes = new List<FileReRoute>()
{ {
new FileReRoute() new FileReRoute()
{ {
DownstreamHostAndPorts = new List<FileHostAndPort> DownstreamHostAndPorts = new List<FileHostAndPort>
{ {
new FileHostAndPort new FileHostAndPort
{ {
Host = "127.0.0.1", Host = "127.0.0.1",
Port = 80, Port = 80,
} }
}, },
DownstreamScheme = "http", DownstreamScheme = "http",
DownstreamPathTemplate = "/geoffrey", DownstreamPathTemplate = "/geoffrey",
UpstreamHttpMethod = new List<string> { "get" }, UpstreamHttpMethod = new List<string> { "get" },
UpstreamPathTemplate = "/" UpstreamPathTemplate = "/"
}, },
new FileReRoute() new FileReRoute()
{ {
DownstreamHostAndPorts = new List<FileHostAndPort> DownstreamHostAndPorts = new List<FileHostAndPort>
{ {
new FileHostAndPort new FileHostAndPort
{ {
Host = "123.123.123", Host = "123.123.123",
Port = 443, Port = 443,
} }
}, },
DownstreamScheme = "https", DownstreamScheme = "https",
DownstreamPathTemplate = "/blooper/{productId}", DownstreamPathTemplate = "/blooper/{productId}",
UpstreamHttpMethod = new List<string> { "post" }, UpstreamHttpMethod = new List<string> { "post" },
UpstreamPathTemplate = "/test" UpstreamPathTemplate = "/test"
} }
} }
}; };
var command = new UpdateFileConfiguration(updatedConfiguration); var command = new UpdateFileConfiguration(updatedConfiguration);
GivenThereIsAConfiguration(configuration); GivenThereIsAConfiguration(configuration);
GivenFiveServersAreRunning(); GivenFiveServersAreRunning();
GivenALeaderIsElected(); GivenIHaveAnOcelotToken("/administration");
GivenIHaveAnOcelotToken("/administration"); WhenISendACommandIntoTheCluster(command);
WhenISendACommandIntoTheCluster(command); ThenTheCommandIsReplicatedToAllStateMachines(command);
ThenTheCommandIsReplicatedToAllStateMachines(command); }
}
[Fact]
[Fact(Skip = "This tests is flakey at the moment so ignoring will be fixed long term see https://github.com/TomPallister/Ocelot/issues/245")] public void should_persist_command_to_five_servers_when_using_administration_api()
public void should_persist_command_to_five_servers_when_using_administration_api() {
{ var configuration = new FileConfiguration
var configuration = new FileConfiguration {
{ };
};
var updatedConfiguration = new FileConfiguration
var updatedConfiguration = new FileConfiguration {
{ ReRoutes = new List<FileReRoute>()
ReRoutes = new List<FileReRoute>() {
{ new FileReRoute()
new FileReRoute() {
{ DownstreamHostAndPorts = new List<FileHostAndPort>
DownstreamHostAndPorts = new List<FileHostAndPort> {
{ new FileHostAndPort
new FileHostAndPort {
{ Host = "127.0.0.1",
Host = "127.0.0.1", Port = 80,
Port = 80, }
} },
}, DownstreamScheme = "http",
DownstreamScheme = "http", DownstreamPathTemplate = "/geoffrey",
DownstreamPathTemplate = "/geoffrey", UpstreamHttpMethod = new List<string> { "get" },
UpstreamHttpMethod = new List<string> { "get" }, UpstreamPathTemplate = "/"
UpstreamPathTemplate = "/" },
}, new FileReRoute()
new FileReRoute() {
{ DownstreamHostAndPorts = new List<FileHostAndPort>
DownstreamHostAndPorts = new List<FileHostAndPort> {
{ new FileHostAndPort
new FileHostAndPort {
{ Host = "123.123.123",
Host = "123.123.123", Port = 443,
Port = 443, }
} },
}, DownstreamScheme = "https",
DownstreamScheme = "https", DownstreamPathTemplate = "/blooper/{productId}",
DownstreamPathTemplate = "/blooper/{productId}", UpstreamHttpMethod = new List<string> { "post" },
UpstreamHttpMethod = new List<string> { "post" }, UpstreamPathTemplate = "/test"
UpstreamPathTemplate = "/test" }
} }
} };
};
var command = new UpdateFileConfiguration(updatedConfiguration);
var command = new UpdateFileConfiguration(updatedConfiguration); GivenThereIsAConfiguration(configuration);
GivenThereIsAConfiguration(configuration); GivenFiveServersAreRunning();
GivenFiveServersAreRunning(); GivenIHaveAnOcelotToken("/administration");
GivenALeaderIsElected(); GivenIHaveAddedATokenToMyRequest();
GivenIHaveAnOcelotToken("/administration"); WhenIPostOnTheApiGateway("/administration/configuration", updatedConfiguration);
GivenIHaveAddedATokenToMyRequest(); ThenTheCommandIsReplicatedToAllStateMachines(command);
WhenIPostOnTheApiGateway("/administration/configuration", updatedConfiguration); }
ThenTheCommandIsReplicatedToAllStateMachines(command);
} private void WhenISendACommandIntoTheCluster(UpdateFileConfiguration command)
{
private void WhenISendACommandIntoTheCluster(UpdateFileConfiguration command) bool SendCommand()
{ {
var p = _peers.Peers.First(); var p = _peers.Peers.First();
var json = JsonConvert.SerializeObject(command,new JsonSerializerSettings() { var json = JsonConvert.SerializeObject(command,new JsonSerializerSettings() {
TypeNameHandling = TypeNameHandling.All TypeNameHandling = TypeNameHandling.All
}); });
var httpContent = new StringContent(json); var httpContent = new StringContent(json);
httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
using(var httpClient = new HttpClient()) using(var httpClient = new HttpClient())
{ {
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken);
var response = httpClient.PostAsync($"{p.HostAndPort}/administration/raft/command", httpContent).GetAwaiter().GetResult(); var response = httpClient.PostAsync($"{p.HostAndPort}/administration/raft/command", httpContent).GetAwaiter().GetResult();
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();
var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
var result = JsonConvert.DeserializeObject<OkResponse<UpdateFileConfiguration>>(content);
result.Command.Configuration.ReRoutes.Count.ShouldBe(2); var errorResult = JsonConvert.DeserializeObject<ErrorResponse<UpdateFileConfiguration>>(content);
}
if(!string.IsNullOrEmpty(errorResult.Error))
//dirty sleep to make sure command replicated... {
var stopwatch = Stopwatch.StartNew(); return false;
while(stopwatch.ElapsedMilliseconds < 10000) }
{
} var okResult = JsonConvert.DeserializeObject<OkResponse<UpdateFileConfiguration>>(content);
}
if(okResult.Command.Configuration.ReRoutes.Count == 2)
private void ThenTheCommandIsReplicatedToAllStateMachines(UpdateFileConfiguration expecteds) {
{ return true;
//dirty sleep to give a chance to replicate... }
var stopwatch = Stopwatch.StartNew(); }
while(stopwatch.ElapsedMilliseconds < 2000)
{ return false;
} }
bool CommandCalledOnAllStateMachines() var commandSent = WaitFor(20000).Until(() => SendCommand());
{ commandSent.ShouldBeTrue();
try }
{
var passed = 0; private void ThenTheCommandIsReplicatedToAllStateMachines(UpdateFileConfiguration expecteds)
foreach (var peer in _peers.Peers) {
{ bool CommandCalledOnAllStateMachines()
var path = $"{peer.HostAndPort.Replace("/","").Replace(":","")}.db"; {
using(var connection = new SqliteConnection($"Data Source={path};")) try
{ {
connection.Open(); var passed = 0;
var sql = @"select count(id) from logs"; foreach (var peer in _peers.Peers)
using(var command = new SqliteCommand(sql, connection)) {
{ var path = $"{peer.HostAndPort.Replace("/","").Replace(":","")}.db";
var index = Convert.ToInt32(command.ExecuteScalar()); using(var connection = new SqliteConnection($"Data Source={path};"))
index.ShouldBe(1); {
} connection.Open();
} var sql = @"select count(id) from logs";
using(var command = new SqliteCommand(sql, connection))
_httpClientForAssertions.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken); {
var result = _httpClientForAssertions.GetAsync($"{peer.HostAndPort}/administration/configuration").Result; var index = Convert.ToInt32(command.ExecuteScalar());
var json = result.Content.ReadAsStringAsync().Result; index.ShouldBe(1);
var response = JsonConvert.DeserializeObject<FileConfiguration>(json, new JsonSerializerSettings{TypeNameHandling = TypeNameHandling.All}); }
response.GlobalConfiguration.RequestIdKey.ShouldBe(expecteds.Configuration.GlobalConfiguration.RequestIdKey); }
response.GlobalConfiguration.ServiceDiscoveryProvider.Host.ShouldBe(expecteds.Configuration.GlobalConfiguration.ServiceDiscoveryProvider.Host);
response.GlobalConfiguration.ServiceDiscoveryProvider.Port.ShouldBe(expecteds.Configuration.GlobalConfiguration.ServiceDiscoveryProvider.Port); _httpClientForAssertions.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken);
var result = _httpClientForAssertions.GetAsync($"{peer.HostAndPort}/administration/configuration").Result;
for (var i = 0; i < response.ReRoutes.Count; i++) var json = result.Content.ReadAsStringAsync().Result;
{ var response = JsonConvert.DeserializeObject<FileConfiguration>(json, new JsonSerializerSettings{TypeNameHandling = TypeNameHandling.All});
for (var j = 0; j < response.ReRoutes[i].DownstreamHostAndPorts.Count; j++) response.GlobalConfiguration.RequestIdKey.ShouldBe(expecteds.Configuration.GlobalConfiguration.RequestIdKey);
{ response.GlobalConfiguration.ServiceDiscoveryProvider.Host.ShouldBe(expecteds.Configuration.GlobalConfiguration.ServiceDiscoveryProvider.Host);
var res = response.ReRoutes[i].DownstreamHostAndPorts[j]; response.GlobalConfiguration.ServiceDiscoveryProvider.Port.ShouldBe(expecteds.Configuration.GlobalConfiguration.ServiceDiscoveryProvider.Port);
var expected = expecteds.Configuration.ReRoutes[i].DownstreamHostAndPorts[j];
res.Host.ShouldBe(expected.Host); for (var i = 0; i < response.ReRoutes.Count; i++)
res.Port.ShouldBe(expected.Port); {
} for (var j = 0; j < response.ReRoutes[i].DownstreamHostAndPorts.Count; j++)
{
response.ReRoutes[i].DownstreamPathTemplate.ShouldBe(expecteds.Configuration.ReRoutes[i].DownstreamPathTemplate); var res = response.ReRoutes[i].DownstreamHostAndPorts[j];
response.ReRoutes[i].DownstreamScheme.ShouldBe(expecteds.Configuration.ReRoutes[i].DownstreamScheme); var expected = expecteds.Configuration.ReRoutes[i].DownstreamHostAndPorts[j];
response.ReRoutes[i].UpstreamPathTemplate.ShouldBe(expecteds.Configuration.ReRoutes[i].UpstreamPathTemplate); res.Host.ShouldBe(expected.Host);
response.ReRoutes[i].UpstreamHttpMethod.ShouldBe(expecteds.Configuration.ReRoutes[i].UpstreamHttpMethod); res.Port.ShouldBe(expected.Port);
} }
passed++; response.ReRoutes[i].DownstreamPathTemplate.ShouldBe(expecteds.Configuration.ReRoutes[i].DownstreamPathTemplate);
} response.ReRoutes[i].DownstreamScheme.ShouldBe(expecteds.Configuration.ReRoutes[i].DownstreamScheme);
response.ReRoutes[i].UpstreamPathTemplate.ShouldBe(expecteds.Configuration.ReRoutes[i].UpstreamPathTemplate);
return passed == 5; response.ReRoutes[i].UpstreamHttpMethod.ShouldBe(expecteds.Configuration.ReRoutes[i].UpstreamHttpMethod);
} }
catch(Exception e)
{ passed++;
Console.WriteLine(e); }
return false;
} return passed == 5;
} }
catch(Exception e)
var commandOnAllStateMachines = WaitFor(20000).Until(() => CommandCalledOnAllStateMachines()); {
commandOnAllStateMachines.ShouldBeTrue(); Console.WriteLine(e);
} return false;
}
private void WhenIPostOnTheApiGateway(string url, FileConfiguration updatedConfiguration) }
{
var json = JsonConvert.SerializeObject(updatedConfiguration); var commandOnAllStateMachines = WaitFor(20000).Until(() => CommandCalledOnAllStateMachines());
var content = new StringContent(json); commandOnAllStateMachines.ShouldBeTrue();
content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); }
_response = _httpClient.PostAsync(url, content).Result;
} private void WhenIPostOnTheApiGateway(string url, FileConfiguration updatedConfiguration)
{
private void GivenIHaveAddedATokenToMyRequest() bool SendCommand()
{ {
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken); var json = JsonConvert.SerializeObject(updatedConfiguration);
} var content = new StringContent(json);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
private void GivenIHaveAnOcelotToken(string adminPath) _response = _httpClient.PostAsync(url, content).Result;
{ var responseContent = _response.Content.ReadAsStringAsync().Result;
var tokenUrl = $"{adminPath}/connect/token";
var formData = new List<KeyValuePair<string, string>> //Console.ForegroundColor = ConsoleColor.Green;
{ //Console.WriteLine(responseContent);
new KeyValuePair<string, string>("client_id", "admin"), //Console.WriteLine(_response.StatusCode);
new KeyValuePair<string, string>("client_secret", "secret"), //Console.ForegroundColor = ConsoleColor.White;
new KeyValuePair<string, string>("scope", "admin"),
new KeyValuePair<string, string>("grant_type", "client_credentials") if(responseContent == "There was a problem. This error message sucks raise an issue in GitHub.")
}; {
var content = new FormUrlEncodedContent(formData); return false;
}
var response = _httpClient.PostAsync(tokenUrl, content).Result;
var responseContent = response.Content.ReadAsStringAsync().Result; if(string.IsNullOrEmpty(responseContent))
response.EnsureSuccessStatusCode(); {
_token = JsonConvert.DeserializeObject<BearerToken>(responseContent); return false;
var configPath = $"{adminPath}/.well-known/openid-configuration"; }
response = _httpClient.GetAsync(configPath).Result;
response.EnsureSuccessStatusCode(); return _response.IsSuccessStatusCode;
} }
private void GivenThereIsAConfiguration(FileConfiguration fileConfiguration) var commandSent = WaitFor(20000).Until(() => SendCommand());
{ commandSent.ShouldBeTrue();
var configurationPath = $"{Directory.GetCurrentDirectory()}/ocelot.json"; }
var jsonConfiguration = JsonConvert.SerializeObject(fileConfiguration); private void GivenIHaveAddedATokenToMyRequest()
{
if (File.Exists(configurationPath)) _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken);
{ }
File.Delete(configurationPath);
} private void GivenIHaveAnOcelotToken(string adminPath)
{
File.WriteAllText(configurationPath, jsonConfiguration); bool AddToken()
{
var text = File.ReadAllText(configurationPath); try
{
configurationPath = $"{AppContext.BaseDirectory}/ocelot.json"; var tokenUrl = $"{adminPath}/connect/token";
var formData = new List<KeyValuePair<string, string>>
if (File.Exists(configurationPath)) {
{ new KeyValuePair<string, string>("client_id", "admin"),
File.Delete(configurationPath); new KeyValuePair<string, string>("client_secret", "secret"),
} new KeyValuePair<string, string>("scope", "admin"),
new KeyValuePair<string, string>("grant_type", "client_credentials")
File.WriteAllText(configurationPath, jsonConfiguration); };
var content = new FormUrlEncodedContent(formData);
text = File.ReadAllText(configurationPath);
} var response = _httpClient.PostAsync(tokenUrl, content).Result;
var responseContent = response.Content.ReadAsStringAsync().Result;
private void GivenAServerIsRunning(string url) if(!response.IsSuccessStatusCode)
{ {
lock(_lock) return false;
{ }
IWebHostBuilder webHostBuilder = new WebHostBuilder(); _token = JsonConvert.DeserializeObject<BearerToken>(responseContent);
webHostBuilder.UseUrls(url) var configPath = $"{adminPath}/.well-known/openid-configuration";
.UseKestrel() response = _httpClient.GetAsync(configPath).Result;
.UseContentRoot(Directory.GetCurrentDirectory()) return response.IsSuccessStatusCode;
.ConfigureAppConfiguration((hostingContext, config) => }
{ catch(Exception e)
config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath); {
var env = hostingContext.HostingEnvironment; return false;
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) }
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); }
config.AddJsonFile("ocelot.json");
config.AddJsonFile("peers.json", optional: true, reloadOnChange: true); var addToken = WaitFor(20000).Until(() => AddToken());
#pragma warning disable CS0618 addToken.ShouldBeTrue();
config.AddOcelotBaseUrl(url);
#pragma warning restore CS0618 }
config.AddEnvironmentVariables();
}) private void GivenThereIsAConfiguration(FileConfiguration fileConfiguration)
.ConfigureServices(x => {
{ var configurationPath = $"{Directory.GetCurrentDirectory()}/ocelot.json";
x.AddSingleton(new NodeId(url));
x var jsonConfiguration = JsonConvert.SerializeObject(fileConfiguration);
.AddOcelot()
.AddAdministration("/administration", "secret") if (File.Exists(configurationPath))
.AddRafty(); {
}) File.Delete(configurationPath);
.Configure(app => }
{
app.UseOcelot().Wait(); File.WriteAllText(configurationPath, jsonConfiguration);
});
var text = File.ReadAllText(configurationPath);
var builder = webHostBuilder.Build();
builder.Start(); configurationPath = $"{AppContext.BaseDirectory}/ocelot.json";
_webHostBuilders.Add(webHostBuilder); if (File.Exists(configurationPath))
_builders.Add(builder); {
} File.Delete(configurationPath);
} }
private void GivenFiveServersAreRunning() File.WriteAllText(configurationPath, jsonConfiguration);
{
var bytes = File.ReadAllText("peers.json"); text = File.ReadAllText(configurationPath);
_peers = JsonConvert.DeserializeObject<FilePeers>(bytes); }
foreach (var peer in _peers.Peers) private void GivenAServerIsRunning(string url)
{ {
var thread = new Thread(() => GivenAServerIsRunning(peer.HostAndPort)); lock(_lock)
thread.Start(); {
_threads.Add(thread); IWebHostBuilder webHostBuilder = new WebHostBuilder();
} webHostBuilder.UseUrls(url)
} .UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
private void GivenALeaderIsElected() .ConfigureAppConfiguration((hostingContext, config) =>
{ {
//dirty sleep to make sure we have a leader config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath);
var stopwatch = Stopwatch.StartNew(); var env = hostingContext.HostingEnvironment;
while(stopwatch.ElapsedMilliseconds < 20000) config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
{ .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
} config.AddJsonFile("ocelot.json");
} config.AddJsonFile("peers.json", optional: true, reloadOnChange: true);
} #pragma warning disable CS0618
} config.AddOcelotBaseUrl(url);
#pragma warning restore CS0618
config.AddEnvironmentVariables();
})
.ConfigureServices(x =>
{
x.AddSingleton(new NodeId(url));
x
.AddOcelot()
.AddAdministration("/administration", "secret")
.AddRafty();
})
.Configure(app =>
{
app.UseOcelot().Wait();
});
var builder = webHostBuilder.Build();
builder.Start();
_webHostBuilders.Add(webHostBuilder);
_builders.Add(builder);
}
}
private void GivenFiveServersAreRunning()
{
var bytes = File.ReadAllText("peers.json");
_peers = JsonConvert.DeserializeObject<FilePeers>(bytes);
foreach (var peer in _peers.Peers)
{
var thread = new Thread(() => GivenAServerIsRunning(peer.HostAndPort));
thread.Start();
_threads.Add(thread);
}
}
}
}