mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-20 07:22:50 +08:00
Update KubeServiceDiscoveryProviderTests
This commit is contained in:
parent
ae9f05ef5e
commit
84907bd4ce
@ -21,7 +21,7 @@ namespace Ocelot.UnitTests.Kubernetes
|
||||
public class KubeServiceDiscoveryProviderTests : IDisposable
|
||||
{
|
||||
private IWebHost _fakeKubeBuilder;
|
||||
private ServiceV1 _serviceEntries;
|
||||
private EndpointsV1 _endpointEntries;
|
||||
private Kube _provider;
|
||||
private readonly string _serviceName;
|
||||
private readonly string _namespaces;
|
||||
@ -41,7 +41,7 @@ namespace Ocelot.UnitTests.Kubernetes
|
||||
_port = 8001;
|
||||
_kubeHost = "localhost";
|
||||
_fakekubeServiceDiscoveryUrl = $"http://{_kubeHost}:{_port}";
|
||||
_serviceEntries = new ServiceV1();
|
||||
_endpointEntries = new EndpointsV1();
|
||||
_factory = new Mock<IOcelotLoggerFactory>();
|
||||
|
||||
var option = new KubeClientOptions
|
||||
@ -49,7 +49,7 @@ namespace Ocelot.UnitTests.Kubernetes
|
||||
ApiEndPoint = new Uri(_fakekubeServiceDiscoveryUrl),
|
||||
AccessToken = "txpc696iUhbVoudg164r93CxDTrKRVWG",
|
||||
AuthStrategy = KubeClient.KubeAuthStrategy.BearerToken,
|
||||
AllowInsecure = true
|
||||
AllowInsecure = true,
|
||||
};
|
||||
|
||||
_clientFactory = KubeApiClient.Create(option);
|
||||
@ -58,7 +58,7 @@ namespace Ocelot.UnitTests.Kubernetes
|
||||
var config = new KubeRegistryConfiguration()
|
||||
{
|
||||
KeyOfServiceInK8s = _serviceName,
|
||||
KubeNamespace = _namespaces
|
||||
KubeNamespace = _namespaces,
|
||||
};
|
||||
_provider = new Kube(config, _factory.Object, _clientFactory);
|
||||
}
|
||||
@ -67,33 +67,29 @@ namespace Ocelot.UnitTests.Kubernetes
|
||||
public void should_return_service_from_k8s()
|
||||
{
|
||||
var token = "Bearer txpc696iUhbVoudg164r93CxDTrKRVWG";
|
||||
var serviceEntryOne = new ServiceV1()
|
||||
var endPointEntryOne = new EndpointsV1
|
||||
{
|
||||
Kind = "service",
|
||||
Kind = "endpoint",
|
||||
ApiVersion = "1.0",
|
||||
Metadata = new ObjectMetaV1()
|
||||
{
|
||||
Namespace = "dev"
|
||||
Namespace = "dev",
|
||||
},
|
||||
Spec = new ServiceSpecV1()
|
||||
{
|
||||
ClusterIP = "localhost"
|
||||
},
|
||||
Status = new ServiceStatusV1()
|
||||
{
|
||||
LoadBalancer = new LoadBalancerStatusV1()
|
||||
}
|
||||
};
|
||||
|
||||
serviceEntryOne.Spec.Ports.Add(
|
||||
new ServicePortV1()
|
||||
{
|
||||
Port = 80
|
||||
}
|
||||
);
|
||||
var endpointSubsetV1 = new EndpointSubsetV1();
|
||||
endpointSubsetV1.Addresses.Add(new EndpointAddressV1()
|
||||
{
|
||||
Ip = "127.0.0.1",
|
||||
Hostname = "localhost",
|
||||
});
|
||||
endpointSubsetV1.Ports.Add(new EndpointPortV1()
|
||||
{
|
||||
Port = 80,
|
||||
});
|
||||
endPointEntryOne.Subsets.Add(endpointSubsetV1);
|
||||
|
||||
this.Given(x => GivenThereIsAFakeKubeServiceDiscoveryProvider(_fakekubeServiceDiscoveryUrl, _serviceName, _namespaces))
|
||||
.And(x => GivenTheServicesAreRegisteredWithKube(serviceEntryOne))
|
||||
.And(x => GivenTheServicesAreRegisteredWithKube(endPointEntryOne))
|
||||
.When(x => WhenIGetTheServices())
|
||||
.Then(x => ThenTheCountIs(1))
|
||||
.And(_ => _receivedToken.ShouldBe(token))
|
||||
@ -110,9 +106,9 @@ namespace Ocelot.UnitTests.Kubernetes
|
||||
_services = _provider.Get().GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
private void GivenTheServicesAreRegisteredWithKube(ServiceV1 serviceEntries)
|
||||
private void GivenTheServicesAreRegisteredWithKube(EndpointsV1 endpointEntries)
|
||||
{
|
||||
_serviceEntries = serviceEntries;
|
||||
_endpointEntries = endpointEntries;
|
||||
}
|
||||
|
||||
private void GivenThereIsAFakeKubeServiceDiscoveryProvider(string url, string serviceName, string namespaces)
|
||||
@ -127,14 +123,14 @@ namespace Ocelot.UnitTests.Kubernetes
|
||||
{
|
||||
app.Run(async context =>
|
||||
{
|
||||
if (context.Request.Path.Value == $"/api/v1/namespaces/{namespaces}/services/{serviceName}")
|
||||
if (context.Request.Path.Value == $"/api/v1/namespaces/{namespaces}/endpoints/{serviceName}")
|
||||
{
|
||||
if (context.Request.Headers.TryGetValue("Authorization", out var values))
|
||||
{
|
||||
_receivedToken = values.First();
|
||||
}
|
||||
|
||||
var json = JsonConvert.SerializeObject(_serviceEntries);
|
||||
var json = JsonConvert.SerializeObject(_endpointEntries);
|
||||
context.Response.Headers.Add("Content-Type", "application/json");
|
||||
await context.Response.WriteAsync(json);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user