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