-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…y providers (#2180) * Fixed Kube/PollKube service provider scope validation * fix pollkube tests * remove BDDfy * Fix tests * Bump KubeClient to 2.5.12 * Code review by @raman-m * no Chinese tests now! * Cover by unit tests * Validate DI scopes in acceptance tests --------- Co-authored-by: kick2nick <[email protected]> Co-authored-by: Raman Maksimchuk <[email protected]>
- Loading branch information
1 parent
1cf5597
commit 8b633af
Showing
10 changed files
with
196 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
test/Ocelot.UnitTests/Kubernetes/KubernetesProviderFactoryTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using KubeClient; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
using Ocelot.Configuration; | ||
using Ocelot.Configuration.Builder; | ||
using Ocelot.DependencyInjection; | ||
using Ocelot.Provider.Kubernetes; | ||
using Ocelot.Provider.Kubernetes.Interfaces; | ||
using Ocelot.ServiceDiscovery; | ||
using Ocelot.ServiceDiscovery.Providers; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace Ocelot.UnitTests.Kubernetes; | ||
|
||
public class KubernetesProviderFactoryTests : UnitTest | ||
{ | ||
private readonly IOcelotBuilder _builder; | ||
|
||
public KubernetesProviderFactoryTests() | ||
{ | ||
_builder = new ServiceCollection() | ||
.AddOcelot().AddKubernetes(); | ||
} | ||
|
||
[Theory] | ||
[Trait("Bug", "977")] | ||
[InlineData(typeof(Kube))] | ||
[InlineData(typeof(PollKube))] | ||
public void CreateProvider_IKubeApiClientHasOriginalLifetimeWithEnabledScopesValidation_ShouldResolveProvider(Type providerType) | ||
{ | ||
// Arrange | ||
var kubeClient = new Mock<IKubeApiClient>(); | ||
kubeClient.Setup(x => x.ResourceClient(It.IsAny<Func<IKubeApiClient, IEndPointClient>>())) | ||
.Returns(Mock.Of<IEndPointClient>()); | ||
var descriptor = _builder.Services.First(x => x.ServiceType == typeof(IKubeApiClient)); | ||
_builder.Services.Replace(ServiceDescriptor.Describe(descriptor.ServiceType, _ => kubeClient.Object, descriptor.Lifetime)); | ||
var serviceProvider = _builder.Services.BuildServiceProvider(validateScopes: true); | ||
var config = GivenServiceProvider(providerType.Name); | ||
var route = GivenRoute(); | ||
|
||
// Act | ||
IServiceDiscoveryProvider actual = null; | ||
var resolving = () => actual = serviceProvider | ||
.GetRequiredService<ServiceDiscoveryFinderDelegate>() // returns KubernetesProviderFactory.Get instance | ||
.Invoke(serviceProvider, config, route); | ||
|
||
// Assert | ||
resolving.ShouldNotThrow(); | ||
actual.ShouldNotBeNull().ShouldBeOfType(providerType); | ||
} | ||
|
||
[Theory] | ||
[Trait("Bug", "977")] | ||
[InlineData(nameof(Kube))] | ||
[InlineData(nameof(PollKube))] | ||
public void CreateProvider_IKubeApiClientHasScopedLifetimeWithEnabledScopesValidation_ShouldFailToResolve(string providerType) | ||
{ | ||
// Arrange | ||
var descriptor = ServiceDescriptor.Describe(typeof(IKubeApiClient), _ => Mock.Of<IKubeApiClient>(), ServiceLifetime.Scoped); | ||
_builder.Services.Replace(descriptor); | ||
var serviceProvider = _builder.Services.BuildServiceProvider(validateScopes: true); | ||
var config = GivenServiceProvider(providerType); | ||
var route = GivenRoute(); | ||
|
||
// Act | ||
IServiceDiscoveryProvider actual = null; | ||
var resolving = () => actual = serviceProvider | ||
.GetRequiredService<ServiceDiscoveryFinderDelegate>() // returns KubernetesProviderFactory.Get instance | ||
.Invoke(serviceProvider, config, route); | ||
|
||
// Assert | ||
var ex = resolving.ShouldThrow<InvalidOperationException>(); | ||
ex.Message.ShouldContain("Cannot resolve scoped service 'KubeClient.IKubeApiClient' from root provider"); | ||
actual.ShouldBeNull(); | ||
} | ||
|
||
private static ServiceProviderConfiguration GivenServiceProvider(string type) => new( | ||
type: type, | ||
scheme: string.Empty, | ||
host: string.Empty, | ||
port: 1, | ||
token: string.Empty, | ||
configurationKey: string.Empty, | ||
pollingInterval: 1); | ||
|
||
private static DownstreamRoute GivenRoute([CallerMemberName] string serviceName = "test-service") | ||
=> new DownstreamRouteBuilder().WithServiceName(serviceName).Build(); | ||
} |
Oops, something went wrong.