Skip to content

Commit

Permalink
Fix unit tests for ConsulServiceDiscoveryProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
raman-m committed Sep 30, 2023
1 parent 78e261d commit 9f560b0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 44 deletions.
10 changes: 5 additions & 5 deletions src/Ocelot.Provider.Consul/Consul.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public Consul(ConsulRegistryConfiguration config, IOcelotLoggerFactory factory,
_logger = factory.CreateLogger<Consul>();
}

public const string ServiceValidationWarningFormat = "Unable to use service address: '{0}' and port: {1} as it is invalid for the service: '{2}'. Address must contain host only e.g. 'localhost', and port must be greater than 0.";

public async Task<List<Service>> GetAsync()
{
var queryResult = await _consul.Health.Service(_config.KeyOfServiceInConsul, string.Empty, true);
Expand All @@ -27,8 +29,7 @@ public async Task<List<Service>> GetAsync()

foreach (var serviceEntry in queryResult.Response)
{
var address = serviceEntry.Service.Address;
var port = serviceEntry.Service.Port;
var service = serviceEntry.Service;
if (IsValid(serviceEntry))
{
var nodes = await _consul.Catalog.Nodes();
Expand All @@ -38,14 +39,13 @@ public async Task<List<Service>> GetAsync()
}
else
{
var serviceNode = nodes.Response.FirstOrDefault(n => n.Address == address);
var serviceNode = nodes.Response.FirstOrDefault(n => n.Address == service.Address);
services.Add(BuildService(serviceEntry, serviceNode));
}
}
else
{
_logger.LogWarning(
$"Unable to use service address: {address} and Port: {port} as it is invalid. Address must contain host only e.g. 'localhost', and port must be greater than 0.");
_logger.LogWarning(string.Format(ServiceValidationWarningFormat, service.Address, service.Port, service.Service));
}
}

Expand Down
49 changes: 10 additions & 39 deletions test/Ocelot.UnitTests/Consul/ConsulServiceDiscoveryProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void should_not_return_services_with_invalid_address()
.And(x => GivenTheServicesAreRegisteredWithConsul(serviceEntryOne, serviceEntryTwo))
.When(x => WhenIGetTheServices())
.Then(x => ThenTheCountIs(0))
.And(x => ThenTheLoggerHasBeenCalledCorrectlyForInvalidAddress())
.And(x => ThenTheLoggerHasBeenCalledCorrectlyWithValidationWarning(serviceEntryOne, serviceEntryTwo))
.BDDfy();
}

Expand Down Expand Up @@ -158,7 +158,7 @@ public void should_not_return_services_with_empty_address()
.And(x => GivenTheServicesAreRegisteredWithConsul(serviceEntryOne, serviceEntryTwo))
.When(x => WhenIGetTheServices())
.Then(x => ThenTheCountIs(0))
.And(x => ThenTheLoggerHasBeenCalledCorrectlyForEmptyAddress())
.And(x => ThenTheLoggerHasBeenCalledCorrectlyWithValidationWarning(serviceEntryOne, serviceEntryTwo))
.BDDfy();
}

Expand Down Expand Up @@ -193,47 +193,18 @@ public void should_not_return_services_with_invalid_port()
.And(x => GivenTheServicesAreRegisteredWithConsul(serviceEntryOne, serviceEntryTwo))
.When(x => WhenIGetTheServices())
.Then(x => ThenTheCountIs(0))
.And(x => ThenTheLoggerHasBeenCalledCorrectlyForInvalidPorts())
.And(x => ThenTheLoggerHasBeenCalledCorrectlyWithValidationWarning(serviceEntryOne, serviceEntryTwo))
.BDDfy();
}

private void ThenTheLoggerHasBeenCalledCorrectlyForInvalidAddress()
private void ThenTheLoggerHasBeenCalledCorrectlyWithValidationWarning(params ServiceEntry[] serviceEntries)
{
_logger.Verify(
x => x.LogWarning(
"Unable to use service Address: http://localhost and Port: 50881 as it is invalid. Address must contain host only e.g. localhost and port must be greater than 0"),
Times.Once);

_logger.Verify(
x => x.LogWarning(
"Unable to use service Address: http://localhost and Port: 50888 as it is invalid. Address must contain host only e.g. localhost and port must be greater than 0"),
Times.Once);
}

private void ThenTheLoggerHasBeenCalledCorrectlyForEmptyAddress()
{
_logger.Verify(
x => x.LogWarning(
"Unable to use service Address: and Port: 50881 as it is invalid. Address must contain host only e.g. localhost and port must be greater than 0"),
Times.Once);

_logger.Verify(
x => x.LogWarning(
"Unable to use service Address: and Port: 50888 as it is invalid. Address must contain host only e.g. localhost and port must be greater than 0"),
Times.Once);
}

private void ThenTheLoggerHasBeenCalledCorrectlyForInvalidPorts()
{
_logger.Verify(
x => x.LogWarning(
"Unable to use service Address: localhost and Port: -1 as it is invalid. Address must contain host only e.g. localhost and port must be greater than 0"),
Times.Once);

_logger.Verify(
x => x.LogWarning(
"Unable to use service Address: localhost and Port: 0 as it is invalid. Address must contain host only e.g. localhost and port must be greater than 0"),
Times.Once);
foreach (var entry in serviceEntries)
{
var service = entry.Service;
var expected = string.Format(ConsulProvider.ServiceValidationWarningFormat, service.Address, service.Port, service.Service);
_logger.Verify(x => x.LogWarning(expected), Times.Once);
}
}

private void ThenTheCountIs(int count)
Expand Down

0 comments on commit 9f560b0

Please sign in to comment.