Skip to content

Commit 5dfe860

Browse files
authored
Merge pull request #2093 from spring-cloud/fixed-ports
Remove use of fixed ports for wiremock
2 parents ab76b82 + 1553748 commit 5dfe860

File tree

19 files changed

+73
-54
lines changed

19 files changed

+73
-54
lines changed

spring-cloud-kubernetes-client-loadbalancer/src/test/java/org/springframework/cloud/kubernetes/client/loadbalancer/it/mode/cache/CacheDisabledTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.springframework.cloud.kubernetes.commons.loadbalancer.KubernetesServiceInstanceMapper;
3939
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
4040
import org.springframework.test.annotation.DirtiesContext;
41+
import org.springframework.test.util.TestSocketUtils;
4142

4243
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
4344
import static org.assertj.core.api.Assertions.assertThat;
@@ -53,7 +54,7 @@
5354
@DirtiesContext
5455
class CacheDisabledTest {
5556

56-
private static final int SERVICE_PORT = 8888;
57+
private static final int SERVICE_PORT = TestSocketUtils.findAvailableTcpPort();
5758

5859
private static WireMockServer wireMockServer;
5960

@@ -81,7 +82,7 @@ static void beforeAll() {
8182
serviceAMockServer.start();
8283
WireMock.configureFor("localhost", SERVICE_PORT);
8384

84-
// we mock host creation so that it becomes something like : localhost:8888
85+
// we mock host creation so that it becomes something like : localhost:<port>
8586
// then wiremock can catch this request, and we can assert for the result
8687
MOCKED_STATIC.when(() -> KubernetesServiceInstanceMapper.createHost("my-service", "a", "cluster.local"))
8788
.thenReturn("localhost");

spring-cloud-kubernetes-client-loadbalancer/src/test/java/org/springframework/cloud/kubernetes/client/loadbalancer/it/mode/cache/CacheEnabledOutsideTTLTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.springframework.cloud.kubernetes.commons.loadbalancer.KubernetesServiceInstanceMapper;
3939
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
4040
import org.springframework.test.annotation.DirtiesContext;
41+
import org.springframework.test.util.TestSocketUtils;
4142

4243
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
4344
import static org.assertj.core.api.Assertions.assertThat;
@@ -54,7 +55,7 @@
5455
@DirtiesContext
5556
class CacheEnabledOutsideTTLTest {
5657

57-
private static final int SERVICE_PORT = 8888;
58+
private static final int SERVICE_PORT = TestSocketUtils.findAvailableTcpPort();
5859

5960
private static WireMockServer wireMockServer;
6061

@@ -82,7 +83,7 @@ static void beforeAll() {
8283
serviceAMockServer.start();
8384
WireMock.configureFor("localhost", SERVICE_PORT);
8485

85-
// we mock host creation so that it becomes something like : localhost:8888
86+
// we mock host creation so that it becomes something like : localhost:<port>
8687
// then wiremock can catch this request, and we can assert for the result
8788
MOCKED_STATIC.when(() -> KubernetesServiceInstanceMapper.createHost("my-service", "a", "cluster.local"))
8889
.thenReturn("localhost");

spring-cloud-kubernetes-client-loadbalancer/src/test/java/org/springframework/cloud/kubernetes/client/loadbalancer/it/mode/cache/CacheEnabledWithinTTLTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.springframework.cloud.kubernetes.commons.loadbalancer.KubernetesServiceInstanceMapper;
3939
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
4040
import org.springframework.test.annotation.DirtiesContext;
41+
import org.springframework.test.util.TestSocketUtils;
4142

4243
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
4344
import static org.assertj.core.api.Assertions.assertThat;
@@ -54,7 +55,7 @@
5455
@DirtiesContext
5556
class CacheEnabledWithinTTLTest {
5657

57-
private static final int SERVICE_PORT = 8888;
58+
private static final int SERVICE_PORT = TestSocketUtils.findAvailableTcpPort();
5859

5960
private static WireMockServer wireMockServer;
6061

@@ -82,7 +83,7 @@ static void beforeAll() {
8283
serviceAMockServer.start();
8384
WireMock.configureFor("localhost", SERVICE_PORT);
8485

85-
// we mock host creation so that it becomes something like : localhost:8888
86+
// we mock host creation so that it becomes something like : localhost:<port>
8687
// then wiremock can catch this request, and we can assert for the result
8788
MOCKED_STATIC.when(() -> KubernetesServiceInstanceMapper.createHost("my-service", "a", "cluster.local"))
8889
.thenReturn("localhost");

spring-cloud-kubernetes-client-loadbalancer/src/test/java/org/springframework/cloud/kubernetes/client/loadbalancer/it/mode/pod/AllNamespacesTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
4747
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
4848
import org.springframework.http.HttpMethod;
49+
import org.springframework.test.util.TestSocketUtils;
4950
import org.springframework.web.reactive.function.client.WebClient;
5051

5152
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
@@ -64,9 +65,9 @@ class AllNamespacesTest {
6465

6566
private static final String SERVICE_B_URL = "http://service-b";
6667

67-
private static final int SERVICE_A_PORT = 8888;
68+
private static final int SERVICE_A_PORT = TestSocketUtils.findAvailableTcpPort();
6869

69-
private static final int SERVICE_B_PORT = 8889;
70+
private static final int SERVICE_B_PORT = TestSocketUtils.findAvailableTcpPort();
7071

7172
private static WireMockServer wireMockServer;
7273

@@ -102,7 +103,7 @@ static void beforeAll() {
102103
serviceBMockServer.start();
103104
WireMock.configureFor("localhost", SERVICE_B_PORT);
104105

105-
// we mock host creation so that it becomes something like : localhost:8888
106+
// we mock host creation so that it becomes something like : localhost:<port>
106107
// then wiremock can catch this request, and we can assert for the result
107108
MOCKED_STATIC.when(() -> KubernetesServiceInstanceMapper.createHost("service-a", "a", "cluster.local"))
108109
.thenReturn("localhost");
@@ -127,8 +128,8 @@ static void afterAll() {
127128

128129
/**
129130
* <pre>
130-
* - service-a is present in namespace a with exposed port 8888
131-
* - service-b is present in namespace b with exposed port 8889
131+
* - service-a is present in namespace a with exposed port
132+
* - service-b is present in namespace b with exposed port
132133
* - we make two calls to them via the load balancer
133134
* </pre>
134135
*/

spring-cloud-kubernetes-client-loadbalancer/src/test/java/org/springframework/cloud/kubernetes/client/loadbalancer/it/mode/pod/SelectiveNamespacesTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
4747
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
4848
import org.springframework.http.HttpMethod;
49+
import org.springframework.test.util.TestSocketUtils;
4950
import org.springframework.web.reactive.function.client.WebClient;
5051

5152
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
@@ -62,11 +63,11 @@ class SelectiveNamespacesTest {
6263

6364
private static final String MY_SERVICE_URL = "http://my-service";
6465

65-
private static final int SERVICE_A_PORT = 8887;
66+
private static final int SERVICE_A_PORT = TestSocketUtils.findAvailableTcpPort();
6667

67-
private static final int SERVICE_B_PORT = 8888;
68+
private static final int SERVICE_B_PORT = TestSocketUtils.findAvailableTcpPort();
6869

69-
private static final int SERVICE_C_PORT = 8889;
70+
private static final int SERVICE_C_PORT = TestSocketUtils.findAvailableTcpPort();
7071

7172
private static WireMockServer wireMockServer;
7273

@@ -108,7 +109,7 @@ static void beforeAll() {
108109
serviceCMockServer.start();
109110
WireMock.configureFor("localhost", SERVICE_C_PORT);
110111

111-
// we mock host creation so that it becomes something like : localhost:8888
112+
// we mock host creation so that it becomes something like : localhost:<port>
112113
// then wiremock can catch this request, and we can assert for the result
113114
MOCKED_STATIC.when(() -> KubernetesServiceInstanceMapper.createHost("my-service", "a", "cluster.local"))
114115
.thenReturn("localhost");

spring-cloud-kubernetes-client-loadbalancer/src/test/java/org/springframework/cloud/kubernetes/client/loadbalancer/it/mode/pod/SpecificNamespaceTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
4747
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
4848
import org.springframework.http.HttpMethod;
49+
import org.springframework.test.util.TestSocketUtils;
4950
import org.springframework.web.reactive.function.client.WebClient;
5051

5152
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
@@ -61,9 +62,9 @@ class SpecificNamespaceTest {
6162

6263
private static final String SERVICE_A_URL = "http://my-service";
6364

64-
private static final int SERVICE_A_PORT = 8888;
65+
private static final int SERVICE_A_PORT = TestSocketUtils.findAvailableTcpPort();
6566

66-
private static final int SERVICE_B_PORT = 8889;
67+
private static final int SERVICE_B_PORT = TestSocketUtils.findAvailableTcpPort();
6768

6869
private static WireMockServer wireMockServer;
6970

@@ -99,7 +100,7 @@ static void beforeAll() {
99100
serviceBMockServer.start();
100101
WireMock.configureFor("localhost", SERVICE_B_PORT);
101102

102-
// we mock host creation so that it becomes something like : localhost:8888
103+
// we mock host creation so that it becomes something like : localhost:<port>
103104
// then wiremock can catch this request, and we can assert for the result
104105
MOCKED_STATIC.when(() -> KubernetesServiceInstanceMapper.createHost("my-service", "a", "cluster.local"))
105106
.thenReturn("localhost");

spring-cloud-kubernetes-client-loadbalancer/src/test/java/org/springframework/cloud/kubernetes/client/loadbalancer/it/mode/service/AllNamespacesTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
4747
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
4848
import org.springframework.http.HttpMethod;
49+
import org.springframework.test.util.TestSocketUtils;
4950
import org.springframework.web.reactive.function.client.WebClient;
5051

5152
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
@@ -64,9 +65,9 @@ class AllNamespacesTest {
6465

6566
private static final String SERVICE_B_URL = "http://service-b";
6667

67-
private static final int SERVICE_A_PORT = 8888;
68+
private static final int SERVICE_A_PORT = TestSocketUtils.findAvailableTcpPort();
6869

69-
private static final int SERVICE_B_PORT = 8889;
70+
private static final int SERVICE_B_PORT = TestSocketUtils.findAvailableTcpPort();
7071

7172
private static WireMockServer wireMockServer;
7273

@@ -102,7 +103,7 @@ static void beforeAll() {
102103
serviceBMockServer.start();
103104
WireMock.configureFor("localhost", SERVICE_B_PORT);
104105

105-
// we mock host creation so that it becomes something like : localhost:8888
106+
// we mock host creation so that it becomes something like : localhost:<port>
106107
// then wiremock can catch this request, and we can assert for the result
107108
MOCKED_STATIC.when(() -> KubernetesServiceInstanceMapper.createHost("service-a", "a", "cluster.local"))
108109
.thenReturn("localhost");

spring-cloud-kubernetes-client-loadbalancer/src/test/java/org/springframework/cloud/kubernetes/client/loadbalancer/it/mode/service/SelectiveNamespacesTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
4747
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
4848
import org.springframework.http.HttpMethod;
49+
import org.springframework.test.util.TestSocketUtils;
4950
import org.springframework.web.reactive.function.client.WebClient;
5051

5152
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
@@ -62,11 +63,11 @@ class SelectiveNamespacesTest {
6263

6364
private static final String MY_SERVICE_URL = "http://my-service";
6465

65-
private static final int SERVICE_A_PORT = 8887;
66+
private static final int SERVICE_A_PORT = TestSocketUtils.findAvailableTcpPort();
6667

67-
private static final int SERVICE_B_PORT = 8888;
68+
private static final int SERVICE_B_PORT = TestSocketUtils.findAvailableTcpPort();
6869

69-
private static final int SERVICE_C_PORT = 8889;
70+
private static final int SERVICE_C_PORT = TestSocketUtils.findAvailableTcpPort();
7071

7172
private static WireMockServer wireMockServer;
7273

@@ -108,7 +109,7 @@ static void beforeAll() {
108109
serviceCMockServer.start();
109110
WireMock.configureFor("localhost", SERVICE_C_PORT);
110111

111-
// we mock host creation so that it becomes something like : localhost:8888
112+
// we mock host creation so that it becomes something like : localhost:<port>
112113
// then wiremock can catch this request, and we can assert for the result
113114
MOCKED_STATIC.when(() -> KubernetesServiceInstanceMapper.createHost("my-service", "a", "cluster.local"))
114115
.thenReturn("localhost");

spring-cloud-kubernetes-client-loadbalancer/src/test/java/org/springframework/cloud/kubernetes/client/loadbalancer/it/mode/service/SpecificNamespaceTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
4747
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
4848
import org.springframework.http.HttpMethod;
49+
import org.springframework.test.util.TestSocketUtils;
4950
import org.springframework.web.reactive.function.client.WebClient;
5051

5152
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
@@ -63,9 +64,9 @@ class SpecificNamespaceTest {
6364

6465
private static final String SERVICE_A_URL = "http://my-service";
6566

66-
private static final int SERVICE_A_PORT = 8888;
67+
private static final int SERVICE_A_PORT = TestSocketUtils.findAvailableTcpPort();
6768

68-
private static final int SERVICE_B_PORT = 8889;
69+
private static final int SERVICE_B_PORT = TestSocketUtils.findAvailableTcpPort();
6970

7071
private static WireMockServer wireMockServer;
7172

spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8ConfigServerTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.boot.autoconfigure.SpringBootApplication;
2828
import org.springframework.boot.test.context.SpringBootTest;
2929
import org.springframework.context.ApplicationContext;
30+
import org.springframework.test.util.TestSocketUtils;
3031

3132
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
3233
import static com.github.tomakehurst.wiremock.client.WireMock.get;
@@ -53,7 +54,7 @@ class Fabric8ConfigServerTest {
5354

5455
@BeforeAll
5556
static void beforeAll() {
56-
wireMockServer = new WireMockServer(options().port(8888));
57+
wireMockServer = new WireMockServer(options().port(TestSocketUtils.findAvailableTcpPort()));
5758
wireMockServer.start();
5859
WireMock.configureFor("localhost", wireMockServer.port());
5960
}

0 commit comments

Comments
 (0)