Skip to content

Commit f134e96

Browse files
author
Dave Syer
committed
Convert Actuator sample to dynamic ports
1 parent 559009b commit f134e96

19 files changed

+118
-69
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfiguration.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@
5353
import org.springframework.boot.autoconfigure.web.ServerProperties;
5454
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
5555
import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext;
56+
import org.springframework.boot.context.embedded.EmbeddedServletContainer;
5657
import org.springframework.boot.context.embedded.EmbeddedServletContainerException;
58+
import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext;
5759
import org.springframework.context.ApplicationContext;
5860
import org.springframework.context.ApplicationContextAware;
5961
import org.springframework.context.ApplicationListener;
@@ -195,6 +197,8 @@ public void onApplicationEvent(ContextClosedEvent event) {
195197
}
196198
try {
197199
childContext.refresh();
200+
registerContainer(this.applicationContext,
201+
childContext.getEmbeddedServletContainer());
198202
}
199203
catch (RuntimeException e) {
200204
// No support currently for deploying a war with management.port=<different>,
@@ -207,6 +211,16 @@ public void onApplicationEvent(ContextClosedEvent event) {
207211
throw e;
208212
}
209213
}
214+
};
215+
216+
private void registerContainer(ApplicationContext applicationContext,
217+
EmbeddedServletContainer embeddedServletContainer) {
218+
if (applicationContext instanceof EmbeddedWebApplicationContext) {
219+
((EmbeddedWebApplicationContext) applicationContext)
220+
.getEmbeddedServletContainers().put("management",
221+
embeddedServletContainer);
222+
// Maybe unregister it when it shuts down?
223+
}
210224
}
211225

212226
protected static enum ManagementServerPort {

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/ManagementServerProperties.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,5 @@ private static Security maybeCreateSecurity() {
126126
}
127127
return null;
128128
}
129+
129130
}

spring-boot-samples/spring-boot-sample-actuator/src/main/resources/application.properties

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
logging.file: /tmp/logs/app.log
2-
management.port: 8080
32
management.address: 127.0.0.1
43
endpoints.shutdown.enabled: true
5-
server.port: 8080
64
server.tomcat.basedir: target/tomcat
75
server.tomcat.access_log_pattern: %h %t "%r" %s %b
86
security.require_ssl: false

spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/EndpointsPropertiesSampleActuatorApplicationTests.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.junit.Test;
2222
import org.junit.runner.RunWith;
23+
import org.springframework.beans.factory.annotation.Value;
2324
import org.springframework.boot.test.IntegrationTest;
2425
import org.springframework.boot.test.SpringApplicationConfiguration;
2526
import org.springframework.boot.test.TestRestTemplate;
@@ -40,16 +41,19 @@
4041
@RunWith(SpringJUnit4ClassRunner.class)
4142
@SpringApplicationConfiguration(classes = SampleActuatorApplication.class)
4243
@WebAppConfiguration
43-
@IntegrationTest
44+
@IntegrationTest("server.port=0")
4445
@DirtiesContext
4546
@ActiveProfiles("endpoints")
4647
public class EndpointsPropertiesSampleActuatorApplicationTests {
4748

49+
@Value("${local.server.port}")
50+
private int port;
51+
4852
@Test
4953
public void testCustomErrorPath() throws Exception {
5054
@SuppressWarnings("rawtypes")
5155
ResponseEntity<Map> entity = new TestRestTemplate("user", "password")
52-
.getForEntity("http://localhost:8080/oops", Map.class);
56+
.getForEntity("http://localhost:" + port + "/oops", Map.class);
5357
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode());
5458
@SuppressWarnings("unchecked")
5559
Map<String, Object> body = entity.getBody();
@@ -60,7 +64,7 @@ public void testCustomErrorPath() throws Exception {
6064
@Test
6165
public void testCustomContextPath() throws Exception {
6266
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
63-
"http://localhost:8080/admin/health", String.class);
67+
"http://localhost:" + port + "/admin/health", String.class);
6468
assertEquals(HttpStatus.OK, entity.getStatusCode());
6569
String body = entity.getBody();
6670
assertEquals("ok", body);

spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ManagementAddressActuatorApplicationTests.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package sample.actuator;
1818

19+
import static org.junit.Assert.assertEquals;
20+
1921
import java.util.Map;
2022

2123
import org.junit.Test;
@@ -29,12 +31,9 @@
2931
import org.springframework.http.HttpStatus;
3032
import org.springframework.http.ResponseEntity;
3133
import org.springframework.test.annotation.DirtiesContext;
32-
import org.springframework.test.context.ActiveProfiles;
3334
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
3435
import org.springframework.test.context.web.WebAppConfiguration;
3536

36-
import static org.junit.Assert.assertEquals;
37-
3837
/**
3938
* Integration tests for separate management and main service ports.
4039
*
@@ -43,18 +42,17 @@
4342
@RunWith(SpringJUnit4ClassRunner.class)
4443
@SpringApplicationConfiguration(classes = SampleActuatorApplication.class)
4544
@WebAppConfiguration
46-
@IntegrationTest
45+
@IntegrationTest({"server.port=0", "management.port=0", "management.address=127.0.0.1", "management.contextPath:/admin"})
4746
@DirtiesContext
48-
@ActiveProfiles("management-address")
4947
public class ManagementAddressActuatorApplicationTests {
5048

5149
@Autowired
5250
private SecurityProperties security;
5351

54-
@Value("${server.port}")
52+
@Value("${local.server.port}")
5553
private int port = 9010;
5654

57-
@Value("${management.port}")
55+
@Value("${local.management.port}")
5856
private int managementPort = 9011;
5957

6058
@Test

spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ManagementPortSampleActuatorApplicationTests.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package sample.actuator;
1818

19+
import static org.junit.Assert.assertEquals;
20+
1921
import java.util.Map;
2022

2123
import org.junit.Test;
@@ -29,12 +31,9 @@
2931
import org.springframework.http.HttpStatus;
3032
import org.springframework.http.ResponseEntity;
3133
import org.springframework.test.annotation.DirtiesContext;
32-
import org.springframework.test.context.ActiveProfiles;
3334
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
3435
import org.springframework.test.context.web.WebAppConfiguration;
3536

36-
import static org.junit.Assert.assertEquals;
37-
3837
/**
3938
* Integration tests for separate management and main service ports.
4039
*
@@ -43,18 +42,17 @@
4342
@RunWith(SpringJUnit4ClassRunner.class)
4443
@SpringApplicationConfiguration(classes = SampleActuatorApplication.class)
4544
@WebAppConfiguration
46-
@IntegrationTest
45+
@IntegrationTest({"server.port=0", "management.port=0"})
4746
@DirtiesContext
48-
@ActiveProfiles("management-port")
4947
public class ManagementPortSampleActuatorApplicationTests {
5048

5149
@Autowired
5250
private SecurityProperties security;
5351

54-
@Value("${server.port}")
52+
@Value("${local.server.port}")
5553
private int port = 9010;
5654

57-
@Value("${management.port}")
55+
@Value("${local.management.port}")
5856
private int managementPort = 9011;
5957

6058
@Test

spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/NoManagementSampleActuatorApplicationTests.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,24 @@
1616

1717
package sample.actuator;
1818

19+
import static org.junit.Assert.assertEquals;
20+
1921
import java.util.Map;
2022

2123
import org.junit.Test;
2224
import org.junit.runner.RunWith;
2325
import org.springframework.beans.factory.annotation.Autowired;
26+
import org.springframework.beans.factory.annotation.Value;
2427
import org.springframework.boot.autoconfigure.security.SecurityProperties;
2528
import org.springframework.boot.test.IntegrationTest;
2629
import org.springframework.boot.test.SpringApplicationConfiguration;
2730
import org.springframework.boot.test.TestRestTemplate;
2831
import org.springframework.http.HttpStatus;
2932
import org.springframework.http.ResponseEntity;
3033
import org.springframework.test.annotation.DirtiesContext;
31-
import org.springframework.test.context.ActiveProfiles;
3234
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
3335
import org.springframework.test.context.web.WebAppConfiguration;
3436

35-
import static org.junit.Assert.assertEquals;
36-
3737
/**
3838
* Integration tests for switching off management endpoints.
3939
*
@@ -42,19 +42,21 @@
4242
@RunWith(SpringJUnit4ClassRunner.class)
4343
@SpringApplicationConfiguration(classes = SampleActuatorApplication.class)
4444
@WebAppConfiguration
45-
@IntegrationTest
45+
@IntegrationTest({"server.port=0", "management.port=-1"})
4646
@DirtiesContext
47-
@ActiveProfiles("nomanagement")
4847
public class NoManagementSampleActuatorApplicationTests {
4948

5049
@Autowired
5150
private SecurityProperties security;
51+
52+
@Value("${local.server.port}")
53+
private int port = 0;
5254

5355
@Test
5456
public void testHome() throws Exception {
5557
@SuppressWarnings("rawtypes")
5658
ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword())
57-
.getForEntity("http://localhost:8080", Map.class);
59+
.getForEntity("http://localhost:" + port, Map.class);
5860
assertEquals(HttpStatus.OK, entity.getStatusCode());
5961
@SuppressWarnings("unchecked")
6062
Map<String, Object> body = entity.getBody();
@@ -66,7 +68,7 @@ public void testMetricsNotAvailable() throws Exception {
6668
testHome(); // makes sure some requests have been made
6769
@SuppressWarnings("rawtypes")
6870
ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword())
69-
.getForEntity("http://localhost:8080/metrics", Map.class);
71+
.getForEntity("http://localhost:" + port + "/metrics", Map.class);
7072
assertEquals(HttpStatus.NOT_FOUND, entity.getStatusCode());
7173
}
7274

spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/SampleActuatorApplicationTests.java

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.junit.Test;
2424
import org.junit.runner.RunWith;
2525
import org.springframework.beans.factory.annotation.Autowired;
26+
import org.springframework.beans.factory.annotation.Value;
2627
import org.springframework.boot.autoconfigure.security.SecurityProperties;
2728
import org.springframework.boot.test.IntegrationTest;
2829
import org.springframework.boot.test.SpringApplicationConfiguration;
@@ -50,18 +51,21 @@
5051
@RunWith(SpringJUnit4ClassRunner.class)
5152
@SpringApplicationConfiguration(classes = SampleActuatorApplication.class)
5253
@WebAppConfiguration
53-
@IntegrationTest
54+
@IntegrationTest("server.port=0")
5455
@DirtiesContext
5556
public class SampleActuatorApplicationTests {
5657

5758
@Autowired
5859
private SecurityProperties security;
5960

61+
@Value("${local.server.port}")
62+
private int port;
63+
6064
@Test
6165
public void testHomeIsSecure() throws Exception {
6266
@SuppressWarnings("rawtypes")
6367
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(
64-
"http://localhost:8080", Map.class);
68+
"http://localhost:" + port, Map.class);
6569
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
6670
@SuppressWarnings("unchecked")
6771
Map<String, Object> body = entity.getBody();
@@ -74,24 +78,24 @@ public void testHomeIsSecure() throws Exception {
7478
public void testMetricsIsSecure() throws Exception {
7579
@SuppressWarnings("rawtypes")
7680
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(
77-
"http://localhost:8080/metrics", Map.class);
81+
"http://localhost:" + port + "/metrics", Map.class);
7882
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
79-
entity = new TestRestTemplate().getForEntity(
80-
"http://localhost:8080/metrics/", Map.class);
83+
entity = new TestRestTemplate().getForEntity("http://localhost:" + port + "/metrics/",
84+
Map.class);
8185
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
82-
entity = new TestRestTemplate().getForEntity(
83-
"http://localhost:8080/metrics/foo", Map.class);
86+
entity = new TestRestTemplate().getForEntity("http://localhost:" + port + "/metrics/foo",
87+
Map.class);
8488
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
8589
entity = new TestRestTemplate().getForEntity(
86-
"http://localhost:8080/metrics.json", Map.class);
90+
"http://localhost:" + port + "/metrics.json", Map.class);
8791
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
8892
}
8993

9094
@Test
9195
public void testHome() throws Exception {
9296
@SuppressWarnings("rawtypes")
9397
ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword())
94-
.getForEntity("http://localhost:8080", Map.class);
98+
.getForEntity("http://localhost:" + port, Map.class);
9599
assertEquals(HttpStatus.OK, entity.getStatusCode());
96100
@SuppressWarnings("unchecked")
97101
Map<String, Object> body = entity.getBody();
@@ -103,7 +107,7 @@ public void testMetrics() throws Exception {
103107
testHome(); // makes sure some requests have been made
104108
@SuppressWarnings("rawtypes")
105109
ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword())
106-
.getForEntity("http://localhost:8080/metrics", Map.class);
110+
.getForEntity("http://localhost:" + port + "/metrics", Map.class);
107111
assertEquals(HttpStatus.OK, entity.getStatusCode());
108112
@SuppressWarnings("unchecked")
109113
Map<String, Object> body = entity.getBody();
@@ -114,7 +118,7 @@ public void testMetrics() throws Exception {
114118
public void testEnv() throws Exception {
115119
@SuppressWarnings("rawtypes")
116120
ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword())
117-
.getForEntity("http://localhost:8080/env", Map.class);
121+
.getForEntity("http://localhost:" + port + "/env", Map.class);
118122
assertEquals(HttpStatus.OK, entity.getStatusCode());
119123
@SuppressWarnings("unchecked")
120124
Map<String, Object> body = entity.getBody();
@@ -124,15 +128,15 @@ public void testEnv() throws Exception {
124128
@Test
125129
public void testHealth() throws Exception {
126130
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
127-
"http://localhost:8080/health", String.class);
131+
"http://localhost:" + port + "/health", String.class);
128132
assertEquals(HttpStatus.OK, entity.getStatusCode());
129133
assertEquals("ok", entity.getBody());
130134
}
131135

132136
@Test
133137
public void testErrorPage() throws Exception {
134138
ResponseEntity<String> entity = new TestRestTemplate("user", getPassword())
135-
.getForEntity("http://localhost:8080/foo", String.class);
139+
.getForEntity("http://localhost:" + port + "/foo", String.class);
136140
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode());
137141
String body = entity.getBody();
138142
assertNotNull(body);
@@ -145,7 +149,7 @@ public void testHtmlErrorPage() throws Exception {
145149
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
146150
HttpEntity<?> request = new HttpEntity<Void>(headers);
147151
ResponseEntity<String> entity = new TestRestTemplate("user", getPassword())
148-
.exchange("http://localhost:8080/foo", HttpMethod.GET, request,
152+
.exchange("http://localhost:" + port + "/foo", HttpMethod.GET, request,
149153
String.class);
150154
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode());
151155
String body = entity.getBody();
@@ -156,10 +160,10 @@ public void testHtmlErrorPage() throws Exception {
156160

157161
@Test
158162
public void testTrace() throws Exception {
159-
new TestRestTemplate().getForEntity("http://localhost:8080/health", String.class);
163+
new TestRestTemplate().getForEntity("http://localhost:" + port + "/health", String.class);
160164
@SuppressWarnings("rawtypes")
161165
ResponseEntity<List> entity = new TestRestTemplate("user", getPassword())
162-
.getForEntity("http://localhost:8080/trace", List.class);
166+
.getForEntity("http://localhost:" + port + "/trace", List.class);
163167
assertEquals(HttpStatus.OK, entity.getStatusCode());
164168
@SuppressWarnings("unchecked")
165169
List<Map<String, Object>> list = entity.getBody();
@@ -174,7 +178,7 @@ public void testTrace() throws Exception {
174178
public void testErrorPageDirectAccess() throws Exception {
175179
@SuppressWarnings("rawtypes")
176180
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(
177-
"http://localhost:8080/error", Map.class);
181+
"http://localhost:" + port + "/error", Map.class);
178182
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode());
179183
@SuppressWarnings("unchecked")
180184
Map<String, Object> body = entity.getBody();
@@ -186,7 +190,7 @@ public void testErrorPageDirectAccess() throws Exception {
186190
public void testBeans() throws Exception {
187191
@SuppressWarnings("rawtypes")
188192
ResponseEntity<List> entity = new TestRestTemplate("user", getPassword())
189-
.getForEntity("http://localhost:8080/beans", List.class);
193+
.getForEntity("http://localhost:" + port + "/beans", List.class);
190194
assertEquals(HttpStatus.OK, entity.getStatusCode());
191195
assertEquals(1, entity.getBody().size());
192196
@SuppressWarnings("unchecked")

0 commit comments

Comments
 (0)