mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-22 02:42:52 +08:00
finally got around to writing some tests for consul file config repo! Should have done these before I wrote it sigh..
This commit is contained in:
parent
5b63f333f7
commit
e9106c30ee
@ -1,137 +1,219 @@
|
|||||||
namespace Ocelot.UnitTests.Configuration
|
namespace Ocelot.UnitTests.Configuration
|
||||||
{
|
{
|
||||||
using Xunit;
|
using Xunit;
|
||||||
using TestStack.BDDfy;
|
using TestStack.BDDfy;
|
||||||
using Shouldly;
|
using Shouldly;
|
||||||
using Ocelot.Configuration.Repository;
|
using Ocelot.Configuration.Repository;
|
||||||
using Moq;
|
using Moq;
|
||||||
using Ocelot.Infrastructure.Consul;
|
using Ocelot.Infrastructure.Consul;
|
||||||
using Ocelot.Logging;
|
using Ocelot.Logging;
|
||||||
using Ocelot.Configuration.File;
|
using Ocelot.Configuration.File;
|
||||||
using Ocelot.Cache;
|
using Ocelot.Cache;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Ocelot.Responses;
|
using Ocelot.Responses;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Ocelot.Configuration;
|
using Ocelot.Configuration;
|
||||||
using Ocelot.Configuration.Builder;
|
using Ocelot.Configuration.Builder;
|
||||||
using Ocelot.ServiceDiscovery.Configuration;
|
using Ocelot.ServiceDiscovery.Configuration;
|
||||||
using Consul;
|
using Consul;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
public class ConsulFileConfigurationRepositoryTests
|
public class ConsulFileConfigurationRepositoryTests
|
||||||
{
|
{
|
||||||
private ConsulFileConfigurationRepository _repo;
|
private ConsulFileConfigurationRepository _repo;
|
||||||
private Mock<IOcelotCache<FileConfiguration>> _cache;
|
private Mock<IOcelotCache<FileConfiguration>> _cache;
|
||||||
private Mock<IInternalConfigurationRepository> _internalRepo;
|
private Mock<IInternalConfigurationRepository> _internalRepo;
|
||||||
private Mock<IConsulClientFactory> _factory;
|
private Mock<IConsulClientFactory> _factory;
|
||||||
private Mock<IOcelotLoggerFactory> _loggerFactory;
|
private Mock<IOcelotLoggerFactory> _loggerFactory;
|
||||||
private Mock<IConsulClient> _client;
|
private Mock<IConsulClient> _client;
|
||||||
private Mock<IKVEndpoint> _kvEndpoint;
|
private Mock<IKVEndpoint> _kvEndpoint;
|
||||||
private FileConfiguration _fileConfiguration;
|
private FileConfiguration _fileConfiguration;
|
||||||
private Response _result;
|
private Response _setResult;
|
||||||
|
private Response<FileConfiguration> _getResult;
|
||||||
public ConsulFileConfigurationRepositoryTests()
|
|
||||||
{
|
public ConsulFileConfigurationRepositoryTests()
|
||||||
_cache = new Mock<IOcelotCache<FileConfiguration>>();
|
{
|
||||||
_internalRepo = new Mock<IInternalConfigurationRepository>();
|
_cache = new Mock<IOcelotCache<FileConfiguration>>();
|
||||||
_loggerFactory = new Mock<IOcelotLoggerFactory>();
|
_internalRepo = new Mock<IInternalConfigurationRepository>();
|
||||||
|
_loggerFactory = new Mock<IOcelotLoggerFactory>();
|
||||||
_factory = new Mock<IConsulClientFactory>();
|
|
||||||
_client = new Mock<IConsulClient>();
|
_factory = new Mock<IConsulClientFactory>();
|
||||||
_kvEndpoint = new Mock<IKVEndpoint>();
|
_client = new Mock<IConsulClient>();
|
||||||
|
_kvEndpoint = new Mock<IKVEndpoint>();
|
||||||
_client
|
|
||||||
.Setup(x => x.KV)
|
_client
|
||||||
.Returns(_kvEndpoint.Object);
|
.Setup(x => x.KV)
|
||||||
_factory
|
.Returns(_kvEndpoint.Object);
|
||||||
.Setup(x => x.Get(It.IsAny<ConsulRegistryConfiguration>()))
|
_factory
|
||||||
.Returns(_client.Object);
|
.Setup(x => x.Get(It.IsAny<ConsulRegistryConfiguration>()))
|
||||||
|
.Returns(_client.Object);
|
||||||
_internalRepo
|
|
||||||
.Setup(x => x.Get())
|
_internalRepo
|
||||||
.Returns(new OkResponse<IInternalConfiguration>(new InternalConfiguration(new List<ReRoute>(), "", new ServiceProviderConfigurationBuilder().Build(), "")));
|
.Setup(x => x.Get())
|
||||||
|
.Returns(new OkResponse<IInternalConfiguration>(new InternalConfiguration(new List<ReRoute>(), "", new ServiceProviderConfigurationBuilder().Build(), "")));
|
||||||
_repo = new ConsulFileConfigurationRepository(_cache.Object, _internalRepo.Object, _factory.Object, _loggerFactory.Object);
|
|
||||||
}
|
_repo = new ConsulFileConfigurationRepository(_cache.Object, _internalRepo.Object, _factory.Object, _loggerFactory.Object);
|
||||||
|
}
|
||||||
[Fact]
|
|
||||||
public void should_set_config()
|
[Fact]
|
||||||
{
|
public void should_set_config()
|
||||||
var config = FakeFileConfiguration();
|
{
|
||||||
|
var config = FakeFileConfiguration();
|
||||||
this.Given(_ => GivenIHaveAConfiguration(config))
|
|
||||||
.And(_ => GivenWritingToConsulSucceeds())
|
this.Given(_ => GivenIHaveAConfiguration(config))
|
||||||
.When(_ => WhenISetTheConfiguration())
|
.And(_ => GivenWritingToConsulSucceeds())
|
||||||
.Then(_ => ThenTheConfigurationIsStoredAs(config))
|
.When(_ => WhenISetTheConfiguration())
|
||||||
.BDDfy();
|
.Then(_ => ThenTheConfigurationIsStoredAs(config))
|
||||||
}
|
.BDDfy();
|
||||||
|
}
|
||||||
private void GivenWritingToConsulSucceeds()
|
|
||||||
{
|
[Fact]
|
||||||
var response = new WriteResult<bool>();
|
public void should_get_config()
|
||||||
response.Response = true;
|
{
|
||||||
|
var config = FakeFileConfiguration();
|
||||||
_kvEndpoint
|
|
||||||
.Setup(x => x.Put(It.IsAny<KVPair>(), It.IsAny<CancellationToken>())).ReturnsAsync(response);
|
this.Given(_ => GivenIHaveAConfiguration(config))
|
||||||
}
|
.And(_ => GivenFetchFromConsulSucceeds())
|
||||||
|
.When(_ => WhenIGetTheConfiguration())
|
||||||
private void ThenTheConfigurationIsStoredAs(FileConfiguration config)
|
.Then(_ => ThenTheConfigurationIs(config))
|
||||||
{
|
.BDDfy();
|
||||||
var json = JsonConvert.SerializeObject(config, Formatting.Indented);
|
}
|
||||||
|
|
||||||
var bytes = Encoding.UTF8.GetBytes(json);
|
[Fact]
|
||||||
|
public void should_get_null_config()
|
||||||
_kvEndpoint
|
{
|
||||||
.Verify(x => x.Put(It.Is<KVPair>(k => k.Value.SequenceEqual(bytes)), It.IsAny<CancellationToken>()), Times.Once);
|
this.Given(_ => GivenFetchFromConsulReturnsNull())
|
||||||
}
|
.When(_ => WhenIGetTheConfiguration())
|
||||||
|
.Then(_ => ThenTheConfigurationIsNull())
|
||||||
private async Task WhenISetTheConfiguration()
|
.BDDfy();
|
||||||
{
|
}
|
||||||
_result = await _repo.Set(_fileConfiguration);
|
|
||||||
}
|
[Fact]
|
||||||
|
public void should_get_config_from_cache()
|
||||||
private void GivenIHaveAConfiguration(FileConfiguration config)
|
{
|
||||||
{
|
var config = FakeFileConfiguration();
|
||||||
_fileConfiguration = config;
|
|
||||||
}
|
this.Given(_ => GivenIHaveAConfiguration(config))
|
||||||
|
.And(_ => GivenFetchFromCacheSucceeds())
|
||||||
private FileConfiguration FakeFileConfiguration()
|
.When(_ => WhenIGetTheConfiguration())
|
||||||
{
|
.Then(_ => ThenTheConfigurationIs(config))
|
||||||
var reRoutes = new List<FileReRoute>
|
.BDDfy();
|
||||||
{
|
}
|
||||||
new FileReRoute
|
|
||||||
{
|
private void ThenTheConfigurationIsNull()
|
||||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
{
|
||||||
{
|
_getResult.Data.ShouldBeNull();
|
||||||
new FileHostAndPort
|
}
|
||||||
{
|
|
||||||
Host = "123.12.12.12",
|
private void ThenTheConfigurationIs(FileConfiguration config)
|
||||||
Port = 80,
|
{
|
||||||
}
|
var expected = JsonConvert.SerializeObject(config, Formatting.Indented);
|
||||||
},
|
var result = JsonConvert.SerializeObject(_getResult.Data, Formatting.Indented);
|
||||||
DownstreamScheme = "https",
|
result.ShouldBe(expected);
|
||||||
DownstreamPathTemplate = "/asdfs/test/{test}"
|
}
|
||||||
}
|
|
||||||
};
|
private async Task WhenIGetTheConfiguration()
|
||||||
|
{
|
||||||
var globalConfiguration = new FileGlobalConfiguration
|
_getResult = await _repo.Get();
|
||||||
{
|
}
|
||||||
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider
|
|
||||||
{
|
private void GivenWritingToConsulSucceeds()
|
||||||
Port = 198,
|
{
|
||||||
Host = "blah"
|
var response = new WriteResult<bool>();
|
||||||
}
|
response.Response = true;
|
||||||
};
|
|
||||||
|
_kvEndpoint
|
||||||
return new FileConfiguration
|
.Setup(x => x.Put(It.IsAny<KVPair>(), It.IsAny<CancellationToken>())).ReturnsAsync(response);
|
||||||
{
|
}
|
||||||
GlobalConfiguration = globalConfiguration,
|
|
||||||
ReRoutes = reRoutes
|
private void GivenFetchFromCacheSucceeds()
|
||||||
};
|
{
|
||||||
}
|
_cache.Setup(x => x.Get(It.IsAny<string>(), It.IsAny<string>())).Returns(_fileConfiguration);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
private void GivenFetchFromConsulReturnsNull()
|
||||||
|
{
|
||||||
|
QueryResult<KVPair> result = new QueryResult<KVPair>();
|
||||||
|
|
||||||
|
_kvEndpoint
|
||||||
|
.Setup(x => x.Get(It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
||||||
|
.ReturnsAsync(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenFetchFromConsulSucceeds()
|
||||||
|
{
|
||||||
|
var json = JsonConvert.SerializeObject(_fileConfiguration, Formatting.Indented);
|
||||||
|
|
||||||
|
var bytes = Encoding.UTF8.GetBytes(json);
|
||||||
|
|
||||||
|
var kvp = new KVPair("OcelotConfiguration");
|
||||||
|
kvp.Value = bytes;
|
||||||
|
|
||||||
|
var query = new QueryResult<KVPair>();
|
||||||
|
query.Response = kvp;
|
||||||
|
|
||||||
|
_kvEndpoint
|
||||||
|
.Setup(x => x.Get(It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
||||||
|
.ReturnsAsync(query);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ThenTheConfigurationIsStoredAs(FileConfiguration config)
|
||||||
|
{
|
||||||
|
var json = JsonConvert.SerializeObject(config, Formatting.Indented);
|
||||||
|
|
||||||
|
var bytes = Encoding.UTF8.GetBytes(json);
|
||||||
|
|
||||||
|
_kvEndpoint
|
||||||
|
.Verify(x => x.Put(It.Is<KVPair>(k => k.Value.SequenceEqual(bytes)), It.IsAny<CancellationToken>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task WhenISetTheConfiguration()
|
||||||
|
{
|
||||||
|
_setResult = await _repo.Set(_fileConfiguration);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenIHaveAConfiguration(FileConfiguration config)
|
||||||
|
{
|
||||||
|
_fileConfiguration = config;
|
||||||
|
}
|
||||||
|
|
||||||
|
private FileConfiguration FakeFileConfiguration()
|
||||||
|
{
|
||||||
|
var reRoutes = new List<FileReRoute>
|
||||||
|
{
|
||||||
|
new FileReRoute
|
||||||
|
{
|
||||||
|
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||||
|
{
|
||||||
|
new FileHostAndPort
|
||||||
|
{
|
||||||
|
Host = "123.12.12.12",
|
||||||
|
Port = 80,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
DownstreamScheme = "https",
|
||||||
|
DownstreamPathTemplate = "/asdfs/test/{test}"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var globalConfiguration = new FileGlobalConfiguration
|
||||||
|
{
|
||||||
|
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider
|
||||||
|
{
|
||||||
|
Port = 198,
|
||||||
|
Host = "blah"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return new FileConfiguration
|
||||||
|
{
|
||||||
|
GlobalConfiguration = globalConfiguration,
|
||||||
|
ReRoutes = reRoutes
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user