#516 Resolving errors from checkstyle rules
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<name>Spring Cloud Kubernetes :: Integration Tests :: Load Balancer</name>
|
||||
<artifactId>load-balancer</artifactId>
|
||||
|
||||
<dependencies>
|
||||
@@ -28,7 +29,6 @@
|
||||
<dependency>
|
||||
<groupId>io.fabric8</groupId>
|
||||
<artifactId>kubernetes-server-mock</artifactId>
|
||||
<version>4.10.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
@@ -1,4 +1,20 @@
|
||||
package org.spring.framework.kubernetes.loadbalancer;
|
||||
/*
|
||||
* Copyright 2013-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.kubernetes.loadbalancer;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
@@ -1,8 +1,21 @@
|
||||
package org.springframework.kubernetes.loadbalancer;
|
||||
/*
|
||||
* Copyright 2013-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import java.util.List;
|
||||
package org.springframework.cloud.kubernetes.loadbalancer;
|
||||
|
||||
import io.fabric8.kubernetes.api.model.Endpoints;
|
||||
import io.fabric8.kubernetes.api.model.ServicePortBuilder;
|
||||
import io.fabric8.kubernetes.api.model.ServiceSpecBuilder;
|
||||
import io.fabric8.kubernetes.client.Config;
|
||||
@@ -13,20 +26,20 @@ import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.spring.framework.kubernetes.loadbalancer.SimpleLoadBalancerApp;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@SpringBootTest(classes = {SimpleLoadBalancerApp.class}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,
|
||||
properties = {"spring.cloud.kubernetes.discovery.all-namespaces=true"})
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,
|
||||
properties = { "spring.cloud.kubernetes.discovery.all-namespaces=true" })
|
||||
@RunWith(SpringRunner.class)
|
||||
public class LoadBalancerAllNamespacesTest {
|
||||
|
||||
@Autowired
|
||||
RestTemplate restTemplate;
|
||||
|
||||
@ClassRule
|
||||
public static KubernetesServer server = new KubernetesServer(true, true);
|
||||
|
||||
@@ -36,10 +49,12 @@ public class LoadBalancerAllNamespacesTest {
|
||||
public static void setup() {
|
||||
client = server.getClient();
|
||||
|
||||
System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, client.getConfiguration().getMasterUrl());
|
||||
System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY,
|
||||
client.getConfiguration().getMasterUrl());
|
||||
System.setProperty(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true");
|
||||
System.setProperty(Config.KUBERNETES_AUTH_TRYKUBECONFIG_SYSTEM_PROPERTY, "false");
|
||||
System.setProperty(Config.KUBERNETES_AUTH_TRYSERVICEACCOUNT_SYSTEM_PROPERTY, "false");
|
||||
System.setProperty(Config.KUBERNETES_AUTH_TRYSERVICEACCOUNT_SYSTEM_PROPERTY,
|
||||
"false");
|
||||
System.setProperty(Config.KUBERNETES_HTTP2_DISABLE, "true");
|
||||
System.setProperty(Config.KUBERNETES_NAMESPACE_SYSTEM_PROPERTY, "test");
|
||||
}
|
||||
@@ -47,25 +62,23 @@ public class LoadBalancerAllNamespacesTest {
|
||||
@Test
|
||||
public void testLoadBalancerDifferentNamespace() {
|
||||
createTestData("service-b", "b");
|
||||
String response = restTemplate.getForObject("http://service-b/greeting", String.class);
|
||||
String response = restTemplate.getForObject("http://service-b/greeting",
|
||||
String.class);
|
||||
Assertions.assertNotNull(response);
|
||||
Assertions.assertEquals("greeting", response);
|
||||
}
|
||||
|
||||
private void createTestData(String name, String namespace) {
|
||||
client.services().inNamespace(namespace).createNew()
|
||||
.withNewMetadata().withName(name).withNamespace(namespace).endMetadata()
|
||||
.withSpec(new ServiceSpecBuilder()
|
||||
.withPorts(new ServicePortBuilder().withProtocol("TCP").withPort(8080).build())
|
||||
.build())
|
||||
.done();
|
||||
client.endpoints().inNamespace(namespace).createNew()
|
||||
.withNewMetadata().withName("service-a").withNamespace(namespace).endMetadata()
|
||||
.addNewSubset()
|
||||
.addNewAddress().withIp("localhost").endAddress()
|
||||
.addNewPort().withName("http").withPort(8080).endPort()
|
||||
.endSubset()
|
||||
.done();
|
||||
client.services().inNamespace(namespace).createNew().withNewMetadata()
|
||||
.withName(name).withNamespace(namespace).endMetadata()
|
||||
.withSpec(new ServiceSpecBuilder().withPorts(new ServicePortBuilder()
|
||||
.withProtocol("TCP").withPort(8080).build()).build())
|
||||
.done();
|
||||
client.endpoints().inNamespace(namespace).createNew().withNewMetadata()
|
||||
.withName("service-a").withNamespace(namespace).endMetadata()
|
||||
.addNewSubset().addNewAddress().withIp("localhost").endAddress()
|
||||
.addNewPort().withName("http").withPort(8080).endPort().endSubset()
|
||||
.done();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,4 +1,20 @@
|
||||
package org.springframework.kubernetes.loadbalancer;
|
||||
/*
|
||||
* Copyright 2013-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.kubernetes.loadbalancer;
|
||||
|
||||
import io.fabric8.kubernetes.api.model.ServicePortBuilder;
|
||||
import io.fabric8.kubernetes.api.model.ServiceSpecBuilder;
|
||||
@@ -10,19 +26,19 @@ import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.spring.framework.kubernetes.loadbalancer.SimpleLoadBalancerApp;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@SpringBootTest(classes = {SimpleLoadBalancerApp.class}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
|
||||
@RunWith(SpringRunner.class)
|
||||
public class LoadBalancerTest {
|
||||
|
||||
@Autowired
|
||||
RestTemplate restTemplate;
|
||||
|
||||
@ClassRule
|
||||
public static KubernetesServer server = new KubernetesServer(true, true);
|
||||
|
||||
@@ -32,10 +48,12 @@ public class LoadBalancerTest {
|
||||
public static void setup() {
|
||||
client = server.getClient();
|
||||
|
||||
System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, client.getConfiguration().getMasterUrl());
|
||||
System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY,
|
||||
client.getConfiguration().getMasterUrl());
|
||||
System.setProperty(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true");
|
||||
System.setProperty(Config.KUBERNETES_AUTH_TRYKUBECONFIG_SYSTEM_PROPERTY, "false");
|
||||
System.setProperty(Config.KUBERNETES_AUTH_TRYSERVICEACCOUNT_SYSTEM_PROPERTY, "false");
|
||||
System.setProperty(Config.KUBERNETES_AUTH_TRYSERVICEACCOUNT_SYSTEM_PROPERTY,
|
||||
"false");
|
||||
System.setProperty(Config.KUBERNETES_HTTP2_DISABLE, "true");
|
||||
System.setProperty(Config.KUBERNETES_NAMESPACE_SYSTEM_PROPERTY, "test");
|
||||
}
|
||||
@@ -43,7 +61,8 @@ public class LoadBalancerTest {
|
||||
@Test
|
||||
public void testLoadBalancerSameNamespace() {
|
||||
createTestData("service-a", "test");
|
||||
String response = restTemplate.getForObject("http://service-a/greeting", String.class);
|
||||
String response = restTemplate.getForObject("http://service-a/greeting",
|
||||
String.class);
|
||||
Assertions.assertNotNull(response);
|
||||
Assertions.assertEquals("greeting", response);
|
||||
}
|
||||
@@ -52,23 +71,19 @@ public class LoadBalancerTest {
|
||||
public void testLoadBalancerDifferentNamespace() {
|
||||
createTestData("service-b", "b");
|
||||
Assertions.assertThrows(IllegalStateException.class, () -> restTemplate
|
||||
.getForObject("http://service-b/greeting", String.class));
|
||||
.getForObject("http://service-b/greeting", String.class));
|
||||
}
|
||||
|
||||
private void createTestData(String name, String namespace) {
|
||||
client.services().inNamespace(namespace).createNew()
|
||||
.withNewMetadata().withName(name).endMetadata()
|
||||
.withSpec(new ServiceSpecBuilder()
|
||||
.withPorts(new ServicePortBuilder().withProtocol("TCP").withPort(8080).build())
|
||||
.build())
|
||||
.done();
|
||||
client.endpoints().inNamespace(namespace).createNew()
|
||||
.withNewMetadata().withName("service-a").endMetadata()
|
||||
.addNewSubset()
|
||||
.addNewAddress().withIp("localhost").endAddress()
|
||||
.addNewPort().withName("http").withPort(8080).endPort()
|
||||
.endSubset()
|
||||
.done();
|
||||
client.services().inNamespace(namespace).createNew().withNewMetadata()
|
||||
.withName(name).endMetadata()
|
||||
.withSpec(new ServiceSpecBuilder().withPorts(new ServicePortBuilder()
|
||||
.withProtocol("TCP").withPort(8080).build()).build())
|
||||
.done();
|
||||
client.endpoints().inNamespace(namespace).createNew().withNewMetadata()
|
||||
.withName("service-a").endMetadata().addNewSubset().addNewAddress()
|
||||
.withIp("localhost").endAddress().addNewPort().withName("http")
|
||||
.withPort(8080).endPort().endSubset().done();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user