-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
/
Copy pathHealthEndpointIntegrationTest.java
241 lines (213 loc) · 10.2 KB
/
HealthEndpointIntegrationTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
*
* The MIT License
* Copyright © 2014-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
import static io.restassured.RestAssured.given;
import com.iluwatar.health.check.App;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.filter.log.LogDetail;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import lombok.extern.slf4j.Slf4j;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
/**
* Integration tests for the health endpoint.
*
* <p>* * Log statement for the test case response in case of "DOWN" status with high CPU load
* during pipeline execution. * Note: During pipeline execution, if the health check shows "DOWN"
* status with high CPU load, it is expected behavior. The service checks CPU usage, and if it's not
* under 90%, it returns this error, example return value:
* {"status":"DOWN","components":{"cpu":{"status":"DOWN","details":{"processCpuLoad":"100.00%", *
* "availableProcessors":2,"systemCpuLoad":"100.00%","loadAverage":1.97,"timestamp":"2023-11-09T08:34:15.974557865Z",
* * "error":"High system CPU load"}}} *
*
*/
@Slf4j
@SpringBootTest(
classes = {App.class},
webEnvironment = WebEnvironment.RANDOM_PORT)
class HealthEndpointIntegrationTest {
/** Autowired TestRestTemplate instance for making HTTP requests. */
@Autowired private TestRestTemplate restTemplate;
// Create a RequestSpecification that logs the request details
private final RequestSpecification requestSpec =
new RequestSpecBuilder().log(LogDetail.ALL).build();
private String getEndpointBasePath() {
return restTemplate.getRootUri() + "/actuator/health";
}
// Common method to log response details
private void logResponseDetails(Response response) {
LOGGER.info("Request URI: " + response.getDetailedCookies());
LOGGER.info("Response Time: " + response.getTime() + "ms");
LOGGER.info("Response Status: " + response.getStatusCode());
LOGGER.info("Response: " + response.getBody().asString());
}
/** Test that the health endpoint returns the UP status. */
@Test
void healthEndpointReturnsUpStatus() {
Response response = given(requestSpec).get(getEndpointBasePath()).andReturn();
logResponseDetails(response);
if (response.getStatusCode() == HttpStatus.SERVICE_UNAVAILABLE.value()) {
LOGGER.warn(
"Health endpoint returned 503 Service Unavailable. This may be due to pipeline "
+ "configuration. Please check the pipeline logs.");
response.then().assertThat().statusCode(HttpStatus.SERVICE_UNAVAILABLE.value());
return;
}
if (response.getStatusCode() != HttpStatus.OK.value()
|| !"UP".equals(response.path("status"))) {
LOGGER.error("Health endpoint response: " + response.getBody().asString());
LOGGER.error("Health endpoint status: " + response.getStatusCode());
}
response.then().assertThat().statusCode(HttpStatus.OK.value());
assertTrue("UP".equals(response.path("status")));
}
/**
* Test that the health endpoint returns complete details about the application's health. If the
* status is 503, the test passes without further checks. If the status is 200, additional checks
* are performed on various components. In case of a "DOWN" status, the test logs the entire
* response for visibility.
*/
@Test
void healthEndpointReturnsCompleteDetails() {
// Make the HTTP request to the health endpoint
Response response = given(requestSpec).get(getEndpointBasePath()).andReturn();
// Log the response details
logResponseDetails(response);
// Check if the status is 503 (SERVICE_UNAVAILABLE)
if (response.getStatusCode() == HttpStatus.SERVICE_UNAVAILABLE.value()) {
LOGGER.warn(
"Health endpoint returned 503 Service Unavailable. This may be due to CI pipeline "
+ "configuration. Please check the CI pipeline logs.");
response
.then()
.assertThat()
.statusCode(HttpStatus.SERVICE_UNAVAILABLE.value())
.log()
.all(); // Log the entire response for visibility
return;
}
// If status is 200, proceed with additional checks
response
.then()
.assertThat()
.statusCode(HttpStatus.OK.value()); // Check that the status is UP
assertTrue("UP".equals(response.path("status"))); // Verify the status body is UP
assertTrue("UP".equals(response.path("components.cpu.status"))); // Check CPU status
assertTrue("UP".equals(response.path("components.db.status"))); // Check DB status
assertTrue("UP".equals(response.path("components.diskSpace.status"))); // Check disk space status
assertTrue("UP".equals(response.path("components.ping.status"))); // Check ping status
assertTrue("UP".equals(response.path("components.custom.status"))); // Check custom component status
// Check for "DOWN" status and high CPU load
if ("DOWN".equals(response.path("status"))) {
LOGGER.error("Health endpoint response: " + response.getBody().asString());
LOGGER.error("Health endpoint status: " + response.path("status"));
LOGGER.error(
"High CPU load detected: " + response.path("components.cpu.details.processCpuLoad"));
}
}
/**
* Test that the liveness endpoint returns the UP status.
*
* <p>The liveness endpoint is used to indicate whether the application is still running and
* responsive.
*/
@Test
void livenessEndpointShouldReturnUpStatus() {
// Make the HTTP request to the liveness endpoint
Response response = given(requestSpec).get(getEndpointBasePath() + "/liveness").andReturn();
// Log the response details
logResponseDetails(response);
// Check if the status is 503 (SERVICE_UNAVAILABLE)
if (response.getStatusCode() == HttpStatus.SERVICE_UNAVAILABLE.value()) {
LOGGER.warn(
"Liveness endpoint returned 503 Service Unavailable. This may be due to CI pipeline "
+ "configuration. Please check the CI pipeline logs.");
// If status is 503, the test passes without further checks
response
.then()
.assertThat()
.statusCode(HttpStatus.SERVICE_UNAVAILABLE.value())
.log()
.all(); // Log the entire response for visibility
return;
}
// If status is 200, proceed with additional checks
response.then().assertThat().statusCode(HttpStatus.OK.value());
assertTrue("UP".equals(response.path("status")));
// Check for "DOWN" status and high CPU load
if ("DOWN".equals(response.path("status"))) {
LOGGER.error("Liveness endpoint response: " + response.getBody().asString());
LOGGER.error("Liveness endpoint status: " + response.path("status"));
LOGGER.error(
"High CPU load detected: " + response.path("components.cpu.details.processCpuLoad"));
}
}
/**
* Test that the custom health indicator returns the UP status and additional details.
*
* <p>The custom health indicator is used to provide more specific information about the health of
* a particular component or aspect of the application.
*/
@Test
void customHealthIndicatorShouldReturnUpStatusAndDetails() {
// Make the HTTP request to the health endpoint
Response response = given(requestSpec).get(getEndpointBasePath()).andReturn();
// Log the response details
logResponseDetails(response);
// Check if the status is 503 (SERVICE_UNAVAILABLE)
if (response.getStatusCode() == HttpStatus.SERVICE_UNAVAILABLE.value()) {
LOGGER.warn(
"Custom health indicator returned 503 Service Unavailable. This may be due to CI pipeline "
+ "configuration. Please check the CI pipeline logs.");
// If status is 503, the test passes without further checks
response
.then()
.assertThat()
.statusCode(HttpStatus.SERVICE_UNAVAILABLE.value())
.log()
.all(); // Log the entire response for visibility
return;
}
// If status is 200, proceed with additional checks
response
.then()
.assertThat()
.statusCode(HttpStatus.OK.value()); // Check that the status is UP
assertTrue("UP".equals(response.path("components.custom.status"))); // Verify the custom component status
assertTrue("reachable".equals(response.path("components.custom.details.database"))); // Verify custom details
// Check for "DOWN" status and high CPU load
if ("DOWN".equals(response.path("status"))) {
LOGGER.error("Custom health indicator response: " + response.getBody().asString());
LOGGER.error("Custom health indicator status: " + response.path("status"));
LOGGER.error(
"High CPU load detected: " + response.path("components.cpu.details.processCpuLoad"));
}
}
}