Skip to content

Commit

Permalink
provide tests for HttpContext.RequestsServices approach
Browse files Browse the repository at this point in the history
  • Loading branch information
kick2nick committed Oct 23, 2024
1 parent ce0227c commit bc2f7e6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 23 deletions.
27 changes: 15 additions & 12 deletions src/Ocelot.Provider.Kubernetes/KubernetesProviderFactory.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Ocelot.Configuration;
using Ocelot.Logging;
using Ocelot.Logging;
using Ocelot.Provider.Kubernetes.Interfaces;

namespace Ocelot.Provider.Kubernetes
{
public static class KubernetesProviderFactory
{
/// <summary>
/// String constant used for provider type definition.
{
/// <summary>
/// String constant used for provider type definition.
/// </summary>
public const string PollKube = nameof(Kubernetes.PollKube);

public static ServiceDiscoveryFinderDelegate Get { get; } = CreateProvider;

public static ServiceDiscoveryFinderDelegate Get { get; } = CreateProvider;

private static IServiceDiscoveryProvider CreateProvider(IServiceProvider provider, ServiceProviderConfiguration config, DownstreamRoute route)
{
var factory = provider.GetService<IOcelotLoggerFactory>();
var kubeClient = provider.GetService<IKubeApiClient>();
var kubeClient = provider
.GetRequiredService<IHttpContextAccessor>()
.HttpContext!.RequestServices.GetRequiredService<IKubeApiClient>();
var serviceBuilder = provider.GetService<IKubeServiceBuilder>();

var configuration = new KubeRegistryConfiguration
Expand All @@ -28,10 +31,10 @@ private static IServiceDiscoveryProvider CreateProvider(IServiceProvider provide
};

var defaultK8sProvider = new Kube(configuration, factory, kubeClient, serviceBuilder);

return PollKube.Equals(config.Type, StringComparison.OrdinalIgnoreCase)
? new PollKube(config.PollingInterval, factory, defaultK8sProvider)
: defaultK8sProvider;

return PollKube.Equals(config.Type, StringComparison.OrdinalIgnoreCase)
? new PollKube(config.PollingInterval, factory, defaultK8sProvider)
: defaultK8sProvider;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,20 @@ namespace Ocelot.AcceptanceTests.ServiceDiscovery;
public sealed class KubernetesServiceDiscoveryTests : ConcurrentSteps, IDisposable
{
private readonly string _kubernetesUrl;
private readonly IKubeApiClient _clientFactory;
private readonly Action<KubeClientOptions> _optionsConfigure;
private readonly ServiceHandler _kubernetesHandler;
private string _receivedToken;

public KubernetesServiceDiscoveryTests()
{
_kubernetesUrl = DownstreamUrl(PortFinder.GetRandomPort());
var option = new KubeClientOptions
_optionsConfigure = opts =>
{
ApiEndPoint = new Uri(_kubernetesUrl),
AccessToken = "txpc696iUhbVoudg164r93CxDTrKRVWG",
AuthStrategy = KubeAuthStrategy.BearerToken,
AllowInsecure = true,
opts.ApiEndPoint = new Uri(_kubernetesUrl);
opts.AccessToken = "txpc696iUhbVoudg164r93CxDTrKRVWG";
opts.AuthStrategy = KubeAuthStrategy.BearerToken;
opts.AllowInsecure = true;
};
_clientFactory = KubeApiClient.Create(option);
_kubernetesHandler = new();
}

Expand All @@ -45,6 +44,29 @@ public override void Dispose()
_kubernetesHandler.Dispose();
base.Dispose();
}

[Fact]
public void Should_Fail_ReturnServicesFromK8s_OnSecondRequest()
{
const string namespaces = nameof(KubernetesServiceDiscoveryTests);
const string serviceName = nameof(ShouldReturnServicesFromK8s);
var servicePort = PortFinder.GetRandomPort();
var downstreamUrl = LoopbackLocalhostUrl(servicePort);
var downstream = new Uri(downstreamUrl);
var subsetV1 = GivenSubsetAddress(downstream);
var endpoints = GivenEndpoints(subsetV1);
var route = GivenRouteWithServiceName(namespaces);
var configuration = GivenKubeConfiguration(namespaces, route);
var downstreamResponse = serviceName;
this.Given(x => GivenServiceInstanceIsRunning(downstreamUrl, downstreamResponse))
.And(x => x.GivenThereIsAFakeKubernetesProvider(endpoints, serviceName, namespaces))
.And(_ => GivenThereIsAConfiguration(configuration))
.And(_ => GivenOcelotIsRunningWithServices(WithKubernetes))
.When(_ => WhenIGetUrlOnTheApiGateway("/"))
.When(_ => WhenIGetUrlOnTheApiGateway("/"))
.Then(_ => ThenTheStatusCodeShouldBe(HttpStatusCode.InternalServerError))
.BDDfy();
}

[Fact]
public void ShouldReturnServicesFromK8s()
Expand Down Expand Up @@ -311,14 +333,14 @@ private void GivenThereIsAFakeKubernetesProvider(EndpointsV1 endpoints, bool isS
}

private void WithKubernetes(IServiceCollection services) => services
.AddOcelot().AddKubernetes()
.Services.RemoveAll<IKubeApiClient>().AddSingleton(_clientFactory);
.AddOcelot().AddKubernetes(false)
.Services.Configure(_optionsConfigure);

private void WithKubernetesAndRoundRobin(IServiceCollection services) => services
.AddOcelot().AddKubernetes()
.AddOcelot().AddKubernetes(false)
.AddCustomLoadBalancer<RoundRobinAnalyzer>(GetRoundRobinAnalyzer)
.Services
.RemoveAll<IKubeApiClient>().AddSingleton(_clientFactory)
.Configure(_optionsConfigure)
.RemoveAll<IKubeServiceCreator>().AddSingleton<IKubeServiceCreator, FakeKubeServiceCreator>();

private int _k8sCounter, _k8sServiceGeneration;
Expand Down

0 comments on commit bc2f7e6

Please sign in to comment.